Publishing nanopb-0.3.5
[apps/agl-service-can-low-level.git] / tests / mem_release / mem_release.c
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 " #x " on line %d failed.\n", __LINE__); \
12     return false; \
13     }
14
15 static char *test_str_arr[] = {"1", "2", ""};
16 static SubMessage test_msg_arr[] = {SubMessage_init_zero, SubMessage_init_zero};
17 static pb_extension_t ext1, ext2;
18
19 static 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->has_static_opt_submsg = true;
29     msg->dynamic_submsg = &msg->static_req_submsg;
30
31     msg->extensions = &ext1;
32     ext1.type = &dynamic_ext;
33     ext1.dest = &msg->static_req_submsg;
34     ext1.next = &ext2;
35     ext2.type = &static_ext;
36     ext2.dest = &msg->static_req_submsg;
37     ext2.next = NULL;
38 }
39
40 /* Basic fields, nested submessages, extensions */
41 static bool test_TestMessage()
42 {
43     uint8_t buffer[256];
44     size_t msgsize;
45     
46     /* Construct a message with various fields filled in */
47     {
48         TestMessage msg = TestMessage_init_zero;
49         pb_ostream_t stream;
50
51         fill_TestMessage(&msg);
52         
53         stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
54         if (!pb_encode(&stream, TestMessage_fields, &msg))
55         {
56             fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
57             return false;
58         }
59         msgsize = stream.bytes_written;
60     }
61     
62     /* Output encoded message for debug */
63     SET_BINARY_MODE(stdout);
64     fwrite(buffer, 1, msgsize, stdout);
65     
66     /* Decode memory using dynamic allocation */
67     {
68         TestMessage msg = TestMessage_init_zero;
69         pb_istream_t stream;
70         SubMessage ext2_dest;
71
72         msg.extensions = &ext1;
73         ext1.type = &dynamic_ext;
74         ext1.dest = NULL;
75         ext1.next = &ext2;
76         ext2.type = &static_ext;
77         ext2.dest = &ext2_dest;
78         ext2.next = NULL;
79         
80         stream = pb_istream_from_buffer(buffer, msgsize);
81         if (!pb_decode(&stream, TestMessage_fields, &msg))
82         {
83             fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
84             return false;
85         }
86         
87         /* Make sure it encodes back to same data */
88         {
89             uint8_t buffer2[256];
90             pb_ostream_t ostream = pb_ostream_from_buffer(buffer2, sizeof(buffer2));
91             TEST(pb_encode(&ostream, TestMessage_fields, &msg));
92             TEST(ostream.bytes_written == msgsize);
93             TEST(memcmp(buffer, buffer2, msgsize) == 0);
94         }
95         
96         /* Make sure that malloc counters work */
97         TEST(get_alloc_count() > 0);
98         
99         /* Make sure that pb_release releases everything */
100         pb_release(TestMessage_fields, &msg);
101         TEST(get_alloc_count() == 0);
102         
103         /* Check that double-free is a no-op */
104         pb_release(TestMessage_fields, &msg);
105         TEST(get_alloc_count() == 0);
106     }
107     
108     return true;
109 }
110
111 /* Oneofs */
112 static bool test_OneofMessage()
113 {
114     uint8_t buffer[256];
115     size_t msgsize;
116
117     {
118         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
119
120         /* Encode first with TestMessage */
121         {
122             OneofMessage msg = OneofMessage_init_zero;
123             msg.which_msgs = OneofMessage_msg1_tag;
124
125             fill_TestMessage(&msg.msgs.msg1);
126
127             if (!pb_encode(&stream, OneofMessage_fields, &msg))
128             {
129                 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
130                 return false;
131             }
132         }
133
134         /* Encode second with SubMessage, invoking 'merge' behaviour */
135         {
136             OneofMessage msg = OneofMessage_init_zero;
137             msg.which_msgs = OneofMessage_msg2_tag;
138
139             msg.first = 999;
140             msg.msgs.msg2.dynamic_str = "ABCD";
141             msg.last = 888;
142
143             if (!pb_encode(&stream, OneofMessage_fields, &msg))
144             {
145                 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
146                 return false;
147             }
148         }
149         msgsize = stream.bytes_written;
150     }
151
152     {
153         OneofMessage msg = OneofMessage_init_zero;
154         pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
155         if (!pb_decode(&stream, OneofMessage_fields, &msg))
156         {
157             fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
158             return false;
159         }
160
161         TEST(msg.first == 999);
162         TEST(msg.which_msgs == OneofMessage_msg2_tag);
163         TEST(msg.msgs.msg2.dynamic_str);
164         TEST(strcmp(msg.msgs.msg2.dynamic_str, "ABCD") == 0);
165         TEST(msg.msgs.msg2.dynamic_str_arr == NULL);
166         TEST(msg.msgs.msg2.dynamic_submsg == NULL);
167         TEST(msg.last == 888);
168
169         pb_release(OneofMessage_fields, &msg);
170         TEST(get_alloc_count() == 0);
171         pb_release(OneofMessage_fields, &msg);
172         TEST(get_alloc_count() == 0);
173     }
174
175     return true;
176 }
177
178 int main()
179 {
180     if (test_TestMessage() && test_OneofMessage())
181         return 0;
182     else
183         return 1;
184 }
185