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