diff options
author | Steven Murdoch <Steven.Murdoch@cl.cam.ac.uk> | 2011-08-30 14:55:51 +0100 |
---|---|---|
committer | Steven Murdoch <Steven.Murdoch@cl.cam.ac.uk> | 2011-08-30 14:55:51 +0100 |
commit | da34360952c0fbbd8effc2789ed72b86c8045531 (patch) | |
tree | 171f3091ee2cb897386f114d5fbecc363b7a9297 /src/test | |
parent | bc97f410802d5b9c66bfba6aebeae1ecd70f8857 (diff) | |
download | tor-da34360952c0fbbd8effc2789ed72b86c8045531.tar.gz tor-da34360952c0fbbd8effc2789ed72b86c8045531.zip |
Factor out and re-write code for splitting lines from a handle
Now handles non-printable characters and will not output a spurious
new-line if given a partial line.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test_util.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c index 9df7bc675d..a3bc003b2c 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -1554,6 +1554,9 @@ test_util_spawn_background_partial_read(void *ptr) ; } +/** + * Test that we can properly format q Windows command line + */ static void test_util_join_cmdline(void *ptr) { @@ -1602,6 +1605,67 @@ test_util_join_cmdline(void *ptr) ; } +#define MAX_SPLIT_LINE_COUNT 3 +struct split_lines_test_t { + const char *orig_line; // Line to be split (may contain \0's) + int orig_length; // Length of orig_line + const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines +}; + +/** + * Test that we properly split a buffer into lines + */ +static void +test_util_split_lines(void *ptr) +{ + /* Test cases. orig_line of last test case must be NULL. + * The last element of split_line[i] must be NULL. */ + struct split_lines_test_t tests[] = { + {"", 0, {NULL}}, + {"foo", 3, {"foo", NULL}}, + {"\n\rfoo\n\rbar\r\n", 12, {"foo", "bar", NULL}}, + {"fo o\r\nb\tar", 10, {"fo o", "b.ar", NULL}}, + {"\x0f""f\0o\0\n\x01""b\0r\0\r", 12, {".f.o.", ".b.r.", NULL}}, + {NULL, 0, {}} + }; + + int i, j; + char *orig_line; + smartlist_t *sl; + + (void)ptr; + + for (i=0; tests[i].orig_line; i++) { + sl = smartlist_create(); + orig_line = tor_malloc(tests[i].orig_length); + memcpy(orig_line, tests[i].orig_line, tests[i].orig_length + 1); + tor_split_lines(sl, orig_line, tests[i].orig_length); + + j = 0; + log_info(LD_GENERAL, "Splitting test %d of length %d", + i, tests[i].orig_length); + SMARTLIST_FOREACH(sl, const char *, line, + { + /* Check we have not got too many lines */ + tt_int_op(j, <, MAX_SPLIT_LINE_COUNT); + /* Check that there actually should be a line here */ + tt_assert(tests[i].split_line[j] != NULL); + log_info(LD_GENERAL, "Line %d of test %d, should be <%s>", + j, i, tests[i].split_line[j]); + /* Check that the line is as expected */ + tt_str_op(tests[i].split_line[j], ==, line); + j++; + }); + /* Check that we didn't miss some lines */ + tt_assert(tests[i].split_line[j] == NULL); + tor_free(orig_line); + smartlist_free(sl); + } + + done: + ; +} + static void test_util_di_ops(void) { @@ -1691,6 +1755,7 @@ struct testcase_t util_tests[] = { UTIL_TEST(spawn_background_fail, 0), UTIL_TEST(spawn_background_partial_read, 0), UTIL_TEST(join_cmdline, 0), + UTIL_TEST(split_lines, 0), END_OF_TESTCASES }; |