/usr.bin/cpio/test/test_option_c.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 221 lines · 148 code · 20 blank · 53 comment · 15 complexity · 314efcde55c6fb3c791dfc5401cf9b5f MD5 · raw file

  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$");
  27. static int
  28. is_octal(const char *p, size_t l)
  29. {
  30. while (l > 0) {
  31. if (*p < '0' || *p > '7')
  32. return (0);
  33. --l;
  34. ++p;
  35. }
  36. return (1);
  37. }
  38. static int
  39. from_octal(const char *p, size_t l)
  40. {
  41. int r = 0;
  42. while (l > 0) {
  43. r *= 8;
  44. r += *p - '0';
  45. --l;
  46. ++p;
  47. }
  48. return (r);
  49. }
  50. DEFINE_TEST(test_option_c)
  51. {
  52. FILE *filelist;
  53. int r;
  54. int uid = -1;
  55. int dev, ino, gid;
  56. time_t t, now;
  57. char *p, *e;
  58. size_t s;
  59. assertUmask(0);
  60. #if !defined(_WIN32)
  61. uid = getuid();
  62. #endif
  63. /*
  64. * Create an assortment of files.
  65. * TODO: Extend this to cover more filetypes.
  66. */
  67. filelist = fopen("filelist", "w");
  68. /* "file" */
  69. assertMakeFile("file", 0644, "1234567890");
  70. fprintf(filelist, "file\n");
  71. /* "symlink" */
  72. if (canSymlink()) {
  73. assertMakeSymlink("symlink", "file");
  74. fprintf(filelist, "symlink\n");
  75. }
  76. /* "dir" */
  77. assertMakeDir("dir", 0775);
  78. /* Record some facts about what we just created: */
  79. now = time(NULL); /* They were all created w/in last two seconds. */
  80. fprintf(filelist, "dir\n");
  81. /* Use the cpio program to create an archive. */
  82. fclose(filelist);
  83. r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog);
  84. /* Verify that nothing went to stderr. */
  85. assertTextFileContents("1 block\n", "basic.err");
  86. /* Assert that the program finished. */
  87. failure("%s -oc crashed", testprog);
  88. if (!assertEqualInt(r, 0))
  89. return;
  90. /* Verify that stdout is a well-formed cpio file in "odc" format. */
  91. p = slurpfile(&s, "basic.out");
  92. assertEqualInt(s, 512);
  93. e = p;
  94. /*
  95. * Some of these assertions could be stronger, but it's
  96. * a little tricky because they depend on the local environment.
  97. */
  98. /* First entry is "file" */
  99. assert(is_octal(e, 76)); /* Entire header is octal digits. */
  100. assertEqualMem(e + 0, "070707", 6); /* Magic */
  101. assert(is_octal(e + 6, 6)); /* dev */
  102. dev = from_octal(e + 6, 6);
  103. assert(is_octal(e + 12, 6)); /* ino */
  104. ino = from_octal(e + 12, 6);
  105. #if defined(_WIN32) && !defined(__CYGWIN__)
  106. /* Group members bits and others bits do not work. */
  107. assertEqualMem(e + 18, "100666", 6); /* Mode */
  108. #else
  109. assertEqualMem(e + 18, "100644", 6); /* Mode */
  110. #endif
  111. if (uid < 0)
  112. uid = from_octal(e + 24, 6);
  113. assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
  114. assert(is_octal(e + 30, 6)); /* gid */
  115. gid = from_octal(e + 30, 6);
  116. assertEqualMem(e + 36, "000001", 6); /* nlink */
  117. failure("file entries should not have rdev set (dev field was 0%o)",
  118. dev);
  119. assertEqualMem(e + 42, "000000", 6); /* rdev */
  120. t = from_octal(e + 48, 11); /* mtime */
  121. assert(t <= now); /* File wasn't created in future. */
  122. assert(t >= now - 2); /* File was created w/in last 2 secs. */
  123. assertEqualMem(e + 59, "000005", 6); /* Name size */
  124. assertEqualMem(e + 65, "00000000012", 11); /* File size */
  125. assertEqualMem(e + 76, "file\0", 5); /* Name contents */
  126. assertEqualMem(e + 81, "1234567890", 10); /* File contents */
  127. e += 91;
  128. /* "symlink" pointing to "file" */
  129. if (canSymlink()) {
  130. assert(is_octal(e, 76)); /* Entire header is octal digits. */
  131. assertEqualMem(e + 0, "070707", 6); /* Magic */
  132. assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */
  133. assert(ino != from_octal(e + 12, 6)); /* ino */
  134. #if !defined(_WIN32) || defined(__CYGWIN__)
  135. /* On Windows, symbolic link and group members bits and
  136. * others bits do not work. */
  137. assertEqualMem(e + 18, "120777", 6); /* Mode */
  138. #endif
  139. assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
  140. assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */
  141. assertEqualMem(e + 36, "000001", 6); /* nlink */
  142. failure("file entries should have rdev == 0 (dev was 0%o)",
  143. from_octal(e + 6, 6));
  144. assertEqualMem(e + 42, "000000", 6); /* rdev */
  145. t = from_octal(e + 48, 11); /* mtime */
  146. assert(t <= now); /* File wasn't created in future. */
  147. assert(t >= now - 2); /* File was created w/in last 2 secs. */
  148. assertEqualMem(e + 59, "000010", 6); /* Name size */
  149. assertEqualMem(e + 65, "00000000004", 11); /* File size */
  150. assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
  151. assertEqualMem(e + 84, "file", 4); /* Symlink target. */
  152. e += 88;
  153. }
  154. /* "dir" */
  155. assert(is_octal(e, 76));
  156. assertEqualMem(e + 0, "070707", 6); /* Magic */
  157. /* Dev should be same as first entry. */
  158. assert(is_octal(e + 6, 6)); /* dev */
  159. assertEqualInt(dev, from_octal(e + 6, 6));
  160. /* Ino must be different from first entry. */
  161. assert(is_octal(e + 12, 6)); /* ino */
  162. assert(dev != from_octal(e + 12, 6));
  163. #if defined(_WIN32) && !defined(__CYGWIN__)
  164. /* Group members bits and others bits do not work. */
  165. assertEqualMem(e + 18, "040777", 6); /* Mode */
  166. #else
  167. /* Accept 042775 to accomodate systems where sgid bit propagates. */
  168. if (memcmp(e + 18, "042775", 6) != 0)
  169. assertEqualMem(e + 18, "040775", 6); /* Mode */
  170. #endif
  171. assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
  172. /* Gid should be same as first entry. */
  173. assert(is_octal(e + 30, 6)); /* gid */
  174. assertEqualInt(gid, from_octal(e + 30, 6));
  175. #ifndef NLINKS_INACCURATE_FOR_DIRS
  176. assertEqualMem(e + 36, "000002", 6); /* Nlink */
  177. #endif
  178. t = from_octal(e + 48, 11); /* mtime */
  179. assert(t <= now); /* File wasn't created in future. */
  180. assert(t >= now - 2); /* File was created w/in last 2 secs. */
  181. assertEqualMem(e + 59, "000004", 6); /* Name size */
  182. assertEqualMem(e + 65, "00000000000", 11); /* File size */
  183. assertEqualMem(e + 76, "dir\0", 4); /* name */
  184. e += 80;
  185. /* TODO: Verify other types of entries. */
  186. /* Last entry is end-of-archive marker. */
  187. assert(is_octal(e, 76));
  188. assertEqualMem(e + 0, "070707", 6); /* Magic */
  189. assertEqualMem(e + 6, "000000", 6); /* dev */
  190. assertEqualMem(e + 12, "000000", 6); /* ino */
  191. assertEqualMem(e + 18, "000000", 6); /* Mode */
  192. assertEqualMem(e + 24, "000000", 6); /* uid */
  193. assertEqualMem(e + 30, "000000", 6); /* gid */
  194. assertEqualMem(e + 36, "000001", 6); /* Nlink */
  195. assertEqualMem(e + 42, "000000", 6); /* rdev */
  196. assertEqualMem(e + 48, "00000000000", 11); /* mtime */
  197. assertEqualMem(e + 59, "000013", 6); /* Name size */
  198. assertEqualMem(e + 65, "00000000000", 11); /* File size */
  199. assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
  200. free(p);
  201. }