testfileinfo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "fspr_file_io.h"
  17. #include "fspr_file_info.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_general.h"
  21. #include "fspr_poll.h"
  22. #include "fspr_lib.h"
  23. #include "testutil.h"
  24. #define FILENAME "data/file_datafile.txt"
  25. #define NEWFILENAME "data/new_datafile.txt"
  26. #define NEWFILEDATA "This is new text in a new file."
  27. static const struct view_fileinfo
  28. {
  29. fspr_int32_t bits;
  30. char *description;
  31. } vfi[] = {
  32. {APR_FINFO_MTIME, "MTIME"},
  33. {APR_FINFO_CTIME, "CTIME"},
  34. {APR_FINFO_ATIME, "ATIME"},
  35. {APR_FINFO_SIZE, "SIZE"},
  36. {APR_FINFO_DEV, "DEV"},
  37. {APR_FINFO_INODE, "INODE"},
  38. {APR_FINFO_NLINK, "NLINK"},
  39. {APR_FINFO_TYPE, "TYPE"},
  40. {APR_FINFO_USER, "USER"},
  41. {APR_FINFO_GROUP, "GROUP"},
  42. {APR_FINFO_UPROT, "UPROT"},
  43. {APR_FINFO_GPROT, "GPROT"},
  44. {APR_FINFO_WPROT, "WPROT"},
  45. {0, NULL}
  46. };
  47. static void finfo_equal(abts_case *tc, fspr_finfo_t *f1, fspr_finfo_t *f2)
  48. {
  49. /* Minimum supported flags across all platforms (APR_FINFO_MIN) */
  50. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo must return APR_FINFO_TYPE",
  51. (f1->valid & f2->valid & APR_FINFO_TYPE));
  52. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in filetype",
  53. f1->filetype == f2->filetype);
  54. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo must return APR_FINFO_SIZE",
  55. (f1->valid & f2->valid & APR_FINFO_SIZE));
  56. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in size",
  57. f1->size == f2->size);
  58. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo must return APR_FINFO_ATIME",
  59. (f1->valid & f2->valid & APR_FINFO_ATIME));
  60. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in atime",
  61. f1->atime == f2->atime);
  62. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo must return APR_FINFO_MTIME",
  63. (f1->valid & f2->valid & APR_FINFO_MTIME));
  64. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in mtime",
  65. f1->mtime == f2->mtime);
  66. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo must return APR_FINFO_CTIME",
  67. (f1->valid & f2->valid & APR_FINFO_CTIME));
  68. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in ctime",
  69. f1->ctime == f2->ctime);
  70. if (f1->valid & f2->valid & APR_FINFO_NAME)
  71. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in name",
  72. !strcmp(f1->name, f2->name));
  73. if (f1->fname && f2->fname)
  74. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in fname",
  75. !strcmp(f1->fname, f2->fname));
  76. /* Additional supported flags not supported on all platforms */
  77. if (f1->valid & f2->valid & APR_FINFO_USER)
  78. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in user",
  79. !fspr_uid_compare(f1->user, f2->user));
  80. if (f1->valid & f2->valid & APR_FINFO_GROUP)
  81. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in group",
  82. !fspr_gid_compare(f1->group, f2->group));
  83. if (f1->valid & f2->valid & APR_FINFO_INODE)
  84. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in inode",
  85. f1->inode == f2->inode);
  86. if (f1->valid & f2->valid & APR_FINFO_DEV)
  87. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in device",
  88. f1->device == f2->device);
  89. if (f1->valid & f2->valid & APR_FINFO_NLINK)
  90. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in nlink",
  91. f1->nlink == f2->nlink);
  92. if (f1->valid & f2->valid & APR_FINFO_CSIZE)
  93. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in csize",
  94. f1->csize == f2->csize);
  95. if (f1->valid & f2->valid & APR_FINFO_PROT)
  96. ABTS_ASSERT(tc, "fspr_stat and fspr_getfileinfo differ in protection",
  97. f1->protection == f2->protection);
  98. }
  99. static void test_info_get(abts_case *tc, void *data)
  100. {
  101. fspr_file_t *thefile;
  102. fspr_finfo_t finfo;
  103. fspr_status_t rv;
  104. rv = fspr_file_open(&thefile, FILENAME, APR_READ, APR_OS_DEFAULT, p);
  105. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  106. rv = fspr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
  107. if (rv == APR_INCOMPLETE) {
  108. char *str;
  109. int i;
  110. str = fspr_pstrdup(p, "APR_INCOMPLETE: Missing ");
  111. for (i = 0; vfi[i].bits; ++i) {
  112. if (vfi[i].bits & ~finfo.valid) {
  113. str = fspr_pstrcat(p, str, vfi[i].description, " ", NULL);
  114. }
  115. }
  116. ABTS_FAIL(tc, str);
  117. }
  118. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  119. fspr_file_close(thefile);
  120. }
  121. static void test_stat(abts_case *tc, void *data)
  122. {
  123. fspr_finfo_t finfo;
  124. fspr_status_t rv;
  125. rv = fspr_stat(&finfo, FILENAME, APR_FINFO_NORM, p);
  126. if (rv == APR_INCOMPLETE) {
  127. char *str;
  128. int i;
  129. str = fspr_pstrdup(p, "APR_INCOMPLETE: Missing ");
  130. for (i = 0; vfi[i].bits; ++i) {
  131. if (vfi[i].bits & ~finfo.valid) {
  132. str = fspr_pstrcat(p, str, vfi[i].description, " ", NULL);
  133. }
  134. }
  135. ABTS_FAIL(tc, str);
  136. }
  137. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  138. }
  139. static void test_stat_eq_finfo(abts_case *tc, void *data)
  140. {
  141. fspr_file_t *thefile;
  142. fspr_finfo_t finfo;
  143. fspr_finfo_t stat_finfo;
  144. fspr_status_t rv;
  145. rv = fspr_file_open(&thefile, FILENAME, APR_READ, APR_OS_DEFAULT, p);
  146. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  147. rv = fspr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
  148. /* Opening the file may have toggled the atime member (time last
  149. * accessed), so fetch our fspr_stat() after getting the fileinfo
  150. * of the open file...
  151. */
  152. rv = fspr_stat(&stat_finfo, FILENAME, APR_FINFO_NORM, p);
  153. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  154. fspr_file_close(thefile);
  155. finfo_equal(tc, &stat_finfo, &finfo);
  156. }
  157. static void test_buffered_write_size(abts_case *tc, void *data)
  158. {
  159. const fspr_size_t data_len = strlen(NEWFILEDATA);
  160. fspr_file_t *thefile;
  161. fspr_finfo_t finfo;
  162. fspr_status_t rv;
  163. fspr_size_t bytes;
  164. rv = fspr_file_open(&thefile, NEWFILENAME,
  165. APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE
  166. | APR_BUFFERED | APR_DELONCLOSE,
  167. APR_OS_DEFAULT, p);
  168. APR_ASSERT_SUCCESS(tc, "open file", rv);
  169. /* A funny thing happened to me the other day: I wrote something
  170. * into a buffered file, then asked for its size using
  171. * fspr_file_info_get; and guess what? The size was 0! That's not a
  172. * nice way to behave.
  173. */
  174. bytes = data_len;
  175. rv = fspr_file_write(thefile, NEWFILEDATA, &bytes);
  176. APR_ASSERT_SUCCESS(tc, "write file contents", rv);
  177. ABTS_TRUE(tc, data_len == bytes);
  178. rv = fspr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
  179. APR_ASSERT_SUCCESS(tc, "get file size", rv);
  180. ABTS_TRUE(tc, bytes == (fspr_size_t) finfo.size);
  181. fspr_file_close(thefile);
  182. }
  183. static void test_mtime_set(abts_case *tc, void *data)
  184. {
  185. fspr_file_t *thefile;
  186. fspr_finfo_t finfo;
  187. fspr_time_t epoch = 0;
  188. fspr_status_t rv;
  189. /* This test sort of depends on the system clock being at least
  190. * marginally ccorrect; We'll be setting the modification time to
  191. * the epoch.
  192. */
  193. rv = fspr_file_open(&thefile, NEWFILENAME,
  194. APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE
  195. | APR_BUFFERED | APR_DELONCLOSE,
  196. APR_OS_DEFAULT, p);
  197. APR_ASSERT_SUCCESS(tc, "open file", rv);
  198. /* Check that the current mtime is not the epoch */
  199. rv = fspr_stat(&finfo, NEWFILENAME, APR_FINFO_MTIME, p);
  200. if (rv == APR_INCOMPLETE) {
  201. char *str;
  202. int i;
  203. str = fspr_pstrdup(p, "APR_INCOMPLETE: Missing ");
  204. for (i = 0; vfi[i].bits; ++i) {
  205. if (vfi[i].bits & ~finfo.valid) {
  206. str = fspr_pstrcat(p, str, vfi[i].description, " ", NULL);
  207. }
  208. }
  209. ABTS_FAIL(tc, str);
  210. }
  211. APR_ASSERT_SUCCESS(tc, "get initial mtime", rv);
  212. ABTS_TRUE(tc, finfo.mtime != epoch);
  213. /* Reset the mtime to the epoch and verify the result.
  214. * Note: we blindly assume that if the first fspr_stat succeeded,
  215. * the second one will, too.
  216. */
  217. rv = fspr_file_mtime_set(NEWFILENAME, epoch, p);
  218. APR_ASSERT_SUCCESS(tc, "set mtime", rv);
  219. rv = fspr_stat(&finfo, NEWFILENAME, APR_FINFO_MTIME, p);
  220. APR_ASSERT_SUCCESS(tc, "get modified mtime", rv);
  221. ABTS_TRUE(tc, finfo.mtime == epoch);
  222. fspr_file_close(thefile);
  223. }
  224. abts_suite *testfileinfo(abts_suite *suite)
  225. {
  226. suite = ADD_SUITE(suite)
  227. abts_run_test(suite, test_info_get, NULL);
  228. abts_run_test(suite, test_stat, NULL);
  229. abts_run_test(suite, test_stat_eq_finfo, NULL);
  230. abts_run_test(suite, test_buffered_write_size, NULL);
  231. abts_run_test(suite, test_mtime_set, NULL);
  232. return suite;
  233. }