More unittests
[apps/agl-service-can-low-level.git] / tests / encode_unittests.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "pb_encode.h"
4 #include "unittests.h"
5 #include "unittestproto.pb.h"
6
7 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
8 {
9     /* Allow only 'x' to be written */
10     while (count--)
11     {
12         if (*buf++ != 'x')
13             return false;
14     }
15     return true;
16 }
17
18 bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
19 {
20     int value = 0x55;
21     if (!pb_encode_tag_for_field(stream, field))
22         return false;
23     return pb_encode_varint(stream, value);
24 }
25
26 bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
27 {
28     /* This callback writes different amount of data the second time. */
29     uint32_t *state = (uint32_t*)arg;
30     *state <<= 8;
31     if (!pb_encode_tag_for_field(stream, field))
32         return false;
33     return pb_encode_varint(stream, *state);
34 }
35
36 /* Check that expression x writes data y.
37  * Y is a string, which may contain null bytes. Null terminator is ignored.
38  */
39 #define WRITES(x, y) \
40 memset(buffer, 0xAA, sizeof(buffer)), \
41 s = pb_ostream_from_buffer(buffer, sizeof(buffer)), \
42 (x) && \
43 memcmp(buffer, y, sizeof(y) - 1) == 0 && \
44 buffer[sizeof(y) - 1] == 0xAA
45
46 int main()
47 {
48     int status = 0;
49     
50     {
51         uint8_t buffer1[] = "foobartest1234";
52         uint8_t buffer2[sizeof(buffer1)];
53         pb_ostream_t stream = pb_ostream_from_buffer(buffer2, sizeof(buffer1));
54         
55         COMMENT("Test pb_write and pb_ostream_t");
56         TEST(pb_write(&stream, buffer1, sizeof(buffer1)));
57         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0);
58         TEST(!pb_write(&stream, buffer1, 1));
59         TEST(stream.bytes_written == sizeof(buffer1));
60     }
61     
62     {
63         uint8_t buffer1[] = "xxxxxxx";
64         pb_ostream_t stream = {&streamcallback, 0, SIZE_MAX, 0};
65         
66         COMMENT("Test pb_write with custom callback");
67         TEST(pb_write(&stream, buffer1, 5));
68         buffer1[0] = 'a';
69         TEST(!pb_write(&stream, buffer1, 5));
70     }
71     
72     {
73         uint8_t buffer[30];
74         pb_ostream_t s;
75         
76         COMMENT("Test pb_encode_varint")
77         TEST(WRITES(pb_encode_varint(&s, 0), "\0"));
78         TEST(WRITES(pb_encode_varint(&s, 1), "\1"));
79         TEST(WRITES(pb_encode_varint(&s, 0x7F), "\x7F"));
80         TEST(WRITES(pb_encode_varint(&s, 0x80), "\x80\x01"));
81         TEST(WRITES(pb_encode_varint(&s, UINT32_MAX), "\xFF\xFF\xFF\xFF\x0F"));
82         TEST(WRITES(pb_encode_varint(&s, UINT64_MAX), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
83     }
84     
85     {
86         uint8_t buffer[30];
87         pb_ostream_t s;
88         
89         COMMENT("Test pb_encode_tag")
90         TEST(WRITES(pb_encode_tag(&s, PB_WT_STRING, 5), "\x2A"));
91         TEST(WRITES(pb_encode_tag(&s, PB_WT_VARINT, 99), "\x98\x06"));
92     }
93     
94     {
95         uint8_t buffer[30];
96         pb_ostream_t s;
97         pb_field_t field = {10, PB_LTYPE_SVARINT};
98         
99         COMMENT("Test pb_encode_tag_for_field")
100         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
101         
102         field.type = PB_LTYPE_FIXED;
103         field.data_size = 8;
104         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
105         
106         field.type = PB_LTYPE_STRING;
107         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
108         
109         field.type = PB_LTYPE_FIXED;
110         field.data_size = 4;
111         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
112     }
113     
114     {
115         uint8_t buffer[30];
116         pb_ostream_t s;
117         
118         COMMENT("Test pb_encode_string")
119         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd", 4), "\x04""abcd"));
120         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd\x00", 5), "\x05""abcd\x00"));
121         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"", 0), "\x00"));
122     }
123     
124     {
125         uint8_t buffer[30];
126         pb_ostream_t s;
127         uint8_t value = 1;
128         int8_t svalue = -1;
129         int32_t max = INT32_MAX;
130         int32_t min = INT32_MIN;
131         int64_t lmax = INT64_MAX;
132         int64_t lmin = INT64_MIN;
133         pb_field_t field = {1, PB_LTYPE_VARINT, 0, 0, sizeof(value)};
134         
135         COMMENT("Test pb_enc_varint and pb_enc_svarint")
136         TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
137         TEST(WRITES(pb_enc_svarint(&s, &field, &svalue), "\x01"));
138         TEST(WRITES(pb_enc_svarint(&s, &field, &value), "\x02"));
139         
140         field.data_size = sizeof(max);
141         TEST(WRITES(pb_enc_svarint(&s, &field, &max), "\xfe\xff\xff\xff\x0f"));
142         TEST(WRITES(pb_enc_svarint(&s, &field, &min), "\xff\xff\xff\xff\x0f"));
143         
144         field.data_size = sizeof(lmax);
145         TEST(WRITES(pb_enc_svarint(&s, &field, &lmax), "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
146         TEST(WRITES(pb_enc_svarint(&s, &field, &lmin), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
147     }
148     
149     {
150         uint8_t buffer[30];
151         pb_ostream_t s;
152         pb_field_t field = {1, PB_LTYPE_FIXED, 0, 0, sizeof(float)};
153         float fvalue;
154         double dvalue;
155         
156         COMMENT("Test pb_enc_fixed using float")
157         fvalue = 0.0f;
158         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\x00\x00"))
159         fvalue = 99.0f;
160         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\xc6\x42"))
161         fvalue = -12345678.0f;
162         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x4e\x61\x3c\xcb"))
163     
164         COMMENT("Test pb_enc_fixed using double")
165         field.data_size = sizeof(double);
166         dvalue = 0.0;
167         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
168         dvalue = 99.0;
169         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
170         dvalue = -12345678.0;
171         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
172     }
173     
174     {
175         uint8_t buffer[30];
176         pb_ostream_t s;
177         struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}};
178     
179         COMMENT("Test pb_enc_bytes")
180         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy"))
181         value.size = 0;
182         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00"))
183     }
184     
185     {
186         uint8_t buffer[30];
187         pb_ostream_t s;
188         char value[] = "xyzzy";
189         
190         COMMENT("Test pb_enc_string")
191         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
192         value[0] = '\0';
193         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
194     }
195     
196     {
197         uint8_t buffer[10];
198         pb_ostream_t s;
199         IntegerArray msg = {5, {1, 2, 3, 4, 5}};
200         
201         COMMENT("Test pb_encode with int32 array")
202         
203         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
204         
205         msg.data_count = 0;
206         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
207         
208         msg.data_count = 10;
209         TEST(!pb_encode(&s, IntegerArray_fields, &msg))
210     }
211     
212     {
213         uint8_t buffer[10];
214         pb_ostream_t s;
215         FloatArray msg = {1, {99.0f}};
216         
217         COMMENT("Test pb_encode with float array")
218         
219         TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg),
220                     "\x0A\x04\x00\x00\xc6\x42"))
221         
222         msg.data_count = 0;
223         TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg), ""))
224         
225         msg.data_count = 3;
226         TEST(!pb_encode(&s, FloatArray_fields, &msg))
227     }
228     
229     {
230         uint8_t buffer[10];
231         pb_ostream_t s;
232         CallbackArray msg;
233         
234         msg.data.funcs.encode = &fieldcallback;
235         
236         COMMENT("Test pb_encode with callback field.")
237         TEST(WRITES(pb_encode(&s, CallbackArray_fields, &msg), "\x08\x55"))
238     }
239     
240     {
241         uint8_t buffer[10];
242         pb_ostream_t s;
243         IntegerContainer msg = {{5, {1,2,3,4,5}}};
244         
245         COMMENT("Test pb_encode with packed array in a submessage.")
246         TEST(WRITES(pb_encode(&s, IntegerContainer_fields, &msg),
247                     "\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
248     }
249     
250     {
251         uint8_t buffer[10];
252         pb_ostream_t s;
253         CallbackContainer msg;
254         CallbackContainerContainer msg2;
255         uint32_t state = 1;
256         
257         msg.submsg.data.funcs.encode = &fieldcallback;
258         msg2.submsg.submsg.data.funcs.encode = &fieldcallback;
259         
260         COMMENT("Test pb_encode with callback field in a submessage.")
261         TEST(WRITES(pb_encode(&s, CallbackContainer_fields, &msg), "\x0A\x02\x08\x55"))
262         TEST(WRITES(pb_encode(&s, CallbackContainerContainer_fields, &msg2),
263                     "\x0A\x04\x0A\x02\x08\x55"))
264         
265         /* Misbehaving callback */
266         msg.submsg.data.funcs.encode = &crazyfieldcallback;
267         msg.submsg.data.arg = &state;
268         msg2.submsg.submsg.data.funcs.encode = &crazyfieldcallback;
269         msg2.submsg.submsg.data.arg = &state;
270         
271         TEST(!pb_encode(&s, CallbackContainer_fields, &msg))
272         state = 1;
273         TEST(!pb_encode(&s, CallbackContainerContainer_fields, &msg2))
274     }
275     
276     if (status != 0)
277         fprintf(stdout, "\n\nSome tests FAILED!\n");
278     
279     return status;
280 }