DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2011-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7// Chris Murphy <cmurphy@aphysci.com>
8//
9//
10// This file is part of the Dynamic Compact Control Language Library
11// ("DCCL").
12//
13// DCCL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// DCCL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25// tests all protobuf types with _default codecs, repeat and non repeat
26
27#include <fstream>
28
29#include <google/protobuf/descriptor.pb.h>
30
31#include "../../binary.h"
32#include "../../codec.h"
33#include "test.pb.h"
34using namespace dccl::test;
35
36void decode_check(const std::string& encoded);
37dccl::Codec codec;
38TestMsg msg_in;
39
40int main(int argc, char* argv[])
41{
42 bool verbose = false;
43 for (int i = 1; i < argc; ++i)
44 {
45 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
46 verbose = true;
47 }
48
49 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
50
51 int i = 0;
52 msg_in.set_double_default_optional(++i + 0.1);
53 msg_in.set_float_default_optional(++i + 0.2);
54
55 msg_in.set_int32_default_optional(++i);
56 msg_in.set_int64_default_optional(-++i);
57 msg_in.set_uint32_default_optional(++i);
58 msg_in.set_uint64_default_optional(++i);
59 msg_in.set_sint32_default_optional(-++i);
60 msg_in.set_sint64_default_optional(++i);
61 msg_in.set_fixed32_default_optional(++i);
62 msg_in.set_fixed64_default_optional(++i);
63 msg_in.set_sfixed32_default_optional(++i);
64 msg_in.set_sfixed64_default_optional(-++i);
65
66 msg_in.set_bool_default_optional(true);
67
68 msg_in.set_string_default_optional("abc123");
69 msg_in.set_bytes_default_optional(dccl::hex_decode("00112233aabbcc1234"));
70
71 msg_in.set_enum_default_optional(ENUM_C);
72 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
73 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
74
75 msg_in.set_double_default_required(++i + 0.1);
76 msg_in.set_float_default_required(++i + 0.2);
77
78 msg_in.set_int32_default_required(++i);
79 msg_in.set_int64_default_required(-++i);
80 msg_in.set_uint32_default_required(++i);
81 msg_in.set_uint64_default_required(++i);
82 msg_in.set_sint32_default_required(-++i);
83 msg_in.set_sint64_default_required(++i);
84 msg_in.set_fixed32_default_required(++i);
85 msg_in.set_fixed64_default_required(++i);
86 msg_in.set_sfixed32_default_required(++i);
87 msg_in.set_sfixed64_default_required(-++i);
88
89 msg_in.set_bool_default_required(true);
90
91 msg_in.set_string_default_required("abc123");
92 msg_in.set_bytes_default_required(dccl::hex_decode("00112233aabbcc1234"));
93
94 msg_in.set_enum_default_required(ENUM_C);
95 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
96 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
97
98 for (int j = 0; j < 2; ++j)
99 {
100 msg_in.add_double_default_repeat(++i + 0.1);
101 msg_in.add_float_default_repeat(++i + 0.2);
102
103 msg_in.add_int32_default_repeat(++i);
104 msg_in.add_int64_default_repeat(-++i);
105 msg_in.add_uint32_default_repeat(++i);
106 msg_in.add_uint64_default_repeat(++i);
107 msg_in.add_sint32_default_repeat(-++i);
108 msg_in.add_sint64_default_repeat(++i);
109 msg_in.add_fixed32_default_repeat(++i);
110 msg_in.add_fixed64_default_repeat(++i);
111 msg_in.add_sfixed32_default_repeat(++i);
112 msg_in.add_sfixed64_default_repeat(-++i);
113
114 msg_in.add_bool_default_repeat(true);
115
116 msg_in.add_string_default_repeat("abc123");
117
118 if (j)
119 msg_in.add_bytes_default_repeat(dccl::hex_decode("00aabbcc"));
120 else
121 msg_in.add_bytes_default_repeat(dccl::hex_decode("ffeedd12"));
122
123 msg_in.add_enum_default_repeat(static_cast<Enum1>((++i % 3) + 1));
124 EmbeddedMsg1* em_msg = msg_in.add_msg_default_repeat();
125 em_msg->set_val(++i + 0.3);
126 em_msg->mutable_msg()->set_val(++i);
127 }
128
129 codec.info(msg_in.GetDescriptor());
130
131 std::ofstream fout("/tmp/testmessage.pb");
132 msg_in.SerializeToOstream(&fout);
133
134 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
135
136 codec.load(msg_in.GetDescriptor());
137
138 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
139 std::string bytes;
140 codec.encode(&bytes, msg_in);
141 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
142
143 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
144 decode_check(bytes);
145
146 // make sure DCCL defaults stay wire compatible
147
148 // v4
149 decode_check(
150 dccl::hex_decode("047f277b16b95660c0b0188000d8c0132858800008005b98d8588ccccc0488109951dd659"
151 "6a079fd8c7905d01a29015a99800738012eb880010ee0052d4c6c2c466626012244665477"
152 "9925682c55335e8158e310b1127e1d4858fab84454a3104ca2983214c4e7a22ad164aae96"
153 "41373bd69314c6c2c8661626331e2bf7bb70401547799a5ea29b51942f5b7a91600"));
154
155 // run a bunch of tests with random strings
156 std::string random = bytes;
157 for (unsigned i = 0; i < 10; ++i)
158 {
159 random[(rand() % (bytes.size() - 1) + 1)] = rand() % 256;
160 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Using junk bytes: " << dccl::hex_encode(random) << std::endl;
161
162 try
163 {
164 TestMsg msg_out;
165 codec.decode(random, &msg_out);
166 }
167 catch (dccl::Exception&)
168 {
169 }
170 }
171
172 // test verbose encode errors output
173 TestMsg empty;
174 try
175 {
176 std::string dummy;
177 codec.encode(&bytes, empty);
178 }
179 catch (const dccl::Exception& e)
180 {
181 // expected long error
182 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Expected verbose error encoding empty message: " << e.what() << std::endl;
183 }
184
185 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
186}
187
188void decode_check(const std::string& encoded)
189{
190 TestMsg msg_out;
191 codec.decode(encoded, &msg_out);
192
193 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
194
195 // truncate to "max_length" as codec should do
196 msg_in.set_string_default_repeat(0, "abc1");
197 msg_in.set_string_default_repeat(1, "abc1");
198
199 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
200}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
Exception class for DCCL.
Definition exception.h:47
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
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
Definition binary.h:46