Skip to content

Commit ee6da85

Browse files
committed
Add AbstractPacketTest containing DSL for packet-related tests
1 parent 30bcc21 commit ee6da85

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.bouncycastle.bcpg.test;
2+
3+
import org.bouncycastle.bcpg.ContainedPacket;
4+
import org.bouncycastle.bcpg.HexDumpUtil;
5+
import org.bouncycastle.util.Arrays;
6+
import org.bouncycastle.util.test.SimpleTest;
7+
8+
import java.io.IOException;
9+
10+
public abstract class AbstractPacketTest extends SimpleTest {
11+
12+
/**
13+
* Test, whether the first byte array and the second byte array are identical.
14+
* If a mismatch is detected, a formatted hex dump of both arrays is printed to stdout.
15+
* @param first first array
16+
* @param second second array
17+
*/
18+
public void isEncodingEqual(byte[] first, byte[] second) {
19+
isEncodingEqual(null, first, second);
20+
}
21+
22+
/**
23+
* Test, whether the first byte array and the second byte array are identical.
24+
* If a mismatch is detected, a formatted hex dump of both arrays is printed to stdout.
25+
* @param message error message to prepend to the hex dump
26+
* @param first first array
27+
* @param second second array
28+
*/
29+
public void isEncodingEqual(String message, byte[] first, byte[] second) {
30+
StringBuilder sb = new StringBuilder();
31+
if (message != null) {
32+
sb.append(message).append("\n");
33+
}
34+
sb.append("Expected: \n").append(HexDumpUtil.hexdump(first)).append("\n");
35+
sb.append("Got: \n").append(HexDumpUtil.hexdump(second));
36+
37+
isTrue(sb.toString(), first == second || Arrays.areEqual(first, second));
38+
}
39+
40+
/**
41+
* Test, whether the encoding of the first and second packet are identical.
42+
* If a mismatch is detected, a formatted hex dump of both packet encodings is printed to stdout.
43+
* @param first first packet
44+
* @param second second packet
45+
*/
46+
public void isEncodingEqual(ContainedPacket first, ContainedPacket second)
47+
throws IOException {
48+
isEncodingEqual(null, first, second);
49+
}
50+
51+
/**
52+
* Test, whether the encoding of the first and second packet are identical.
53+
* If a mismatch is detected, a formatted hex dump of both packet encodings is printed to stdout.
54+
* @param message error message to prepend to the hex dump
55+
* @param first first packet
56+
* @param second second packet
57+
*/
58+
public void isEncodingEqual(String message, ContainedPacket first, ContainedPacket second)
59+
throws IOException {
60+
StringBuilder sb = new StringBuilder();
61+
if (message != null) {
62+
sb.append(message).append("\n");
63+
}
64+
sb.append("Expected: \n").append(HexDumpUtil.hexdump(first)).append("\n");
65+
sb.append("Got: \n").append(HexDumpUtil.hexdump(second));
66+
isTrue(sb.toString(), first == second || Arrays.areEqual(first.getEncoded(), second.getEncoded()));
67+
}
68+
69+
/**
70+
* Test, whether the value is false.
71+
* @param value value
72+
*/
73+
public void isFalse(boolean value) {
74+
isFalse("Value is not false.", value);
75+
}
76+
77+
/**
78+
* Test, whether the value is false.
79+
* @param message custom error message
80+
* @param value value
81+
*/
82+
public void isFalse(String message, boolean value) {
83+
isTrue(message, !value);
84+
}
85+
86+
/**
87+
* Test, whether the value is null.
88+
* @param value value
89+
*/
90+
public void isNull(Object value) {
91+
isNull("Value is not null.", value);
92+
}
93+
94+
/**
95+
* Test, whether the value is null.
96+
* @param message custom error message
97+
* @param value value
98+
*/
99+
public void isNull(String message, Object value) {
100+
isTrue(message, value == null);
101+
}
102+
103+
/**
104+
* Test, whether the value is not null.
105+
* @param value value
106+
*/
107+
public void isNotNull(Object value) {
108+
isNotNull("Value is not null.", value);
109+
}
110+
111+
/**
112+
* Test, whether the value is not null.
113+
* @param message custom error message
114+
* @param value value
115+
*/
116+
public void isNotNull(String message, Object value) {
117+
isTrue(message, value != null);
118+
}
119+
}

0 commit comments

Comments
 (0)