1 #include <stdio.h>
2 #include <string.h>
3 #include <pb_decode.h>
4 #include <pb_encode.h>
5 #include "unittests.h"
6 #include "fixed_count.pb.h"
7 
main()8 int main()
9 {
10     int status = 0;
11 
12     {
13       pb_byte_t buffer[Message1_size];
14       Message1 msg_a = Message1_init_zero;
15       Message1 msg_b = Message1_init_zero;
16 
17       pb_ostream_t ostream;
18       pb_istream_t istream;
19       size_t message_length;
20 
21       msg_a.data[0] = 1;
22       msg_a.data[0] = 2;
23       msg_a.data[0] = 3;
24 
25       COMMENT("Test encode and decode with three entries");
26 
27       ostream = pb_ostream_from_buffer(buffer, Message1_size);
28       TEST(pb_encode(&ostream, Message1_fields, &msg_a));
29       message_length = ostream.bytes_written;
30 
31       istream = pb_istream_from_buffer(buffer, message_length);
32       TEST(pb_decode(&istream, Message1_fields, &msg_b));
33 
34       TEST(istream.bytes_left == 0);
35       TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
36     }
37 
38     {
39       pb_byte_t input[] = {0x08, 0x00, 0x08, 0x01, 0x08, 0x02, 0x08, 0x03};
40       pb_istream_t stream;
41       Message1 msg = Message1_init_zero;
42 
43       COMMENT("Test wrong number of entries");
44 
45       stream = pb_istream_from_buffer(input, 6);
46       TEST(pb_decode(&stream, Message1_fields, &msg));
47       TEST(msg.data[0] == 0 && msg.data[1] == 1 && msg.data[2] == 2);
48 
49       stream = pb_istream_from_buffer(input, 8);
50       TEST(!pb_decode(&stream, Message1_fields, &msg));
51       TEST(strcmp(stream.errmsg, "array overflow") == 0);
52 
53       stream = pb_istream_from_buffer(input, 4);
54       TEST(!pb_decode(&stream, Message1_fields, &msg));
55       TEST(strcmp(stream.errmsg, "wrong size for fixed count field") == 0);
56     }
57 
58     {
59       pb_byte_t buffer[Message2_size];
60       Message2 msg_a = Message2_init_zero;
61       Message2 msg_b = Message2_init_zero;
62 
63       pb_ostream_t ostream;
64       pb_istream_t istream;
65       size_t message_length;
66 
67       COMMENT("Test encode and decode with nested messages");
68 
69       msg_a.data[0].data[0] = 1;
70       msg_a.data[0].data[1] = 2;
71       msg_a.data[0].data[2] = 3;
72       msg_a.data[1].data[0] = 4;
73       msg_a.data[1].data[1] = 5;
74       msg_a.data[1].data[2] = 6;
75 
76       ostream = pb_ostream_from_buffer(buffer, Message2_size);
77       TEST(pb_encode(&ostream, Message2_fields, &msg_a));
78       message_length = ostream.bytes_written;
79 
80       istream = pb_istream_from_buffer(buffer, message_length);
81       TEST(pb_decode(&istream, Message2_fields, &msg_b));
82 
83       TEST(istream.bytes_left == 0);
84       TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
85     }
86 
87     {
88       pb_byte_t buffer[Message3_size];
89       Message3 msg_a = Message3_init_zero;
90       Message3 msg_b = Message3_init_zero;
91 
92       pb_ostream_t ostream;
93       pb_istream_t istream;
94       size_t message_length;
95 
96       COMMENT("Test encode and decode with two nested messages");
97 
98       msg_a.data1[0].data[0].data[0] = 1;
99       msg_a.data1[0].data[0].data[1] = 2;
100       msg_a.data1[0].data[0].data[2] = 3;
101       msg_a.data1[0].data[1].data[0] = 4;
102       msg_a.data1[0].data[1].data[1] = 5;
103       msg_a.data1[0].data[1].data[2] = 6;
104 
105       msg_a.data1[1].data[0].data[0] = 7;
106       msg_a.data1[1].data[0].data[1] = 8;
107       msg_a.data1[1].data[0].data[2] = 9;
108       msg_a.data1[1].data[1].data[0] = 10;
109       msg_a.data1[1].data[1].data[1] = 11;
110       msg_a.data1[1].data[1].data[2] = 12;
111 
112       msg_a.data2[0].data[0].data[0] = 11;
113       msg_a.data2[0].data[0].data[1] = 12;
114       msg_a.data2[0].data[0].data[2] = 13;
115       msg_a.data2[0].data[1].data[0] = 14;
116       msg_a.data2[0].data[1].data[1] = 15;
117       msg_a.data2[0].data[1].data[2] = 16;
118 
119       msg_a.data2[1].data[0].data[0] = 17;
120       msg_a.data2[1].data[0].data[1] = 18;
121       msg_a.data2[1].data[0].data[2] = 19;
122       msg_a.data2[1].data[1].data[0] = 110;
123       msg_a.data2[1].data[1].data[1] = 111;
124       msg_a.data2[1].data[1].data[2] = 112;
125 
126       ostream = pb_ostream_from_buffer(buffer, Message3_size);
127       TEST(pb_encode(&ostream, Message3_fields, &msg_a));
128       message_length = ostream.bytes_written;
129 
130       istream = pb_istream_from_buffer(buffer, message_length);
131       TEST(pb_decode(&istream, Message3_fields, &msg_b));
132 
133       TEST(istream.bytes_left == 0);
134       TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
135     }
136 
137     if (status != 0)
138         fprintf(stdout, "\n\nSome tests FAILED!\n");
139 
140     return status;
141 }
142