test_basic.c 5.3 KB

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