From a57d8491c02a821dc2054e59ccfc092fac8b7a9d Mon Sep 17 00:00:00 2001 From: Koichiro IWAO Date: Tue, 7 Sep 2021 14:17:15 +0900 Subject: [PATCH] tests: add g_file_get_size() tests for files larger than 2GiB --- tests/common/test_os_calls.c | 64 ++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/tests/common/test_os_calls.c b/tests/common/test_os_calls.c index 5de96fd0..3f1112ea 100644 --- a/tests/common/test_os_calls.c +++ b/tests/common/test_os_calls.c @@ -44,7 +44,7 @@ START_TEST(test_g_file_get_size__1GiB) "dd if=/dev/zero of=%s bs=%u seek=%u count=1", file, block_size, - block_count -1); + block_count - 1); system_rv = system(cmd); size = g_file_get_size(file); @@ -73,7 +73,65 @@ START_TEST(test_g_file_get_size__just_less_than_2GiB) "dd if=/dev/zero of=%s bs=%u seek=%u count=1", file, block_size, - block_count -1); + block_count - 1); + + system_rv = system(cmd); + size = g_file_get_size(file); + g_file_delete(file); + ck_assert_int_eq(system_rv, 0); + ck_assert_int_eq(size, expected_size); +} +END_TEST + +START_TEST(test_g_file_get_size__2GiB) +{ + const char *file = "./file_2GiB.dat"; + unsigned int block_size = 4096; + unsigned int block_count = 524288; + /* C90 5.2.4.2.1 guarantees long long is at least 64 bits */ + const long long expected_size = + (long long)block_size * block_count; + long long size; + int system_rv; + + char cmd[256]; + + /* Create a sparse file of the expected size with one block of data + * at the end */ + g_snprintf(cmd, sizeof(cmd), + "dd if=/dev/zero of=%s bs=%u seek=%u count=1", + file, + block_size, + block_count - 1); + + system_rv = system(cmd); + size = g_file_get_size(file); + g_file_delete(file); + ck_assert_int_eq(system_rv, 0); + ck_assert_int_eq(size, expected_size); +} +END_TEST + +START_TEST(test_g_file_get_size__5GiB) +{ + const char *file = "./file_5GiB.dat"; + unsigned int block_size = 4096; + unsigned int block_count = 1310720; + /* C90 5.2.4.2.1 guarantees long long is at least 64 bits */ + const long long expected_size = + (long long)block_size * block_count; + long long size; + int system_rv; + + char cmd[256]; + + /* Create a sparse file of the expected size with one block of data + * at the end */ + g_snprintf(cmd, sizeof(cmd), + "dd if=/dev/zero of=%s bs=%u seek=%u count=1", + file, + block_size, + block_count - 1); system_rv = system(cmd); size = g_file_get_size(file); @@ -97,6 +155,8 @@ make_suite_test_os_calls(void) tcase_add_test(tc_os_calls, test_g_file_get_size__returns_file_size); tcase_add_test(tc_os_calls, test_g_file_get_size__1GiB); tcase_add_test(tc_os_calls, test_g_file_get_size__just_less_than_2GiB); + tcase_add_test(tc_os_calls, test_g_file_get_size__2GiB); + tcase_add_test(tc_os_calls, test_g_file_get_size__5GiB); return s; }