diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-05-07 15:29:56 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-05-07 15:29:56 -0400 |
commit | e086db795238da9187aa9824fda3d708cff10bf6 (patch) | |
tree | 13dc823d24ea89a6fbf47c92994b8fa997887a53 /src | |
parent | a2a6a19db165dd08b2dd6e7e493f21d2336a85d0 (diff) | |
parent | 79e85313aa611b599f19fea61c38ff3928e1fd59 (diff) | |
download | tor-e086db795238da9187aa9824fda3d708cff10bf6.tar.gz tor-e086db795238da9187aa9824fda3d708cff10bf6.zip |
Merge branch 'writing_tests'
Diffstat (limited to 'src')
-rw-r--r-- | src/common/tortls.c | 4 | ||||
-rw-r--r-- | src/common/tortls.h | 2 | ||||
-rw-r--r-- | src/common/util.c | 2 | ||||
-rw-r--r-- | src/common/util.h | 2 | ||||
-rw-r--r-- | src/or/buffers.c | 2 | ||||
-rw-r--r-- | src/test/test_buffers.c | 55 | ||||
-rw-r--r-- | src/test/test_util.c | 30 |
7 files changed, 91 insertions, 6 deletions
diff --git a/src/common/tortls.c b/src/common/tortls.c index bd7e95c033..63b40258c2 100644 --- a/src/common/tortls.c +++ b/src/common/tortls.c @@ -2092,8 +2092,8 @@ tor_tls_free(tor_tls_t *tls) * number of characters read. On failure, returns TOR_TLS_ERROR, * TOR_TLS_CLOSE, TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE. */ -int -tor_tls_read(tor_tls_t *tls, char *cp, size_t len) +MOCK_IMPL(int, +tor_tls_read,(tor_tls_t *tls, char *cp, size_t len)) { int r, err; tor_assert(tls); diff --git a/src/common/tortls.h b/src/common/tortls.h index f8c6d5913b..2aac8af18e 100644 --- a/src/common/tortls.h +++ b/src/common/tortls.h @@ -77,7 +77,7 @@ int tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity); int tor_tls_check_lifetime(int severity, tor_tls_t *tls, int past_tolerance, int future_tolerance); -int tor_tls_read(tor_tls_t *tls, char *cp, size_t len); +MOCK_DECL(int, tor_tls_read, (tor_tls_t *tls, char *cp, size_t len)); int tor_tls_write(tor_tls_t *tls, const char *cp, size_t n); int tor_tls_handshake(tor_tls_t *tls); int tor_tls_finish_handshake(tor_tls_t *tls); diff --git a/src/common/util.c b/src/common/util.c index f8d1b7be4b..942d0c290e 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -3555,7 +3555,7 @@ finish_daemon(const char *cp) /** Write the current process ID, followed by NL, into <b>filename</b>. */ void -write_pidfile(char *filename) +write_pidfile(const char *filename) { FILE *pidfile; diff --git a/src/common/util.h b/src/common/util.h index 30ac248b30..ac4ebfc6f3 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -418,7 +418,7 @@ int path_is_relative(const char *filename); /* Process helpers */ void start_daemon(void); void finish_daemon(const char *desired_cwd); -void write_pidfile(char *filename); +void write_pidfile(const char *filename); /* Port forwarding */ void tor_check_port_forwarding(const char *filename, diff --git a/src/or/buffers.c b/src/or/buffers.c index 8ef3279fde..2d7dd937d8 100644 --- a/src/or/buffers.c +++ b/src/or/buffers.c @@ -615,7 +615,7 @@ read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf) if (r < 0) return r; /* Error */ tor_assert(total_read+r < INT_MAX); - total_read += r; + total_read += r; if ((size_t)r < readlen) /* eof, block, or no more to read. */ break; } diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c index 0ede8081d8..22ee6aa8e3 100644 --- a/src/test/test_buffers.c +++ b/src/test/test_buffers.c @@ -698,6 +698,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 }, @@ -710,6 +763,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 6a4c3ec072..30dc59844a 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -4302,6 +4302,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), @@ -4368,6 +4397,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 }; |