Merged 0003-Fixed-format-specifiers.patch by Matt Kern.
[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_FIXED64;
103         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
104         
105         field.type = PB_LTYPE_STRING;
106         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
107         
108         field.type = PB_LTYPE_FIXED32;
109         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
110     }
111     
112     {
113         uint8_t buffer[30];
114         pb_ostream_t s;
115         
116         COMMENT("Test pb_encode_string")
117         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd", 4), "\x04""abcd"));
118         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd\x00", 5), "\x05""abcd\x00"));
119         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"", 0), "\x00"));
120     }
121     
122     {
123         uint8_t buffer[30];
124         pb_ostream_t s;
125         uint8_t value = 1;
126         int8_t svalue = -1;
127         int32_t max = INT32_MAX;
128         int32_t min = INT32_MIN;
129         int64_t lmax = INT64_MAX;
130         int64_t lmin = INT64_MIN;
131         pb_field_t field = {1, PB_LTYPE_VARINT, 0, 0, sizeof(value)};
132         
133         COMMENT("Test pb_enc_varint and pb_enc_svarint")
134         TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
135         TEST(WRITES(pb_enc_svarint(&s, &field, &svalue), "\x01"));
136         TEST(WRITES(pb_enc_svarint(&s, &field, &value), "\x02"));
137         
138         field.data_size = sizeof(max);
139         TEST(WRITES(pb_enc_svarint(&s, &field, &max), "\xfe\xff\xff\xff\x0f"));
140         TEST(WRITES(pb_enc_svarint(&s, &field, &min), "\xff\xff\xff\xff\x0f"));
141         
142         field.data_size = sizeof(lmax);
143         TEST(WRITES(pb_enc_svarint(&s, &field, &lmax), "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
144         TEST(WRITES(pb_enc_svarint(&s, &field, &lmin), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
145     }
146     
147     {
148         uint8_t buffer[30];
149         pb_ostream_t s;
150         float fvalue;
151         double dvalue;
152         
153         COMMENT("Test pb_enc_fixed32 using float")
154         fvalue = 0.0f;
155         TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\x00\x00"))
156         fvalue = 99.0f;
157         TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\xc6\x42"))
158         fvalue = -12345678.0f;
159         TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x4e\x61\x3c\xcb"))
160     
161         COMMENT("Test pb_enc_fixed64 using double")
162         dvalue = 0.0;
163         TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
164         dvalue = 99.0;
165         TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
166         dvalue = -12345678.0;
167         TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
168     }
169     
170     {
171         uint8_t buffer[30];
172         pb_ostream_t s;
173         struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}};
174     
175         COMMENT("Test pb_enc_bytes")
176         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy"))
177         value.size = 0;
178         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00"))
179     }
180     
181     {
182         uint8_t buffer[30];
183         pb_ostream_t s;
184         char value[] = "xyzzy";
185         
186         COMMENT("Test pb_enc_string")
187         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
188         value[0] = '\0';
189         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
190     }
191     
192     {
193         uint8_t buffer[10];
194         pb_ostream_t s;
195         IntegerArray msg = {5, {1, 2, 3, 4, 5}};
196         
197         COMMENT("Test pb_encode with int32 array")
198         
199         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
200         
201         msg.data_count = 0;
202         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
203         
204         msg.data_count = 10;
205         TEST(!pb_encode(&s, IntegerArray_fields, &msg))
206     }
207     
208     {
209         uint8_t buffer[10];
210         pb_ostream_t s;
211         FloatArray msg = {1, {99.0f}};
212         
213         COMMENT("Test pb_encode with float array")
214         
215         TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg),
216                     "\x0A\x04\x00\x00\xc6\x42"))
217         
218         msg.data_count = 0;
219         TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg), ""))
220         
221         msg.data_count = 3;
222         TEST(!pb_encode(&s, FloatArray_fields, &msg))
223     }
224     
225     {
226         uint8_t buffer[10];
227         pb_ostream_t s;
228         CallbackArray msg;
229         
230         msg.data.funcs.encode = &fieldcallback;
231         
232         COMMENT("Test pb_encode with callback field.")
233         TEST(WRITES(pb_encode(&s, CallbackArray_fields, &msg), "\x08\x55"))
234     }
235     
236     {
237         uint8_t buffer[10];
238         pb_ostream_t s;
239         IntegerContainer msg = {{5, {1,2,3,4,5}}};
240         
241         COMMENT("Test pb_encode with packed array in a submessage.")
242         TEST(WRITES(pb_encode(&s, IntegerContainer_fields, &msg),
243                     "\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
244     }
245     
246     {
247         uint8_t buffer[10];
248         pb_ostream_t s;
249         CallbackContainer msg;
250         CallbackContainerContainer msg2;
251         uint32_t state = 1;
252         
253         msg.submsg.data.funcs.encode = &fieldcallback;
254         msg2.submsg.submsg.data.funcs.encode = &fieldcallback;
255         
256         COMMENT("Test pb_encode with callback field in a submessage.")
257         TEST(WRITES(pb_encode(&s, CallbackContainer_fields, &msg), "\x0A\x02\x08\x55"))
258         TEST(WRITES(pb_encode(&s, CallbackContainerContainer_fields, &msg2),
259                     "\x0A\x04\x0A\x02\x08\x55"))
260         
261         /* Misbehaving callback: varying output between calls */
262         msg.submsg.data.funcs.encode = &crazyfieldcallback;
263         msg.submsg.data.arg = &state;
264         msg2.submsg.submsg.data.funcs.encode = &crazyfieldcallback;
265         msg2.submsg.submsg.data.arg = &state;
266         
267         TEST(!pb_encode(&s, CallbackContainer_fields, &msg))
268         state = 1;
269         TEST(!pb_encode(&s, CallbackContainerContainer_fields, &msg2))
270     }
271     
272     if (status != 0)
273         fprintf(stdout, "\n\nSome tests FAILED!\n");
274     
275     return status;
276 }