common_path.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Calls with invalid pathnames
  31. */
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <limits.h>
  39. #include <errno.h>
  40. #include <err.h>
  41. #include "config.h"
  42. #include "test.h"
  43. static
  44. int
  45. open_badpath(const char *path)
  46. {
  47. return open(path, O_RDONLY);
  48. }
  49. static
  50. int
  51. remove_badpath(const char *path)
  52. {
  53. return remove(path);
  54. }
  55. static
  56. int
  57. rename_badpath1(const char *path)
  58. {
  59. return rename(path, TESTFILE);
  60. }
  61. static
  62. int
  63. rename_badpath2(const char *path)
  64. {
  65. return rename(TESTFILE, path);
  66. }
  67. static
  68. int
  69. link_badpath1(const char *path)
  70. {
  71. return link(path, TESTFILE);
  72. }
  73. static
  74. int
  75. link_badpath2(const char *path)
  76. {
  77. return link(TESTFILE, path);
  78. }
  79. static
  80. int
  81. mkdir_badpath(const char *path)
  82. {
  83. return mkdir(path, 0775);
  84. }
  85. static
  86. int
  87. rmdir_badpath(const char *path)
  88. {
  89. return rmdir(path);
  90. }
  91. static
  92. int
  93. chdir_badpath(const char *path)
  94. {
  95. return chdir(path);
  96. }
  97. static
  98. int
  99. symlink_badpath1(const char *path)
  100. {
  101. return symlink(path, TESTFILE);
  102. }
  103. static
  104. int
  105. symlink_badpath2(const char *path)
  106. {
  107. return symlink(TESTFILE, path);
  108. }
  109. static
  110. int
  111. readlink_badpath(const char *path)
  112. {
  113. char buf[128];
  114. return readlink(path, buf, sizeof(buf));
  115. }
  116. static
  117. int
  118. lstat_badpath(const char *name)
  119. {
  120. struct stat sb;
  121. return lstat(name, &sb);
  122. }
  123. static
  124. int
  125. stat_badpath(const char *name)
  126. {
  127. struct stat sb;
  128. return stat(name, &sb);
  129. }
  130. ////////////////////////////////////////////////////////////
  131. static
  132. void
  133. common_badpath(int (*func)(const char *path), int mk, int rm, const char *path,
  134. const char *call, const char *pathdesc)
  135. {
  136. char mydesc[128];
  137. int rv;
  138. if (mk) {
  139. if (create_testfile()<0) {
  140. return;
  141. }
  142. }
  143. snprintf(mydesc, sizeof(mydesc), "%s with %s path", call, pathdesc);
  144. rv = func(path);
  145. report_test(rv, errno, EFAULT, mydesc);
  146. if (mk || rm) {
  147. remove(TESTFILE);
  148. }
  149. }
  150. static
  151. void
  152. any_badpath(int (*func)(const char *path), const char *call, int mk, int rm)
  153. {
  154. common_badpath(func, mk, rm, NULL, call, "NULL");
  155. common_badpath(func, mk, rm, INVAL_PTR, call, "invalid-pointer");
  156. common_badpath(func, mk, rm, KERN_PTR, call, "kernel-pointer");
  157. }
  158. ////////////////////////////////////////////////////////////
  159. /* functions with one pathname */
  160. #define T(call) \
  161. void \
  162. test_##call##_path(void) \
  163. { \
  164. any_badpath(call##_badpath, #call, 0, 0); \
  165. }
  166. T(open);
  167. T(remove);
  168. T(mkdir);
  169. T(rmdir);
  170. T(chdir);
  171. T(readlink);
  172. T(stat);
  173. T(lstat);
  174. /* functions with two pathnames */
  175. #define T2(call) \
  176. void \
  177. test_##call##_paths(void) \
  178. { \
  179. any_badpath(call##_badpath1, #call "(arg1)", 0, 1); \
  180. any_badpath(call##_badpath2, #call "(arg2)", 1, 1); \
  181. }
  182. T2(rename);
  183. T2(link);
  184. T2(symlink);