1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.
#include "../../../src/base.hh"
#include "../unit_tests.hh"
#include "../../../src/netcmd.hh"
#include "../../../src/transforms.hh"
#include "../../../src/lexical_cast.hh"
using std::string;
UNIT_TEST(mac)
{
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
string buf;
netsync_session_key key(constants::netsync_key_initializer);
{
chained_hmac mac(key, true);
// mutates mac
out_cmd.write(buf, mac);
UNIT_TEST_CHECK_THROW(in_cmd.read_string(buf, mac), bad_decode);
}
{
chained_hmac mac(key, true);
out_cmd.write(buf, mac);
}
buf[0] ^= 0xff;
{
chained_hmac mac(key, true);
UNIT_TEST_CHECK_THROW(in_cmd.read_string(buf, mac), bad_decode);
}
{
chained_hmac mac(key, true);
out_cmd.write(buf, mac);
}
buf[buf.size() - 1] ^= 0xff;
{
chained_hmac mac(key, true);
UNIT_TEST_CHECK_THROW(in_cmd.read_string(buf, mac), bad_decode);
}
{
chained_hmac mac(key, true);
out_cmd.write(buf, mac);
}
buf += '\0';
{
chained_hmac mac(key, true);
UNIT_TEST_CHECK_THROW(in_cmd.read_string(buf, mac), bad_decode);
}
}
static void
do_netcmd_roundtrip(netcmd const & out_cmd, netcmd & in_cmd, string & buf)
{
netsync_session_key key(constants::netsync_key_initializer);
{
chained_hmac mac(key, true);
out_cmd.write(buf, mac);
}
{
chained_hmac mac(key, true);
UNIT_TEST_CHECK(in_cmd.read_string(buf, mac));
}
UNIT_TEST_CHECK(in_cmd == out_cmd);
}
UNIT_TEST(functions)
{
try
{
// error_cmd
{
L(FL("checking i/o round trip on error_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
string out_errmsg("your shoelaces are untied"), in_errmsg;
string buf;
out_cmd.write_error_cmd(out_errmsg);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_error_cmd(in_errmsg);
UNIT_TEST_CHECK(in_errmsg == out_errmsg);
L(FL("errmsg_cmd test done, buffer was %d bytes") % buf.size());
}
// hello_cmd
{
L(FL("checking i/o round trip on hello_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(0);
string buf;
key_name out_server_keyname("server@there"), in_server_keyname;
rsa_pub_key out_server_key("9387938749238792874"), in_server_key;
id out_nonce(raw_sha1("nonce it up"), origin::internal), in_nonce;
out_cmd.write_hello_cmd(out_server_keyname, out_server_key, out_nonce);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
u8 ver(0);
in_cmd.read_hello_cmd(ver, in_server_keyname, in_server_key, in_nonce);
UNIT_TEST_CHECK(ver == constants::netcmd_current_protocol_version);
UNIT_TEST_CHECK(in_server_keyname == out_server_keyname);
UNIT_TEST_CHECK(in_server_key == out_server_key);
UNIT_TEST_CHECK(in_nonce == out_nonce);
L(FL("hello_cmd test done, buffer was %d bytes") % buf.size());
}
// bye_cmd
{
L(FL("checking i/o round trip on bye_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
u8 out_phase(1), in_phase;
string buf;
out_cmd.write_bye_cmd(out_phase);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_bye_cmd(in_phase);
UNIT_TEST_CHECK(in_phase == out_phase);
L(FL("bye_cmd test done, buffer was %d bytes") % buf.size());
}
// anonymous_cmd
{
L(FL("checking i/o round trip on anonymous_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
protocol_role out_role = source_and_sink_role, in_role;
string buf;
// total cheat, since we don't actually verify that rsa_oaep_sha_data
// is sensible anywhere here...
rsa_oaep_sha_data out_key("nonce start my heart"), in_key;
globish out_include_pattern("radishes galore!", origin::internal),
in_include_pattern;
globish out_exclude_pattern("turnips galore!", origin::internal),
in_exclude_pattern;
out_cmd.write_anonymous_cmd(out_role, out_include_pattern, out_exclude_pattern, out_key);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_anonymous_cmd(in_role, in_include_pattern, in_exclude_pattern, in_key);
UNIT_TEST_CHECK(in_key == out_key);
UNIT_TEST_CHECK(in_include_pattern() == out_include_pattern());
UNIT_TEST_CHECK(in_exclude_pattern() == out_exclude_pattern());
UNIT_TEST_CHECK(in_role == out_role);
L(FL("anonymous_cmd test done, buffer was %d bytes") % buf.size());
}
// auth_cmd
{
L(FL("checking i/o round trip on auth_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
protocol_role out_role = source_and_sink_role, in_role;
string buf;
key_id out_client(raw_sha1("happy client day"), origin::internal);
id out_nonce1(raw_sha1("nonce me amadeus"), origin::internal);
key_id in_client;
id in_nonce1;
// total cheat, since we don't actually verify that rsa_oaep_sha_data
// is sensible anywhere here...
rsa_oaep_sha_data out_key("nonce start my heart"), in_key;
rsa_sha1_signature out_signature(raw_sha1("burble") + raw_sha1("gorby"),
origin::internal), in_signature;
globish out_include_pattern("radishes galore!", origin::user),
in_include_pattern;
globish out_exclude_pattern("turnips galore!", origin::user),
in_exclude_pattern;
out_cmd.write_auth_cmd(out_role, out_include_pattern, out_exclude_pattern
, out_client, out_nonce1, out_key, out_signature);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_auth_cmd(in_role, in_include_pattern, in_exclude_pattern,
in_client, in_nonce1, in_key, in_signature);
UNIT_TEST_CHECK(in_client == out_client);
UNIT_TEST_CHECK(in_nonce1 == out_nonce1);
UNIT_TEST_CHECK(in_key == out_key);
UNIT_TEST_CHECK(in_signature == out_signature);
UNIT_TEST_CHECK(in_role == out_role);
UNIT_TEST_CHECK(in_include_pattern() == out_include_pattern());
UNIT_TEST_CHECK(in_exclude_pattern() == out_exclude_pattern());
L(FL("auth_cmd test done, buffer was %d bytes") % buf.size());
}
// automate_cmd
{
L(FL("checking i/o round trip on auth_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
string buf;
key_id out_client(raw_sha1("happy client day"), origin::internal);
id out_nonce1(raw_sha1("nonce me amadeus"), origin::internal);
key_id in_client;
id in_nonce1;
// total cheat, since we don't actually verify that rsa_oaep_sha_data
// is sensible anywhere here...
rsa_oaep_sha_data out_key("nonce start my heart"), in_key;
rsa_sha1_signature out_signature(raw_sha1("burble") + raw_sha1("gorby"),
origin::internal), in_signature;
out_cmd.write_automate_cmd(out_client, out_nonce1, out_key, out_signature);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_automate_cmd(in_client, in_nonce1, in_key, in_signature);
UNIT_TEST_CHECK(in_client == out_client);
UNIT_TEST_CHECK(in_nonce1 == out_nonce1);
UNIT_TEST_CHECK(in_key == out_key);
UNIT_TEST_CHECK(in_signature == out_signature);
L(FL("automate_cmd test done, buffer was %d bytes") % buf.size());
}
// confirm_cmd
{
L(FL("checking i/o round trip on confirm_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
string buf;
out_cmd.write_confirm_cmd();
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_confirm_cmd();
L(FL("confirm_cmd test done, buffer was %d bytes") % buf.size());
}
// refine_cmd
{
L(FL("checking i/o round trip on refine_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
string buf;
refinement_type out_ty (refinement_query), in_ty(refinement_response);
merkle_node out_node, in_node;
out_node.set_raw_slot(0, id(raw_sha1("The police pulled Kris Kringle over"), origin::internal));
out_node.set_raw_slot(3, id(raw_sha1("Kris Kringle tried to escape from the police"), origin::internal));
out_node.set_raw_slot(8, id(raw_sha1("He was arrested for auto theft"), origin::internal));
out_node.set_raw_slot(15, id(raw_sha1("He was whisked away to jail"), origin::internal));
out_node.set_slot_state(0, subtree_state);
out_node.set_slot_state(3, leaf_state);
out_node.set_slot_state(8, leaf_state);
out_node.set_slot_state(15, subtree_state);
out_cmd.write_refine_cmd(out_ty, out_node);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_refine_cmd(in_ty, in_node);
UNIT_TEST_CHECK(in_ty == out_ty);
UNIT_TEST_CHECK(in_node == out_node);
L(FL("refine_cmd test done, buffer was %d bytes") % buf.size());
}
// done_cmd
{
L(FL("checking i/o round trip on done_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
size_t out_n_items(12), in_n_items(0);
netcmd_item_type out_type(key_item), in_type(revision_item);
string buf;
out_cmd.write_done_cmd(out_type, out_n_items);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_done_cmd(in_type, in_n_items);
UNIT_TEST_CHECK(in_n_items == out_n_items);
UNIT_TEST_CHECK(in_type == out_type);
L(FL("done_cmd test done, buffer was %d bytes") % buf.size());
}
// data_cmd
{
L(FL("checking i/o round trip on data_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
netcmd_item_type out_type(file_item), in_type(key_item);
id out_id(raw_sha1("tuna is not yummy"), origin::internal), in_id;
string out_dat("thank you for flying northwest"), in_dat;
string buf;
out_cmd.write_data_cmd(out_type, out_id, out_dat);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_data_cmd(in_type, in_id, in_dat);
UNIT_TEST_CHECK(in_id == out_id);
UNIT_TEST_CHECK(in_dat == out_dat);
L(FL("data_cmd test done, buffer was %d bytes") % buf.size());
}
// delta_cmd
{
L(FL("checking i/o round trip on delta_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
netcmd_item_type out_type(file_item), in_type(key_item);
id out_head(raw_sha1("your seat cusion can be reused"), origin::internal), in_head;
id out_base(raw_sha1("as a floatation device"), origin::internal), in_base;
delta out_delta("goodness, this is not an xdelta"), in_delta;
string buf;
out_cmd.write_delta_cmd(out_type, out_head, out_base, out_delta);
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_delta_cmd(in_type, in_head, in_base, in_delta);
UNIT_TEST_CHECK(in_type == out_type);
UNIT_TEST_CHECK(in_head == out_head);
UNIT_TEST_CHECK(in_base == out_base);
UNIT_TEST_CHECK(in_delta == out_delta);
L(FL("delta_cmd test done, buffer was %d bytes") % buf.size());
}
// automate_command_cmd
{
L(FL("checking i/o round trip on automate_command_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
std::vector<string> in_args, out_args;
std::vector<std::pair<string, string> > in_opts, out_opts;
in_args.push_back("foo");
in_args.push_back("bar");
in_opts.push_back(std::make_pair("abc", "def"));
out_cmd.write_automate_command_cmd(in_args, in_opts);
string buf;
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_automate_command_cmd(out_args, out_opts);
UNIT_TEST_CHECK(in_args == out_args);
UNIT_TEST_CHECK(in_opts == out_opts);
L(FL("automate_command_cmd test done, buffer was %d bytes") % buf.size());
}
// automate_packet_cmd
{
L(FL("checking i/o round trip on automate_packet_cmd"));
netcmd out_cmd(constants::netcmd_current_protocol_version);
netcmd in_cmd(constants::netcmd_current_protocol_version);
int in_cmd_num(3), out_cmd_num;
char in_stream('k'), out_stream;
string in_data("this is some packet data"), out_data;
out_cmd.write_automate_packet_cmd(in_cmd_num, in_stream, in_data);
string buf;
do_netcmd_roundtrip(out_cmd, in_cmd, buf);
in_cmd.read_automate_packet_cmd(out_cmd_num, out_stream, out_data);
UNIT_TEST_CHECK(in_cmd_num == out_cmd_num);
UNIT_TEST_CHECK(in_stream == out_stream);
UNIT_TEST_CHECK(in_data == out_data);
L(FL("automate_packet_cmd test done, buffer was %d bytes") % buf.size());
}
}
catch (bad_decode & d)
{
L(FL("bad decode exception: '%s'") % d.what);
throw;
}
}
// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
|