006ad9001076b2c679fb2c319f4b501b8f298f76
[apps/agl-service-can-low-level.git] / tests / decode_unittests.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "pb_decode.h"
4 #include "unittests.h"
5 #include "unittestproto.pb.h"
6
7 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
8
9 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
10 {
11     if (stream->state != NULL)
12         return false; /* Simulate error */
13     
14     if (buf != NULL)
15         memset(buf, 'x', count);
16     return true;
17 }
18
19 /* Verifies that the stream passed to callback matches the byte array pointed to by arg. */
20 bool callback_check(pb_istream_t *stream, const pb_field_t *field, void *arg)
21 {
22     int i;
23     uint8_t byte;
24     pb_bytes_array_t *ref = (pb_bytes_array_t*) arg;
25     
26     for (i = 0; i < ref->size; i++)
27     {
28         if (!pb_read(stream, &byte, 1))
29             return false;
30         
31         if (byte != ref->bytes[i])
32             return false;
33     }
34     
35     return true;
36 }
37
38 int main()
39 {
40     int status = 0;
41     
42     {
43         uint8_t buffer1[] = "foobartest1234";
44         uint8_t buffer2[sizeof(buffer1)];
45         pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
46         
47         COMMENT("Test pb_read and pb_istream_t");
48         TEST(pb_read(&stream, buffer2, 6))
49         TEST(memcmp(buffer2, "foobar", 6) == 0)
50         TEST(stream.bytes_left == sizeof(buffer1) - 6)
51         TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
52         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
53         TEST(stream.bytes_left == 0)
54         TEST(!pb_read(&stream, buffer2, 1))
55     }
56     
57     {
58         uint8_t buffer[20];
59         pb_istream_t stream = {&stream_callback, NULL, 20};
60         
61         COMMENT("Test pb_read with custom callback");
62         TEST(pb_read(&stream, buffer, 5))
63         TEST(memcmp(buffer, "xxxxx", 5) == 0)
64         TEST(!pb_read(&stream, buffer, 50))
65         stream.state = (void*)1; /* Simulated error return from callback */
66         TEST(!pb_read(&stream, buffer, 5))
67         stream.state = NULL;
68         TEST(pb_read(&stream, buffer, 15))
69     }
70     
71     {
72         pb_istream_t s;
73         uint64_t u;
74         int64_t i;
75         
76         COMMENT("Test pb_decode_varint");
77         TEST((s = S("\x00"), pb_decode_varint(&s, &u) && u == 0));
78         TEST((s = S("\x01"), pb_decode_varint(&s, &u) && u == 1));
79         TEST((s = S("\xAC\x02"), pb_decode_varint(&s, &u) && u == 300));
80         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, &u) && u == UINT32_MAX));
81         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, (uint64_t*)&i) && i == UINT32_MAX));
82         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
83               pb_decode_varint(&s, (uint64_t*)&i) && i == -1));
84         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
85               pb_decode_varint(&s, &u) && u == UINT64_MAX));
86     }
87     
88     {
89         pb_istream_t s;
90         COMMENT("Test pb_skip_varint");
91         TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
92         TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
93         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
94               pb_skip_varint(&s) && s.bytes_left == 6))
95         TEST((s = S("\xFF"), !pb_skip_varint(&s)))
96     }
97     
98     {
99         pb_istream_t s;
100         COMMENT("Test pb_skip_string")
101         TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 6))
102         TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 6))
103         TEST((s = S("\x04"), !pb_skip_string(&s)))
104         TEST((s = S("\xFF"), !pb_skip_string(&s)))
105     }
106     
107     {
108         pb_istream_t s = S("\x01\xFF\xFF\x03");
109         pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0};
110         uint32_t d;
111         COMMENT("Test pb_dec_varint using uint32_t")
112         TEST(pb_dec_varint(&s, &f, &d) && d == 1)
113         
114         /* Verify that no more than data_size is written. */
115         d = 0;
116         f.data_size = 1;
117         TEST(pb_dec_varint(&s, &f, &d) && (d == 0xFF || d == 0xFF000000))
118     }
119     
120     {
121         pb_istream_t s;
122         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
123         int32_t d;
124         
125         COMMENT("Test pb_dec_svarint using int32_t")
126         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
127         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
128         TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX))
129         TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN))
130     }
131     
132     {
133         pb_istream_t s;
134         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 8, 0, 0};
135         uint64_t d;
136         
137         COMMENT("Test pb_dec_svarint using uint64_t")
138         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
139         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
140         TEST((s = S("\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MAX))
141         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MIN))
142     }
143     
144     {
145         pb_istream_t s;
146         pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 4, 0, 0};
147         float d;
148         
149         COMMENT("Test pb_dec_fixed using float (failures here may be caused by imperfect rounding)")
150         TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0f))
151         TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed(&s, &f, &d) && d == 99.0f))
152         TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
153         TEST((s = S("\x00"), !pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
154     }
155     
156     {
157         pb_istream_t s;
158         pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 8, 0, 0};
159         double d;
160         
161         COMMENT("Test pb_dec_fixed using double (failures here may be caused by imperfect rounding)")
162         TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0))
163         TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed(&s, &f, &d) && d == 99.0))
164         TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
165     }
166     
167     {
168         pb_istream_t s;
169         struct { size_t size; uint8_t bytes[5]; } d;
170         pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, 5, 0, 0};
171         
172         COMMENT("Test pb_dec_bytes")
173         TEST((s = S("\x00"), pb_dec_bytes(&s, &f, &d) && d.size == 0))
174         TEST((s = S("\x01\xFF"), pb_dec_bytes(&s, &f, &d) && d.size == 1 && d.bytes[0] == 0xFF))
175         TEST((s = S("\x06xxxxxx"), !pb_dec_bytes(&s, &f, &d)))
176         TEST((s = S("\x05xxxxx"), pb_dec_bytes(&s, &f, &d) && d.size == 5))
177         TEST((s = S("\x05xxxx"), !pb_dec_bytes(&s, &f, &d)))
178     }
179     
180     {
181         pb_istream_t s;
182         pb_field_t f = {1, PB_LTYPE_STRING, 0, 0, 5, 0, 0};
183         char d[5];
184         
185         COMMENT("Test pb_dec_string")
186         TEST((s = S("\x00"), pb_dec_string(&s, &f, &d) && d[0] == '\0'))
187         TEST((s = S("\x04xyzz"), pb_dec_string(&s, &f, &d) && strcmp(d, "xyzz") == 0))
188         TEST((s = S("\x05xyzzy"), !pb_dec_string(&s, &f, &d)))
189     }
190     
191     {
192         pb_istream_t s;
193         IntegerArray dest;
194         
195         COMMENT("Testing pb_decode with repeated int32 field")
196         TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 0))
197         TEST((s = S("\x08\x01\x08\x02"), pb_decode(&s, IntegerArray_fields, &dest)
198              && dest.data_count == 2 && dest.data[0] == 1 && dest.data[1] == 2))
199         s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A");
200         TEST(pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 10 && dest.data[9] == 10)
201         s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A\x08\x0B");
202         TEST(!pb_decode(&s, IntegerArray_fields, &dest))
203     }
204     
205     {
206         pb_istream_t s;
207         IntegerArray dest;
208         
209         COMMENT("Testing pb_decode with packed int32 field")
210         TEST((s = S("\x0A\x00"), pb_decode(&s, IntegerArray_fields, &dest)
211             && dest.data_count == 0))
212         TEST((s = S("\x0A\x01\x01"), pb_decode(&s, IntegerArray_fields, &dest)
213             && dest.data_count == 1 && dest.data[0] == 1))
214         TEST((s = S("\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"), pb_decode(&s, IntegerArray_fields, &dest)
215             && dest.data_count == 10 && dest.data[0] == 1 && dest.data[9] == 10))
216         TEST((s = S("\x0A\x0B\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"), !pb_decode(&s, IntegerArray_fields, &dest)))
217         
218         /* Test invalid wire data */
219         TEST((s = S("\x0A\xFF"), !pb_decode(&s, IntegerArray_fields, &dest)))
220         TEST((s = S("\x0A\x01"), !pb_decode(&s, IntegerArray_fields, &dest)))
221     }
222     
223     {
224         pb_istream_t s;
225         IntegerArray dest;
226         
227         COMMENT("Testing pb_decode with unknown fields")
228         TEST((s = S("\x18\x0F\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
229             && dest.data_count == 1 && dest.data[0] == 1))
230         TEST((s = S("\x19\x00\x00\x00\x00\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
231             && dest.data_count == 1 && dest.data[0] == 1))
232         TEST((s = S("\x1A\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
233             && dest.data_count == 1 && dest.data[0] == 1))
234         TEST((s = S("\x1B\x08\x01"), !pb_decode(&s, IntegerArray_fields, &dest)))
235         TEST((s = S("\x1D\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
236             && dest.data_count == 1 && dest.data[0] == 1))
237     }
238     
239     {
240         pb_istream_t s;
241         CallbackArray dest;
242         struct { size_t size; uint8_t bytes[10]; } ref;
243         dest.data.funcs.decode = &callback_check;
244         dest.data.arg = &ref;
245         
246         COMMENT("Testing pb_decode with callbacks")
247         /* Single varint */
248         ref.size = 1; ref.bytes[0] = 0x55;
249         TEST((s = S("\x08\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
250         /* Packed varint */
251         ref.size = 3; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = 0x55;
252         TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
253         /* Packed varint with loop */
254         ref.size = 1; ref.bytes[0] = 0x55;
255         TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
256         /* Single fixed32 */
257         ref.size = 4; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = ref.bytes[3] = 0xAA;
258         TEST((s = S("\x0D\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest)))
259         /* Single fixed64 */
260         ref.size = 8; memset(ref.bytes, 0xAA, 8);
261         TEST((s = S("\x09\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest)))
262         /* Unsupported field type */
263         TEST((s = S("\x0B\x00"), !pb_decode(&s, CallbackArray_fields, &dest)))
264         
265         /* Just make sure that our test function works */
266         ref.size = 1; ref.bytes[0] = 0x56;
267         TEST((s = S("\x08\x55"), !pb_decode(&s, CallbackArray_fields, &dest)))
268     }
269     
270     {
271         pb_istream_t s;
272         IntegerArray dest;
273         
274         COMMENT("Testing pb_decode message termination")
275         TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest)))
276         TEST((s = S("\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
277         TEST((s = S("\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)))
278         TEST((s = S("\x08\x01\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
279         TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest)))
280     }
281     
282     if (status != 0)
283         fprintf(stdout, "\n\nSome tests FAILED!\n");
284     
285     return status;
286 }