Beispiel #1
0
/* Oneofs */
static bool test_OneofMessage()
{
    uint8_t buffer[256];
    size_t msgsize;

    {
        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

        /* Encode first with TestMessage */
        {
            OneofMessage msg = OneofMessage_init_zero;
            msg.which_msgs = OneofMessage_msg1_tag;

            fill_TestMessage(&msg.msgs.msg1);

            if (!pb_encode(&stream, OneofMessage_fields, &msg))
            {
                fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
                return false;
            }
        }

        /* Encode second with SubMessage, invoking 'merge' behaviour */
        {
            OneofMessage msg = OneofMessage_init_zero;
            msg.which_msgs = OneofMessage_msg2_tag;

            msg.first = 999;
            msg.msgs.msg2.dynamic_str = "ABCD";
            msg.last = 888;

            if (!pb_encode(&stream, OneofMessage_fields, &msg))
            {
                fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
                return false;
            }
        }
        msgsize = stream.bytes_written;
    }

    {
        OneofMessage msg = OneofMessage_init_zero;
        pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
        if (!pb_decode(&stream, OneofMessage_fields, &msg))
        {
            fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
            return false;
        }

        TEST(msg.first == 999);
        TEST(msg.which_msgs == OneofMessage_msg2_tag);
        TEST(msg.msgs.msg2.dynamic_str);
        TEST(strcmp(msg.msgs.msg2.dynamic_str, "ABCD") == 0);
        TEST(msg.msgs.msg2.dynamic_str_arr == NULL);
        TEST(msg.msgs.msg2.dynamic_submsg == NULL);
        TEST(msg.last == 888);

        pb_release(OneofMessage_fields, &msg);
        TEST(get_alloc_count() == 0);
        pb_release(OneofMessage_fields, &msg);
        TEST(get_alloc_count() == 0);
    }

    return true;
}
Beispiel #2
0
/* Basic fields, nested submessages, extensions */
static bool test_TestMessage()
{
    uint8_t buffer[256];
    size_t msgsize;
    
    /* Construct a message with various fields filled in */
    {
        TestMessage msg = TestMessage_init_zero;
        pb_ostream_t stream;

        fill_TestMessage(&msg);
        
        stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
        if (!pb_encode(&stream, TestMessage_fields, &msg))
        {
            fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
            return false;
        }
        msgsize = stream.bytes_written;
    }
    
    /* Output encoded message for debug */
    SET_BINARY_MODE(stdout);
    fwrite(buffer, 1, msgsize, stdout);
    
    /* Decode memory using dynamic allocation */
    {
        TestMessage msg = TestMessage_init_zero;
        pb_istream_t stream;
        SubMessage ext2_dest;

        msg.extensions = &ext1;
        ext1.type = &dynamic_ext;
        ext1.dest = NULL;
        ext1.next = &ext2;
        ext2.type = &static_ext;
        ext2.dest = &ext2_dest;
        ext2.next = NULL;
        
        stream = pb_istream_from_buffer(buffer, msgsize);
        if (!pb_decode(&stream, TestMessage_fields, &msg))
        {
            fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
            return false;
        }
        
        /* Make sure it encodes back to same data */
        {
            uint8_t buffer2[256];
            pb_ostream_t ostream = pb_ostream_from_buffer(buffer2, sizeof(buffer2));
            TEST(pb_encode(&ostream, TestMessage_fields, &msg));
            TEST(ostream.bytes_written == msgsize);
            TEST(memcmp(buffer, buffer2, msgsize) == 0);
        }
        
        /* Make sure that malloc counters work */
        TEST(get_alloc_count() > 0);
        
        /* Make sure that pb_release releases everything */
        pb_release(TestMessage_fields, &msg);
        TEST(get_alloc_count() == 0);
        
        /* Check that double-free is a no-op */
        pb_release(TestMessage_fields, &msg);
        TEST(get_alloc_count() == 0);
    }
    
    return true;
}
/* This function is called once from main(), it handles
   the decoding and checks the fields. */
