#include #include #include #include #include #include #include struct rest { //# decoder encoder int x; //#1 int z[2]; //#3 num_at=num_z uint8_t b[4]; //#5 num_at=num_b char s[5]; //#7 truncate uint8_t rest[20]; //# rest=here int num_z; int num_b; }; #include "test_rest.pb.h" void test_rest_reset(void **state) { struct rest r; uint8_t buf[1]; (void)state; memset(&r, 0, sizeof(r)); memset(r.rest, 0xff, sizeof(r.rest)); pb_reset_rest(&r); // 1 byte buffer assert_ptr_equal(pb_encode_rest(&r, buf, buf+sizeof(buf)), buf); // 0 bytes buffer assert_ptr_equal(pb_encode_rest(&r, buf, buf), buf); } void test_rest_reencode(void **state) { // x:1, x2:2, z:3, z:4, z2:5, z2:6, z2:7, b:"\x08\x09\x0a", b2:"\x0b\x0c\x0d", s:"abc", s2:"def" const uint8_t raw[] = { 0x08, 0x01, 0x10, 0x02, 0x18, 0x03, 0x18, 0x04, 0x20, 0x05, 0x20, 0x06, 0x20, 0x07, 0x2a, 0x03, 0x08, 0x09, 0x0a, 0x32, 0x03, 0x0b, 0x0c, 0x0d, 0x3a, 0x03, 0x61, 0x62, 0x63, 0x42, 0x03, 0x64, 0x65, 0x66, }; const uint8_t exp[] = { 0x08, 0x01, // x:1 0x18, 0x03, 0x18, 0x04, // z:3, z:4 0x2a, 0x03, 0x08, 0x09, 0x0a, // b:"\x08\x09\x0a" 0x3a, 0x03, 0x61, 0x62, 0x63, // s:"abc" 0x10, 0x02, // x2:2 0x20, 0x05, 0x20, 0x06, 0x20, 0x07, // z2:5, z2:6, z2:7 0x32, 0x03, 0x0b, 0x0c, 0x0d, // b2:"\x0b\x0c\x0d", 0x42, 0x03, 0x64, 0x65, 0x66, // s2:"def" }; uint8_t buf[sizeof(exp)]; struct rest r; (void)state; assert_non_null(pb_decode_rest(&r, raw, raw+sizeof(raw))); assert_int_equal(r.x, 1); assert_int_equal(r.num_z, 2); assert_int_equal(r.z[0], 3); assert_int_equal(r.z[1], 4); assert_int_equal(r.num_b, 3); assert_int_equal(r.b[0], 0x08); assert_int_equal(r.b[1], 0x09); assert_int_equal(r.b[2], 0x0a); assert_string_equal(r.s, "abc"); // re-encode the same message back assert_ptr_equal(pb_encode_rest(&r, buf, buf+sizeof(buf)), buf+sizeof(buf)); assert_memory_equal(buf, exp, sizeof(exp)); // try re-encoding with a smaller buffer assert_null(pb_encode_rest(&r, buf, buf+sizeof(buf)-1)); }