testnames.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "testutil.h"
  17. #include "fspr_file_io.h"
  18. #include "fspr_file_info.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_general.h"
  21. #include "fspr_pools.h"
  22. #include "fspr_lib.h"
  23. #if WIN32
  24. #define ABS_ROOT "C:/"
  25. #elif defined(NETWARE)
  26. #define ABS_ROOT "SYS:/"
  27. #else
  28. #define ABS_ROOT "/"
  29. #endif
  30. static void merge_aboveroot(abts_case *tc, void *data)
  31. {
  32. fspr_status_t rv;
  33. char *dstpath = NULL;
  34. char errmsg[256];
  35. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"bar", APR_FILEPATH_NOTABOVEROOT,
  36. p);
  37. fspr_strerror(rv, errmsg, sizeof(errmsg));
  38. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EABOVEROOT(rv));
  39. ABTS_PTR_EQUAL(tc, NULL, dstpath);
  40. ABTS_STR_EQUAL(tc, "The given path was above the root path", errmsg);
  41. }
  42. static void merge_belowroot(abts_case *tc, void *data)
  43. {
  44. fspr_status_t rv;
  45. char *dstpath = NULL;
  46. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"foo/bar",
  47. APR_FILEPATH_NOTABOVEROOT, p);
  48. ABTS_PTR_NOTNULL(tc, dstpath);
  49. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  50. ABTS_STR_EQUAL(tc, ABS_ROOT"foo/bar", dstpath);
  51. }
  52. static void merge_noflag(abts_case *tc, void *data)
  53. {
  54. fspr_status_t rv;
  55. char *dstpath = NULL;
  56. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"foo/bar", 0, p);
  57. ABTS_PTR_NOTNULL(tc, dstpath);
  58. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  59. ABTS_STR_EQUAL(tc, ABS_ROOT"foo/bar", dstpath);
  60. }
  61. static void merge_dotdot(abts_case *tc, void *data)
  62. {
  63. fspr_status_t rv;
  64. char *dstpath = NULL;
  65. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz", 0, p);
  66. ABTS_PTR_NOTNULL(tc, dstpath);
  67. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  68. ABTS_STR_EQUAL(tc, ABS_ROOT"foo/baz", dstpath);
  69. rv = fspr_filepath_merge(&dstpath, "", "../test", 0, p);
  70. ABTS_INT_EQUAL(tc, 0, APR_SUCCESS);
  71. ABTS_STR_EQUAL(tc, "../test", dstpath);
  72. /* Very dangerous assumptions here about what the cwd is. However, let's assume
  73. * that the testall is invoked from within apr/test/ so the following test should
  74. * return ../test unless a previously fixed bug remains or the developer changes
  75. * the case of the test directory:
  76. */
  77. rv = fspr_filepath_merge(&dstpath, "", "../test", APR_FILEPATH_TRUENAME, p);
  78. ABTS_INT_EQUAL(tc, 0, APR_SUCCESS);
  79. ABTS_STR_EQUAL(tc, "../test", dstpath);
  80. }
  81. static void merge_dotdot_dotdot_dotdot(abts_case *tc, void *data)
  82. {
  83. fspr_status_t rv;
  84. char *dstpath = NULL;
  85. rv = fspr_filepath_merge(&dstpath, "",
  86. "../../..", APR_FILEPATH_TRUENAME, p);
  87. ABTS_PTR_NOTNULL(tc, dstpath);
  88. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  89. ABTS_STR_EQUAL(tc, "../../..", dstpath);
  90. rv = fspr_filepath_merge(&dstpath, "",
  91. "../../../", APR_FILEPATH_TRUENAME, p);
  92. ABTS_PTR_NOTNULL(tc, dstpath);
  93. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  94. ABTS_STR_EQUAL(tc, "../../../", dstpath);
  95. }
  96. static void merge_secure(abts_case *tc, void *data)
  97. {
  98. fspr_status_t rv;
  99. char *dstpath = NULL;
  100. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../bar/baz", 0, p);
  101. ABTS_PTR_NOTNULL(tc, dstpath);
  102. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  103. ABTS_STR_EQUAL(tc, ABS_ROOT"foo/bar/baz", dstpath);
  104. }
  105. static void merge_notrel(abts_case *tc, void *data)
  106. {
  107. fspr_status_t rv;
  108. char *dstpath = NULL;
  109. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz",
  110. APR_FILEPATH_NOTRELATIVE, p);
  111. ABTS_PTR_NOTNULL(tc, dstpath);
  112. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  113. ABTS_STR_EQUAL(tc, ABS_ROOT"foo/baz", dstpath);
  114. }
  115. static void merge_notrelfail(abts_case *tc, void *data)
  116. {
  117. fspr_status_t rv;
  118. char *dstpath = NULL;
  119. char errmsg[256];
  120. rv = fspr_filepath_merge(&dstpath, "foo/bar", "../baz",
  121. APR_FILEPATH_NOTRELATIVE, p);
  122. fspr_strerror(rv, errmsg, sizeof(errmsg));
  123. ABTS_PTR_EQUAL(tc, NULL, dstpath);
  124. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
  125. ABTS_STR_EQUAL(tc, "The given path is relative", errmsg);
  126. }
  127. static void merge_notabsfail(abts_case *tc, void *data)
  128. {
  129. fspr_status_t rv;
  130. char *dstpath = NULL;
  131. char errmsg[256];
  132. rv = fspr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz",
  133. APR_FILEPATH_NOTABSOLUTE, p);
  134. fspr_strerror(rv, errmsg, sizeof(errmsg));
  135. ABTS_PTR_EQUAL(tc, NULL, dstpath);
  136. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EABSOLUTE(rv));
  137. ABTS_STR_EQUAL(tc, "The given path is absolute", errmsg);
  138. }
  139. static void merge_notabs(abts_case *tc, void *data)
  140. {
  141. fspr_status_t rv;
  142. char *dstpath = NULL;
  143. rv = fspr_filepath_merge(&dstpath, "foo/bar", "../baz",
  144. APR_FILEPATH_NOTABSOLUTE, p);
  145. ABTS_PTR_NOTNULL(tc, dstpath);
  146. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  147. ABTS_STR_EQUAL(tc, "foo/baz", dstpath);
  148. }
  149. static void root_absolute(abts_case *tc, void *data)
  150. {
  151. fspr_status_t rv;
  152. const char *root = NULL;
  153. const char *path = ABS_ROOT"foo/bar";
  154. rv = fspr_filepath_root(&root, &path, 0, p);
  155. ABTS_PTR_NOTNULL(tc, root);
  156. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  157. ABTS_STR_EQUAL(tc, ABS_ROOT, root);
  158. }
  159. static void root_relative(abts_case *tc, void *data)
  160. {
  161. fspr_status_t rv;
  162. const char *root = NULL;
  163. const char *path = "foo/bar";
  164. char errmsg[256];
  165. rv = fspr_filepath_root(&root, &path, 0, p);
  166. fspr_strerror(rv, errmsg, sizeof(errmsg));
  167. ABTS_PTR_EQUAL(tc, NULL, root);
  168. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
  169. ABTS_STR_EQUAL(tc, "The given path is relative", errmsg);
  170. }
  171. static void root_from_slash(abts_case *tc, void *data)
  172. {
  173. fspr_status_t rv;
  174. const char *root = NULL;
  175. const char *path = "//";
  176. rv = fspr_filepath_root(&root, &path, APR_FILEPATH_TRUENAME, p);
  177. #if defined(WIN32) || defined(OS2)
  178. ABTS_INT_EQUAL(tc, APR_EINCOMPLETE, rv);
  179. ABTS_STR_EQUAL(tc, "//", root);
  180. #else
  181. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  182. ABTS_STR_EQUAL(tc, "/", root);
  183. #endif
  184. ABTS_STR_EQUAL(tc, "", path);
  185. }
  186. static void root_from_cwd_and_back(abts_case *tc, void *data)
  187. {
  188. fspr_status_t rv;
  189. const char *root = NULL;
  190. const char *path = "//";
  191. char *origpath;
  192. char *testpath;
  193. ABTS_INT_EQUAL(tc, APR_SUCCESS, fspr_filepath_get(&origpath, 0, p));
  194. path = origpath;
  195. rv = fspr_filepath_root(&root, &path, APR_FILEPATH_TRUENAME, p);
  196. #if defined(WIN32) || defined(OS2)
  197. ABTS_INT_EQUAL(tc, origpath[0], root[0]);
  198. ABTS_INT_EQUAL(tc, ':', root[1]);
  199. ABTS_INT_EQUAL(tc, '/', root[2]);
  200. ABTS_INT_EQUAL(tc, 0, root[3]);
  201. ABTS_STR_EQUAL(tc, origpath + 3, path);
  202. #elif defined(NETWARE)
  203. ABTS_INT_EQUAL(tc, origpath[0], root[0]);
  204. {
  205. char *pt = strchr(root, ':');
  206. ABTS_PTR_NOTNULL(tc, pt);
  207. ABTS_INT_EQUAL(tc, ':', pt[0]);
  208. ABTS_INT_EQUAL(tc, '/', pt[1]);
  209. ABTS_INT_EQUAL(tc, 0, pt[2]);
  210. pt = strchr(origpath, ':');
  211. ABTS_PTR_NOTNULL(tc, pt);
  212. ABTS_STR_EQUAL(tc, (pt+2), path);
  213. }
  214. #else
  215. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  216. ABTS_STR_EQUAL(tc, "/", root);
  217. ABTS_STR_EQUAL(tc, origpath + 1, path);
  218. #endif
  219. rv = fspr_filepath_merge(&testpath, root, path,
  220. APR_FILEPATH_TRUENAME
  221. | APR_FILEPATH_NOTABOVEROOT
  222. | APR_FILEPATH_NOTRELATIVE, p);
  223. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  224. ABTS_STR_EQUAL(tc, origpath, testpath);
  225. }
  226. abts_suite *testnames(abts_suite *suite)
  227. {
  228. suite = ADD_SUITE(suite)
  229. abts_run_test(suite, merge_aboveroot, NULL);
  230. abts_run_test(suite, merge_belowroot, NULL);
  231. abts_run_test(suite, merge_noflag, NULL);
  232. abts_run_test(suite, merge_dotdot, NULL);
  233. abts_run_test(suite, merge_secure, NULL);
  234. abts_run_test(suite, merge_notrel, NULL);
  235. abts_run_test(suite, merge_notrelfail, NULL);
  236. abts_run_test(suite, merge_notabs, NULL);
  237. abts_run_test(suite, merge_notabsfail, NULL);
  238. abts_run_test(suite, merge_dotdot_dotdot_dotdot, NULL);
  239. abts_run_test(suite, root_absolute, NULL);
  240. abts_run_test(suite, root_relative, NULL);
  241. abts_run_test(suite, root_from_slash, NULL);
  242. abts_run_test(suite, root_from_cwd_and_back, NULL);
  243. return suite;
  244. }