DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2019-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Dynamic Compact Control Language Library
9// ("DCCL").
10//
11// DCCL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// DCCL is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23// tests all protobuf types with _default codecs, repeat and non repeat
24
25#include <fstream>
26
27
28#include "../../binary.h"
29#include "../../codec.h"
30#include "test.pb.h"
31using namespace dccl::test;
32
33void decode_check(const TestMsg& msg_in);
34dccl::Codec codec;
35
36int main(int argc, char* argv[])
37{
38 bool verbose = false;
39 for (int i = 1; i < argc; ++i)
40 {
41 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
42 verbose = true;
43 }
44
45 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
46
47 codec.info<TestMsg>();
48 codec.load<TestMsg>();
49
50 // enable strict mode
51 codec.set_strict(true);
52
53 {
54 TestMsg msg_in;
55 msg_in.set_d(10.0);
56 msg_in.set_i(1000);
57 msg_in.set_s("foo");
58 msg_in.set_b(std::string(9, '1'));
59 msg_in.add_ri(1);
60 msg_in.add_ri(2);
61 msg_in.add_ri(3);
62
63 decode_check(msg_in);
64 }
65
66 // double out of range
67 try
68 {
69 TestMsg msg_in;
70 msg_in.set_d(150);
71 msg_in.set_i(1000);
72 msg_in.set_s("foo");
73 msg_in.set_b(std::string(9, '1'));
74
75 decode_check(msg_in);
76 bool expected_out_of_range_exception = false;
77 assert(expected_out_of_range_exception);
78 }
80 {
81 assert(e.field() == TestMsg::descriptor()->FindFieldByName("d"));
82 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
83 }
84
85 // int out of range
86 try
87 {
88 TestMsg msg_in;
89 msg_in.set_d(10.0);
90 msg_in.set_i(-30);
91 msg_in.set_s("foo");
92 msg_in.set_b(std::string(9, '1'));
93
94 decode_check(msg_in);
95 bool expected_out_of_range_exception = false;
96 assert(expected_out_of_range_exception);
97 }
99 {
100 assert(e.field() == TestMsg::descriptor()->FindFieldByName("i"));
101 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
102 }
103
104 // string (version 2) out of range
105 try
106 {
107 TestMsg msg_in;
108 msg_in.set_d(10.0);
109 msg_in.set_i(1000);
110 msg_in.set_s2("foobar1234546789");
111 msg_in.set_b(std::string(9, '1'));
112
113 decode_check(msg_in);
114 bool expected_out_of_range_exception = false;
115 assert(expected_out_of_range_exception);
116 }
118 {
119 assert(e.field() == TestMsg::descriptor()->FindFieldByName("s2"));
120 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
121 }
122
123 // string (version 3) out of range
124 try
125 {
126 TestMsg msg_in;
127 msg_in.set_d(10.0);
128 msg_in.set_i(1000);
129 msg_in.set_s("foobar1234546789");
130 msg_in.set_b(std::string(9, '1'));
131
132 decode_check(msg_in);
133 bool expected_out_of_range_exception = false;
134 assert(expected_out_of_range_exception);
135 }
137 {
138 assert(e.field() == TestMsg::descriptor()->FindFieldByName("s"));
139 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
140 }
141
142 // bytes out of range
143 try
144 {
145 TestMsg msg_in;
146 msg_in.set_d(10.0);
147 msg_in.set_i(1000);
148 msg_in.set_s("foo");
149 msg_in.set_b(std::string(12, '1'));
150
151 decode_check(msg_in);
152 bool expected_out_of_range_exception = false;
153 assert(expected_out_of_range_exception);
154 }
156 {
157 assert(e.field() == TestMsg::descriptor()->FindFieldByName("b"));
158 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
159 }
160
161 // var bytes out of range
162 try
163 {
164 TestMsg msg_in;
165 msg_in.set_d(10.0);
166 msg_in.set_i(1000);
167 msg_in.set_s("foo");
168 msg_in.set_vb(std::string(12, '1'));
169
170 decode_check(msg_in);
171 bool expected_out_of_range_exception = false;
172 assert(expected_out_of_range_exception);
173 }
175 {
176 assert(e.field() == TestMsg::descriptor()->FindFieldByName("vb"));
177 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
178 }
179
180 // repeat size out of range
181 try
182 {
183 TestMsg msg_in;
184 msg_in.set_d(10.0);
185 msg_in.set_i(1000);
186 msg_in.set_s("foo");
187 msg_in.set_b(std::string(9, '1'));
188 msg_in.add_ri(1);
189 msg_in.add_ri(2);
190 msg_in.add_ri(3);
191 msg_in.add_ri(4);
192
193 decode_check(msg_in);
194 }
196 {
197 assert(e.field() == TestMsg::descriptor()->FindFieldByName("ri"));
198 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught (as expected) " << e.what() << std::endl;
199 }
200
201 // disable strict mode
202 codec.set_strict(false);
203
204 {
205 TestMsg msg_in;
206 msg_in.set_d(10.0);
207 msg_in.set_i(1000);
208 msg_in.set_s("foo");
209 msg_in.set_b(std::string(9, '1'));
210 msg_in.add_ri(1);
211 msg_in.add_ri(2);
212 msg_in.add_ri(3);
213
214 decode_check(msg_in);
215 }
216
217 // double out of range
218 {
219 TestMsg msg_in;
220 msg_in.set_d(150);
221 msg_in.set_i(1000);
222 msg_in.set_s("foo");
223 msg_in.set_b(std::string(9, '1'));
224
225 decode_check(msg_in);
226 }
227
228 // int out of range
229 {
230 TestMsg msg_in;
231 msg_in.set_d(10.0);
232 msg_in.set_i(-30);
233 msg_in.set_s("foo");
234 msg_in.set_b(std::string(9, '1'));
235
236 decode_check(msg_in);
237 }
238
239 // string (version 2) out of range
240 {
241 TestMsg msg_in;
242 msg_in.set_d(10.0);
243 msg_in.set_i(1000);
244 msg_in.set_s2("foobar1234546789");
245 msg_in.set_b(std::string(9, '1'));
246
247 decode_check(msg_in);
248 }
249
250 // string (version 3) out of range
251 {
252 TestMsg msg_in;
253 msg_in.set_d(10.0);
254 msg_in.set_i(1000);
255 msg_in.set_s("foobar1234546789");
256 msg_in.set_b(std::string(9, '1'));
257
258 decode_check(msg_in);
259 }
260
261 // bytes out of range
262 {
263 TestMsg msg_in;
264 msg_in.set_d(10.0);
265 msg_in.set_i(1000);
266 msg_in.set_s("foo");
267 msg_in.set_b(std::string(12, '1'));
268
269 decode_check(msg_in);
270 }
271
272 // var bytes out of range
273 {
274 TestMsg msg_in;
275 msg_in.set_d(10.0);
276 msg_in.set_i(1000);
277 msg_in.set_s("foo");
278 msg_in.set_vb(std::string(12, '1'));
279
280 decode_check(msg_in);
281 }
282
283 // repeat size out of range
284 {
285 TestMsg msg_in;
286 msg_in.set_d(10.0);
287 msg_in.set_i(1000);
288 msg_in.set_s("foo");
289 msg_in.set_b(std::string(9, '1'));
290 msg_in.add_ri(1);
291 msg_in.add_ri(2);
292 msg_in.add_ri(3);
293 msg_in.add_ri(4);
294
295 decode_check(msg_in);
296 }
297
298 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
299}
300
301void decode_check(const TestMsg& msg_in)
302{
303 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
304
305 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode (in bounds)..." << std::endl;
306 std::string bytes;
307 codec.encode(&bytes, msg_in);
308 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
309
310 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
311
312 TestMsg msg_out;
313 codec.decode(bytes, &msg_out);
314
315 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
316}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition logger.h:213
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition binary.h:95