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
9 static const char *
make_files(void)10 make_files(void)
11 {
12 FILE *f;
13
14 /* File with 10 bytes content. */
15 f = fopen("file", "wb");
16 assert(f != NULL);
17 assertEqualInt(10, fwrite("123456789", 1, 10, f));
18 fclose(f);
19
20 /* hardlink to above file. */
21 assertMakeHardlink("linkfile", "file");
22 assertIsHardlink("file", "linkfile");
23
24 /* Symlink to above file. */
25 if (canSymlink())
26 assertMakeSymlink("symlink", "file", 0);
27
28 /* Directory. */
29 assertMakeDir("dir", 0775);
30
31 return canSymlink()
32 ? "file linkfile symlink dir"
33 : "file linkfile dir";
34 }
35
36 static void
verify_files(const char * target)37 verify_files(const char *target)
38 {
39 assertChdir(target);
40
41 /* Regular file with 2 links. */
42 failure("%s", target);
43 assertIsReg("file", -1);
44 failure("%s", target);
45 assertFileSize("file", 10);
46 failure("%s", target);
47 assertFileContents("123456789", 10, "file");
48 failure("%s", target);
49 assertFileNLinks("file", 2);
50
51 /* Another name for the same file. */
52 failure("%s", target);
53 assertIsReg("linkfile", -1);
54 failure("%s", target);
55 assertFileSize("linkfile", 10);
56 assertFileContents("123456789", 10, "linkfile");
57 assertFileNLinks("linkfile", 2);
58 assertIsHardlink("file", "linkfile");
59
60 /* Symlink */
61 if (canSymlink())
62 assertIsSymlink("symlink", "file", 0);
63
64 /* dir */
65 failure("%s", target);
66 assertIsDir("dir", 0775);
67 assertChdir("..");
68 }
69
70 static void
run_tar(const char * target,const char * pack_options,const char * unpack_options,const char * flist)71 run_tar(const char *target, const char *pack_options,
72 const char *unpack_options, const char *flist)
73 {
74 int r;
75
76 assertMakeDir(target, 0775);
77
78 /* Use the tar program to create an archive. */
79 r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
80 failure("Error invoking %s cf -%s", testprog, pack_options);
81 assertEqualInt(r, 0);
82
83 assertChdir(target);
84
85 /* Verify that nothing went to stderr. */
86 assertEmptyFile("pack.err");
87
88 /*
89 * Use tar to unpack the archive into another directory.
90 */
91 r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
92 testprog, unpack_options);
93 failure("Error invoking %s xf archive %s", testprog, unpack_options);
94 assertEqualInt(r, 0);
95
96 /* Verify that nothing went to stderr. */
97 assertEmptyFile("unpack.err");
98 assertChdir("..");
99 }
100
DEFINE_TEST(test_basic)101 DEFINE_TEST(test_basic)
102 {
103 const char *flist;
104
105 assertUmask(0);
106 flist = make_files();
107 /* Archive/dearchive with a variety of options. */
108 run_tar("copy", "", "", flist);
109 verify_files("copy");
110
111 run_tar("copy_ustar", "--format=ustar", "", flist);
112 verify_files("copy_ustar");
113
114 /* tar doesn't handle cpio symlinks correctly */
115 /* run_tar("copy_odc", "--format=odc", ""); */
116 }
117