From 34551cda6f699cee5816a5935b56787ccb7b8f67 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 20 Aug 2010 13:24:54 -0400 Subject: Add unit test for tor_listdir. --- src/test/test.c | 2 ++ src/test/test_util.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'src/test') 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 }; -- cgit v1.2.3