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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
#include "../test.h"
#include "../../src/alloc.h"
#include "../../src/asfd.h"
#include "../../src/async.h"
#include "../../src/conf.h"
#include "../../src/fsops.h"
#include "../../src/iobuf.h"
#include "../../src/sbuf.h"
#include "../../src/server/extra_comms.h"
#include "../../src/strlist.h"
#include "../builders/build.h"
#include "../builders/build_file.h"
#include "../builders/build_asfd_mock.h"
#define BASE "utest_server_extra_comms"
#define TESTCLIENT "testclient"
#define SPOOL BASE "/spool"
#define SRESTORE_FILE SPOOL "/testclient/restore"
#define SRESTORE_FILE_CLI2 SPOOL "/cli2/restore"
static struct ioevent_list reads;
static struct ioevent_list writes;
static struct conf **setup_conf(void)
{
struct conf **confs=NULL;
fail_unless((confs=confs_alloc())!=NULL);
fail_unless(!confs_init(confs));
return confs;
}
static struct async *setup_async(void)
{
struct async *as;
fail_unless((as=async_alloc())!=NULL);
as->init(as, 0 /* estimate */);
return as;
}
static void clean(void)
{
fail_unless(!recursive_delete(BASE));
fail_unless(!recursive_delete(CLIENTCONFDIR));
}
static void setup(struct async **as,
struct conf ***confs, struct conf ***cconfs)
{
clean();
if(as) *as=setup_async();
if(confs) *confs=setup_conf();
if(cconfs) *cconfs=setup_conf();
}
static void tear_down(struct async **as, struct asfd **asfd,
struct conf ***confs, struct conf ***cconfs)
{
async_free(as);
asfd_free(asfd);
asfd_mock_teardown(&reads, &writes);
confs_free(confs);
confs_free(cconfs);
clean();
alloc_check();
}
static int async_rw_simple(struct async *as)
{
return as->asfd->read(as->asfd);
}
static void run_test(int expected_ret,
void setup_callback(struct asfd *asfd,
struct conf **confs, struct conf **cconfs),
void checks_callback(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore))
{
struct async *as;
struct asfd *asfd;
struct conf **confs;
struct conf **cconfs;
char *incexc=NULL;
int srestore=0;
setup(&as, &confs, &cconfs);
asfd=asfd_mock_setup(&reads, &writes);
as->asfd_add(as, asfd);
as->read_write=async_rw_simple;
asfd->as=as;
setup_callback(asfd, confs, cconfs);
fail_unless(extra_comms(
as,
&incexc,
&srestore,
confs,
cconfs
)==expected_ret);
if(checks_callback)
checks_callback(confs, cconfs, incexc, srestore);
free_w(&incexc);
tear_down(&as, &asfd, &confs, &cconfs);
}
static void setup_no_version(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
}
static void setup_old_version(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
set_string(cconfs[OPT_PEER_VERSION], "1.2.0");
}
static void setup_unexpected_first_string(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0;
set_string(cconfs[OPT_PEER_VERSION], "1.4.40");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "blah");
}
static void setup_1_3_0_write_problem(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
set_string(cconfs[OPT_PEER_VERSION], "1.3.0");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "extra_comms_begin");
asfd_assert_write(asfd, &w, -1, CMD_GEN, "extra_comms_begin ok");
}
static void common_confs(struct conf **cconfs, const char *version)
{
set_string(cconfs[OPT_PEER_VERSION], version);
set_string(cconfs[OPT_DIRECTORY], BASE "/spool");
set_string(cconfs[OPT_CNAME], TESTCLIENT);
set_string(cconfs[OPT_DEDUP_GROUP], "global");
}
static const char *get_features(int srestore, const char *version)
{
char rshash[32]="";
int old_version=0;
static char features[256]="";
#ifdef HAVE_BLAKE2
snprintf(rshash, sizeof(rshash), "rshash=blake2:");
#endif
if(version && !strcmp(version, "1.4.40"))
old_version=1;
snprintf(features, sizeof(features), "extra_comms_begin ok:autoupgrade:incexc:orig_client:uname:failover:vss_restore:regex_icase:%s%smsg:forceproto=1:%sseed:", srestore?"srestore:":"", old_version?"":"counters_json:", rshash);
return features;
}
static void setup_feature_write_problem(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
const char *features;
common_confs(cconfs, PACKAGE_VERSION);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "extra_comms_begin");
features=get_features(/*srestore*/0, NULL/*version*/);
asfd_assert_write(asfd, &w, -1, CMD_GEN, features);
}
static void setup_send_features_begin(struct asfd *asfd,
struct conf **confs, struct conf **cconfs,
int *r, int *w, const char *version, int srestore)
{
const char *features=NULL;
common_confs(cconfs, version);
asfd_mock_read(asfd, r, 0, CMD_GEN, "extra_comms_begin");
features=get_features(srestore, version);
asfd_assert_write(asfd, w, 0, CMD_GEN, features);
}
static void setup_send_features_end(struct asfd *asfd, int *r, int *w)
{
asfd_mock_read(asfd, r, 0, CMD_GEN, "extra_comms_end");
asfd_assert_write(asfd, w, 0, CMD_GEN, "extra_comms_end ok");
}
static void setup_unexpected_cmd_feature(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_FILE, "blah");
}
static void setup_simple(struct asfd *asfd,
struct conf **confs, struct conf **cconfs, const char *feature,
int srestore)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, srestore);
asfd_mock_read(asfd, &r, 0, CMD_GEN, feature);
setup_send_features_end(asfd, &r, &w);
}
static void setup_autoupgrade_no_os(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
setup_simple(asfd, confs, cconfs, "autoupgrade:", /*srestore*/0);
}
static void setup_autoupgrade(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "autoupgrade:some_os");
// Server does not have an autoupgrade_dir set.
asfd_assert_write(asfd, &w, 0, CMD_GEN, "do not autoupgrade");
setup_send_features_end(asfd, &r, &w);
}
static void setup_rshash_blake2(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
#ifdef HAVE_BLAKE2
setup_simple(asfd, confs, cconfs, "rshash=blake2", /*srestore*/0);
#else
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "rshash=blake2");
#endif
}
static void checks_rshash_blake2(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
#ifdef HAVE_BLAKE2
fail_unless(get_e_rshash(confs[OPT_RSHASH])==RSHASH_BLAKE2);
fail_unless(get_e_rshash(cconfs[OPT_RSHASH])==RSHASH_BLAKE2);
#else
fail_unless(get_e_rshash(confs[OPT_RSHASH])==RSHASH_UNSET);
fail_unless(get_e_rshash(cconfs[OPT_RSHASH])==RSHASH_UNSET);
#endif
}
static void setup_msg(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
setup_simple(asfd, confs, cconfs, "msg", /*srestore*/0);
}
static void checks_msg(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
fail_unless(get_int(confs[OPT_MESSAGE])==1);
fail_unless(get_int(cconfs[OPT_MESSAGE])==1);
}
static void setup_counters_ok(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
setup_simple(asfd, confs, cconfs, "counters_json ok", /*srestore*/0);
}
static void checks_counters_ok(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
fail_unless(get_int(cconfs[OPT_SEND_CLIENT_CNTR])==1);
}
static void setup_uname(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
setup_simple(asfd, confs, cconfs, "uname=some_os", /*srestore*/0);
}
static void checks_uname(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
fail_unless(get_int(cconfs[OPT_CLIENT_IS_WINDOWS])==0);
}
static void setup_uname_is_windows(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
setup_simple(asfd, confs, cconfs, "uname=Windows", /*srestore*/0);
}
static void checks_uname_is_windows(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
fail_unless(get_int(cconfs[OPT_CLIENT_IS_WINDOWS])==1);
}
static void setup_unexpected_feature(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "somenonsense");
}
static void setup_srestore_not_ok(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
build_file(SRESTORE_FILE, "");
setup_simple(asfd, confs, cconfs, "srestore not ok", /*srestore*/1);
}
static void checks_srestore_not_ok(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
struct stat statp;
// Should have deleted the restore file.
fail_unless(lstat(SRESTORE_FILE, &statp));
fail_unless(get_string(cconfs[OPT_RESTORE_PATH])==NULL);
}
static void setup_srestore(struct asfd *asfd,
struct conf **confs, struct conf **cconfs, int *r, int *w)
{
struct strlist *strlist=NULL;
build_file(SRESTORE_FILE, "");
setup_send_features_begin(asfd, confs, cconfs,
r, w, PACKAGE_VERSION, /*srestore*/1);
strlist_add(&strlist, "/some/path", 1);
// This needs to get unset.
set_strlist(cconfs[OPT_INCEXCDIR], strlist);
asfd_mock_read(asfd, r, 0, CMD_GEN, "srestore ok");
asfd_assert_write(asfd, w, 0, CMD_GEN, "overwrite = 0");
asfd_assert_write(asfd, w, 0, CMD_GEN, "strip = 0");
asfd_assert_write(asfd, w, 0, CMD_GEN, "regex_case_insensitive = 0");
asfd_assert_write(asfd, w, 0, CMD_GEN, "srestore end");
}
static void setup_srestore_ok(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_srestore(asfd, confs, cconfs, &r, &w);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "srestore end ok");
setup_send_features_end(asfd, &r, &w);
}
static void checks_srestore_ok(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
struct stat statp;
// Should not have deleted the restore file.
fail_unless(!lstat(SRESTORE_FILE, &statp));
fail_unless(!strcmp(get_string(cconfs[OPT_RESTORE_PATH]),
SRESTORE_FILE));
fail_unless(srestore==1);
fail_unless(get_strlist(cconfs[OPT_INCEXCDIR])==NULL);
}
static void setup_srestore_ok_error(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_srestore(asfd, confs, cconfs, &r, &w);
asfd_mock_read(asfd, &r, -1, CMD_GEN, "srestore end ok");
}
static void setup_sincexc_ok(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "sincexc ok");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "cross_all_filesystems = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "read_all_fifos = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "read_all_blockdevs = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "min_file_size = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "max_file_size = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "split_vss = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "strip_vss = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "acl = 1");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "xattr = 1");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "atime = 0");
asfd_assert_write(asfd, &w, 0,
CMD_GEN, "scan_problem_raises_error = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "overwrite = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "strip = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "regex_case_insensitive = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "sincexc end");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "sincexc end ok");
setup_send_features_end(asfd, &r, &w);
}
static void setup_incexc(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "incexc");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "incexc ok");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "include = /some/path");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "incexc end");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "incexc end ok");
setup_send_features_end(asfd, &r, &w);
}
static void checks_incexc(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
fail_unless(!strcmp(incexc,
"include = /some/path\n\ncompression = 9\n"));
}
static void setup_orig_client_not_existing(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "orig_client=cli2");
}
static void setup_orig_client(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
common_confs(cconfs, PACKAGE_VERSION);
set_string(confs[OPT_CLIENTCONFDIR], CLIENTCONFDIR);
build_file(CLIENTCONFDIR "/cli2", "restore_client=" TESTCLIENT);
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/0);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "orig_client=cli2");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "orig_client ok");
setup_send_features_end(asfd, &r, &w);
}
static void checks_orig_client(struct conf **confs, struct conf **cconfs,
const char *incexc, int srestore)
{
const char *orig_client;
const char *restore_client;
fail_unless(!strcmp(get_string(cconfs[OPT_CNAME]), "cli2"));
restore_client=get_string(cconfs[OPT_SUPER_CLIENT]);
orig_client=get_string(cconfs[OPT_ORIG_CLIENT]);
fail_unless(!strcmp(orig_client, restore_client));
}
static void setup_orig_client_srestore(struct asfd *asfd,
struct conf **confs, struct conf **cconfs)
{
int r=0; int w=0;
build_file(SRESTORE_FILE, "overwrite=1");
build_file(SRESTORE_FILE_CLI2, "strip=1");
common_confs(cconfs, PACKAGE_VERSION);
set_string(confs[OPT_CLIENTCONFDIR], CLIENTCONFDIR);
build_file(CLIENTCONFDIR "/cli2", "restore_client=" TESTCLIENT);
setup_send_features_begin(asfd, confs, cconfs,
&r, &w, PACKAGE_VERSION, /*srestore*/1);
asfd_mock_read(asfd, &r, 0, CMD_GEN, "srestore ok");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "overwrite = 1");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "strip = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "regex_case_insensitive = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "srestore end");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "srestore end ok");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "orig_client=cli2");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "orig_client ok");
// Should get the same as before, with orig_client added.
// That is, it should not read from cli2's restore file.
asfd_mock_read(asfd, &r, 0, CMD_GEN, "srestore ok");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "overwrite = 1");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "strip = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "regex_case_insensitive = 0");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "orig_client = cli2");
asfd_assert_write(asfd, &w, 0, CMD_GEN, "srestore end");
asfd_mock_read(asfd, &r, 0, CMD_GEN, "srestore end ok");
setup_send_features_end(asfd, &r, &w);
}
static void checks_orig_client_srestore(struct conf **confs,
struct conf **cconfs, const char *incexc, int srestore)
{
struct stat statp;
fail_unless(srestore==1);
// Should not have deleted either restore file.
fail_unless(!lstat(SRESTORE_FILE, &statp));
fail_unless(!lstat(SRESTORE_FILE_CLI2, &statp));
}
START_TEST(test_server_extra_comms)
{
run_test(0, setup_no_version, NULL);
run_test(0, setup_old_version, NULL);
run_test(-1, setup_unexpected_first_string, NULL);
run_test(-1, setup_1_3_0_write_problem, NULL);
run_test(-1, setup_feature_write_problem, NULL);
run_test(-1, setup_unexpected_cmd_feature,
NULL);
run_test(0, setup_autoupgrade_no_os,
NULL);
run_test(0, setup_autoupgrade,
NULL);
#ifdef HAVE_BLAKE2
run_test(0, setup_rshash_blake2, checks_rshash_blake2);
#else
run_test(-1, setup_rshash_blake2, checks_rshash_blake2);
#endif
run_test(0, setup_counters_ok, checks_counters_ok);
run_test(0, setup_msg, checks_msg);
run_test(0, setup_uname, checks_uname);
run_test(0, setup_uname_is_windows, checks_uname_is_windows);
run_test(-1, setup_unexpected_feature, NULL);
run_test(0, setup_srestore_not_ok, checks_srestore_not_ok);
run_test(0, setup_srestore_ok, checks_srestore_ok);
run_test(-1, setup_srestore_ok_error, NULL);
run_test(0, setup_sincexc_ok, NULL);
run_test(0, setup_incexc, checks_incexc);
run_test(-1, setup_orig_client_not_existing, NULL);
run_test(0, setup_orig_client, checks_orig_client);
run_test(0, setup_orig_client_srestore, checks_orig_client_srestore);
}
END_TEST
Suite *suite_server_extra_comms(void)
{
Suite *s;
TCase *tc_core;
s=suite_create("server_extra_comms");
tc_core=tcase_create("Core");
tcase_set_timeout(tc_core, 60);
tcase_add_test(tc_core, test_server_extra_comms);
suite_add_tcase(s, tc_core);
return s;
}
|