5 #include "unittestproto.pb.h"
7 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
9 /* Allow only 'x' to be written */
18 bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
21 if (!pb_encode_tag_for_field(stream, field))
23 return pb_encode_varint(stream, value);
26 bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
28 /* This callback writes different amount of data the second time. */
29 uint32_t *state = (uint32_t*)arg;
31 if (!pb_encode_tag_for_field(stream, field))
33 return pb_encode_varint(stream, *state);
36 /* Check that expression x writes data y.
37 * Y is a string, which may contain null bytes. Null terminator is ignored.
39 #define WRITES(x, y) \
40 memset(buffer, 0xAA, sizeof(buffer)), \
41 s = pb_ostream_from_buffer(buffer, sizeof(buffer)), \
43 memcmp(buffer, y, sizeof(y) - 1) == 0 && \
44 buffer[sizeof(y) - 1] == 0xAA
51 uint8_t buffer1[] = "foobartest1234";
52 uint8_t buffer2[sizeof(buffer1)];
53 pb_ostream_t stream = pb_ostream_from_buffer(buffer2, sizeof(buffer1));
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));
63 uint8_t buffer1[] = "xxxxxxx";
64 pb_ostream_t stream = {&streamcallback, 0, SIZE_MAX, 0};
66 COMMENT("Test pb_write with custom callback");
67 TEST(pb_write(&stream, buffer1, 5));
69 TEST(!pb_write(&stream, buffer1, 5));
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"));
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"));
97 pb_field_t field = {10, PB_LTYPE_SVARINT};
99 COMMENT("Test pb_encode_tag_for_field")
100 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
102 field.type = PB_LTYPE_FIXED64;
103 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
105 field.type = PB_LTYPE_STRING;
106 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
108 field.type = PB_LTYPE_FIXED32;
109 TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
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"));
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)};
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"));
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"));
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"));
153 COMMENT("Test pb_enc_fixed32 using float")
155 TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\x00\x00"))
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"))
161 COMMENT("Test pb_enc_fixed64 using double")
163 TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
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"))
173 struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}};
175 COMMENT("Test pb_enc_bytes")
176 TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy"))
178 TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00"))
184 char value[] = "xyzzy";
186 COMMENT("Test pb_enc_string")
187 TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
189 TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
195 IntegerArray msg = {5, {1, 2, 3, 4, 5}};
197 COMMENT("Test pb_encode with int32 array")
199 TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
202 TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
205 TEST(!pb_encode(&s, IntegerArray_fields, &msg))
211 FloatArray msg = {1, {99.0f}};
213 COMMENT("Test pb_encode with float array")
215 TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg),
216 "\x0A\x04\x00\x00\xc6\x42"))
219 TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg), ""))
222 TEST(!pb_encode(&s, FloatArray_fields, &msg))
230 msg.data.funcs.encode = &fieldcallback;
232 COMMENT("Test pb_encode with callback field.")
233 TEST(WRITES(pb_encode(&s, CallbackArray_fields, &msg), "\x08\x55"))
239 IntegerContainer msg = {{5, {1,2,3,4,5}}};
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"))
249 CallbackContainer msg;
250 CallbackContainerContainer msg2;
253 msg.submsg.data.funcs.encode = &fieldcallback;
254 msg2.submsg.submsg.data.funcs.encode = &fieldcallback;
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"))
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;
267 TEST(!pb_encode(&s, CallbackContainer_fields, &msg))
269 TEST(!pb_encode(&s, CallbackContainerContainer_fields, &msg2))
273 fprintf(stdout, "\n\nSome tests FAILED!\n");