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