diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-08-20 13:24:54 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-08-20 13:24:54 -0400 |
commit | 34551cda6f699cee5816a5935b56787ccb7b8f67 (patch) | |
tree | 0f5eaa0d0f01871fb860f963ab0ed194de1793c1 /src/test | |
parent | d8a8e8339781f7a1e98838c100d3f875c9bb9df7 (diff) | |
download | tor-34551cda6f699cee5816a5935b56787ccb7b8f67.tar tor-34551cda6f699cee5816a5935b56787ccb7b8f67.tar.gz |
Add unit test for tor_listdir.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test.c | 2 | ||||
-rw-r--r-- | src/test/test_util.c | 34 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/test/test.c b/src/test/test.c index 16ac1e4a6..d9528328b 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -107,6 +107,8 @@ get_fname(const char *name) { static char buf[1024]; setup_directory(); + if (!name) + return temp_dir; tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name); return buf; } diff --git a/src/test/test_util.c b/src/test/test_util.c index 22f2707d7..8a1359797 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -1106,6 +1106,39 @@ test_util_asprintf(void *ptr) tor_free(cp2); } +static void +test_util_listdir(void *ptr) +{ + smartlist_t *dir_contents = NULL; + char *fname1=NULL, *fname2=NULL, *dirname=NULL; + (void)ptr; + + fname1 = tor_strdup(get_fname("hopscotch")); + fname2 = tor_strdup(get_fname("mumblety-peg")); + dirname = tor_strdup(get_fname(NULL)); + + tt_int_op(write_str_to_file(fname1, "X\n", 0), ==, 0); + tt_int_op(write_str_to_file(fname2, "Y\n", 0), ==, 0); + + dir_contents = tor_listdir(dirname); + tt_assert(dir_contents); + /* make sure that each filename is listed. */ + tt_assert(smartlist_string_isin_case(dir_contents, "hopscotch")); + tt_assert(smartlist_string_isin_case(dir_contents, "mumblety-peg")); + + tt_assert(!smartlist_string_isin(dir_contents, ".")); + tt_assert(!smartlist_string_isin(dir_contents, "..")); + + done: + tor_free(fname1); + tor_free(fname2); + tor_free(dirname); + if (dir_contents) { + SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp)); + smartlist_free(dir_contents); + } +} + #define UTIL_LEGACY(name) \ { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name } @@ -1128,6 +1161,7 @@ struct testcase_t util_tests[] = { UTIL_LEGACY(strtok), UTIL_TEST(find_str_at_start_of_line, 0), UTIL_TEST(asprintf, 0), + UTIL_TEST(listdir, 0), END_OF_TESTCASES }; |