29#include "../../binary.h"
30#include "../../codec.h"
47bool same_contents(
const CryptoMsg& a,
const CryptoMsg& b)
49 return a.source() == b.source() && a.x() == b.x() && a.y() == b.y();
52std::string encode(
dccl::Codec& codec,
const google::protobuf::Message& msg)
55 codec.encode(&bytes, msg);
56 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"encoded (hex): " <<
dccl::hex_encode(bytes)
62int main(
int argc,
char* argv[])
65 for (
int i = 1; i < argc; ++i)
67 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
71 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
73 const CryptoMsg msg_in = make_msg();
76 plain_codec.
load<CryptoMsg>();
77 const std::string plain_bytes = encode(plain_codec, msg_in);
84 codec.set_crypto_passphrase(
"my_passphrase");
85 codec.load<CryptoMsg>();
87 const std::string bytes = encode(codec, msg_in);
88 assert(bytes.size() == plain_bytes.size());
89 assert(bytes != plain_bytes);
93 plain_codec.
encode(&head, msg_in,
true);
94 const std::size_t head_bytes = head.size();
95 assert(bytes.substr(0, head_bytes) == plain_bytes.substr(0, head_bytes));
96 assert(bytes.substr(head_bytes) != plain_bytes.substr(head_bytes));
99 codec.decode(bytes, &msg_out);
100 assert(same_contents(msg_in, msg_out));
103 assert(plain_codec.
id(bytes) == plain_codec.
id<CryptoMsg>());
113 enc_codec.
load<CryptoMsg>();
114 dec_codec.
load<CryptoMsg>();
116 const std::string bytes = encode(enc_codec, msg_in);
118 bool rejected =
false;
122 dec_codec.
decode(bytes, &msg_out);
124 catch (
const std::exception& e)
127 dccl::dlog.
is(dccl::logger::INFO) &&
128 dccl::dlog <<
"expected decode failure: " << e.what() << std::endl;
138 codec.set_crypto_passphrase(
"my_passphrase");
139 codec.load<CryptoMsg>();
141 std::string bytes = encode(codec, msg_in);
142 bytes[bytes.size() - 1] ^= 0x01;
144 bool rejected =
false;
148 codec.decode(bytes, &msg_out);
150 catch (
const std::exception& e)
153 dccl::dlog.
is(dccl::logger::INFO) &&
154 dccl::dlog <<
"expected decode failure: " << e.what() << std::endl;
164 codec.set_crypto_passphrase(
"my_passphrase", std::set<dccl::int32>{21});
165 codec.load<CryptoMsg>();
166 codec.load<PlaintextMsg>();
168 PlaintextMsg plain_in;
169 plain_in.set_source(5);
170 plain_in.set_x(1234);
171 plain_in.set_y(-5678);
174 plain_codec.
load<PlaintextMsg>();
175 assert(encode(codec, plain_in) == encode(plain_codec, plain_in));
176 assert(encode(codec, msg_in) != plain_bytes);
179 std::cout <<
"all tests passed" << std::endl;
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
int32 id() const
Gives the DCCL id (defined by the custom message option extension "(dccl.msg).id" in the ....
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false, int user_id=-1)
Encodes a DCCL message.
void set_crypto_passphrase(const std::string &passphrase, const std::set< int32 > &do_not_encrypt_ids=std::set< int32 >())
Set a passphrase to be used when encoded messages to encrypt them and to decrypt messages after decod...
CharIterator decode(CharIterator begin, CharIterator end, ProtobufMessage *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
std::size_t load()
All messages must be explicited loaded and validated (size checks, option extensions checks,...
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...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Dynamic Compact Control Language namespace.
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...