HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.2KB

  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * All rights reserved.
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "test.h"
  26. __FBSDID("$FreeBSD$");
  27. #define MAGIC 123456789
  28. struct my_data {
  29. int magic;
  30. int read_return;
  31. int read_called;
  32. int write_return;
  33. int write_called;
  34. int open_return;
  35. int open_called;
  36. int close_return;
  37. int close_called;
  38. };
  39. static ssize_t
  40. my_read(struct archive *a, void *_private, const void **buff)
  41. {
  42. struct my_data *private = (struct my_data *)_private;
  43. (void)a; /* UNUSED */
  44. (void)buff; /* UNUSED */
  45. assertEqualInt(MAGIC, private->magic);
  46. ++private->read_called;
  47. return (private->read_return);
  48. }
  49. static ssize_t
  50. my_write(struct archive *a, void *_private, const void *buff, size_t s)
  51. {
  52. struct my_data *private = (struct my_data *)_private;
  53. (void)a; /* UNUSED */
  54. (void)buff; /* UNUSED */
  55. (void)s; /* UNUSED */
  56. assertEqualInt(MAGIC, private->magic);
  57. ++private->write_called;
  58. return (private->write_return);
  59. }
  60. static int
  61. my_open(struct archive *a, void *_private)
  62. {
  63. struct my_data *private = (struct my_data *)_private;
  64. (void)a; /* UNUSED */
  65. assertEqualInt(MAGIC, private->magic);
  66. ++private->open_called;
  67. return (private->open_return);
  68. }
  69. static int
  70. my_close(struct archive *a, void *_private)
  71. {
  72. struct my_data *private = (struct my_data *)_private;
  73. (void)a; /* UNUSED */
  74. assertEqualInt(MAGIC, private->magic);
  75. ++private->close_called;
  76. return (private->close_return);
  77. }
  78. DEFINE_TEST(test_open_failure)
  79. {
  80. struct archive *a;
  81. struct my_data private;
  82. memset(&private, 0, sizeof(private));
  83. private.magic = MAGIC;
  84. private.open_return = ARCHIVE_FATAL;
  85. a = archive_read_new();
  86. assert(a != NULL);
  87. assertEqualInt(ARCHIVE_FATAL,
  88. archive_read_open(a, &private, my_open, my_read, my_close));
  89. assertEqualInt(1, private.open_called);
  90. assertEqualInt(0, private.read_called);
  91. assertEqualInt(1, private.close_called);
  92. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  93. assertEqualInt(1, private.open_called);
  94. assertEqualInt(0, private.read_called);
  95. assertEqualInt(1, private.close_called);
  96. memset(&private, 0, sizeof(private));
  97. private.magic = MAGIC;
  98. private.open_return = ARCHIVE_FAILED;
  99. a = archive_read_new();
  100. assert(a != NULL);
  101. assertEqualInt(ARCHIVE_FAILED,
  102. archive_read_open(a, &private, my_open, my_read, my_close));
  103. assertEqualInt(1, private.open_called);
  104. assertEqualInt(0, private.read_called);
  105. assertEqualInt(1, private.close_called);
  106. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  107. assertEqualInt(1, private.open_called);
  108. assertEqualInt(0, private.read_called);
  109. assertEqualInt(1, private.close_called);
  110. memset(&private, 0, sizeof(private));
  111. private.magic = MAGIC;
  112. private.open_return = ARCHIVE_WARN;
  113. a = archive_read_new();
  114. assert(a != NULL);
  115. assertEqualInt(ARCHIVE_WARN,
  116. archive_read_open(a, &private, my_open, my_read, my_close));
  117. assertEqualInt(1, private.open_called);
  118. assertEqualInt(0, private.read_called);
  119. assertEqualInt(1, private.close_called);
  120. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  121. assertEqualInt(1, private.open_called);
  122. assertEqualInt(0, private.read_called);
  123. assertEqualInt(1, private.close_called);
  124. memset(&private, 0, sizeof(private));
  125. private.magic = MAGIC;
  126. private.open_return = ARCHIVE_OK;
  127. private.read_return = ARCHIVE_FATAL;
  128. a = archive_read_new();
  129. assert(a != NULL);
  130. assertEqualInt(ARCHIVE_OK,
  131. archive_read_support_filter_compress(a));
  132. assertEqualInt(ARCHIVE_OK, archive_read_support_format_tar(a));
  133. assertEqualInt(ARCHIVE_FATAL,
  134. archive_read_open(a, &private, my_open, my_read, my_close));
  135. assertEqualInt(1, private.open_called);
  136. assertEqualInt(1, private.read_called);
  137. assertEqualInt(1, private.close_called);
  138. assertEqualInt(ARCHIVE_OK, archive_read_free(a));
  139. assertEqualInt(1, private.open_called);
  140. assertEqualInt(1, private.read_called);
  141. assertEqualInt(1, private.close_called);
  142. memset(&private, 0, sizeof(private));
  143. private.magic = MAGIC;
  144. private.open_return = ARCHIVE_FATAL;
  145. a = archive_write_new();
  146. assert(a != NULL);
  147. assertEqualInt(ARCHIVE_FATAL,
  148. archive_write_open(a, &private, my_open, my_write, my_close));
  149. assertEqualInt(1, private.open_called);
  150. assertEqualInt(0, private.write_called);
  151. assertEqualInt(0, private.close_called);
  152. assertEqualInt(ARCHIVE_OK, archive_write_free(a));
  153. assertEqualInt(1, private.open_called);
  154. assertEqualInt(0, private.write_called);
  155. assertEqualInt(0, private.close_called);
  156. memset(&private, 0, sizeof(private));
  157. private.magic = MAGIC;
  158. private.open_return = ARCHIVE_FATAL;
  159. a = archive_write_new();
  160. assert(a != NULL);
  161. archive_write_add_filter_compress(a);
  162. archive_write_set_format_ustar(a);
  163. assertEqualInt(ARCHIVE_FATAL,
  164. archive_write_open(a, &private, my_open, my_write, my_close));
  165. assertEqualInt(1, private.open_called);
  166. assertEqualInt(0, private.write_called);
  167. assertEqualInt(0, private.close_called);
  168. assertEqualInt(ARCHIVE_OK, archive_write_free(a));
  169. assertEqualInt(1, private.open_called);
  170. assertEqualInt(0, private.write_called);
  171. assertEqualInt(0, private.close_called);
  172. memset(&private, 0, sizeof(private));
  173. private.magic = MAGIC;
  174. private.open_return = ARCHIVE_FATAL;
  175. a = archive_write_new();
  176. assert(a != NULL);
  177. archive_write_set_format_zip(a);
  178. assertEqualInt(ARCHIVE_FATAL,
  179. archive_write_open(a, &private, my_open, my_write, my_close));
  180. assertEqualInt(1, private.open_called);
  181. assertEqualInt(0, private.write_called);
  182. assertEqualInt(0, private.close_called);
  183. assertEqualInt(ARCHIVE_OK, archive_write_free(a));
  184. assertEqualInt(1, private.open_called);
  185. assertEqualInt(0, private.write_called);
  186. assertEqualInt(0, private.close_called);
  187. memset(&private, 0, sizeof(private));
  188. private.magic = MAGIC;
  189. private.open_return = ARCHIVE_FATAL;
  190. a = archive_write_new();
  191. assert(a != NULL);
  192. archive_write_add_filter_gzip(a);
  193. assertEqualInt(ARCHIVE_FATAL,
  194. archive_write_open(a, &private, my_open, my_write, my_close));
  195. assertEqualInt(1, private.open_called);
  196. assertEqualInt(0, private.write_called);
  197. assertEqualInt(0, private.close_called);
  198. assertEqualInt(ARCHIVE_OK, archive_write_free(a));
  199. assertEqualInt(1, private.open_called);
  200. assertEqualInt(0, private.write_called);
  201. assertEqualInt(0, private.close_called);
  202. }