Home | History | Annotate | Download | only in decode_unittests
      1 /* This includes the whole .c file to get access to static functions. */
      2 #include "pb_decode.c"
      3 
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include "unittests.h"
      7 #include "unittestproto.pb.h"
      8 
      9 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
     10 
     11 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
     12 {
     13     if (stream->state != NULL)
     14         return false; /* Simulate error */
     15 
     16     if (buf != NULL)
     17         memset(buf, 'x', count);
     18     return true;
     19 }
     20 
     21 /* Verifies that the stream passed to callback matches the byte array pointed to by arg. */
     22 bool callback_check(pb_istream_t *stream, const pb_field_t *field, void **arg)
     23 {
     24     int i;
     25     uint8_t byte;
     26     pb_bytes_array_t *ref = (pb_bytes_array_t*) *arg;
     27 
     28     for (i = 0; i < ref->size; i++)
     29     {
     30         if (!pb_read(stream, &byte, 1))
     31             return false;
     32 
     33         if (byte != ref->bytes[i])
     34             return false;
     35     }
     36 
     37     return true;
     38 }
     39 
     40 int main()
     41 {
     42     int status = 0;
     43 
     44     {
     45         uint8_t buffer1[] = "foobartest1234";
     46         uint8_t buffer2[sizeof(buffer1)];
     47         pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
     48 
     49         COMMENT("Test pb_read and pb_istream_t");
     50         TEST(pb_read(&stream, buffer2, 6))
     51         TEST(memcmp(buffer2, "foobar", 6) == 0)
     52         TEST(stream.bytes_left == sizeof(buffer1) - 6)
     53         TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
     54         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
     55         TEST(stream.bytes_left == 0)
     56         TEST(!pb_read(&stream, buffer2, 1))
     57     }
     58 
     59     {
     60         uint8_t buffer[20];
     61         pb_istream_t stream = {&stream_callback, NULL, 20};
     62 
     63         COMMENT("Test pb_read with custom callback");
     64         TEST(pb_read(&stream, buffer, 5))
     65         TEST(memcmp(buffer, "xxxxx", 5) == 0)
     66         TEST(!pb_read(&stream, buffer, 50))
     67         stream.state = (void*)1; /* Simulated error return from callback */
     68         TEST(!pb_read(&stream, buffer, 5))
     69         stream.state = NULL;
     70         TEST(pb_read(&stream, buffer, 15))
     71     }
     72 
     73     {
     74         pb_istream_t s;
     75         uint64_t u;
     76         int64_t i;
     77 
     78         COMMENT("Test pb_decode_varint");
     79         TEST((s = S("\x00"), pb_decode_varint(&s, &u) && u == 0));
     80         TEST((s = S("\x01"), pb_decode_varint(&s, &u) && u == 1));
     81         TEST((s = S("\xAC\x02"), pb_decode_varint(&s, &u) && u == 300));
     82         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, &u) && u == UINT32_MAX));
     83         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, (uint64_t*)&i) && i == UINT32_MAX));
     84         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
     85               pb_decode_varint(&s, (uint64_t*)&i) && i == -1));
     86         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
     87               pb_decode_varint(&s, &u) && u == UINT64_MAX));
     88     }
     89 
     90     {
     91         pb_istream_t s;
     92         COMMENT("Test pb_skip_varint");
     93         TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
     94         TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
     95         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
     96               pb_skip_varint(&s) && s.bytes_left == 6))
     97         TEST((s = S("\xFF"), !pb_skip_varint(&s)))
     98     }
     99 
    100     {
    101         pb_istream_t s;
    102         COMMENT("Test pb_skip_string")
    103         TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 6))
    104         TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 6))
    105         TEST((s = S("\x04"), !pb_skip_string(&s)))
    106         TEST((s = S("\xFF"), !pb_skip_string(&s)))
    107     }
    108 
    109     {
    110         pb_istream_t s = S("\x01\xFF\xFF\x03");
    111         pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0};
    112         uint32_t d;
    113         COMMENT("Test pb_dec_varint using uint32_t")
    114         TEST(pb_dec_varint(&s, &f, &d) && d == 1)
    115 
    116         /* Verify that no more than data_size is written. */
    117         d = 0;
    118         f.data_size = 1;
    119         TEST(pb_dec_varint(&s, &f, &d) && (d == 0xFF || d == 0xFF000000))
    120     }
    121 
    122     {
    123         pb_istream_t s;
    124         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
    125         int32_t d;
    126 
    127         COMMENT("Test pb_dec_svarint using int32_t")
    128         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
    129         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
    130         TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX))
    131         TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN))
    132     }
    133 
    134     {
    135         pb_istream_t s;
    136         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 8, 0, 0};
    137         uint64_t d;
    138 
    139         COMMENT("Test pb_dec_svarint using uint64_t")
    140         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
    141         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
    142         TEST((s = S("\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MAX))
    143         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MIN))
    144     }
    145 
    146     {
    147         pb_istream_t s;
    148         pb_field_t f = {1, PB_LTYPE_FIXED32, 0, 0, 4, 0, 0};
    149         float d;
    150 
    151         COMMENT("Test pb_dec_fixed32 using float (failures here may be caused by imperfect rounding)")
    152         TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed32(&s, &f, &d) && d == 0.0f))
    153         TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed32(&s, &f, &d) && d == 99.0f))
    154         TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f))
    155         TEST((s = S("\x00"), !pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f))
    156     }
    157 
    158     {
    159         pb_istream_t s;
    160         pb_field_t f = {1, PB_LTYPE_FIXED64, 0, 0, 8, 0, 0};
    161         double d;
    162 
    163         COMMENT("Test pb_dec_fixed64 using double (failures here may be caused by imperfect rounding)")
    164         TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed64(&s, &f, &d) && d == 0.0))
    165         TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed64(&s, &f, &d) && d == 99.0))
    166         TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed64(&s, &f, &d) && d == -12345678.0f))
    167     }
    168 
    169     {
    170         pb_istream_t s;
    171         struct { size_t size; uint8_t bytes[5]; } d;
    172         pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, sizeof(d), 0, 0};
    173 
    174         COMMENT("Test pb_dec_bytes")
    175         TEST((s = S("\x00"), pb_dec_bytes(&s, &f, &d) && d.size == 0))
    176         TEST((s = S("\x01\xFF"), pb_dec_bytes(&s, &f, &d) && d.size == 1 && d.bytes[0] == 0xFF))
    177         TEST((s = S("\x05xxxxx"), pb_dec_bytes(&s, &f, &d) && d.size == 5))
    178         TEST((s = S("\x05xxxx"), !pb_dec_bytes(&s, &f, &d)))
    179 
    180         /* Note: the size limit on bytes-fields is not strictly obeyed, as
    181          * the compiler may add some padding to the struct. Using this padding
    182          * is not a very good thing to do, but it is difficult to avoid when
    183          * we use only a single uint8_t to store the size of the field.
    184          * Therefore this tests against a 10-byte string, while otherwise even
    185          * 6 bytes should error out.
    186          */
    187         TEST((s = S("\x10xxxxxxxxxx"), !pb_dec_bytes(&s, &f, &d)))
    188     }
    189 
    190     {
    191         pb_istream_t s;
    192         pb_field_t f = {1, PB_LTYPE_STRING, 0, 0, 5, 0, 0};
    193         char d[5];
    194 
    195         COMMENT("Test pb_dec_string")
    196         TEST((s = S("\x00"), pb_dec_string(&s, &f, &d) && d[0] == '\0'))
    197         TEST((s = S("\x04xyzz"), pb_dec_string(&s, &f, &d) && strcmp(d, "xyzz") == 0))
    198         TEST((s = S("\x05xyzzy"), !pb_dec_string(&s, &f, &d)))
    199     }
    200 
    201     {
    202         pb_istream_t s;
    203         IntegerArray dest;
    204 
    205         COMMENT("Testing pb_decode with repeated int32 field")
    206         TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 0))
    207         TEST((s = S("\x08\x01\x08\x02"), pb_decode(&s, IntegerArray_fields, &dest)
    208              && dest.data_count == 2 && dest.data[0] == 1 && dest.data[1] == 2))
    209         s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A");
    210         TEST(pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 10 && dest.data[9] == 10)
    211         s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A\x08\x0B");
    212         TEST(!pb_decode(&s, IntegerArray_fields, &dest))
    213     }
    214 
    215     {
    216         pb_istream_t s;
    217         IntegerArray dest;
    218 
    219         COMMENT("Testing pb_decode with packed int32 field")
    220         TEST((s = S("\x0A\x00"), pb_decode(&s, IntegerArray_fields, &dest)
    221             && dest.data_count == 0))
    222         TEST((s = S("\x0A\x01\x01"), pb_decode(&s, IntegerArray_fields, &dest)
    223             && dest.data_count == 1 && dest.data[0] == 1))
    224         TEST((s = S("\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"), pb_decode(&s, IntegerArray_fields, &dest)
    225             && dest.data_count == 10 && dest.data[0] == 1 && dest.data[9] == 10))
    226         TEST((s = S("\x0A\x0B\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"), !pb_decode(&s, IntegerArray_fields, &dest)))
    227 
    228         /* Test invalid wire data */
    229         TEST((s = S("\x0A\xFF"), !pb_decode(&s, IntegerArray_fields, &dest)))
    230         TEST((s = S("\x0A\x01"), !pb_decode(&s, IntegerArray_fields, &dest)))
    231     }
    232 
    233     {
    234         pb_istream_t s;
    235         IntegerArray dest;
    236 
    237         COMMENT("Testing pb_decode with unknown fields")
    238         TEST((s = S("\x18\x0F\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
    239             && dest.data_count == 1 && dest.data[0] == 1))
    240         TEST((s = S("\x19\x00\x00\x00\x00\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
    241             && dest.data_count == 1 && dest.data[0] == 1))
    242         TEST((s = S("\x1A\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
    243             && dest.data_count == 1 && dest.data[0] == 1))
    244         TEST((s = S("\x1B\x08\x01"), !pb_decode(&s, IntegerArray_fields, &dest)))
    245         TEST((s = S("\x1D\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)
    246             && dest.data_count == 1 && dest.data[0] == 1))
    247     }
    248 
    249     {
    250         pb_istream_t s;
    251         CallbackArray dest;
    252         struct { size_t size; uint8_t bytes[10]; } ref;
    253         dest.data.funcs.decode = &callback_check;
    254         dest.data.arg = &ref;
    255 
    256         COMMENT("Testing pb_decode with callbacks")
    257         /* Single varint */
    258         ref.size = 1; ref.bytes[0] = 0x55;
    259         TEST((s = S("\x08\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
    260         /* Packed varint */
    261         ref.size = 3; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = 0x55;
    262         TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
    263         /* Packed varint with loop */
    264         ref.size = 1; ref.bytes[0] = 0x55;
    265         TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest)))
    266         /* Single fixed32 */
    267         ref.size = 4; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = ref.bytes[3] = 0xAA;
    268         TEST((s = S("\x0D\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest)))
    269         /* Single fixed64 */
    270         ref.size = 8; memset(ref.bytes, 0xAA, 8);
    271         TEST((s = S("\x09\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest)))
    272         /* Unsupported field type */
    273         TEST((s = S("\x0B\x00"), !pb_decode(&s, CallbackArray_fields, &dest)))
    274 
    275         /* Just make sure that our test function works */
    276         ref.size = 1; ref.bytes[0] = 0x56;
    277         TEST((s = S("\x08\x55"), !pb_decode(&s, CallbackArray_fields, &dest)))
    278     }
    279 
    280     {
    281         pb_istream_t s;
    282         IntegerArray dest;
    283 
    284         COMMENT("Testing pb_decode message termination")
    285         TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest)))
    286         TEST((s = S("\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
    287         TEST((s = S("\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)))
    288         TEST((s = S("\x08\x01\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
    289         TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest)))
    290     }
    291 
    292     {
    293         pb_istream_t s;
    294         IntegerContainer dest = {{0}};
    295 
    296         COMMENT("Testing pb_decode_delimited")
    297         TEST((s = S("\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"),
    298               pb_decode_delimited(&s, IntegerContainer_fields, &dest)) &&
    299               dest.submsg.data_count == 5)
    300     }
    301 
    302     if (status != 0)
    303         fprintf(stdout, "\n\nSome tests FAILED!\n");
    304 
    305     return status;
    306 }
    307