aboutsummaryrefslogtreecommitdiff
path: root/test/org/gnunet/construct/StringTest.java
blob: 7839f8bd382a3e1a67e80c81113cab46748f5d7a (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
package org.gnunet.construct;

import junit.framework.Assert;
import org.junit.Test;

/**
 * ...
 *
 * @author Florian Dold
 */
public class StringTest {
    public static class StrMsg implements Message {
        @FrameSize
        @UInt32
        public int len;
        @ZeroTerminatedString(optional = false)
        public String str1;
        @ZeroTerminatedString(optional = true)
        public String str2;
    }


    @Test
    public void test_empty() {
        StrMsg m = new StrMsg();
        m.str1 = "";
        m.str2 = "";
        Construct.patch(m);
        byte[] data = Construct.toBinary(m);
        Assert.assertEquals(4+1+1, data.length);
        StrMsg m2 = Construct.parseAs(data, StrMsg.class);
        Assert.assertEquals("", m2.str1);
        Assert.assertEquals("", m2.str2);
    }

    @Test
    public void test_null() {
        StrMsg m = new StrMsg();
        m.str1 = "";
        m.str2 = null;
        Construct.patch(m);
        byte[] data = Construct.toBinary(m);
        Assert.assertEquals(4+1, data.length);
        Assert.assertEquals(4+1, m.len);
        StrMsg m2 = Construct.parseAs(data, StrMsg.class);
        Assert.assertEquals("", m2.str1);
        Assert.assertEquals(null, m2.str2);
    }
}