diff options
author | David Goulet <dgoulet@torproject.org> | 2021-01-28 12:46:24 -0500 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2021-01-28 12:46:24 -0500 |
commit | 9556276f07e62e8b2e6a496e9b2a918293367839 (patch) | |
tree | 7975fcdb2fa954722dc1628224f957bfed092a1c /src/test | |
parent | 290007e3c48624060620b0340c1b4874487aab09 (diff) | |
parent | 623af0155e0664f46adb1fcc218bc193a9d15916 (diff) | |
download | tor-9556276f07e62e8b2e6a496e9b2a918293367839.tar.gz tor-9556276f07e62e8b2e6a496e9b2a918293367839.zip |
Merge branch 'tor-gitlab/mr/50' into maint-0.3.5
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test_util.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c index 3a0eb15157..06ea8191a2 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -173,6 +173,54 @@ test_util_read_file_eof_zero_bytes(void *arg) test_util_read_until_eof_impl("tor_test_fifo_empty", 0, 10000); } +static void +test_util_read_file_endlines(void *arg) +{ + (void)arg; + + char *fname = NULL; + char *read_content = NULL; + int r = -1; + + /* Write a file that contains both \n and \r\n as line ending. */ + const char *file_content = "foo bar\n" + "foo bar baz\r\n" + "foo bar\r\n"; + + const char *expected_file_content = "foo bar\n" + "foo bar baz\n" + "foo bar\n"; + + fname = tor_strdup(get_fname("file_with_crlf_ending")); + + r = write_bytes_to_file(fname, file_content, strlen(file_content), 1); + tt_int_op(r, OP_EQ, 0); + + /* Read the file in text mode: we strip \r's from the files on both Windows + * and UNIX. */ + read_content = read_file_to_str(fname, 0, NULL); + + tt_ptr_op(read_content, OP_NE, NULL); + tt_int_op(strlen(read_content), OP_EQ, strlen(expected_file_content)); + tt_str_op(read_content, OP_EQ, expected_file_content); + + tor_free(read_content); + + /* Read the file in binary mode: we should preserve the \r here. */ + read_content = read_file_to_str(fname, RFTS_BIN, NULL); + + tt_ptr_op(read_content, OP_NE, NULL); + tt_int_op(strlen(read_content), OP_EQ, strlen(file_content)); + tt_str_op(read_content, OP_EQ, file_content); + + tor_free(read_content); + + done: + unlink(fname); + tor_free(fname); + tor_free(read_content); +} + /* Test the basic expected behaviour for write_chunks_to_file. * NOTE: This will need to be updated if we ever change the tempfile location * or extension */ @@ -6508,6 +6556,7 @@ struct testcase_t util_tests[] = { UTIL_TEST(read_file_eof_two_loops, 0), UTIL_TEST(read_file_eof_two_loops_b, 0), UTIL_TEST(read_file_eof_zero_bytes, 0), + UTIL_TEST(read_file_endlines, 0), UTIL_TEST(write_chunks_to_file, 0), UTIL_TEST(mathlog, 0), UTIL_TEST(fraction, 0), |