aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_util.c
diff options
context:
space:
mode:
authorEsteban Manchado Velázquez <emanchado@demiurgo.org>2012-02-20 17:42:48 +0100
committerNick Mathewson <nickm@torproject.org>2012-03-08 21:16:45 -0500
commit043e154cddea9247c5e8432a7445a57ea5ab08c8 (patch)
tree23514a966ba66d928665617a176f0b2e18abbdf3 /src/test/test_util.c
parent6beec6a37338216f53e5e319c60005c1b970419a (diff)
downloadtor-043e154cddea9247c5e8432a7445a57ea5ab08c8.tar.gz
tor-043e154cddea9247c5e8432a7445a57ea5ab08c8.zip
Add some tests for expand_filename
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r--src/test/test_util.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 6c14718454..ecf4db2d4c 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -542,6 +542,89 @@ test_util_config_line_escaped_content(void)
tor_free(v);
}
+#ifndef MS_WINDOWS
+static void
+test_util_expand_filename(void)
+{
+ char *str;
+
+ setenv("HOME", "/home/itv", 1); /* For "internal test value" */
+
+ str = expand_filename("");
+ test_streq("", str);
+ tor_free(str);
+
+ str = expand_filename("/normal/path");
+ test_streq("/normal/path", str);
+ tor_free(str);
+
+ str = expand_filename("/normal/trailing/path/");
+ test_streq("/normal/trailing/path/", str);
+ tor_free(str);
+
+ str = expand_filename("~");
+ test_streq("/home/itv/", str);
+ tor_free(str);
+
+ str = expand_filename("$HOME/nodice");
+ test_streq("$HOME/nodice", str);
+ tor_free(str);
+
+ str = expand_filename("~/");
+ test_streq("/home/itv/", str);
+ tor_free(str);
+
+ str = expand_filename("~/foobarqux");
+ test_streq("/home/itv/foobarqux", str);
+ tor_free(str);
+
+ str = expand_filename("~/../../etc/passwd");
+ test_streq("/home/itv/../../etc/passwd", str);
+ tor_free(str);
+
+ str = expand_filename("~/trailing/");
+ test_streq("/home/itv/trailing/", str);
+ tor_free(str);
+ /* Ideally we'd test ~anotheruser, but that's shady to test (we'd
+ have to somehow inject/fake the get_user_homedir call) */
+
+ /* Try with empty $HOME */
+
+ setenv("HOME", "", 1);
+
+ str = expand_filename("~");
+ test_streq("/", str);
+ tor_free(str);
+
+ str = expand_filename("~/");
+ test_streq("/", str);
+ tor_free(str);
+
+ str = expand_filename("~/foobar");
+ test_streq("/foobar", str);
+ tor_free(str);
+
+ /* Try with $HOME unset */
+
+ unsetenv("HOME");
+
+ str = expand_filename("~");
+ test_streq("/", str);
+ tor_free(str);
+
+ str = expand_filename("~/");
+ test_streq("/", str);
+ tor_free(str);
+
+ str = expand_filename("~/foobar");
+ test_streq("/foobar", str);
+ tor_free(str);
+
+ done:
+ tor_free(str);
+}
+#endif
+
/** Test basic string functionality. */
static void
test_util_strmisc(void)
@@ -2740,6 +2823,7 @@ struct testcase_t util_tests[] = {
UTIL_LEGACY(config_line_quotes),
UTIL_LEGACY(config_line_comment_character),
UTIL_LEGACY(config_line_escaped_content),
+ UTIL_LEGACY(expand_filename),
UTIL_LEGACY(strmisc),
UTIL_LEGACY(pow2),
UTIL_LEGACY(gzip),