[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add some tests for expand_filename



commit 043e154cddea9247c5e8432a7445a57ea5ab08c8
Author: Esteban Manchado Velázquez <emanchado@xxxxxxxxxxxx>
Date:   Mon Feb 20 17:42:48 2012 +0100

    Add some tests for expand_filename
---
 src/test/test_util.c |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/src/test/test_util.c b/src/test/test_util.c
index 6c14718..ecf4db2 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),



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits