/usr.bin/tar/test/test_basic.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 115 lines · 51 code · 20 blank · 44 comment · 4 complexity · 2c1860834414fb336f077fcc060bcd49 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 void
  28. basic_tar(const char *target, const char *pack_options,
  29. const char *unpack_options, const char *flist)
  30. {
  31. int r;
  32. assertMakeDir(target, 0775);
  33. /* Use the tar program to create an archive. */
  34. r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
  35. failure("Error invoking %s cf -", testprog, pack_options);
  36. assertEqualInt(r, 0);
  37. assertChdir(target);
  38. /* Verify that nothing went to stderr. */
  39. assertEmptyFile("pack.err");
  40. /*
  41. * Use tar to unpack the archive into another directory.
  42. */
  43. r = systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog, unpack_options);
  44. failure("Error invoking %s xf archive %s", testprog, unpack_options);
  45. assertEqualInt(r, 0);
  46. /* Verify that nothing went to stderr. */
  47. assertEmptyFile("unpack.err");
  48. /*
  49. * Verify unpacked files.
  50. */
  51. /* Regular file with 2 links. */
  52. assertIsReg("file", -1);
  53. assertFileSize("file", 10);
  54. failure("%s", target);
  55. assertFileNLinks("file", 2);
  56. /* Another name for the same file. */
  57. assertIsReg("linkfile", -1);
  58. assertFileSize("linkfile", 10);
  59. assertFileNLinks("linkfile", 2);
  60. assertIsHardlink("file", "linkfile");
  61. /* Symlink */
  62. if (canSymlink())
  63. assertIsSymlink("symlink", "file");
  64. /* dir */
  65. assertIsDir("dir", 0775);
  66. assertChdir("..");
  67. }
  68. DEFINE_TEST(test_basic)
  69. {
  70. FILE *f;
  71. const char *flist;
  72. assertUmask(0);
  73. /* File with 10 bytes content. */
  74. f = fopen("file", "wb");
  75. assert(f != NULL);
  76. assertEqualInt(10, fwrite("123456789", 1, 10, f));
  77. fclose(f);
  78. /* hardlink to above file. */
  79. assertMakeHardlink("linkfile", "file");
  80. assertIsHardlink("file", "linkfile");
  81. /* Symlink to above file. */
  82. if (canSymlink())
  83. assertMakeSymlink("symlink", "file");
  84. /* Directory. */
  85. assertMakeDir("dir", 0775);
  86. if (canSymlink())
  87. flist = "file linkfile symlink dir";
  88. else
  89. flist = "file linkfile dir";
  90. /* Archive/dearchive with a variety of options. */
  91. basic_tar("copy", "", "", flist);
  92. /* tar doesn't handle cpio symlinks correctly */
  93. /* basic_tar("copy_odc", "--format=odc", ""); */
  94. basic_tar("copy_ustar", "--format=ustar", "", flist);
  95. }