aboutsummaryrefslogtreecommitdiff
path: root/test/org/gnunet/construct/ConstructTest.java
blob: 6cecd8373297822d815382521d159e945674d15f (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 This file is part of GNUnet.
 (C) 2011, 2012 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 3, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */

package org.gnunet.construct;

public class ConstructTest {
    /*
    
    @Test
    public void test_ByteFillMessage() {
        
        ByteFillMessage bfm = new ByteFillMessage();
        bfm.header = new MessageHeader();
        bfm.someValue = 42;
        bfm.rest = new byte[] {1,2,3,4,5,6,7,100, 8};
        
        Construct.patch(bfm);
        
        bfm.header.messageSize = Construct.getSize(bfm);

        bfm.header.messageType = 42;
        
        System.out.println(bfm.header.messageSize);
        
        byte[] data = Construct.toBinary(bfm);
        
        Assert.assertEquals(Construct.getSize(bfm), data.length);
        
        System.out.println("data size: " + data.length);

        ByteFillMessage bfm2 = Construct.parseAs(ByteBuffer.wrap(data), ByteFillMessage.class);
        
        Assert.assertArrayEquals(bfm.rest, bfm2.rest);
    }


    @Test
    public void test_VarTestMessage() {
        VarTestMessage vtm = new VarTestMessage();
        vtm.length = 5;
        vtm.msgs = new SimpleTestMessage2[5];
        for (int i = 0; i < 5; ++i) {
            vtm.msgs[i] = new SimpleTestMessage2();
            vtm.msgs[i].value = i;
        }

        byte[] a = Construct.toBinary(vtm);

        VarTestMessage vtm2 = Construct.parseAs(ByteBuffer.wrap(a), VarTestMessage.class);

        Assert.assertEquals(vtm2.length, 5);
        for (int i = 0; i < 5; ++i) {
            Assert.assertEquals(vtm2.msgs[i].value, i);
        }

    }

    @Test
    public void test_SimpleTestMessage() {
        SimpleTestMessage stm = new SimpleTestMessage();
        stm.v1 = 20;
        stm.v2 = 21;
        stm.mn = new SimpleTestMessage2();
        stm.mn.value = 42;
        stm.mns = new SimpleTestMessage2[5];
        for (int i = 0; i < stm.mns.length; i++) {
            stm.mns[i] = new SimpleTestMessage2();
            stm.mns[i].value = i;
        }

        byte[] a = Construct.toBinary(stm);
        SimpleTestMessage stm2 = Construct.parseAs(ByteBuffer.wrap(a),
                SimpleTestMessage.class);

        Assert.assertEquals(stm.v1, stm2.v1);
        Assert.assertEquals(stm.v2, stm2.v2);
        Assert.assertEquals(stm.mn.value, stm2.mn.value);
        Assert.assertEquals(stm.mns.length, stm2.mns.length);

        for (int i = 0; i < stm.mns.length; i++) {
            Assert.assertEquals(stm.mns[i].value, stm2.mns[i].value);
        }

    }

    public static String toHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        return String.format("%0" + (bytes.length << 1) + "X", bi);
    }

    @Test
    public void test_StringMessage() {
        StringMessage strm = new StringMessage();
        strm.num = 58;
        strm.str = "ab";
        strm.num2 = 80;

        byte[] a = Construct.toBinary(strm);
        StringMessage strm2 = Construct.parseAs(ByteBuffer.wrap(a), StringMessage.class);

        for (byte b : a) {
            System.out.print((char) b);
        }

        Assert.assertEquals(strm.num, strm2.num);
        Assert.assertEquals(strm.num2, strm2.num2);
        Assert.assertEquals(strm.str, strm2.str);

    }

    @Test
    public void test_QueryMessage() {
        QueryMessage qm = new QueryMessage();
        qm.header = new MessageHeader();
        qm.header.messageType = 0x42;
        qm.query = 0x43;
        qm.varsize = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
                14, 15 };

        Construct.patch(qm);

        byte[] a = Construct.toBinary(qm);
        
        Assert.assertEquals(a.length, qm.header.messageSize);

        QueryMessage qm2 = Construct.parseAs(ByteBuffer.wrap(a), QueryMessage.class);

        Assert.assertEquals(qm.header.messageSize, qm2.header.messageSize);
        Assert.assertEquals(qm.header.messageType, qm2.header.messageType);
        Assert.assertEquals(qm.query, qm2.query);

        Assert.assertArrayEquals(qm.varsize, qm2.varsize);

    }

    @Test
    public void test_SizeTestMessage() {
        SizeTestMessage stm = new SizeTestMessage();
        stm.someValue = 42;
        stm.rest = new byte[] { 1, 2, 3, 4, 5 };

        Construct.patch(stm);

        byte[] a = Construct.toBinary(stm);

        SizeTestMessage stm2 = Construct.parseAs(ByteBuffer.wrap(a), SizeTestMessage.class);

        Assert.assertEquals(stm.someValue, stm2.someValue);
        Assert.assertEquals(stm.totalSize, stm2.totalSize);

        Assert.assertArrayEquals(stm.rest, stm2.rest);
    }

    @Test
    public void test_MessageHeader() {
        MessageHeader h1 = new MessageHeader();

        h1.messageSize = 42;
        h1.messageType = 52;

        byte[] a = Construct.toBinary(h1);

        MessageHeader h2 = Construct.parseAs(ByteBuffer.wrap(a), MessageHeader.class);

        byte[] b = Construct.toBinary(h2);

        Assert.assertArrayEquals(a, b);

        Assert.assertEquals(h1.messageSize, h2.messageSize);
        Assert.assertEquals(h1.messageType, h2.messageType);
    }

    */
}