1 #include <pb_encode.h>
2 #include <pb_decode.h>
3 #include "extensions.pb.h"
4 #include "unittests.h"
5 
main()6 int main()
7 {
8     uint8_t buffer[256];
9     int status = 0;
10     size_t msglen;
11 
12     {
13         pb_ostream_t stream;
14         SimpleMessage msg = SimpleMessage_init_zero;
15         ExtMessage extmsg = ExtMessage_init_zero;
16         pb_extension_t ext = pb_extension_init_zero;
17 
18         COMMENT("Encoding message");
19         msg.has_number = true;
20         msg.number = 1234;
21         extmsg.has_second_number = true;
22         extmsg.second_number = 5678;
23 
24         msg.extensions = &ext;
25         ext.type = &ExtMessage_ext_message_ext;
26         ext.dest = &extmsg;
27         ext.next = NULL;
28 
29         stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
30         TEST(pb_encode(&stream, SimpleMessage_fields, &msg));
31         msglen = stream.bytes_written;
32         TEST(msglen == 9); /* 3 for number, 3 for submsg tag+len, 3 for second_number */
33         TEST(buffer[0] == 0x08); /* varint 1 */
34         TEST(buffer[1] == 0xD2); /* number = 1234 */
35         TEST(buffer[2] == 0x09);
36         TEST(buffer[3] == 0xE2); /* submsg 1100 */
37         TEST(buffer[4] == 0x44);
38         TEST(buffer[5] == 0x03); /* length 3 */
39         TEST(buffer[6] == 0x08); /* varint 1 */
40         TEST(buffer[7] == 0xae); /* second_number = 5678 */
41         TEST(buffer[8] == 0x2c);
42     }
43 
44     {
45         pb_istream_t stream;
46         SimpleMessage msg = SimpleMessage_init_zero;
47         ExtMessage extmsg = ExtMessage_init_zero;
48         pb_extension_t ext = pb_extension_init_zero;
49 
50         COMMENT("Decoding message");
51 
52         msg.extensions = &ext;
53         ext.type = &ExtMessage_ext_message_ext;
54         ext.dest = &extmsg;
55         ext.next = NULL;
56         ext.found = false;
57 
58         stream = pb_istream_from_buffer(buffer, msglen);
59         TEST(pb_decode(&stream, SimpleMessage_fields, &msg));
60         TEST(msg.has_number);
61         TEST(msg.number == 1234);
62         TEST(ext.found);
63         TEST(extmsg.has_second_number);
64         TEST(extmsg.second_number == 5678);
65     }
66 
67     return status;
68 }
69