|
@@ -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
|
|
|
};
|
|
|
|