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