static void test_entropy_source(void *arg) { struct entropy_source_test_data *d = arg; u8 buf[ENTROPY_CHUNK * 2]; int n; unsigned flags; if (d->fn == NULL) tt_skip(); memset(buf, 0, sizeof(buf)); n = d->fn(buf, &flags); if (n == -2) tt_skip(); if (n == -1 && d->is_iffy) tt_skip(); tt_int_op(n, ==, ENTROPY_CHUNK); tt_assert(!iszero(buf, ENTROPY_CHUNK)); tt_assert(iszero(buf + ENTROPY_CHUNK, ENTROPY_CHUNK)); end: ; }
static void test_crypto_rng_strongest(void *arg) { const char *how = arg; int broken = 0; if (how == NULL) { ; } else if (!strcmp(how, "nosyscall")) { break_strongest_rng_syscall = 1; } else if (!strcmp(how, "nofallback")) { break_strongest_rng_fallback = 1; } else if (!strcmp(how, "broken")) { broken = break_strongest_rng_syscall = break_strongest_rng_fallback = 1; } #define N 128 uint8_t combine_and[N]; uint8_t combine_or[N]; int i, j; memset(combine_and, 0xff, N); memset(combine_or, 0, N); for (i = 0; i < 100; ++i) { /* 2^-100 chances just don't happen. */ uint8_t output[N]; memset(output, 0, N); if (how == NULL) { /* this one can't fail. */ crypto_strongest_rand(output, sizeof(output)); } else { int r = crypto_strongest_rand_raw(output, sizeof(output)); if (r == -1) { if (broken) { goto done; /* we're fine. */ } /* This function is allowed to break, but only if it always breaks. */ tt_int_op(i, OP_EQ, 0); tt_skip(); } else { tt_assert(! broken); } } for (j = 0; j < N; ++j) { combine_and[j] &= output[j]; combine_or[j] |= output[j]; } } for (j = 0; j < N; ++j) { tt_int_op(combine_and[j], OP_EQ, 0); tt_int_op(combine_or[j], OP_EQ, 0xff); } done: ; #undef N }
static void dns_gethostbyname6(void) { dns_ok = 0; evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL); event_dispatch(); if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) { tt_skip(); } tt_int_op(dns_ok, ==, DNS_IPv6_AAAA); test_ok = 1; end: ; }
static void test_crypto_scrypt_vectors(void *arg) { char *mem_op_hex_tmp = NULL; uint8_t spec[64], out[64]; (void)arg; #ifndef HAVE_LIBSCRYPT_H if (1) tt_skip(); #endif /* Test vectors from http://tools.ietf.org/html/draft-josefsson-scrypt-kdf-00 section 11. Note that the names of 'r' and 'N' are switched in that section. Or possibly in libscrypt. */ base16_decode((char*)spec, sizeof(spec), "0400", 4); memset(out, 0x00, sizeof(out)); tt_int_op(64, OP_EQ, secret_to_key_compute_key(out, 64, spec, 2, "", 0, 2)); test_memeq_hex(out, "77d6576238657b203b19ca42c18a0497" "f16b4844e3074ae8dfdffa3fede21442" "fcd0069ded0948f8326a753a0fc81f17" "e8d3e0fb2e0d3628cf35e20c38d18906"); base16_decode((char*)spec, sizeof(spec), "4e61436c" "0A34", 12); memset(out, 0x00, sizeof(out)); tt_int_op(64, OP_EQ, secret_to_key_compute_key(out, 64, spec, 6, "password", 8, 2)); test_memeq_hex(out, "fdbabe1c9d3472007856e7190d01e9fe" "7c6ad7cbc8237830e77376634b373162" "2eaf30d92e22a3886ff109279d9830da" "c727afb94a83ee6d8360cbdfa2cc0640"); base16_decode((char*)spec, sizeof(spec), "536f6469756d43686c6f72696465" "0e30", 32); memset(out, 0x00, sizeof(out)); tt_int_op(64, OP_EQ, secret_to_key_compute_key(out, 64, spec, 16, "pleaseletmein", 13, 2)); test_memeq_hex(out, "7023bdcb3afd7348461c06cd81fd38eb" "fda8fbba904f8e3ea9b543f6545da1f2" "d5432955613f0fcf62d49705242a9af9" "e61e85dc0d651e40dfcf017b45575887"); base16_decode((char*)spec, sizeof(spec), "536f6469756d43686c6f72696465" "1430", 32); memset(out, 0x00, sizeof(out)); tt_int_op(64, OP_EQ, secret_to_key_compute_key(out, 64, spec, 16, "pleaseletmein", 13, 2)); test_memeq_hex(out, "2101cb9b6a511aaeaddbbe09cf70f881" "ec568d574a2ffd4dabe5ee9820adaa47" "8e56fd8f4ba5d09ffa1c6d927c40f4c3" "37304049e8a952fbcbf45c6fa77a41a4"); done: tor_free(mem_op_hex_tmp); }
void test_km_readline_realloc (void *ptr) { char *lorem_fn = NULL; char *buf = NULL; char *smallbuf = NULL; const size_t smallbuf_len = 1<<4; FILE *fp = NULL; ssize_t ret = 0; size_t line_num; char *nulcp = NULL; FILE *nulfp = NULL; char *tmpcp = NULL; size_t tmpsz = 0; size_t our_bufsize = bufsize; size_t our_smallbuf_len = smallbuf_len; km_test_err = 0; /* This should always work, so long as you run it from the right dir */ lorem_fn = get_test_filename("loremipsum.txt"); if (lorem_fn == NULL) tt_abort_msg("Broken test - get_test_filename failed\n"); if ((fp = fopen(lorem_fn, "r")) == NULL) { fprintf(stderr, "Could not open test file '%s' -- %s\n", lorem_fn, strerror(errno)); tt_skip(); } else { buf = calloc(our_bufsize, sizeof(*buf)); smallbuf = calloc(our_smallbuf_len, sizeof(*smallbuf)); } for (line_num = 0; line_num < n_loremipsum_lines; line_num++) { ret = km_readline_realloc(&buf, fp, &our_bufsize, &test_err_handler); tt_int_op(km_test_err, ==, 0); tt_int_op(strncmp(buf, loremipsum_lines[line_num], our_bufsize), ==, 0); tt_int_op(strlen(buf), ==, loremipsum_line_lens[line_num]); tt_int_op(ret, ==, loremipsum_line_lens[line_num]); tt_int_op(our_bufsize, ==, bufsize); km_test_err = 0; } ret = km_readline_realloc(&buf, fp, &our_bufsize, &test_err_handler); tt_int_op(km_test_err, ==, 0); /* check it leaves \0 in buf */ tt_int_op(strncmp(buf, "", our_bufsize), ==, 0); tt_int_op(strlen(buf), ==, 0); tt_int_op(ret, ==, EOF); tt_int_op(our_bufsize, ==, bufsize); km_test_err = 0; /* Naughty tests that try and make it fail */ rewind(fp); /* Null buf */ ret = km_readline_realloc(&nulcp, fp, &our_bufsize, &test_err_handler); tt_int_op(km_test_err, ==, 3); tt_int_op(ret, ==, -2); tt_int_op(our_bufsize, ==, bufsize); km_test_err = 0; /* Null fp */ ret = km_readline_realloc(&buf, nulfp, &our_bufsize, &test_err_handler); tt_int_op(km_test_err, ==, 3); tt_int_op(ret, ==, -2); tt_int_op(our_bufsize, ==, bufsize); km_test_err = 0; /* Both buf & fp null */ ret = km_readline_realloc(&nulcp, nulfp, &our_bufsize, &test_err_handler); tt_int_op(km_test_err, ==, 3); tt_int_op(ret, ==, -2); tt_int_op(our_bufsize, ==, bufsize); km_test_err = 0; /* Test that should require it to resize the buffer */ rewind(fp); ret = km_readline_realloc(&smallbuf, fp, &our_smallbuf_len, &test_err_handler); tt_int_op(km_test_err, ==, 0); tt_int_op(ret, ==, loremipsum_line_lens[0]); tt_int_op(strlen(smallbuf), ==, loremipsum_line_lens[0]); tmpsz = loremipsum_line_lens[0]; tt_int_op(our_smallbuf_len, ==, kmroundupz(tmpsz)); end: if (lorem_fn != NULL) free(lorem_fn); if (buf != NULL) free(buf); if (smallbuf != NULL) free(smallbuf); if (fp != NULL) fclose(fp); }