From 7551c44a5375c390b653c7b8f28d904a3156e1ae Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 19 Oct 2006 23:05:02 +0000 Subject: r9274@Kushana: nickm | 2006-10-19 16:16:58 -0400 Add unit tests for tor_mmap_file(); make tor_mmap_t.size always be the size of the file (not the size of the mapping); add an extra argument to read_file_to_str() so it can return the size of the result string. svn:r8762 --- src/or/config.c | 6 +++--- src/or/dirserv.c | 2 +- src/or/hibernate.c | 2 +- src/or/routerlist.c | 4 ++-- src/or/test.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) (limited to 'src/or') diff --git a/src/or/config.c b/src/or/config.c index b3934c4f6..1a7b6c8a0 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -2801,7 +2801,7 @@ options_init_from_torrc(int argc, char **argv) /* get config lines, assign them */ if (file_status(fname) != FN_FILE || - !(cf = read_file_to_str(fname,0))) { + !(cf = read_file_to_str(fname,0,NULL))) { if (using_default_torrc == 1) { log(LOG_NOTICE, LD_CONFIG, "Configuration file \"%s\" not present, " "using reasonable defaults.", fname); @@ -3421,7 +3421,7 @@ write_configuration_file(const char *fname, or_options_t *options) if (fname) { switch (file_status(fname)) { case FN_FILE: - old_val = read_file_to_str(fname, 0); + old_val = read_file_to_str(fname, 0, NULL); if (strcmpstart(old_val, GENERATED_FILE_PREFIX)) { rename_old = 1; } @@ -3835,7 +3835,7 @@ or_state_load(void) fname = get_or_state_fname(); switch (file_status(fname)) { case FN_FILE: - if (!(contents = read_file_to_str(fname, 0))) { + if (!(contents = read_file_to_str(fname, 0, NULL))) { log_warn(LD_FS, "Unable to read state file \"%s\"", fname); goto done; } diff --git a/src/or/dirserv.c b/src/or/dirserv.c index 16625d99c..355dcef52 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -148,7 +148,7 @@ dirserv_load_fingerprint_file(void) log_info(LD_GENERAL, "Reloading approved fingerprints from \"%s\"...", fname); - cf = read_file_to_str(fname, 0); + cf = read_file_to_str(fname, 0, NULL); if (!cf) { if (options->NamingAuthoritativeDir) { log_warn(LD_FS, "Cannot open fingerprint file '%s'. Failing.", fname); diff --git a/src/or/hibernate.c b/src/or/hibernate.c index 2144a86fe..be6761a30 100644 --- a/src/or/hibernate.c +++ b/src/or/hibernate.c @@ -587,7 +587,7 @@ read_bandwidth_usage(void) tor_snprintf(fname, sizeof(fname), "%s/bw_accounting", get_options()->DataDirectory); - if (!(s = read_file_to_str(fname, 0))) { + if (!(s = read_file_to_str(fname, 0, NULL))) { return 0; } elts = smartlist_create(); diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 2f7e0e096..25e54dfef 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -136,7 +136,7 @@ router_reload_networkstatus(void) } tor_snprintf(filename,sizeof(filename),"%s/cached-status/%s", get_options()->DataDirectory, fn); - s = read_file_to_str(filename, 0); + s = read_file_to_str(filename, 0, NULL); if (s) { stat(filename, &st); if (router_set_networkstatus(s, st.st_mtime, NS_FROM_CACHE, NULL)<0) { @@ -369,7 +369,7 @@ router_reload_router_list(void) tor_snprintf(fname, fname_len, "%s/cached-routers.new", options->DataDirectory); - contents = read_file_to_str(fname, 1); + contents = read_file_to_str(fname, 1, NULL); if (contents) { stat(fname, &st); router_load_routers_from_string(contents, diff --git a/src/or/test.c b/src/or/test.c index 5a7903f09..2b441aeec 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -1163,6 +1163,58 @@ test_strmap(void) strmap_free(map,NULL); } +static void +test_mmap(void) +{ + char *fname1 = tor_strdup(get_fname("mapped_1")); + char *fname2 = tor_strdup(get_fname("mapped_2")); + char *fname3 = tor_strdup(get_fname("mapped_3")); + const size_t buflen = 17000; + char *buf = tor_malloc(17000); + tor_mmap_t *mapping; + + crypto_rand(buf, buflen); + + write_str_to_file(fname1, "Short file.", 1); + write_bytes_to_file(fname2, buf, buflen, 1); + write_bytes_to_file(fname3, buf, 16384, 1); + + mapping = tor_mmap_file(fname1); + test_assert(mapping); + test_eq(mapping->size, strlen("Short file.")); + test_streq(mapping->data, "Short file."); + /* make sure we can unlink. */ + test_assert(unlink(fname1) == 0); + test_streq(mapping->data, "Short file."); + tor_munmap_file(mapping); + + /* Make sure that we fail to map a no-longer-existant file. */ + mapping = tor_mmap_file(fname1); + test_assert(mapping == NULL); + + /* Now try a big file that stretches across a few pages and isn't aligned */ + mapping = tor_mmap_file(fname2); + test_assert(mapping); + test_eq(mapping->size, buflen); + test_memeq(mapping->data, buf, buflen); + tor_munmap_file(mapping); + + /* Now try a big aligned file. */ + mapping = tor_mmap_file(fname3); + test_assert(mapping); + test_eq(mapping->size, 16384); + test_memeq(mapping->data, buf, 16384); + + /* fname1 got unlinked above */ + unlink(fname2); + unlink(fname3); + + tor_free(fname1); + tor_free(fname2); + tor_free(fname3); + tor_free(buf); +} + static void test_control_formats(void) { @@ -1771,6 +1823,7 @@ main(int c, char**v) test_strmap(); test_control_formats(); test_pqueue(); + test_mmap(); puts("\n========================= Onion Skins ====================="); test_onion(); test_onion_handshake(); -- cgit v1.2.3