summaryrefslogtreecommitdiff
path: root/test/testfile.c
diff options
context:
space:
mode:
authorivan <ivan@13f79535-47bb-0310-9956-ffa450edef68>2017-03-27 13:33:54 +0000
committerivan <ivan@13f79535-47bb-0310-9956-ffa450edef68>2017-03-27 13:33:54 +0000
commit356e5599bbc356238aa2b9cecbf9addb079ed54c (patch)
tree741743640d377749686d7a6d3a4c66f3b0e89ef8 /test/testfile.c
parente078efa9f43a8bda0b5c470120e4ea93fa3f53c6 (diff)
downloadlibapr-356e5599bbc356238aa2b9cecbf9addb079ed54c.tar.gz
Fix two issues with apr_file_trunc() for buffered files:
- The Win32 implementation incorrectly flushes the buffered writes _after_ truncating a file. Such files will have unexpected data written after the position at which they should've been truncated. PR 51017. (Under Unix, this issue has been fixed in r1044440) - Both Win32 and Unix implementations incorrectly keep the data read into a buffer after the file is truncated. Thus, reading from a file after apr_file_trunc() can return invalid data from the previous file offset. * file_io/win32/seek.c (apr_file_trunc): Flush the write buffer or discard the read buffer before truncating. Propely update the internal file offset (filePtr) and the eof_hit marker. * file_io/unix/seek.c (apr_file_trunc): Discard the read buffer before truncating. * test/testfile.c (test_file_trunc): Extend the checks. Factor out part of this test... (test_file_trunc_buffered_write): ...into this new test. (test_file_trunc_buffered_write2, test_file_trunc_buffered_read): New tests. (testfile): Run the new tests. Patch by: Evgeny Kotkov <evgeny.kotkov {at} visualsvn.com> git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@1788929 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/testfile.c')
-rw-r--r--test/testfile.c146
1 files changed, 142 insertions, 4 deletions
diff --git a/test/testfile.c b/test/testfile.c
index f4a742643..477f5791f 100644
--- a/test/testfile.c
+++ b/test/testfile.c
@@ -1034,10 +1034,10 @@ static void test_file_trunc(abts_case *tc, void *data)
const char *s;
apr_size_t nbytes;
apr_finfo_t finfo;
+ char c;
apr_file_remove(fname, p);
- /* Test unbuffered */
rv = apr_file_open(&f, fname,
APR_FOPEN_CREATE | APR_FOPEN_READ |
APR_FOPEN_WRITE,
@@ -1051,37 +1051,172 @@ static void test_file_trunc(abts_case *tc, void *data)
ABTS_SIZE_EQUAL(tc, strlen(s), nbytes);
rv = apr_file_trunc(f, 4);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ /* Test apr_file_info_get(). */
+ rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ ABTS_INT_EQUAL(tc, 4, (int)finfo.size);
+ /* EOF is not reported until the next read. */
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ rv = apr_file_getc(&c, f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+
rv = apr_file_close(f);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_stat(&finfo, fname, APR_FINFO_SIZE, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
- ABTS_ASSERT(tc, "File size mismatch, expected 4", finfo.size == 4);
+ ABTS_INT_EQUAL(tc, 4, (int)finfo.size);
rv = apr_file_remove(fname, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
- /* Test buffered */
+}
+
+static void test_file_trunc_buffered_write(abts_case *tc, void *data)
+{
+ apr_status_t rv;
+ apr_file_t *f;
+ const char *fname = "data/testtruncate_buffered_write.dat";
+ const char *s;
+ apr_size_t nbytes;
+ apr_finfo_t finfo;
+ char c;
+
+ apr_file_remove(fname, p);
+
rv = apr_file_open(&f, fname,
APR_FOPEN_CREATE | APR_FOPEN_READ |
APR_FOPEN_WRITE | APR_FOPEN_BUFFERED,
APR_FPROT_UREAD | APR_FPROT_UWRITE, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ s = "some data";
nbytes = strlen(s);
rv = apr_file_write(f, s, &nbytes);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
ABTS_SIZE_EQUAL(tc, strlen(s), nbytes);
rv = apr_file_trunc(f, 4);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ /* Test apr_file_info_get(). */
+ rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ ABTS_INT_EQUAL(tc, 4, (int)finfo.size);
+ /* EOF is not reported until the next read. */
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ rv = apr_file_getc(&c, f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+
rv = apr_file_close(f);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_stat(&finfo, fname, APR_FINFO_SIZE, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
- ABTS_ASSERT(tc, "File size mismatch, expected 4", finfo.size == 4);
+ ABTS_INT_EQUAL(tc, 4, (int)finfo.size);
rv = apr_file_remove(fname, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
}
+static void test_file_trunc_buffered_write2(abts_case *tc, void *data)
+{
+ apr_status_t rv;
+ apr_file_t *f;
+ const char *fname = "data/testtruncate_buffered_write2.dat";
+ apr_finfo_t finfo;
+ char c;
+
+ apr_file_remove(fname, p);
+
+ rv = apr_file_open(&f, fname,
+ APR_FOPEN_CREATE | APR_FOPEN_READ |
+ APR_FOPEN_WRITE | APR_FOPEN_BUFFERED,
+ APR_FPROT_OS_DEFAULT, p);
+ APR_ASSERT_SUCCESS(tc, "open test file", rv);
+
+ rv = apr_file_puts("abc", f);
+ APR_ASSERT_SUCCESS(tc, "write first string", rv);
+ rv = apr_file_flush(f);
+ APR_ASSERT_SUCCESS(tc, "flush", rv);
+ rv = apr_file_puts("def", f);
+ APR_ASSERT_SUCCESS(tc, "write second string", rv);
+ /* Truncate behind the write buffer. */
+ rv = apr_file_trunc(f, 2);
+ APR_ASSERT_SUCCESS(tc, "truncate the file", rv);
+ /* Test apr_file_info_get(). */
+ rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, f);
+ APR_ASSERT_SUCCESS(tc, "get file info", rv);
+ ABTS_INT_EQUAL(tc, 2, (int)finfo.size);
+ /* EOF is not reported until the next read. */
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ rv = apr_file_getc(&c, f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+
+ apr_file_close(f);
+
+ rv = apr_stat(&finfo, fname, APR_FINFO_SIZE, p);
+ APR_ASSERT_SUCCESS(tc, "stat file", rv);
+ ABTS_INT_EQUAL(tc, 2, (int)finfo.size);
+
+ apr_file_remove(fname, p);
+}
+
+static void test_file_trunc_buffered_read(abts_case *tc, void *data)
+{
+ apr_status_t rv;
+ apr_file_t *f;
+ const char *fname = "data/testtruncate_buffered_read.dat";
+ apr_finfo_t finfo;
+ char c;
+
+ apr_file_remove(fname, p);
+
+ rv = apr_file_open(&f, fname,
+ APR_FOPEN_CREATE | APR_FOPEN_READ |
+ APR_FOPEN_WRITE, APR_FPROT_OS_DEFAULT, p);
+ APR_ASSERT_SUCCESS(tc, "open test file", rv);
+
+ rv = apr_file_puts("abc", f);
+ APR_ASSERT_SUCCESS(tc, "write test data", rv);
+ apr_file_close(f);
+
+ rv = apr_file_open(&f, fname,
+ APR_FOPEN_READ | APR_FOPEN_WRITE |
+ APR_FOPEN_BUFFERED, APR_FPROT_OS_DEFAULT, p);
+ APR_ASSERT_SUCCESS(tc, "re-open test file", rv);
+
+ /* Read to fill in the buffer. */
+ rv = apr_file_getc(&c, f);
+ APR_ASSERT_SUCCESS(tc, "read char", rv);
+ /* Truncate the file. */
+ rv = apr_file_trunc(f, 1);
+ APR_ASSERT_SUCCESS(tc, "truncate the file", rv);
+ /* Test apr_file_info_get(). */
+ rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, f);
+ APR_ASSERT_SUCCESS(tc, "get file info", rv);
+ ABTS_INT_EQUAL(tc, 1, (int)finfo.size);
+ /* EOF is not reported until the next read. */
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
+ rv = apr_file_getc(&c, f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+ rv = apr_file_eof(f);
+ ABTS_INT_EQUAL(tc, APR_EOF, rv);
+
+ apr_file_close(f);
+
+ rv = apr_stat(&finfo, fname, APR_FINFO_SIZE, p);
+ APR_ASSERT_SUCCESS(tc, "stat file", rv);
+ ABTS_INT_EQUAL(tc, 1, (int)finfo.size);
+
+ apr_file_remove(fname, p);
+}
+
static void test_bigfprintf(abts_case *tc, void *data)
{
apr_file_t *f;
@@ -1351,6 +1486,9 @@ abts_suite *testfile(abts_suite *suite)
abts_run_test(suite, test_mod_neg, NULL);
abts_run_test(suite, test_truncate, NULL);
abts_run_test(suite, test_file_trunc, NULL);
+ abts_run_test(suite, test_file_trunc_buffered_write, NULL);
+ abts_run_test(suite, test_file_trunc_buffered_write2, NULL);
+ abts_run_test(suite, test_file_trunc_buffered_read, NULL);
abts_run_test(suite, test_bigfprintf, NULL);
abts_run_test(suite, test_fail_write_flush, NULL);
abts_run_test(suite, test_fail_read_flush, NULL);