aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/poifs/crypt
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2019-12-27 23:00:13 +0000
committerAndreas Beeker <kiwiwings@apache.org>2019-12-27 23:00:13 +0000
commitc66575c1e7059ed403b74b76e699200fdee507b4 (patch)
tree0fe96421471b53e9f72a2370ce76d61b49a659bc /src/testcases/org/apache/poi/poifs/crypt
parent37282aae8f25970b157c40ae0888684f974ba666 (diff)
downloadpoi-c66575c1e7059ed403b74b76e699200fdee507b4.tar.gz
poi-c66575c1e7059ed403b74b76e699200fdee507b4.zip
Migrate all junit tests to Junit 4
get rid of references to junit.framework don't throw AssertionFailedErrors, but use Assert.fail instead add try-with-resources where it was missing git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872041 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/poifs/crypt')
-rw-r--r--src/testcases/org/apache/poi/poifs/crypt/TestBiff8DecryptingStream.java67
1 files changed, 15 insertions, 52 deletions
diff --git a/src/testcases/org/apache/poi/poifs/crypt/TestBiff8DecryptingStream.java b/src/testcases/org/apache/poi/poifs/crypt/TestBiff8DecryptingStream.java
index d8a78582f5..ca109ec0ca 100644
--- a/src/testcases/org/apache/poi/poifs/crypt/TestBiff8DecryptingStream.java
+++ b/src/testcases/org/apache/poi/poifs/crypt/TestBiff8DecryptingStream.java
@@ -17,11 +17,11 @@
package org.apache.poi.poifs.crypt;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import java.io.InputStream;
-import java.util.Arrays;
import javax.crypto.spec.SecretKeySpec;
@@ -30,9 +30,6 @@ import org.apache.poi.util.HexDump;
import org.apache.poi.util.HexRead;
import org.junit.Test;
-import junit.framework.AssertionFailedError;
-import junit.framework.ComparisonFailure;
-
/**
* Tests for {@link Biff8DecryptingStream}
*/
@@ -58,25 +55,19 @@ public final class TestBiff8DecryptingStream {
}
private static final class StreamTester {
- private static final boolean ONLY_LOG_ERRORS = true;
-
- private final MockStream _ms;
private final Biff8DecryptingStream _bds;
- private boolean _errorsOccurred;
/**
* @param expectedFirstInt expected value of the first int read from the decrypted stream
*/
public StreamTester(MockStream ms, String keyDigestHex, int expectedFirstInt) {
- _ms = ms;
byte[] keyDigest = HexRead.readFromString(keyDigestHex);
EncryptionInfo ei = new EncryptionInfo(EncryptionMode.binaryRC4);
Decryptor dec = ei.getDecryptor();
dec.setSecretKey(new SecretKeySpec(keyDigest, "RC4"));
-
- _bds = new Biff8DecryptingStream(_ms, 0, ei);
+
+ _bds = new Biff8DecryptingStream(ms, 0, ei);
assertEquals(expectedFirstInt, _bds.readInt());
- _errorsOccurred = false;
}
public Biff8DecryptingStream getBDS() {
@@ -106,7 +97,7 @@ public final class TestBiff8DecryptingStream {
public void confirmUShort(int expVal) {
assertEquals(HexDump.shortToHex(expVal), HexDump.shortToHex(_bds.readUShort()));
}
-
+
public short readShort() {
return _bds.readShort();
}
@@ -122,32 +113,13 @@ public final class TestBiff8DecryptingStream {
public void confirmLong(long expVal) {
assertEquals(HexDump.longToHex(expVal), HexDump.longToHex(_bds.readLong()));
}
-
+
public void confirmData(String expHexData) {
byte[] expData = HexRead.readFromString(expHexData);
byte[] actData = new byte[expData.length];
_bds.readFully(actData);
- if (Arrays.equals(expData, actData)) {
- return;
- }
- _errorsOccurred = true;
- if (ONLY_LOG_ERRORS) {
- logErr(2, "Data mismatch " + HexDump.toHex(expData) + " - "
- + HexDump.toHex(actData));
- return;
- }
- throw new ComparisonFailure("Data mismatch", HexDump.toHex(expData), HexDump.toHex(actData));
- }
-
- private static void logErr(int stackFrameCount, String msg) {
- StackTraceElement ste = new Exception().getStackTrace()[stackFrameCount];
- System.err.print("(" + ste.getFileName() + ":" + ste.getLineNumber() + ") ");
- System.err.println(msg);
- }
-
- public void assertNoErrors() {
- assertFalse("Some values decrypted incorrectly", _errorsOccurred);
+ assertArrayEquals("Data mismatch", expData, actData);
}
}
@@ -156,7 +128,7 @@ public final class TestBiff8DecryptingStream {
*/
@Test
public void readsAlignedWithBoundary() {
- StreamTester st = createStreamTester(0x50, "BA AD F0 0D 00", 0x96C66829);
+ StreamTester st = createStreamTester();
st.rollForward(0x0004, 0x03FF);
st.confirmByte(0x3E);
@@ -179,7 +151,6 @@ public final class TestBiff8DecryptingStream {
assertEquals(0xFFFF, st.readUShort());
st.rollForward(0x37D9B, 0x4A6F2);
assertEquals(-1, st.readShort());
- st.assertNoErrors();
}
/**
@@ -187,7 +158,7 @@ public final class TestBiff8DecryptingStream {
*/
@Test
public void readsSpanningBoundary() {
- StreamTester st = createStreamTester(0x50, "BA AD F0 0D 00", 0x96C66829);
+ StreamTester st = createStreamTester();
st.rollForward(0x0004, 0x03FC);
st.confirmLong(0x885243283E2A5EEFL);
@@ -195,7 +166,6 @@ public final class TestBiff8DecryptingStream {
st.confirmInt(0xD83E76CC);
st.rollForward(0x0802, 0x0BFF);
st.confirmShort(0x9B25);
- st.assertNoErrors();
}
/**
@@ -204,28 +174,22 @@ public final class TestBiff8DecryptingStream {
*/
@Test
public void readHeaderUShort() {
- StreamTester st = createStreamTester(0x50, "BA AD F0 0D 00", 0x96C66829);
+ StreamTester st = createStreamTester();
st.rollForward(0x0004, 0x03FF);
Biff8DecryptingStream bds = st.getBDS();
int hval = bds.readDataSize(); // unencrypted
int nextInt = bds.readInt();
- if (nextInt == 0x8F534029) {
- throw new AssertionFailedError(
- "Indentified bug in key alignment after call to readHeaderUShort()");
- }
+ assertNotEquals("Indentified bug in key alignment after call to readHeaderUShort()",0x8F534029, nextInt);
assertEquals(0x16885243, nextInt);
- if (hval == 0x283E) {
- throw new AssertionFailedError("readHeaderUShort() incorrectly decrypted result");
- }
+ assertNotEquals("readHeaderUShort() incorrectly decrypted result", 0x283E, hval);
assertEquals(0x504F, hval);
// confirm next key change
st.rollForward(0x0405, 0x07FC);
st.confirmInt(0x76CC1223);
st.confirmInt(0x4842D83E);
- st.assertNoErrors();
}
/**
@@ -233,17 +197,16 @@ public final class TestBiff8DecryptingStream {
*/
@Test
public void readByteArrays() {
- StreamTester st = createStreamTester(0x50, "BA AD F0 0D 00", 0x96C66829);
+ StreamTester st = createStreamTester();
st.rollForward(0x0004, 0x2FFC);
st.confirmData("66 A1 20 B1 04 A3 35 F5"); // 4 bytes on either side of boundary
st.rollForward(0x3004, 0x33F8);
st.confirmData("F8 97 59 36"); // last 4 bytes in block
st.confirmData("01 C2 4E 55"); // first 4 bytes in next block
- st.assertNoErrors();
}
- private static StreamTester createStreamTester(int mockStreamStartVal, String keyDigestHex, int expectedFirstInt) {
- return new StreamTester(new MockStream(mockStreamStartVal), keyDigestHex, expectedFirstInt);
+ private static StreamTester createStreamTester() {
+ return new StreamTester(new MockStream(0x50), "BA AD F0 0D 00", 0x96C66829);
}
}