diff options
Diffstat (limited to 'src/test/java/org/gnunet/construct/ConstructTest.java')
-rw-r--r-- | src/test/java/org/gnunet/construct/ConstructTest.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/test/java/org/gnunet/construct/ConstructTest.java b/src/test/java/org/gnunet/construct/ConstructTest.java new file mode 100644 index 0000000..ed6ac51 --- /dev/null +++ b/src/test/java/org/gnunet/construct/ConstructTest.java | |||
@@ -0,0 +1,82 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | import java.util.Random; | ||
7 | |||
8 | /** | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class ConstructTest { | ||
12 | public static class ByteFillTestMessage implements Message { | ||
13 | @FrameSize | ||
14 | @UInt32 | ||
15 | public int frameSize; | ||
16 | @FillWith @UInt8 | ||
17 | public byte[] bytes; | ||
18 | } | ||
19 | |||
20 | @Test | ||
21 | public void test_ByteFill() { | ||
22 | ByteFillTestMessage msg = new ByteFillTestMessage(); | ||
23 | msg.bytes = new byte[]{0,1,2,3}; | ||
24 | Construct.patch(msg); | ||
25 | byte[] bin = Construct.toBinary(msg); | ||
26 | |||
27 | ByteFillTestMessage msg_r = Construct.parseAs(bin, ByteFillTestMessage.class); | ||
28 | |||
29 | Assert.assertArrayEquals(new byte[]{0,1,2,3}, msg_r.bytes); | ||
30 | } | ||
31 | |||
32 | |||
33 | @Test | ||
34 | public void test_IntMessage() { | ||
35 | IntMessage im = new IntMessage(); | ||
36 | Random r = new Random(); | ||
37 | im.i1 = r.nextLong(); | ||
38 | im.i2 = r.nextLong(); | ||
39 | im.i3 = r.nextLong(); | ||
40 | im.i4 = r.nextLong(); | ||
41 | im.i5 = r.nextLong(); | ||
42 | im.i6 = r.nextLong(); | ||
43 | im.i7 = r.nextLong(); | ||
44 | im.i8 = r.nextLong(); | ||
45 | |||
46 | byte[] data = Construct.toBinary(im); | ||
47 | |||
48 | Construct.parseAs(data, IntMessage.class); | ||
49 | |||
50 | Assert.assertEquals((1+2+4+8)*2, data.length); | ||
51 | Assert.assertEquals((1+2+4+8)*2, Construct.getStaticSize(im)); | ||
52 | } | ||
53 | |||
54 | @Test(expected = AssertionError.class) | ||
55 | public void test_PrivateMemberMessage() { | ||
56 | PrivateMemberMessage m1 = new PrivateMemberMessage(); | ||
57 | byte[] data = Construct.toBinary(m1); | ||
58 | Construct.parseAs(data, PrivateMemberMessage.class); | ||
59 | } | ||
60 | |||
61 | |||
62 | @Test | ||
63 | public void test_variable_size() { | ||
64 | VariableSizeMessage m1 = new VariableSizeMessage(); | ||
65 | m1.n1 = 2; | ||
66 | m1.n2 = 3; | ||
67 | m1.a1 = new int[]{42,43}; | ||
68 | m1.a2 = new int[]{1,2,1000}; | ||
69 | |||
70 | byte[] data = Construct.toBinary(m1); | ||
71 | |||
72 | VariableSizeMessage m2 = Construct.parseAs(data, VariableSizeMessage.class); | ||
73 | |||
74 | Assert.assertEquals(m1.n1, m2.n1); | ||
75 | Assert.assertEquals(m1.n2, m2.n2); | ||
76 | Assert.assertArrayEquals(m1.a1, m2.a1); | ||
77 | Assert.assertArrayEquals(m1.a2, m2.a2); | ||
78 | } | ||
79 | |||
80 | |||
81 | } | ||
82 | |||