test_basic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*-
  2. * Copyright (c) 2003-2007 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: src/usr.bin/cpio/test/test_basic.c,v 1.4 2008/08/25 06:39:29 kientzle Exp $");
  27. static void
  28. verify_files(const char *msg)
  29. {
  30. /*
  31. * Verify unpacked files.
  32. */
  33. /* Regular file with 2 links. */
  34. failure("%s", msg);
  35. assertIsReg("file", 0644);
  36. failure("%s", msg);
  37. assertFileSize("file", 10);
  38. failure("%s", msg);
  39. assertFileNLinks("file", 2);
  40. /* Another name for the same file. */
  41. failure("%s", msg);
  42. assertIsHardlink("linkfile", "file");
  43. /* Symlink */
  44. if (canSymlink())
  45. assertIsSymlink("symlink", "file", 0);
  46. /* Another file with 1 link and different permissions. */
  47. failure("%s", msg);
  48. assertIsReg("file2", 0777);
  49. failure("%s", msg);
  50. assertFileSize("file2", 10);
  51. failure("%s", msg);
  52. assertFileNLinks("file2", 1);
  53. /* dir */
  54. assertIsDir("dir", 0775);
  55. }
  56. static void
  57. basic_cpio(const char *target,
  58. const char *pack_options,
  59. const char *unpack_options,
  60. const char *se, const char *se2)
  61. {
  62. int r;
  63. if (!assertMakeDir(target, 0775))
  64. return;
  65. /* Use the cpio program to create an archive. */
  66. r = systemf("%s -R 1000:1000 -o %s < filelist >%s/archive 2>%s/pack.err",
  67. testprog, pack_options, target, target);
  68. failure("Error invoking %s -o %s", testprog, pack_options);
  69. assertEqualInt(r, 0);
  70. assertChdir(target);
  71. /* Verify stderr. */
  72. failure("Expected: %s, options=%s", se, pack_options);
  73. assertTextFileContents(se, "pack.err");
  74. /*
  75. * Use cpio to unpack the archive into another directory.
  76. */
  77. r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
  78. testprog, unpack_options);
  79. failure("Error invoking %s -i %s", testprog, unpack_options);
  80. assertEqualInt(r, 0);
  81. /* Verify stderr. */
  82. failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
  83. assertTextFileContents(se2, "unpack.err");
  84. verify_files(pack_options);
  85. assertChdir("..");
  86. }
  87. static void
  88. passthrough(const char *target)
  89. {
  90. int r;
  91. if (!assertMakeDir(target, 0775))
  92. return;
  93. /*
  94. * Use cpio passthrough mode to copy files to another directory.
  95. */
  96. r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
  97. testprog, target, target, target);
  98. failure("Error invoking %s -p", testprog);
  99. assertEqualInt(r, 0);
  100. assertChdir(target);
  101. /* Verify stderr. */
  102. failure("Error invoking %s -p in dir %s",
  103. testprog, target);
  104. assertTextFileContents("1 block\n", "stderr");
  105. verify_files("passthrough");
  106. assertChdir("..");
  107. }
  108. DEFINE_TEST(test_basic)
  109. {
  110. FILE *filelist;
  111. const char *msg;
  112. char result[1024];
  113. assertUmask(0);
  114. /*
  115. * Create an assortment of files on disk.
  116. */
  117. filelist = fopen("filelist", "w");
  118. memset(result, 0, sizeof(result));
  119. /* File with 10 bytes content. */
  120. assertMakeFile("file", 0644, "1234567890");
  121. fprintf(filelist, "file\n");
  122. if (is_LargeInode("file")) {
  123. strncat(result,
  124. "bsdcpio: file: large inode number truncated: ",
  125. sizeof(result) - strlen(result) -1);
  126. strncat(result,
  127. strerror(ERANGE),
  128. sizeof(result) - strlen(result) -1);
  129. strncat(result,
  130. "\n",
  131. sizeof(result) - strlen(result) -1);
  132. }
  133. /* hardlink to above file. */
  134. assertMakeHardlink("linkfile", "file");
  135. fprintf(filelist, "linkfile\n");
  136. if (is_LargeInode("linkfile")) {
  137. strncat(result,
  138. "bsdcpio: linkfile: large inode number truncated: ",
  139. sizeof(result) - strlen(result) -1);
  140. strncat(result,
  141. strerror(ERANGE),
  142. sizeof(result) - strlen(result) -1);
  143. strncat(result,
  144. "\n",
  145. sizeof(result) - strlen(result) -1);
  146. }
  147. /* Symlink to above file. */
  148. if (canSymlink()) {
  149. assertMakeSymlink("symlink", "file", 0);
  150. fprintf(filelist, "symlink\n");
  151. if (is_LargeInode("symlink")) {
  152. strncat(result,
  153. "bsdcpio: symlink: large inode number truncated: ",
  154. sizeof(result) - strlen(result) -1);
  155. strncat(result,
  156. strerror(ERANGE),
  157. sizeof(result) - strlen(result) -1);
  158. strncat(result,
  159. "\n",
  160. sizeof(result) - strlen(result) -1);
  161. }
  162. }
  163. /* Another file with different permissions. */
  164. assertMakeFile("file2", 0777, "1234567890");
  165. fprintf(filelist, "file2\n");
  166. if (is_LargeInode("file2")) {
  167. strncat(result,
  168. "bsdcpio: file2: large inode number truncated: ",
  169. sizeof(result) - strlen(result) -1);
  170. strncat(result,
  171. strerror(ERANGE),
  172. sizeof(result) - strlen(result) -1);
  173. strncat(result,
  174. "\n",
  175. sizeof(result) - strlen(result) -1);
  176. }
  177. /* Directory. */
  178. assertMakeDir("dir", 0775);
  179. fprintf(filelist, "dir\n");
  180. if (is_LargeInode("dir")) {
  181. strncat(result,
  182. "bsdcpio: dir: large inode number truncated: ",
  183. sizeof(result) - strlen(result) -1);
  184. strncat(result,
  185. strerror(ERANGE),
  186. sizeof(result) - strlen(result) -1);
  187. strncat(result,
  188. "\n",
  189. sizeof(result) - strlen(result) -1);
  190. }
  191. strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
  192. /* All done. */
  193. fclose(filelist);
  194. assertUmask(022);
  195. /* Archive/dearchive with a variety of options. */
  196. msg = canSymlink() ? "2 blocks\n" : "1 block\n";
  197. basic_cpio("copy", "", "", msg, msg);
  198. basic_cpio("copy_odc", "--format=odc", "", msg, msg);
  199. basic_cpio("copy_newc", "-H newc", "", result, "2 blocks\n");
  200. basic_cpio("copy_cpio", "-H odc", "", msg, msg);
  201. msg = "1 block\n";
  202. basic_cpio("copy_bin", "-H bin", "", msg, msg);
  203. msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
  204. basic_cpio("copy_ustar", "-H ustar", "", msg, msg);
  205. /* Copy in one step using -p */
  206. passthrough("passthrough");
  207. }