MOS Source Code
Loading...
Searching...
No Matches
test_extensions.c
Go to the documentation of this file.
1#include <pb_encode.h>
2#include <pb_decode.h>
3#include "extensions.pb.h"
4#include "unittests.h"
5
6int 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}
int main()
#define pb_extension_init_zero
Definition pb.h:477
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
#define NULL
Definition pb_syshdr.h:46
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3
static char buffer[2048]
Definition test_printf.c:7
#define COMMENT(x)
Definition unittests.h:17