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