testdir.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "fspr_file_io.h"
  20. #include "fspr_file_info.h"
  21. #include "fspr_errno.h"
  22. #include "fspr_general.h"
  23. #include "fspr_lib.h"
  24. #include "testutil.h"
  25. static void test_mkdir(abts_case *tc, void *data)
  26. {
  27. fspr_status_t rv;
  28. fspr_finfo_t finfo;
  29. rv = fspr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
  30. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  31. rv = fspr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
  32. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  33. ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
  34. }
  35. static void test_mkdir_recurs(abts_case *tc, void *data)
  36. {
  37. fspr_status_t rv;
  38. fspr_finfo_t finfo;
  39. rv = fspr_dir_make_recursive("data/one/two/three",
  40. APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
  41. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  42. rv = fspr_stat(&finfo, "data/one", APR_FINFO_TYPE, p);
  43. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  44. ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
  45. rv = fspr_stat(&finfo, "data/one/two", APR_FINFO_TYPE, p);
  46. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  47. ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
  48. rv = fspr_stat(&finfo, "data/one/two/three", APR_FINFO_TYPE, p);
  49. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  50. ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
  51. }
  52. static void test_remove(abts_case *tc, void *data)
  53. {
  54. fspr_status_t rv;
  55. fspr_finfo_t finfo;
  56. rv = fspr_dir_remove("data/testdir", p);
  57. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  58. rv = fspr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
  59. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  60. }
  61. static void test_removeall_fail(abts_case *tc, void *data)
  62. {
  63. fspr_status_t rv;
  64. rv = fspr_dir_remove("data/one", p);
  65. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
  66. }
  67. static void test_removeall(abts_case *tc, void *data)
  68. {
  69. fspr_status_t rv;
  70. rv = fspr_dir_remove("data/one/two/three", p);
  71. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  72. rv = fspr_dir_remove("data/one/two", p);
  73. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  74. rv = fspr_dir_remove("data/one", p);
  75. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  76. }
  77. static void test_remove_notthere(abts_case *tc, void *data)
  78. {
  79. fspr_status_t rv;
  80. rv = fspr_dir_remove("data/notthere", p);
  81. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  82. }
  83. static void test_mkdir_twice(abts_case *tc, void *data)
  84. {
  85. fspr_status_t rv;
  86. rv = fspr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
  87. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  88. rv = fspr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
  89. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EEXIST(rv));
  90. rv = fspr_dir_remove("data/testdir", p);
  91. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  92. }
  93. static void test_opendir(abts_case *tc, void *data)
  94. {
  95. fspr_status_t rv;
  96. fspr_dir_t *dir;
  97. rv = fspr_dir_open(&dir, "data", p);
  98. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  99. fspr_dir_close(dir);
  100. }
  101. static void test_opendir_notthere(abts_case *tc, void *data)
  102. {
  103. fspr_status_t rv;
  104. fspr_dir_t *dir;
  105. rv = fspr_dir_open(&dir, "notthere", p);
  106. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  107. }
  108. static void test_closedir(abts_case *tc, void *data)
  109. {
  110. fspr_status_t rv;
  111. fspr_dir_t *dir;
  112. rv = fspr_dir_open(&dir, "data", p);
  113. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  114. rv = fspr_dir_close(dir);
  115. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  116. }
  117. static void test_rewind(abts_case *tc, void *data)
  118. {
  119. fspr_dir_t *dir;
  120. fspr_finfo_t first, second;
  121. APR_ASSERT_SUCCESS(tc, "fspr_dir_open failed", fspr_dir_open(&dir, "data", p));
  122. APR_ASSERT_SUCCESS(tc, "fspr_dir_read failed",
  123. fspr_dir_read(&first, APR_FINFO_DIRENT, dir));
  124. APR_ASSERT_SUCCESS(tc, "fspr_dir_rewind failed", fspr_dir_rewind(dir));
  125. APR_ASSERT_SUCCESS(tc, "second fspr_dir_read failed",
  126. fspr_dir_read(&second, APR_FINFO_DIRENT, dir));
  127. APR_ASSERT_SUCCESS(tc, "fspr_dir_close failed", fspr_dir_close(dir));
  128. ABTS_STR_EQUAL(tc, first.name, second.name);
  129. }
  130. /* Test for a (fixed) bug in fspr_dir_read(). This bug only happened
  131. in threadless cases. */
  132. static void test_uncleared_errno(abts_case *tc, void *data)
  133. {
  134. fspr_file_t *thefile = NULL;
  135. fspr_finfo_t finfo;
  136. fspr_int32_t finfo_flags = APR_FINFO_TYPE | APR_FINFO_NAME;
  137. fspr_dir_t *this_dir;
  138. fspr_status_t rv;
  139. rv = fspr_dir_make("dir1", APR_OS_DEFAULT, p);
  140. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  141. rv = fspr_dir_make("dir2", APR_OS_DEFAULT, p);
  142. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  143. rv = fspr_file_open(&thefile, "dir1/file1",
  144. APR_READ | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, p);
  145. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  146. rv = fspr_file_close(thefile);
  147. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  148. /* Try to remove dir1. This should fail because it's not empty.
  149. However, on a platform with threads disabled (such as FreeBSD),
  150. `errno' will be set as a result. */
  151. rv = fspr_dir_remove("dir1", p);
  152. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
  153. /* Read `.' and `..' out of dir2. */
  154. rv = fspr_dir_open(&this_dir, "dir2", p);
  155. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  156. rv = fspr_dir_read(&finfo, finfo_flags, this_dir);
  157. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  158. rv = fspr_dir_read(&finfo, finfo_flags, this_dir);
  159. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  160. /* Now, when we attempt to do a third read of empty dir2, and the
  161. underlying system readdir() returns NULL, the old value of
  162. errno shouldn't cause a false alarm. We should get an ENOENT
  163. back from fspr_dir_read, and *not* the old errno. */
  164. rv = fspr_dir_read(&finfo, finfo_flags, this_dir);
  165. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  166. rv = fspr_dir_close(this_dir);
  167. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  168. /* Cleanup */
  169. rv = fspr_file_remove("dir1/file1", p);
  170. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  171. rv = fspr_dir_remove("dir1", p);
  172. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  173. rv = fspr_dir_remove("dir2", p);
  174. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  175. }
  176. static void test_rmkdir_nocwd(abts_case *tc, void *data)
  177. {
  178. char *cwd, *path;
  179. fspr_status_t rv;
  180. APR_ASSERT_SUCCESS(tc, "make temp dir",
  181. fspr_dir_make("dir3", APR_OS_DEFAULT, p));
  182. APR_ASSERT_SUCCESS(tc, "obtain cwd", fspr_filepath_get(&cwd, 0, p));
  183. APR_ASSERT_SUCCESS(tc, "determine path to temp dir",
  184. fspr_filepath_merge(&path, cwd, "dir3", 0, p));
  185. APR_ASSERT_SUCCESS(tc, "change to temp dir", fspr_filepath_set(path, p));
  186. rv = fspr_dir_remove(path, p);
  187. /* Some platforms cannot remove a directory which is in use. */
  188. if (rv == APR_SUCCESS) {
  189. ABTS_ASSERT(tc, "fail to create dir",
  190. fspr_dir_make_recursive("foobar", APR_OS_DEFAULT,
  191. p) != APR_SUCCESS);
  192. }
  193. APR_ASSERT_SUCCESS(tc, "restore cwd", fspr_filepath_set(cwd, p));
  194. if (rv) {
  195. fspr_dir_remove(path, p);
  196. ABTS_NOT_IMPL(tc, "cannot remove in-use directory");
  197. }
  198. }
  199. abts_suite *testdir(abts_suite *suite)
  200. {
  201. suite = ADD_SUITE(suite)
  202. abts_run_test(suite, test_mkdir, NULL);
  203. abts_run_test(suite, test_mkdir_recurs, NULL);
  204. abts_run_test(suite, test_remove, NULL);
  205. abts_run_test(suite, test_removeall_fail, NULL);
  206. abts_run_test(suite, test_removeall, NULL);
  207. abts_run_test(suite, test_remove_notthere, NULL);
  208. abts_run_test(suite, test_mkdir_twice, NULL);
  209. abts_run_test(suite, test_rmkdir_nocwd, NULL);
  210. abts_run_test(suite, test_rewind, NULL);
  211. abts_run_test(suite, test_opendir, NULL);
  212. abts_run_test(suite, test_opendir_notthere, NULL);
  213. abts_run_test(suite, test_closedir, NULL);
  214. abts_run_test(suite, test_uncleared_errno, NULL);
  215. return suite;
  216. }