wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
package nodomain.freeyourgadget.gadgetbridge.test;
 
import junit.framework.TestCase;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
 
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
import nodomain.freeyourgadget.gadgetbridge.util.protobuf.messagefields.NestedMessageField;
import nodomain.freeyourgadget.gadgetbridge.util.protobuf.messagefields.RootMessageField;
import nodomain.freeyourgadget.gadgetbridge.util.protobuf.messagefields.StringMessageField;
import nodomain.freeyourgadget.gadgetbridge.util.protobuf.messagefields.VarintMessageField;
import nodomain.freeyourgadget.gadgetbridge.util.protobuf.ProtobufUtils;
 
public class ProtobufTest extends TestCase {
    private void assertEncodeVarint(byte[] expected, int valueToEncode){
        byte[] encoded = ProtobufUtils.encode_varint(valueToEncode);
        assertEquals(expected.length, encoded.length);
 
        for(int i = 0; i < expected.length; i++){
            assertEquals(expected[i], encoded[i]);
        }
    }
 
    public void testEncodeVarint(){
        assertEncodeVarint(new byte[]{0x00}, 0);
        assertEncodeVarint(new byte[]{0x01}, 1);
        assertEncodeVarint(new byte[]{0b1111111}, 0b1111111);
        assertEncodeVarint(new byte[]{(byte)0b11111111, 0b00000001}, 0b11111111);
        assertEncodeVarint(new byte[]{(byte)0xeb, (byte)0x86, (byte)0x4e}, 1278827);
        assertEncodeVarint(new byte[]{(byte)0xf9, (byte) 0xa5 , (byte) 0x3c}, 987897);
        assertEncodeVarint(new byte[]{(byte) 0xfd, (byte) 0xe5, (byte) 0x90, (byte) 0x01}, 2372349);
    }
 
    private void assertEncodeVarintMessageField(byte[] expected, int fieldId, int value) throws IOException {
        VarintMessageField field = new VarintMessageField(fieldId, value);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        field.encode(os);
        byte[] result = os.toByteArray();
 
        assertEquals(expected.length, result.length);
 
        for(int i = 0; i < expected.length; i++){
            assertEquals(expected[i], result[i]);
        }
    }
 
    public void testEncodeVarintMessageField() throws IOException {
        assertEncodeVarintMessageField(new byte[]{0b00001000, 0b00000000}, 1, 0);
        assertEncodeVarintMessageField(new byte[]{(byte) 0b10000000, 0b00000001, 0b01111111}, 16, 127);
        assertEncodeVarintMessageField(new byte[]{(byte) 0b10000000, 0b00000001, (byte) 0xfd, (byte) 0xe5, (byte) 0x90, (byte) 0x01}, 16, 2372349);
    }
 
    private void assertEncodeStringMessageField(byte[] expected, int fieldId, String value) throws IOException {
        StringMessageField field = new StringMessageField(fieldId, value);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        field.encode(os);
        byte[] result = os.toByteArray();
 
        assertEquals(expected.length, result.length);
 
        for(int i = 0; i < expected.length; i++){
            assertEquals(expected[i], result[i]);
        }
    }
 
    private void assertEncodeRootMessageField(byte[] expected, RootMessageField value) throws IOException {
        byte[] result = value.encodeToBytes();
        
        System.out.println(StringUtils.bytesToHex(result));
 
        assertEquals(expected.length, result.length);
 
        for(int i = 0; i < expected.length; i++){
            assertEquals(expected[i], result[i]);
        }
    }
 
    public void testEncodeStringMessageField() throws IOException {
        assertEncodeStringMessageField(new byte[]{0b00001010, 0b00000000}, 1, "");
        assertEncodeStringMessageField(new byte[]{0b01010010, 0x04, 0x74, 0x65, 0x73, 0x74}, 10, "test");
    }
 
    public void testNestedMessageField() throws IOException {
        RootMessageField startAppRequest = new RootMessageField(
                new VarintMessageField(1, 2),
                new NestedMessageField(16,
                        new StringMessageField(1, "Infrared"),
                        new StringMessageField(2, "RPC")
                )
        );
        assertEncodeRootMessageField(
                new byte[]{(byte) 0x14, (byte) 0x08, (byte) 0x02, (byte) 0x82, (byte) 0x01, (byte) 0x0f, (byte) 0x0a, (byte) 0x08, (byte) 0x49, (byte) 0x6e, (byte) 0x66, (byte) 0x72, (byte) 0x61, (byte) 0x72, (byte) 0x65, (byte) 0x64, (byte) 0x12, (byte) 0x03, (byte) 0x52, (byte) 0x50, (byte) 0x43},
                startAppRequest
        );
 
        RootMessageField loadFileRequest = new RootMessageField(
                new VarintMessageField(1, 3),
                new NestedMessageField(48,
                        new StringMessageField(1, "/any/infrared/Remote.ir")
                )
        );
        assertEncodeRootMessageField(
                new byte[]{(byte) 0x1e, (byte) 0x08, (byte) 0x03, (byte) 0x82, (byte) 0x03, (byte) 0x19, (byte) 0x0a, (byte) 0x17, (byte) 0x2f, (byte) 0x61, (byte) 0x6e, (byte) 0x79, (byte) 0x2f, (byte) 0x69, (byte) 0x6e, (byte) 0x66, (byte) 0x72, (byte) 0x61, (byte) 0x72, (byte) 0x65, (byte) 0x64, (byte) 0x2f, (byte) 0x52, (byte) 0x65, (byte) 0x6d, (byte) 0x6f, (byte) 0x74, (byte) 0x65, (byte) 0x2e, (byte) 0x69, (byte) 0x72},
                loadFileRequest
        );
 
        RootMessageField pressButtonRequest = new RootMessageField(
                new VarintMessageField(1, 4),
                new NestedMessageField(49,
                        new StringMessageField(1, "Pwr")
                )
        );
        assertEncodeRootMessageField(new byte[]{(byte) 0x0a, (byte) 0x08, (byte) 0x04, (byte) 0x8a, (byte) 0x03, (byte) 0x05, (byte) 0x0a, (byte) 0x03, (byte) 0x50, (byte) 0x77, (byte) 0x72}, pressButtonRequest);
 
        RootMessageField releaseButtonRequest = new RootMessageField(
                new VarintMessageField(1, 5),
                new NestedMessageField(50)
        );
        assertEncodeRootMessageField(new byte[]{(byte) 0x05, (byte) 0x08, (byte) 0x05, (byte) 0x92, (byte) 0x03, (byte) 0x00}, releaseButtonRequest);
 
        RootMessageField exitAppRequest = new RootMessageField(
                new VarintMessageField(1, 6),
                new NestedMessageField(47)
        );
        assertEncodeRootMessageField(new byte[]{(byte) 0x05, (byte) 0x08, (byte) 0x06, (byte) 0xfa, (byte) 0x02, (byte) 0x00}, exitAppRequest);
 
    }
}