MOS Source Code
Loading...
Searching...
No Matches
mem_release.c
Go to the documentation of this file.
1/* Make sure that all fields are freed in various scenarios. */
2
3#include <pb_decode.h>
4#include <pb_encode.h>
5#include <malloc_wrappers.h>
6#include <stdio.h>
7#include <test_helpers.h>
8#include "mem_release.pb.h"
9
10#define TEST(x) if (!(x)) { \
11 fprintf(stderr, "Test %s on line %d failed.\n", #x, __LINE__); \
12 return false; \
13 }
14
15static char *test_str_arr[] = {"1", "2", ""};
17static pb_extension_t ext1, ext2;
18
19static void fill_TestMessage(TestMessage *msg)
20{
21 msg->static_req_submsg.dynamic_str = "12345";
22 msg->static_req_submsg.dynamic_str_arr_count = 3;
23 msg->static_req_submsg.dynamic_str_arr = test_str_arr;
24 msg->static_req_submsg.dynamic_submsg_count = 2;
25 msg->static_req_submsg.dynamic_submsg = test_msg_arr;
26 msg->static_req_submsg.dynamic_submsg[1].dynamic_str = "abc";
27 msg->static_opt_submsg.dynamic_str = "abc";
28 msg->static_rep_submsg_count = 2;
29 msg->static_rep_submsg[1].dynamic_str = "abc";
30 msg->has_static_opt_submsg = true;
31 msg->dynamic_submsg = &msg->static_req_submsg;
32
33 msg->extensions = &ext1;
34 ext1.type = &dynamic_ext;
35 ext1.dest = &msg->static_req_submsg;
36 ext1.next = &ext2;
37 ext2.type = &static_ext;
38 ext2.dest = &msg->static_req_submsg;
39 ext2.next = NULL;
40}
41
42/* Basic fields, nested submessages, extensions */
43static bool test_TestMessage()
44{
45 uint8_t buffer[256];
46 size_t msgsize;
47
48 /* Construct a message with various fields filled in */
49 {
50 TestMessage msg = TestMessage_init_zero;
51 pb_ostream_t stream;
52
53 fill_TestMessage(&msg);
54
55 stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
56 if (!pb_encode(&stream, TestMessage_fields, &msg))
57 {
58 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
59 return false;
60 }
61 msgsize = stream.bytes_written;
62 }
63
64 /* Output encoded message for debug */
66 fwrite(buffer, 1, msgsize, stdout);
67
68 /* Decode memory using dynamic allocation */
69 {
70 TestMessage msg = TestMessage_init_zero;
71 pb_istream_t stream;
72 SubMessage ext2_dest;
73
74 msg.extensions = &ext1;
75 ext1.type = &dynamic_ext;
76 ext1.dest = NULL;
77 ext1.next = &ext2;
78 ext2.type = &static_ext;
79 ext2.dest = &ext2_dest;
80 ext2.next = NULL;
81
82 stream = pb_istream_from_buffer(buffer, msgsize);
83 if (!pb_decode(&stream, TestMessage_fields, &msg))
84 {
85 fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
86 return false;
87 }
88
89 /* Make sure it encodes back to same data */
90 {
91 uint8_t buffer2[256];
92 pb_ostream_t ostream = pb_ostream_from_buffer(buffer2, sizeof(buffer2));
93 TEST(pb_encode(&ostream, TestMessage_fields, &msg));
94 TEST(ostream.bytes_written == msgsize);
95 TEST(memcmp(buffer, buffer2, msgsize) == 0);
96 }
97
98 /* Make sure that malloc counters work */
99 TEST(get_alloc_count() > 0);
100
101 /* Make sure that pb_release releases everything */
102 pb_release(TestMessage_fields, &msg);
103 TEST(get_alloc_count() == 0);
104
105 /* Check that double-free is a no-op */
106 pb_release(TestMessage_fields, &msg);
107 TEST(get_alloc_count() == 0);
108 }
109
110 return true;
111}
112
113/* Oneofs */
114static bool test_OneofMessage()
115{
116 uint8_t buffer[256];
117 size_t msgsize;
118
119 {
120 pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
121
122 /* Encode first with TestMessage */
123 {
124 OneofMessage msg = OneofMessage_init_zero;
125 msg.which_msgs = OneofMessage_msg1_tag;
126
127 fill_TestMessage(&msg.msgs.msg1);
128
129 if (!pb_encode(&stream, OneofMessage_fields, &msg))
130 {
131 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
132 return false;
133 }
134 }
135
136 /* Encode second with SubMessage, replacing the oneof item */
137 {
138 OneofMessage msg = OneofMessage_init_zero;
139 char *teststr = "1";
140 msg.which_msgs = OneofMessage_msg2_tag;
141
142 msg.first = 999;
143 msg.msgs.msg2.dynamic_str = "ABCD";
144 msg.msgs.msg2.dynamic_str_arr_count = 1;
145 msg.msgs.msg2.dynamic_str_arr = &teststr;
146 msg.last = 888;
147
148 if (!pb_encode(&stream, OneofMessage_fields, &msg))
149 {
150 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
151 return false;
152 }
153 }
154
155 /* Encode second SubMessage, invoking submessage merge behavior */
156 {
157 OneofMessage msg = OneofMessage_init_zero;
158 char *teststr = "2";
159 msg.which_msgs = OneofMessage_msg2_tag;
160
161 msg.first = 99;
162 msg.msgs.msg2.dynamic_str = "EFGH";
163 msg.msgs.msg2.dynamic_str_arr_count = 1;
164 msg.msgs.msg2.dynamic_str_arr = &teststr;
165 msg.last = 88;
166
167 if (!pb_encode(&stream, OneofMessage_fields, &msg))
168 {
169 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
170 return false;
171 }
172 }
173 msgsize = stream.bytes_written;
174 }
175
176 {
177 OneofMessage msg = OneofMessage_init_zero;
178 pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
179 if (!pb_decode(&stream, OneofMessage_fields, &msg))
180 {
181 fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
182 return false;
183 }
184
185 TEST(msg.first == 99);
186 TEST(msg.which_msgs == OneofMessage_msg2_tag);
187 TEST(msg.msgs.msg2.dynamic_str);
188 TEST(strcmp(msg.msgs.msg2.dynamic_str, "EFGH") == 0);
189 TEST(msg.msgs.msg2.dynamic_str_arr != NULL);
190 TEST(msg.msgs.msg2.dynamic_str_arr_count == 2);
191 TEST(strcmp(msg.msgs.msg2.dynamic_str_arr[0], "1") == 0);
192 TEST(strcmp(msg.msgs.msg2.dynamic_str_arr[1], "2") == 0);
193 TEST(msg.msgs.msg2.dynamic_submsg == NULL);
194 TEST(msg.last == 88);
195
196 pb_release(OneofMessage_fields, &msg);
197 TEST(get_alloc_count() == 0);
198 pb_release(OneofMessage_fields, &msg);
199 TEST(get_alloc_count() == 0);
200 }
201
202 return true;
203}
204
205static bool dummy_decode_cb(pb_istream_t *stream, const pb_field_t *field, void **arg)
206{
207 return false;
208}
209
210/* Garbage input */
211static bool test_Garbage()
212{
213 const uint8_t buffer[] = "I'm only happy when it rains";
214 const size_t msgsize = sizeof(buffer);
215
216 {
217 OneofMessage msg = OneofMessage_init_zero;
218 pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
219 TEST(!pb_decode(&stream, OneofMessage_fields, &msg));
220 }
221
222 {
223 TestMessage msg = TestMessage_init_zero;
224 pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
225 TEST(!pb_decode(&stream, TestMessage_fields, &msg));
226 }
227
228 {
229 RepeatedMessage msg = RepeatedMessage_init_zero;
230 pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
231 msg.subs.arg = NULL;
232 msg.subs.funcs.decode = dummy_decode_cb;
233 TEST(!pb_decode(&stream, RepeatedMessage_fields, &msg));
234 }
235
236 return true;
237}
238
239int main()
240{
242 return 0;
243 else
244 return 1;
245}
246
#define SubMessage_init_zero
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
#define stdout
Definition mos_stdio.h:31
#define stderr
Definition mos_stdio.h:32
size_t fwrite(const void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
size_t get_alloc_count()
static pb_extension_t ext2
Definition mem_release.c:17
static bool dummy_decode_cb(pb_istream_t *stream, const pb_field_t *field, void **arg)
static SubMessage test_msg_arr[]
Definition mem_release.c:16
static void fill_TestMessage(TestMessage *msg)
Definition mem_release.c:19
static char * test_str_arr[]
Definition mem_release.c:15
#define TEST(x)
Definition mem_release.c:10
static bool test_Garbage()
static bool test_OneofMessage()
static bool test_TestMessage()
Definition mem_release.c:43
int main()
static pb_extension_t ext1
Definition mem_release.c:17
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_field_iter_t pb_field_t
Definition pb.h:359
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1336
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 SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7