diff options
author | Nick Mathewson <nickm@torproject.org> | 2014-09-18 14:03:49 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-05-07 15:29:16 -0400 |
commit | 79e85313aa611b599f19fea61c38ff3928e1fd59 (patch) | |
tree | d6c25dae4999d42a33810134f49d7d650a25b2dc /src/test | |
parent | 3668a4126e0c2502a77c2ba5d7885add489a964a (diff) | |
download | tor-79e85313aa611b599f19fea61c38ff3928e1fd59.tar.gz tor-79e85313aa611b599f19fea61c38ff3928e1fd59.zip |
Write the outlines of a WritingTests.txt document
Also, add some sample tests to be examples.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test_buffers.c | 55 | ||||
-rw-r--r-- | src/test/test_util.c | 30 |
2 files changed, 85 insertions, 0 deletions
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c index 101f448472..8ca8be567f 100644 --- a/src/test/test_buffers.c +++ b/src/test/test_buffers.c @@ -714,6 +714,59 @@ test_buffers_zlib_fin_at_chunk_end(void *arg) tor_free(msg); } +const uint8_t *tls_read_ptr; +int n_remaining; +int next_reply_val[16]; + +static int +mock_tls_read(tor_tls_t *tls, char *cp, size_t len) +{ + (void)tls; + int rv = next_reply_val[0]; + if (rv > 0) { + int max = rv > (int)len ? (int)len : rv; + if (max > n_remaining) + max = n_remaining; + memcpy(cp, tls_read_ptr, max); + rv = max; + n_remaining -= max; + tls_read_ptr += max; + } + + memmove(next_reply_val, next_reply_val + 1, 15*sizeof(int)); + return rv; +} + +static void +test_buffers_tls_read_mocked(void *arg) +{ + uint8_t *mem; + buf_t *buf; + (void)arg; + + mem = tor_malloc(64*1024); + crypto_rand((char*)mem, 64*1024); + tls_read_ptr = mem; + n_remaining = 64*1024; + + MOCK(tor_tls_read, mock_tls_read); + + buf = buf_new(); + + next_reply_val[0] = 1024; + tt_int_op(128, ==, read_to_buf_tls(NULL, 128, buf)); + + next_reply_val[0] = 5000; + next_reply_val[1] = 5000; + tt_int_op(6000, ==, read_to_buf_tls(NULL, 6000, buf)); + + + done: + UNMOCK(tor_tls_read); + tor_free(mem); + buf_free(buf); +} + struct testcase_t buffer_tests[] = { { "basic", test_buffers_basic, TT_FORK, NULL, NULL }, { "copy", test_buffer_copy, TT_FORK, NULL, NULL }, @@ -726,6 +779,8 @@ struct testcase_t buffer_tests[] = { { "zlib_fin_with_nil", test_buffers_zlib_fin_with_nil, TT_FORK, NULL, NULL }, { "zlib_fin_at_chunk_end", test_buffers_zlib_fin_at_chunk_end, TT_FORK, NULL, NULL}, + { "tls_read_mocked", test_buffers_tls_read_mocked, 0, + NULL, NULL }, END_OF_TESTCASES }; diff --git a/src/test/test_util.c b/src/test/test_util.c index 15470e8efa..3bebe60490 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -4905,6 +4905,35 @@ test_util_ipv4_validation(void *arg) return; } +static void +test_util_writepid(void *arg) +{ + (void) arg; + + char *contents = NULL; + const char *fname = get_fname("tmp_pid"); + unsigned long pid; + char c; + + write_pidfile(fname); + + contents = read_file_to_str(fname, 0, NULL); + tt_assert(contents); + + int n = sscanf(contents, "%lu\n%c", &pid, &c); + tt_int_op(n, OP_EQ, 1); + tt_uint_op(pid, OP_EQ, +#ifdef _WIN32 + _getpid() +#else + getpid() +#endif + ); + + done: + tor_free(contents); +} + struct testcase_t util_tests[] = { UTIL_LEGACY(time), UTIL_TEST(parse_http_time, 0), @@ -4981,6 +5010,7 @@ struct testcase_t util_tests[] = { UTIL_TEST(max_mem, 0), UTIL_TEST(hostname_validation, 0), UTIL_TEST(ipv4_validation, 0), + UTIL_TEST(writepid, 0), END_OF_TESTCASES }; |