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