/contrib/libarchive/libarchive/test/test_archive_string.c

https://bitbucket.org/iorivur/freebsd-bhyve-with-suspend-resume · C · 344 lines · 224 code · 65 blank · 55 comment · 28 complexity · 51111804b3b3bf2b09c9da55704a4e57 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2011 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 __LIBARCHIVE_TEST
  28. #include "archive_string.h"
  29. #define EXTENT 32
  30. #define assertStringSizes(strlen, buflen, as) \
  31. assertEqualInt(strlen, (as).length); \
  32. assertEqualInt(buflen, (as).buffer_length);
  33. #define assertExactString(strlen, buflen, data, as) \
  34. do { \
  35. assertStringSizes(strlen, buflen, as); \
  36. assertEqualString(data, (as).s); \
  37. } while (0)
  38. #define assertNonNULLString(strlen, buflen, as) \
  39. do { \
  40. assertStringSizes(strlen, buflen, as); \
  41. assert(NULL != (as).s); \
  42. } while (0)
  43. static void
  44. test_archive_string_ensure(void)
  45. {
  46. struct archive_string s;
  47. archive_string_init(&s);
  48. assertExactString(0, 0, NULL, s);
  49. /* single-extent allocation */
  50. assert(&s == archive_string_ensure(&s, 5));
  51. assertNonNULLString(0, EXTENT, s);
  52. /* what happens around extent boundaries? */
  53. assert(&s == archive_string_ensure(&s, EXTENT - 1));
  54. assertNonNULLString(0, EXTENT, s);
  55. assert(&s == archive_string_ensure(&s, EXTENT));
  56. assertNonNULLString(0, EXTENT, s);
  57. assert(&s == archive_string_ensure(&s, EXTENT + 1));
  58. assertNonNULLString(0, 2 * EXTENT, s);
  59. }
  60. static void
  61. test_archive_strcat(void)
  62. {
  63. struct archive_string s;
  64. archive_string_init(&s);
  65. assertExactString(0, 0, NULL, s);
  66. /* null target, empty source */
  67. assert(&s == archive_strcat(&s, ""));
  68. assertExactString(0, EXTENT, "", s);
  69. /* empty target, empty source */
  70. assert(&s == archive_strcat(&s, ""));
  71. assertExactString(0, EXTENT, "", s);
  72. /* empty target, non-empty source */
  73. assert(&s == archive_strcat(&s, "fubar"));
  74. assertExactString(5, EXTENT, "fubar", s);
  75. /* non-empty target, non-empty source */
  76. assert(&s == archive_strcat(&s, "baz"));
  77. assertExactString(8, EXTENT, "fubarbaz", s);
  78. }
  79. static void
  80. test_archive_strappend_char(void)
  81. {
  82. struct archive_string s;
  83. archive_string_init(&s);
  84. assertExactString(0, 0, NULL, s);
  85. /* null target */
  86. archive_strappend_char(&s, 'X');
  87. assertExactString(1, EXTENT, "X", s);
  88. /* non-empty target */
  89. archive_strappend_char(&s, 'Y');
  90. assertExactString(2, EXTENT, "XY", s);
  91. }
  92. /* archive_strnXXX() tests focus on length handling.
  93. * other behaviors are tested by proxy through archive_strXXX()
  94. */
  95. static void
  96. test_archive_strncat(void)
  97. {
  98. struct archive_string s;
  99. archive_string_init(&s);
  100. assertExactString(0, 0, NULL, s);
  101. /* perfect length */
  102. assert(&s == archive_strncat(&s, "snafu", 5));
  103. assertExactString(5, EXTENT, "snafu", s);
  104. /* short read */
  105. assert(&s == archive_strncat(&s, "barbazqux", 3));
  106. assertExactString(8, EXTENT, "snafubar", s);
  107. /* long read is ok too! */
  108. assert(&s == archive_strncat(&s, "snafu", 8));
  109. assertExactString(13, EXTENT, "snafubarsnafu", s);
  110. }
  111. static void
  112. test_archive_strncpy(void)
  113. {
  114. struct archive_string s;
  115. archive_string_init(&s);
  116. assertExactString(0, 0, NULL, s);
  117. /* perfect length */
  118. assert(&s == archive_strncpy(&s, "fubar", 5));
  119. assertExactString(5, EXTENT, "fubar", s);
  120. /* short read */
  121. assert(&s == archive_strncpy(&s, "snafubar", 5));
  122. assertExactString(5, EXTENT, "snafu", s);
  123. /* long read is ok too! */
  124. assert(&s == archive_strncpy(&s, "snafu", 8));
  125. assertExactString(5, EXTENT, "snafu", s);
  126. }
  127. static void
  128. test_archive_strcpy(void)
  129. {
  130. struct archive_string s;
  131. archive_string_init(&s);
  132. assertExactString(0, 0, NULL, s);
  133. /* null target */
  134. assert(&s == archive_strcpy(&s, "snafu"));
  135. assertExactString(5, EXTENT, "snafu", s);
  136. /* dirty target */
  137. assert(&s == archive_strcpy(&s, "foo"));
  138. assertExactString(3, EXTENT, "foo", s);
  139. /* dirty target, empty source */
  140. assert(&s == archive_strcpy(&s, ""));
  141. assertExactString(0, EXTENT, "", s);
  142. }
  143. static void
  144. test_archive_string_concat(void)
  145. {
  146. struct archive_string s, t, u, v;
  147. archive_string_init(&s);
  148. assertExactString(0, 0, NULL, s);
  149. archive_string_init(&t);
  150. assertExactString(0, 0, NULL, t);
  151. archive_string_init(&u);
  152. assertExactString(0, 0, NULL, u);
  153. archive_string_init(&v);
  154. assertExactString(0, 0, NULL, v);
  155. /* null target, null source */
  156. archive_string_concat(&t, &s);
  157. assertExactString(0, 0, NULL, s);
  158. assertExactString(0, EXTENT, "", t);
  159. /* null target, empty source */
  160. assert(&s == archive_strcpy(&s, ""));
  161. archive_string_concat(&u, &s);
  162. assertExactString(0, EXTENT, "", s);
  163. assertExactString(0, EXTENT, "", u);
  164. /* null target, non-empty source */
  165. assert(&s == archive_strcpy(&s, "foo"));
  166. archive_string_concat(&v, &s);
  167. assertExactString(3, EXTENT, "foo", s);
  168. assertExactString(3, EXTENT, "foo", v);
  169. /* empty target, empty source */
  170. assert(&s == archive_strcpy(&s, ""));
  171. assert(&t == archive_strcpy(&t, ""));
  172. archive_string_concat(&t, &s);
  173. assertExactString(0, EXTENT, "", s);
  174. assertExactString(0, EXTENT, "", t);
  175. /* empty target, non-empty source */
  176. assert(&s == archive_strcpy(&s, "snafu"));
  177. assert(&t == archive_strcpy(&t, ""));
  178. archive_string_concat(&t, &s);
  179. assertExactString(5, EXTENT, "snafu", s);
  180. assertExactString(5, EXTENT, "snafu", t);
  181. }
  182. static void
  183. test_archive_string_copy(void)
  184. {
  185. struct archive_string s, t, u, v;
  186. archive_string_init(&s);
  187. assertExactString(0, 0, NULL, s);
  188. archive_string_init(&t);
  189. assertExactString(0, 0, NULL, t);
  190. archive_string_init(&u);
  191. assertExactString(0, 0, NULL, u);
  192. archive_string_init(&v);
  193. assertExactString(0, 0, NULL, v);
  194. /* null target, null source */
  195. archive_string_copy(&t, &s);
  196. assertExactString(0, 0, NULL, s);
  197. assertExactString(0, EXTENT, "", t);
  198. /* null target, empty source */
  199. archive_string_copy(&u, &t);
  200. assertExactString(0, EXTENT, "", t);
  201. assertExactString(0, EXTENT, "", u);
  202. /* empty target, empty source */
  203. archive_string_copy(&u, &t);
  204. assertExactString(0, EXTENT, "", t);
  205. assertExactString(0, EXTENT, "", u);
  206. /* null target, non-empty source */
  207. assert(NULL != archive_strcpy(&s, "snafubar"));
  208. assertExactString(8, EXTENT, "snafubar", s);
  209. archive_string_copy(&v, &s);
  210. assertExactString(8, EXTENT, "snafubar", s);
  211. assertExactString(8, EXTENT, "snafubar", v);
  212. /* empty target, non-empty source */
  213. assertExactString(0, EXTENT, "", t);
  214. archive_string_copy(&t, &s);
  215. assertExactString(8, EXTENT, "snafubar", s);
  216. assertExactString(8, EXTENT, "snafubar", t);
  217. /* non-empty target, non-empty source */
  218. assert(NULL != archive_strcpy(&s, "fubar"));
  219. assertExactString(5, EXTENT, "fubar", s);
  220. archive_string_copy(&t, &s);
  221. assertExactString(5, EXTENT, "fubar", s);
  222. assertExactString(5, EXTENT, "fubar", t);
  223. }
  224. static void
  225. test_archive_string_sprintf(void)
  226. {
  227. struct archive_string s;
  228. #define S16 "0123456789abcdef"
  229. #define S32 S16 S16
  230. #define S64 S32 S32
  231. #define S128 S64 S64
  232. const char *s32 = S32;
  233. const char *s33 = S32 "0";
  234. const char *s64 = S64;
  235. const char *s65 = S64 "0";
  236. const char *s128 = S128;
  237. const char *s129 = S128 "0";
  238. #undef S16
  239. #undef S32
  240. #undef S64
  241. #undef S128
  242. archive_string_init(&s);
  243. assertExactString(0, 0, NULL, s);
  244. archive_string_sprintf(&s, "%s", "");
  245. assertExactString(0, 2 * EXTENT, "", s);
  246. archive_string_empty(&s);
  247. archive_string_sprintf(&s, "%s", s32);
  248. assertExactString(32, 2 * EXTENT, s32, s);
  249. archive_string_empty(&s);
  250. archive_string_sprintf(&s, "%s", s33);
  251. assertExactString(33, 2 * EXTENT, s33, s);
  252. archive_string_empty(&s);
  253. archive_string_sprintf(&s, "%s", s64);
  254. assertExactString(64, 4 * EXTENT, s64, s);
  255. archive_string_empty(&s);
  256. archive_string_sprintf(&s, "%s", s65);
  257. assertExactString(65, 4 * EXTENT, s65, s);
  258. archive_string_empty(&s);
  259. archive_string_sprintf(&s, "%s", s128);
  260. assertExactString(128, 8 * EXTENT, s128, s);
  261. archive_string_empty(&s);
  262. archive_string_sprintf(&s, "%s", s129);
  263. assertExactString(129, 8 * EXTENT, s129, s);
  264. archive_string_empty(&s);
  265. archive_string_sprintf(&s, "%d", 1234567890);
  266. assertExactString(10, 8 * EXTENT, "1234567890", s);
  267. }
  268. DEFINE_TEST(test_archive_string)
  269. {
  270. test_archive_string_ensure();
  271. test_archive_strcat();
  272. test_archive_strappend_char();
  273. test_archive_strncat();
  274. test_archive_strncpy();
  275. test_archive_strcpy();
  276. test_archive_string_concat();
  277. test_archive_string_copy();
  278. test_archive_string_sprintf();
  279. }