summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2009-12-17 18:29:37 -0500
committerNick Mathewson <nickm@torproject.org>2009-12-17 18:29:37 -0500
commit235f1e1a967cb070c7246617461f58f0413394b3 (patch)
tree63c260d4d5c4a6f54fa620836ed291a94f6d332e /src/test
parent498c293afe3570210e964456e6cf5a8f315dbfc6 (diff)
downloadtor-235f1e1a967cb070c7246617461f58f0413394b3.tar.gz
tor-235f1e1a967cb070c7246617461f58f0413394b3.zip
Refactor out the 'find string at start of any line' logic.
We do this in too many places throughout the code; it's time to start clamping down. Also, refactor Karsten's patch to use strchr-then-strndup, rather than malloc-then-strlcpy-then-strchr-then-clear.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test_util.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 51b788e725..3ed6a7aab8 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1023,9 +1023,39 @@ test_util_strtok(void)
;
}
+static void
+test_util_find_str_at_start_of_line(void *ptr)
+{
+ const char *long_string =
+ "hello world. hello world. hello hello. howdy.\n"
+ "hello hello world\n";
+
+ (void)ptr;
+
+ /* not-found case. */
+ tt_assert(! find_str_at_start_of_line(long_string, "fred"));
+
+ /* not-found case where haystack doesn't end with \n */
+ tt_assert(! find_str_at_start_of_line("foobar\nbaz", "fred"));
+
+ /* start-of-string case */
+ tt_assert(long_string ==
+ find_str_at_start_of_line(long_string, "hello world."));
+
+ /* start-of-line case */
+ tt_assert(strchr(long_string,'\n')+1 ==
+ find_str_at_start_of_line(long_string, "hello hello"));
+ done:
+ ;
+}
+
+
#define UTIL_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
+#define UTIL_TEST(name, flags) \
+ { #name, test_util_ ## name, flags, NULL, NULL }
+
struct testcase_t util_tests[] = {
UTIL_LEGACY(time),
UTIL_LEGACY(config_line),
@@ -1040,6 +1070,7 @@ struct testcase_t util_tests[] = {
UTIL_LEGACY(threads),
UTIL_LEGACY(sscanf),
UTIL_LEGACY(strtok),
+ UTIL_TEST(find_str_at_start_of_line, 0),
END_OF_TESTCASES
};