bool check_alltypes(pb_istream_t *stream, int mode)
{
    /* Values for use from callbacks through pointers. */
    bool status;
    uint32_t    req_fixed32     = 1008;
    int32_t     req_sfixed32    = -1009;
    float       req_float       = 1010.0f;
    uint64_t    req_fixed64     = 1011;
    int64_t     req_sfixed64    = -1012;
    double      req_double      = 1013.0;
    SubMessage  req_submsg      = {"1016", 1016, false, 3};
    
    int32_t     rep_int32[5]    = {0, 0, 0, 0, -2001};
    int32_t     rep_int64[5]    = {0, 0, 0, 0, -2002};
    int32_t     rep_uint32[5]   = {0, 0, 0, 0,  2003};
    int32_t     rep_uint64[5]   = {0, 0, 0, 0,  2004};
    int32_t     rep_sint32[5]   = {0, 0, 0, 0, -2005};
    int32_t     rep_sint64[5]   = {0, 0, 0, 0, -2006};
    int32_t     rep_bool[5]     = {false, false, false, false, true};
    uint32_t    rep_fixed32[5]  = {0, 0, 0, 0,  2008};
    int32_t     rep_sfixed32[5] = {0, 0, 0, 0, -2009};
    float       rep_float[5]    = {0, 0, 0, 0,  2010.0f};
    uint64_t    rep_fixed64[5]  = {0, 0, 0, 0,  2011};
    int64_t     rep_sfixed64[5] = {0, 0, 0, 0, -2012};
    double      rep_double[5]   = {0, 0, 0, 0,  2013.0};
    char*       rep_string[5]   = {"", "", "", "", "2014"};
    char*       rep_bytes[5]    = {"", "", "", "", "2015"};
    SubMessage  rep_submsg[5]   = {{"", 0, 0, 3},
                                   {"", 0, 0, 3},
                                   {"", 0, 0, 3},
                                   {"", 0, 0, 3},
                                   {"2016", 2016, true, 2016}};
    int32_t     rep_enum[5]     = {0, 0, 0, 0, MyEnum_Truth};
    
    uint32_t    opt_fixed32     = 3048;
    int32_t     opt_sfixed32    = 3049;
    float       opt_float       = 3050.0f;
    uint64_t    opt_fixed64     = 3051;
    int64_t     opt_sfixed64    = 3052;
    double      opt_double      = 3053.0f;
    SubMessage  opt_submsg      = {"3056", 3056, false, 3};

    SubMessage  oneof_msg1      = {"4059", 4059, false, 3};
    
    /* Bind callbacks for required fields */
    AllTypes alltypes = AllTypes_init_zero;
    
    alltypes.req_int32.funcs.decode = &read_varint;
    alltypes.req_int32.arg = (void*)-1001;
    
    alltypes.req_int64.funcs.decode = &read_varint;
    alltypes.req_int64.arg = (void*)-1002;
    
    alltypes.req_uint32.funcs.decode = &read_varint;
    alltypes.req_uint32.arg = (void*)1003;

    alltypes.req_uint32.funcs.decode = &read_varint;
    alltypes.req_uint32.arg = (void*)1003;    
    
    alltypes.req_uint64.funcs.decode = &read_varint;
    alltypes.req_uint64.arg = (void*)1004;
    
    alltypes.req_sint32.funcs.decode = &read_svarint;
    alltypes.req_sint32.arg = (void*)-1005;   
    
    alltypes.req_sint64.funcs.decode = &read_svarint;
    alltypes.req_sint64.arg = (void*)-1006;   
    
    alltypes.req_bool.funcs.decode = &read_varint;
    alltypes.req_bool.arg = (void*)true;   
    
    alltypes.req_fixed32.funcs.decode = &read_fixed32;
    alltypes.req_fixed32.arg = &req_fixed32;
    
    alltypes.req_sfixed32.funcs.decode = &read_fixed32;
    alltypes.req_sfixed32.arg = &req_sfixed32;
    
    alltypes.req_float.funcs.decode = &read_fixed32;
    alltypes.req_float.arg = &req_float;
    
    alltypes.req_fixed64.funcs.decode = &read_fixed64;
    alltypes.req_fixed64.arg = &req_fixed64;
    
    alltypes.req_sfixed64.funcs.decode = &read_fixed64;
    alltypes.req_sfixed64.arg = &req_sfixed64;
    
    alltypes.req_double.funcs.decode = &read_fixed64;
    alltypes.req_double.arg = &req_double;
    
    alltypes.req_string.funcs.decode = &read_string;
    alltypes.req_string.arg = "1014";
    
    alltypes.req_bytes.funcs.decode = &read_string;
    alltypes.req_bytes.arg = "1015";
    
    alltypes.req_submsg.funcs.decode = &read_submsg;
    alltypes.req_submsg.arg = &req_submsg;
    
    alltypes.req_enum.funcs.decode = &read_varint;
    alltypes.req_enum.arg = (void*)MyEnum_Truth;
    
    alltypes.req_emptymsg.funcs.decode = &read_emptymsg;
    
    /* Bind callbacks for repeated fields */
    alltypes.rep_int32.funcs.decode = &read_repeated_varint;
    alltypes.rep_int32.arg = rep_int32;
    
    alltypes.rep_int64.funcs.decode = &read_repeated_varint;
    alltypes.rep_int64.arg = rep_int64;
    
    alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
    alltypes.rep_uint32.arg = rep_uint32;
    
    alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
    alltypes.rep_uint64.arg = rep_uint64;
    
    alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
    alltypes.rep_sint32.arg = rep_sint32;
    
    alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
    alltypes.rep_sint64.arg = rep_sint64;
    
    alltypes.rep_bool.funcs.decode = &read_repeated_varint;
    alltypes.rep_bool.arg = rep_bool;
    
    alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
    alltypes.rep_fixed32.arg = rep_fixed32;
    
    alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
    alltypes.rep_sfixed32.arg = rep_sfixed32;
    
    alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
    alltypes.rep_float.arg = rep_float;
    
    alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
    alltypes.rep_fixed64.arg = rep_fixed64;
    
    alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
    alltypes.rep_sfixed64.arg = rep_sfixed64;
    
    alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
    alltypes.rep_double.arg = rep_double;
    
    alltypes.rep_string.funcs.decode = &read_repeated_string;
    alltypes.rep_string.arg = rep_string;
    
    alltypes.rep_bytes.funcs.decode = &read_repeated_string;
    alltypes.rep_bytes.arg = rep_bytes;
    
    alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
    alltypes.rep_submsg.arg = rep_submsg;
    
    alltypes.rep_enum.funcs.decode = &read_repeated_varint;
    alltypes.rep_enum.arg = rep_enum;
    
    alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
    
    alltypes.req_limits.funcs.decode = &read_limits;
    
    alltypes.end.funcs.decode = &read_varint;
    alltypes.end.arg = (void*)1099;
    
    /* Bind callbacks for optional fields */
    if (mode == 1)
    {
        alltypes.opt_int32.funcs.decode = &read_varint;
        alltypes.opt_int32.arg = (void*)3041;
        
        alltypes.opt_int64.funcs.decode = &read_varint;
        alltypes.opt_int64.arg = (void*)3042;
        
        alltypes.opt_uint32.funcs.decode = &read_varint;
        alltypes.opt_uint32.arg = (void*)3043;
        
        alltypes.opt_uint64.funcs.decode = &read_varint;
        alltypes.opt_uint64.arg = (void*)3044;
        
        alltypes.opt_sint32.funcs.decode = &read_svarint;
        alltypes.opt_sint32.arg = (void*)3045;
        
        alltypes.opt_sint64.funcs.decode = &read_svarint;
        alltypes.opt_sint64.arg = (void*)3046;
        
        alltypes.opt_bool.funcs.decode = &read_varint;
        alltypes.opt_bool.arg = (void*)true;

        alltypes.opt_fixed32.funcs.decode = &read_fixed32;
        alltypes.opt_fixed32.arg = &opt_fixed32;
        
        alltypes.opt_sfixed32.funcs.decode = &read_fixed32;
        alltypes.opt_sfixed32.arg = &opt_sfixed32;
        
        alltypes.opt_float.funcs.decode = &read_fixed32;
        alltypes.opt_float.arg = &opt_float;
        
        alltypes.opt_fixed64.funcs.decode = &read_fixed64;
        alltypes.opt_fixed64.arg = &opt_fixed64;
        
        alltypes.opt_sfixed64.funcs.decode = &read_fixed64;
        alltypes.opt_sfixed64.arg = &opt_sfixed64;
        
        alltypes.opt_double.funcs.decode = &read_fixed64;
        alltypes.opt_double.arg = &opt_double;
        
        alltypes.opt_string.funcs.decode = &read_string;
        alltypes.opt_string.arg = "3054";
        
        alltypes.opt_bytes.funcs.decode = &read_string;
        alltypes.opt_bytes.arg = "3055";
        
        alltypes.opt_submsg.funcs.decode = &read_submsg;
        alltypes.opt_submsg.arg = &opt_submsg;
        
        alltypes.opt_enum.funcs.decode = &read_varint;
        alltypes.opt_enum.arg = (void*)MyEnum_Truth;
        
        alltypes.opt_emptymsg.funcs.decode = &read_emptymsg;

        alltypes.oneof_msg1.funcs.decode = &read_submsg;
        alltypes.oneof_msg1.arg = &oneof_msg1;
    }
    
    status = pb_decode(stream, AllTypes_fields, &alltypes);
    
#ifdef PB_ENABLE_MALLOC
    /* Just to check for any interference between pb_release() and callback fields */
    pb_release(AllTypes_fields, &alltypes);
#endif

    return status;
}