From d14f8f2547df35434547d8e3e2b4c1a6b7736597 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 27 Feb 2008 08:10:28 +0000 Subject: r14516@tombo: nickm | 2008-02-27 03:10:26 -0500 Write some unit tests for a few functions and cases that needed them. svn:r13751 --- src/common/test.h | 10 ++++++++-- src/or/test.c | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/common/test.h b/src/common/test.h index 72dde2b007..ead2bfadd3 100644 --- a/src/common/test.h +++ b/src/common/test.h @@ -125,14 +125,20 @@ extern int have_failed; #define test_memeq(expr1, expr2, len) \ STMT_BEGIN \ const void *_test_v1=(expr1), *_test_v2=(expr2); \ + char *mem1, *mem2; \ if (!memcmp(_test_v1,_test_v2,(len))) { \ printf("."); fflush(stdout); } else { \ have_failed = 1; \ - printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \ + mem1 = tor_malloc(len*2+1); \ + mem2 = tor_malloc(len*2+1); \ + base16_encode(mem1, len*2+1, _test_v1, len); \ + base16_encode(mem2, len*2+1, _test_v2, len); \ + printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \ + " %s != %s\n", \ _SHORT_FILE_, \ __LINE__, \ PRETTY_FUNCTION, \ - #expr1, #expr2); \ + #expr1, #expr2, mem1, mem2); \ return; \ } STMT_END diff --git a/src/or/test.c b/src/or/test.c index b0dcc26594..9a2e2fb971 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -1381,6 +1381,7 @@ test_util_smartlist(void) { smartlist_t *sl; char *cp; + size_t sz; /* XXXX test sort_digests, uniq_strings, uniq_digests */ @@ -1569,6 +1570,8 @@ test_util_smartlist(void) test_eq(smartlist_len(sl), 6); SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); smartlist_clear(sl); + cp = smartlist_pop_last(sl); + test_eq(cp, NULL); /* Test uniq() */ smartlist_split_string(sl, @@ -1603,8 +1606,9 @@ test_util_smartlist(void) test_streq(cp, "Some,say,the,Earth,fire,end,in,ice,and,some,in"); tor_free(cp); smartlist_string_remove(sl, "in"); - cp = smartlist_join_strings2(sl, "+XX", 1, 0, NULL); + cp = smartlist_join_strings2(sl, "+XX", 1, 0, &sz); test_streq(cp, "Some+say+the+Earth+fire+end+some+ice+and"); + test_eq((int)sz, 40); tor_free(cp); SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); @@ -1658,6 +1662,33 @@ test_util_smartlist(void) smartlist_clear(sl); } + { + /* digest_isin. */ + smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN)); + smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); + smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); + test_eq(0, smartlist_digest_isin(NULL, "AAAAAAAAAAAAAAAAAAAA")); + test_assert(smartlist_digest_isin(sl, "AAAAAAAAAAAAAAAAAAAA")); + test_assert(smartlist_digest_isin(sl, "\00090AAB2AAAAaasdAAAAA")); + test_eq(0, smartlist_digest_isin(sl, "\00090AAB2AAABaasdAAAAA")); + + /* sort digests */ + smartlist_sort_digests(sl); + test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); + test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); + test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); + test_eq(3, smartlist_len(sl)); + + /* uniq_digests */ + smartlist_uniq_digests(sl); + test_eq(2, smartlist_len(sl)); + test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); + test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); + + SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); + smartlist_clear(sl); + } + smartlist_free(sl); } @@ -2079,6 +2110,13 @@ test_util_mmap(void) tor_munmap_file(mapping); #endif + /* Now a zero-length file. */ + write_str_to_file(fname1, "", 1); + mapping = tor_mmap_file(fname1); + test_eq(mapping, NULL); + test_eq(ERANGE, errno); + unlink(fname1); + /* Make sure that we fail to map a no-longer-existent file. */ mapping = tor_mmap_file(fname1); test_assert(mapping == NULL); -- cgit v1.2.3-54-g00ecf