test_1.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. *
  18. * Author: Adrian Hunter
  19. */
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include "tests.h"
  28. static void test_1(void)
  29. {
  30. int fd;
  31. pid_t pid;
  32. uint64_t i;
  33. uint64_t block;
  34. uint64_t actual_size;
  35. char name[256];
  36. char old[16];
  37. char buf[16];
  38. off_t old_len;
  39. char dir_name[256];
  40. /* Create a directory to test in */
  41. pid = getpid();
  42. tests_cat_pid(dir_name, "test_1_test_dir_", pid);
  43. if (chdir(dir_name) == -1)
  44. CHECK(mkdir(dir_name, 0777) != -1);
  45. CHECK(chdir(dir_name) != -1);
  46. /* Create a file that fills half the free space on the file system */
  47. tests_create_file("big_file", tests_get_big_file_size(1,2));
  48. CHECK(tests_count_files_in_dir(".") == 1);
  49. fd = open("big_file", O_RDWR | tests_maybe_sync_flag());
  50. CHECK(fd != -1);
  51. CHECK(read(fd, old, 5) == 5);
  52. CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
  53. CHECK(write(fd,"start", 5) == 5);
  54. CHECK(lseek(fd,0,SEEK_END) != (off_t) -1);
  55. CHECK(write(fd, "end", 3) == 3);
  56. tests_maybe_sync(fd);
  57. /* Delete the file while it is still open */
  58. tests_delete_file("big_file");
  59. CHECK(tests_count_files_in_dir(".") == 0);
  60. /* Create files to file up the file system */
  61. for (block = 1000000, i = 1; ; block /= 10) {
  62. while (i != 0) {
  63. sprintf(name, "fill_up_%llu", (unsigned long long)i);
  64. actual_size = tests_create_file(name, block);
  65. if (actual_size != 0)
  66. ++i;
  67. if (actual_size != block)
  68. break;
  69. }
  70. if (block == 1)
  71. break;
  72. }
  73. /* Check the big file */
  74. CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
  75. CHECK(read(fd, buf, 5) == 5);
  76. CHECK(strncmp(buf, "start", 5) == 0);
  77. CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1);
  78. CHECK(read(fd, buf, 3) == 3);
  79. CHECK(strncmp(buf, "end", 3) == 0);
  80. /* Check the other files and delete them */
  81. i -= 1;
  82. CHECK(tests_count_files_in_dir(".") == i);
  83. for (; i > 0; --i) {
  84. sprintf(name, "fill_up_%llu", (unsigned long long)i);
  85. tests_check_filled_file(name);
  86. tests_delete_file(name);
  87. }
  88. CHECK(tests_count_files_in_dir(".") == 0);
  89. /* Check the big file again */
  90. CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
  91. CHECK(read(fd, buf, 5) == 5);
  92. CHECK(strncmp(buf, "start", 5) == 0);
  93. CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1);
  94. CHECK(read(fd, buf, 3) == 3);
  95. CHECK(strncmp(buf, "end", 3) == 0);
  96. CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1);
  97. CHECK(write(fd,old, 5) == 5);
  98. old_len = lseek(fd, -3, SEEK_END);
  99. CHECK(old_len != (off_t) -1);
  100. CHECK(ftruncate(fd,old_len) != -1);
  101. tests_check_filled_file_fd(fd);
  102. /* Close the big file*/
  103. CHECK(close(fd) != -1);
  104. CHECK(tests_count_files_in_dir(".") == 0);
  105. CHECK(chdir("..") != -1);
  106. CHECK(rmdir(dir_name) != -1);
  107. }
  108. /* Title of this test */
  109. static const char *test_1_get_title(void)
  110. {
  111. return "Fill file system while holding deleted big file descriptor";
  112. }
  113. /* Description of this test */
  114. static const char *test_1_get_description(void)
  115. {
  116. return
  117. "Create a directory named test_1_test_dir_pid, where " \
  118. "pid is the process id. Within that directory, " \
  119. "create a big file (approx. half the file system in size), " \
  120. "open it, and unlink it. " \
  121. "Create many smaller files until the file system is full. " \
  122. "Check the big file is ok. " \
  123. "Delete all the smaller files. " \
  124. "Check the big file again. " \
  125. "Finally delete the big file and directory.";
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129. int run_test;
  130. /* Handle common arguments */
  131. run_test = tests_get_args(argc, argv, test_1_get_title(),
  132. test_1_get_description(), "s");
  133. if (!run_test)
  134. return 1;
  135. /* Change directory to the file system and check it is ok for testing */
  136. tests_check_test_file_system();
  137. /* Do the actual test */
  138. test_1();
  139. return 0;
  140. }