int main(int argc, char **argv) { unsigned long k; struct test_data *td; t_set_colors(0); t_start("fanout tests"); run_tests(10, 64); run_tests(512, 64); run_tests(64, 64); run_tests(511, 17); destroyed = 0; fot = fanout_create(512); ok_int(fanout_remove(fot, 12398) == NULL, 1, "remove on empty table must yield NULL"); ok_int(fanout_get(fot, 123887987) == NULL, 1, "get on empty table must yield NULL"); for (k = 0; k < 16385; k++) { struct test_data *tdata = calloc(1, sizeof(*td)); tdata->key = k; asprintf(&tdata->name, "%lu", k); fanout_add(fot, k, tdata); } td = fanout_get(fot, k - 1); ok_int(td != NULL, 1, "get must get what add inserts"); ok_int(fanout_remove(fot, k + 1) == NULL, 1, "remove on non-inserted key must yield NULL"); ok_int(fanout_get(fot, k + 1) == NULL, 1, "get on non-inserted must yield NULL"); fanout_destroy(fot, pdest); ok_int((int)destroyed, (int)k, "destroy counter while free()'ing"); return t_end(); }
int main(int argc, char **argv) { int ret, r2; runcmd_init(); t_set_colors(0); t_start("exec output comparison"); { int i; char *out = calloc(1, BUF_SIZE); for (i = 0; cases[i].input != NULL; i++) { memset(out, 0, BUF_SIZE); int pfd[2] = {-1, -1}, pfderr[2] = {-1, -1}; int fd; char *cmd; asprintf(&cmd, "/bin/echo -n %s", cases[i].input); fd = runcmd_open(cmd, pfd, pfderr, NULL); read(pfd[0], out, BUF_SIZE); ok_str(cases[i].output, out, "Echoing a command should give expected output"); close(pfd[0]); close(pfderr[0]); close(fd); } } ret = t_end(); t_reset(); t_start("anomaly detection"); { int i; for (i = 0; anomaly[i].cmd; i++) { int out_argc; char *out_argv[256]; int result = runcmd_cmd2strv(anomaly[i].cmd, &out_argc, out_argv); ok_int(result, anomaly[i].ret, anomaly[i].cmd); } } r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); t_start("argument splitting"); { int i; for (i = 0; parse_case[i].cmd; i++) { int x, out_argc; char *out_argv[256]; int result = runcmd_cmd2strv(parse_case[i].cmd, &out_argc, out_argv); out_argv[out_argc] = NULL; ok_int(result, 0, parse_case[i].cmd); ok_int(out_argc, parse_case[i].argc_exp, parse_case[i].cmd); for (x = 0; x < parse_case[x].argc_exp && out_argv[x]; x++) { ok_str(parse_case[i].argv_exp[x], out_argv[x], "argv comparison test"); } } } r2 = t_end(); return r2 ? r2 : ret; }
int main(int argc, char **argv) { int listen_fd, flags, sockopt = 1; struct sockaddr_in sain; int error; const char *err_msg; t_set_colors(0); t_start("iobroker ipc test"); error = iobroker_get_max_fds(NULL); ok_int(error, IOBROKER_ENOSET, "test errors when passing null"); err_msg = iobroker_strerror(error); test(err_msg && !strcmp(err_msg, iobroker_errors[(~error) + 1].string), "iobroker_strerror() returns the right string"); iobs = iobroker_create(); error = iobroker_get_max_fds(iobs); test(iobs && error >= 0, "max fd's for real iobroker set must be > 0"); listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); flags = fcntl(listen_fd, F_GETFD); flags |= FD_CLOEXEC; fcntl(listen_fd, F_SETFD, flags); (void)setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); memset(&sain, 0, sizeof(sain)); sain.sin_port = ntohs(9123); sain.sin_family = AF_INET; bind(listen_fd, (struct sockaddr *)&sain, sizeof(sain)); listen(listen_fd, 128); iobroker_register(iobs, listen_fd, iobs, listen_handler); if (argc == 1) conn_spam(&sain); for (;;) { iobroker_poll(iobs, -1); if (iobroker_get_num_fds(iobs) <= 1) { break; } } iobroker_close(iobs, listen_fd); iobroker_destroy(iobs, 0); t_end(); return 0; }
int main(int argc, char **argv) { unsigned int i; struct strcode sc[] = { ADDSTR("\n"), ADDSTR("\0\0"), ADDSTR("XXXxXXX"), ADDSTR("LALALALALALALAKALASBALLE\n"), }; t_set_colors(0); t_start("iocache_use_delim() test"); for (i = 0; i < ARRAY_SIZE(sc); i++) { t_start("Testing delimiter of len %d", sc[i].len); test_delimiter(sc[i].str, sc[i].len); t_end(); } return t_end(); }
int main(int argc, char **argv) { int ret, r2; runcmd_init(); t_set_colors(0); t_start("exec output comparison"); { int i; char *out = calloc(1, BUF_SIZE); for (i = 0; cases[i].input != NULL; i++) { memset(out, 0, BUF_SIZE); int pfd[2] = {-1, -1}, pfderr[2] = {-1, -1}; /* We need a stub iobregarg since runcmd_open()'s prototype * declares it attribute non-null. */ int stub_iobregarg = 0; int fd; char *cmd; asprintf(&cmd, ECHO_COMMAND " -n %s", cases[i].input); fd = runcmd_open(cmd, pfd, pfderr, NULL, stub_iobreg, &stub_iobregarg); free(cmd); read(pfd[0], out, BUF_SIZE); ok_str(cases[i].output, out, "Echoing a command should give expected output"); close(pfd[0]); close(pfderr[0]); close(fd); } free(out); } ret = t_end(); t_reset(); t_start("anomaly detection"); { int i; for (i = 0; anomaly[i].cmd; i++) { int out_argc; char *out_argv[256]; int result = runcmd_cmd2strv(anomaly[i].cmd, &out_argc, out_argv); ok_int(result, anomaly[i].ret, anomaly[i].cmd); if (out_argv[0]) free(out_argv[0]); } } r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); t_start("argument splitting"); { int i; for (i = 0; parse_case[i].cmd; i++) { int x, out_argc; char *out_argv[256]; int result = runcmd_cmd2strv(parse_case[i].cmd, &out_argc, out_argv); /*out_argv[out_argc] = NULL;*//* This must be NULL terminated already. */ ok_int(result, parse_case[i].ret, parse_case[i].cmd); ok_int(out_argc, parse_case[i].argc_exp, parse_case[i].cmd); for (x = 0; x < parse_case[x].argc_exp && out_argv[x]; x++) { ok_str(parse_case[i].argv_exp[x], out_argv[x], "argv comparison test"); } if (out_argv[0]) free(out_argv[0]); } } r2 = t_end(); return r2 ? r2 : ret; }
int main(int argc, char **argv) { int i, j; struct kvvec *kvv, *kvv2, *kvv3; struct kvvec_buf *kvvb, *kvvb2; struct kvvec k = KVVEC_INITIALIZER; t_set_colors(0); t_start("key/value vector tests"); kvv = kvvec_create(1); kvv2 = kvvec_create(1); kvv3 = kvvec_create(1); add_vars(kvv, test_data, 1239819); add_vars(kvv, (const char **)argv + 1, argc - 1); kvvec_sort(kvv); kvvec_foreach(kvv, NULL, walker); /* kvvec2buf -> buf2kvvec -> kvvec2buf -> buf2kvvec conversion */ kvvb = kvvec2buf(kvv, KVSEP, PAIRSEP, OVERALLOC); kvv3 = buf2kvvec(kvvb->buf, kvvb->buflen, KVSEP, PAIRSEP, KVVEC_COPY); kvvb2 = kvvec2buf(kvv3, KVSEP, PAIRSEP, OVERALLOC); buf2kvvec_prealloc(kvv2, kvvb->buf, kvvb->buflen, KVSEP, PAIRSEP, KVVEC_ASSIGN); kvvec_foreach(kvv2, kvv, walker); kvvb = kvvec2buf(kvv, KVSEP, PAIRSEP, OVERALLOC); test(kvv->kv_pairs == kvv2->kv_pairs, "pairs should be identical"); for (i = 0; i < kvv->kv_pairs; i++) { struct key_value *kv1, *kv2; kv1 = &kvv->kv[i]; if (i >= kvv2->kv_pairs) { t_fail("missing var %d in kvv2", i); printf("[%s=%s] (%d+%d)\n", kv1->key, kv1->value, kv1->key_len, kv1->value_len); continue; } kv2 = &kvv2->kv[i]; if (!test(!kv_compare(kv1, kv2), "kv pair %d must match", i)) { printf("%d failed: [%s=%s] (%d+%d) != [%s=%s (%d+%d)]\n", i, kv1->key, kv1->value, kv1->key_len, kv1->value_len, kv2->key, kv2->value, kv2->key_len, kv2->value_len); } } test(kvvb2->buflen == kvvb->buflen, "buflens must match"); test(kvvb2->bufsize == kvvb->bufsize, "bufsizes must match"); if (kvvb2->buflen == kvvb->buflen && kvvb2->bufsize == kvvb->bufsize && !memcmp(kvvb2->buf, kvvb->buf, kvvb->bufsize)) { t_pass("kvvec -> buf -> kvvec conversion works flawlessly"); } else { t_fail("kvvec -> buf -> kvvec conversion failed :'("); } free(kvvb->buf); free(kvvb); free(kvvb2->buf); free(kvvb2); kvvec_destroy(kvv, 1); kvvec_destroy(kvv3, KVVEC_FREE_ALL); for (j = 0; pair_term_missing[j]; j++) { buf2kvvec_prealloc(&k, strdup(pair_term_missing[j]), strlen(pair_term_missing[j]), '=', ';', KVVEC_COPY); for (i = 0; i < k.kv_pairs; i++) { struct key_value *kv = &k.kv[i]; test(kv->key_len == kv->value_len, "%d.%d; key_len=%d; value_len=%d (%s = %s)", j, i, kv->key_len, kv->value_len, kv->key, kv->value); test(kv->value_len == strlen(kv->value), "%d.%d; kv->value_len(%d) == strlen(%s)(%d)", j, i, kv->value_len, kv->value, (int)strlen(kv->value)); } } t_end(); return 0; }
int main(int argc, char **argv) { squeue_t *sq; struct timeval tv; sq_test_event a, b, c, d, *x; t_set_colors(0); t_start("squeue tests"); a.id = 1; b.id = 2; c.id = 3; d.id = 4; gettimeofday(&tv, NULL); /* Order in is a, b, c, d, but we should get b, c, d, a out. */ srand(tv.tv_usec ^ tv.tv_sec); t((sq = squeue_create(1024)) != NULL); t(squeue_size(sq) == 0); /* we fill and empty the squeue completely once before testing */ sq_test_random(sq); t(squeue_size(sq) == 0, "Size should be 0 after first sq_test_random"); t((a.evt = squeue_add(sq, time(NULL) + 9, &a)) != NULL); t(squeue_size(sq) == 1); t((b.evt = squeue_add(sq, time(NULL) + 3, &b)) != NULL); t(squeue_size(sq) == 2); t((c.evt = squeue_add_msec(sq, time(NULL) + 5, 0, &c)) != NULL); t(squeue_size(sq) == 3); t((d.evt = squeue_add_usec(sq, time(NULL) + 5, 1, &d)) != NULL); t(squeue_size(sq) == 4); /* add and remove lots. remainder should be what we have above */ sq_test_random(sq); /* testing squeue_peek() */ t((x = (sq_test_event *)squeue_peek(sq)) != NULL); t(x == &b, "x: %p; a: %p; b: %p; c: %p; d: %p\n", x, &a, &b, &c, &d); t(x->id == b.id); t(squeue_size(sq) == 4); /* testing squeue_remove() and re-add */ t(squeue_remove(sq, b.evt) == 0); t(squeue_size(sq) == 3); t((x = squeue_peek(sq)) != NULL); t(x == &c); t((b.evt = squeue_add(sq, time(NULL) + 3, &b)) != NULL); t(squeue_size(sq) == 4); /* peek should now give us the &b event (again) */ t((x = squeue_peek(sq)) != NULL); if (x != &b) { printf("about to fail pretty f*****g hard...\n"); printf("ea: %p; &b: %p; &c: %p; ed: %p; x: %p\n", &a, &b, &c, &d, x); } t(x == &b); t(x->id == b.id); t(squeue_size(sq) == 4); /* testing squeue_pop(), lifo manner */ t((x = squeue_pop(sq)) != NULL); t(squeue_size(sq) == 3, "squeue_size(sq) = %d\n", squeue_size(sq)); t(x == &b, "x: %p; &b: %p\n", x, &b); t(x->id == b.id, "x->id: %lu; d.id: %lu\n", x->id, d.id); /* Test squeue_pop() */ t((x = squeue_pop(sq)) != NULL); t(squeue_size(sq) == 2); t(x == &c, "x->id: %lu; c.id: %lu\n", x->id, c.id); t(x->id == c.id, "x->id: %lu; c.id: %lu\n", x->id, c.id); /* this should fail gracefully (-1 return from squeue_remove()) */ t(squeue_remove(NULL, NULL) == -1); t(squeue_remove(NULL, a.evt) == -1); squeue_foreach(sq, sq_walker, NULL); /* clean up to prevent false valgrind positives */ squeue_destroy(sq, 0); return t_end(); }
int main(int argc, char **argv) { dkhash_table *tx, *t; unsigned int x; int ret, r2; struct test_data s; char *p1, *p2; char *strs[10]; char tmp[32]; t_set_colors(0); t_start("dkhash basic test"); t = dkhash_create(512); p1 = strdup("a not-so secret value"); dkhash_insert(t, "nisse", NULL, p1); ok_int(dkhash_num_entries_max(t), 1, "Added one entry, so that's max"); ok_int(dkhash_num_entries_added(t), 1, "Added one entry, so one added"); ok_int(dkhash_table_size(t), 512, "Table must be sized properly"); ok_int(dkhash_collisions(t), 0, "One entry, so zero collisions"); p2 = dkhash_get(t, "nisse", NULL); test(p1 == p2, "get should get what insert set"); dkhash_insert(t, "kalle", "bananas", p1); p2 = dkhash_get(t, "kalle", "bezinga"); test(p1 != p2, "we should never get the wrong key"); ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts"); p2 = dkhash_remove(t, "kalle", "bezinga"); ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts and 1 failed remove"); ok_int(0, dkhash_num_entries_removed(t), "should be 0 removed entries after failed remove"); p2 = dkhash_remove(t, "kalle", "bananas"); test(p1 == p2, "dkhash_remove() should return removed data"); ok_int(dkhash_num_entries(t), 1, "should be 1 entries after 2 inserts and 1 successful remove"); p2 = dkhash_remove(t, "nisse", NULL); test(p1 == p2, "dkhash_remove() should return removed data"); ret = t_end(); t_reset(); /* lots of tests below, so we shut up while they're running */ t_verbose = 0; t_start("dkhash_walk_data() test"); memset(&s, 0, sizeof(s)); /* first we set up the dkhash-tables */ tx = dkhash_create(16); for (x = 0; x < ARRAY_SIZE(keys); x++) { dkhash_insert(tx, keys[x].k1, NULL, ddup(x, 0, 0)); dkhash_insert(tx, keys[x].k2, NULL, ddup(x, 0, 0)); dkhash_insert(tx, keys[x].k1, keys[x].k2, ddup(x, 0, 0)); s.x += 3; ok_int(s.x, dkhash_num_entries(tx), "x table adding"); } ok_int(s.x, dkhash_num_entries(tx), "x table done adding"); for (x = 0; x < ARRAY_SIZE(keys); x++) { del.x = x; del.i = del.j = 0; ok_int(s.x, dkhash_num_entries(tx), "x table pre-delete"); s.x -= 3; dkhash_walk_data(tx, del_matching); ok_int(s.x, dkhash_num_entries(tx), "x table post-delete"); } test(0 == dkhash_num_entries(tx), "x table post all ops"); test(0 == dkhash_check_table(tx), "x table consistency post all ops"); dkhash_debug_table(tx, 0); r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); for (x = 0; x < 10; x++) { sprintf(tmp, "string %d", x); strs[x] = strdup(tmp); } t_start("dkhash single bucket add remove forward"); t = dkhash_create(1); for (x = 0; x < 10; x++) { dkhash_insert(t, strs[x], NULL, strs[x]); } for (x = 0; x < 10; x++) { p1 = strs[x]; p2 = dkhash_remove(t, p1, NULL); test(p1 == p2, "remove should return a value"); } r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); t_start("dkhash single bucket add remove backward"); t = dkhash_create(1); for (x = 0; x < 10; x++) { dkhash_insert(t, strs[x], NULL, strs[x]); } for (x = 9; x; x--) { p1 = strs[x]; p2 = dkhash_remove(t, p1, NULL); test(p1 == p2, "remove should return a value"); } dkhash_destroy(t); r2 = t_end(); return r2 ? r2 : ret; }