Skip to content

Commit

Permalink
add insertByteArray and better test verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed Jan 7, 2018
1 parent ad0effc commit b8589ef
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See https://jitpack.io/#inwc3/JMPQ3/
Gradle Example:
```gradle
dependencies {
compile 'com.github.inwc3:JMPQ3:1.5.6'
compile 'com.github.inwc3:JMPQ3:1.5.8'
}
allprojects {
repositories {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'systems.crigges'
version '1.5.7'
version '1.5.8'

repositories {
jcenter()
Expand All @@ -28,7 +28,7 @@ dist.archiveName = "${jar.baseName}.${jar.extension}"
dependencies {
compile 'com.jcraft:jzlib:1.1.3'
compile 'com.esotericsoftware.minlog:minlog:1.2'
testCompile 'org.testng:testng:6.1.1'
testCompile 'org.testng:testng:6.13.1'
}

test {
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/systems/crigges/jmpq3/JMpqEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,32 @@ public MpqFile getMpqFile(String name) throws IOException {
* @param name of the file inside the mpq
* @throws JMpqException if file is not found or access errors occur
*/
public void deleteFile(String name) throws JMpqException {
public void deleteFile(String name) {
if (!canWrite) {
throw new NonWritableChannelException();
}

listFile.removeFile(name);
}

/**
* Inserts the specified byte array into the mpq once you close the editor.
*
* @param name of the file inside the mpq
* @param input the input byte array
* @throws JMpqException if file is not found or access errors occur
*/
public void insertByteArray(String name, byte[] input) throws NonWritableChannelException {
if (!canWrite) {
throw new NonWritableChannelException();
}

listFile.addFile(name);
ByteBuffer data = ByteBuffer.wrap(input);
filesToAdd.add(data);
internalFilename.put(data, name);
}

/**
* Inserts the specified file into the mpq once you close the editor.
*
Expand All @@ -616,7 +634,7 @@ public void deleteFile(String name) throws JMpqException {
* further changes won't affect the resulting mpq
* @throws JMpqException if file is not found or access errors occur
*/
public void insertFile(String name, File file, boolean backupFile) throws JMpqException {
public void insertFile(String name, File file, boolean backupFile) throws IOException {
if (!canWrite) {
throw new NonWritableChannelException();
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/systems/crigges/jmpq3/TestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package systems.crigges.jmpq3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* Created by Frotty on 11.06.2017.
*/
public class TestHelper {
// stolen from http://stackoverflow.com/a/304350/303637
public static String md5(File f) {
try {
byte[] buf = new byte[1024];
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = new FileInputStream(f);
DigestInputStream dis = new DigestInputStream(is, md);) {
while (dis.read(buf) >= 0);
}
byte[] digest = md.digest();
return bytesToHex(digest);
} catch (NoSuchAlgorithmException | IOException e) {
throw new RuntimeException(e);
}
}

// stolen from http://stackoverflow.com/a/9855338/303637
final protected static char[] hexArray = "0123456789abcdef".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
51 changes: 40 additions & 11 deletions src/test/java/systems/crigges/jmpq3test/MpqTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public void testInsertDeleteRegularFile() throws IOException {
}
}

@Test
public void testInsertByteArray() throws IOException {
File[] mpqs = getMpqs();
for (File mpq : mpqs) {
insertByteArrayAndVerify(mpq, "Example.txt");
}
}

@Test
public void testInsertDeleteZeroLengthFile() throws IOException {
File[] mpqs = getMpqs();
Expand Down Expand Up @@ -186,26 +194,47 @@ public void testIncompressibleFile() throws IOException {
}
}

private void insertAndVerify(File mpq, String filename) throws IOException {
private void insertByteArrayAndVerify(File mpq, String filename) throws IOException {
JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0);
try {
File file = getFile(filename);
String hashBefore = TestHelper.md5(mpq);
byte[] bytes = Files.readAllBytes(file.toPath());
mpqEditor.insertFile(filename, getFile(filename), false);
mpqEditor.insertByteArray(filename, Files.readAllBytes(getFile(filename).toPath()));
mpqEditor.close();

String hashAfter = TestHelper.md5(mpq);
// If this fails, the mpq is not changed by the insert file command and something went wrong
Assert.assertNotEquals(hashBefore, hashAfter);
mpqEditor = verifyMpq(mpq, filename, hashBefore, bytes);
Assert.assertFalse(mpqEditor.hasFile(filename));
} catch (NonWritableChannelException ignored) {
}
}

mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0);
Assert.assertTrue(mpqEditor.hasFile(filename));
byte[] bytes2 = mpqEditor.extractFileAsBytes(filename);
Assert.assertEquals(bytes, bytes2);
mpqEditor.deleteFile(filename);
private JMpqEditor verifyMpq(File mpq, String filename, String hashBefore, byte[] bytes) throws IOException {
JMpqEditor mpqEditor;
String hashAfter = TestHelper.md5(mpq);
// If this fails, the mpq is not changed by the insert file command and something went wrong
Assert.assertNotEquals(hashBefore, hashAfter);

mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0);
Assert.assertTrue(mpqEditor.hasFile(filename));
byte[] bytes2 = mpqEditor.extractFileAsBytes(filename);
Assert.assertEquals(bytes, bytes2);
mpqEditor.deleteFile(filename);
mpqEditor.close();
mpqEditor = new JMpqEditor(mpq, MPQOpenOption.READ_ONLY, MPQOpenOption.FORCE_V0);
return mpqEditor;
}

private void insertAndVerify(File mpq, String filename) throws IOException {
JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0);
try {
File file = getFile(filename);
String hashBefore = TestHelper.md5(mpq);
byte[] bytes = Files.readAllBytes(file.toPath());
mpqEditor.insertFile(filename, getFile(filename), false);
mpqEditor.close();
mpqEditor = new JMpqEditor(mpq, MPQOpenOption.READ_ONLY, MPQOpenOption.FORCE_V0);

mpqEditor = verifyMpq(mpq, filename, hashBefore, bytes);
Assert.assertFalse(mpqEditor.hasFile(filename));
} catch (NonWritableChannelException ignored) {
}
Expand Down

0 comments on commit b8589ef

Please sign in to comment.