1 #define NANOPB_INTERNALS
7 #include "unittestproto.pb.h"
9 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
11 /* Allow only 'x' to be written */
20 bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
23 if (!pb_encode_tag_for_field(stream, field))
25 return pb_encode_varint(stream, value);
28 bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
30 /* This callback writes different amount of data the second time. */
31 uint32_t *state = (uint32_t*)arg;
33 if (!pb_encode_tag_for_field(stream, field))
35 return pb_encode_varint(stream, *state);
38 /* Check that expression x writes data y.
39 * Y is a string, which may contain null bytes. Null terminator is ignored.
41 #define WRITES(x, y) \
42 memset(buffer, 0xAA, sizeof(buffer)), \
43 s = pb_ostream_from_buffer(buffer, sizeof(buffer)), \
45 memcmp(buffer, y, sizeof(y) - 1) == 0 && \
46 buffer[sizeof(y) - 1] == 0xAA
53 uint8_t buffer1[] = "foobartest1234";
54 uint8_t buffer2[sizeof(buffer1)];
55 pb_ostream_t stream = pb_ostream_from_buffer(buffer2, sizeof(buffer1));
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));
65 uint8_t buffer1[] = "xxxxxxx";
66 pb_ostream_t stream = {&streamcallback, 0, SIZE_MAX, 0};
68 COMMENT("Test pb_write with custom callback");
69 TEST(pb_write(&stream, buffer1, 5));
71 TEST(!pb_write(&stream, buffer1, 5));
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"));
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"));
99 pb_field_t field = {10, PB_LTYPE_SVARINT};
101 COMMENT("Test pb_encode_tag_for_field")
102 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
104 field.type = PB_LTYPE_FIXED64;
105 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
107 field.type = PB_LTYPE_STRING;
108 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
110 field.type = PB_LTYPE_FIXED32;
111 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
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"));
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)};
134 COMMENT("Test pb_enc_varint and pb_enc_svarint")
135 TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
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"));
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"));
152 COMMENT("Test pb_enc_fixed32 using float")
154 TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\x00\x00"))
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"))
160 COMMENT("Test pb_enc_fixed64 using double")
162 TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
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"))
172 struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}};
174 COMMENT("Test pb_enc_bytes")
175 TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy"))
177 TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00"))
183 char value[30] = "xyzzy";
185 COMMENT("Test pb_enc_string")
186 TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x05xyzzy"))
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"))
196 IntegerArray msg = {5, {1, 2, 3, 4, 5}};
198 COMMENT("Test pb_encode with int32 array")
200 TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
203 TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
206 TEST(!pb_encode(&s, IntegerArray_fields, &msg))
212 FloatArray msg = {1, {99.0f}};
214 COMMENT("Test pb_encode with float array")
216 TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg),
217 "\x0A\x04\x00\x00\xc6\x42"))
220 TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg), ""))
223 TEST(!pb_encode(&s, FloatArray_fields, &msg))
231 msg.data.funcs.encode = &fieldcallback;
233 COMMENT("Test pb_encode with callback field.")
234 TEST(WRITES(pb_encode(&s, CallbackArray_fields, &msg), "\x08\x55"))
240 IntegerContainer msg = {{5, {1,2,3,4,5}}};
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"))
250 CallbackContainer msg;
251 CallbackContainerContainer msg2;
254 msg.submsg.data.funcs.encode = &fieldcallback;
255 msg2.submsg.submsg.data.funcs.encode = &fieldcallback;
257 COMMENT("Test pb_encode with callback field in a submessage.")
258 TEST(WRITES(pb_encode(&s, CallbackContainer_fields, &msg), "\x0A\x02\x08\x55"))
259 TEST(WRITES(pb_encode(&s, CallbackContainerContainer_fields, &msg2),
260 "\x0A\x04\x0A\x02\x08\x55"))
262 /* Misbehaving callback: varying output between calls */
263 msg.submsg.data.funcs.encode = &crazyfieldcallback;
264 msg.submsg.data.arg = &state;
265 msg2.submsg.submsg.data.funcs.encode = &crazyfieldcallback;
266 msg2.submsg.submsg.data.arg = &state;
268 TEST(!pb_encode(&s, CallbackContainer_fields, &msg))
270 TEST(!pb_encode(&s, CallbackContainerContainer_fields, &msg2))
274 fprintf(stdout, "\n\nSome tests FAILED!\n");