1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/tar/test/test_basic.c 358088 2020-02-19 01:50:47Z mm $");
27228753Smm
28232153Smmstatic const char *
29232153Smmmake_files(void)
30228753Smm{
31232153Smm	FILE *f;
32228753Smm
33232153Smm	/* File with 10 bytes content. */
34232153Smm	f = fopen("file", "wb");
35232153Smm	assert(f != NULL);
36232153Smm	assertEqualInt(10, fwrite("123456789", 1, 10, f));
37232153Smm	fclose(f);
38228753Smm
39232153Smm	/* hardlink to above file. */
40232153Smm	assertMakeHardlink("linkfile", "file");
41232153Smm	assertIsHardlink("file", "linkfile");
42228753Smm
43232153Smm	/* Symlink to above file. */
44232153Smm	if (canSymlink())
45348607Smm		assertMakeSymlink("symlink", "file", 0);
46228753Smm
47232153Smm	/* Directory. */
48232153Smm	assertMakeDir("dir", 0775);
49228753Smm
50232153Smm	return canSymlink()
51232153Smm	    ? "file linkfile symlink dir"
52232153Smm	    : "file linkfile dir";
53232153Smm}
54228753Smm
55232153Smmstatic void
56232153Smmverify_files(const char *target)
57232153Smm{
58232153Smm	assertChdir(target);
59228753Smm
60228753Smm	/* Regular file with 2 links. */
61238909Smm	failure("%s", target);
62228753Smm	assertIsReg("file", -1);
63238909Smm	failure("%s", target);
64228753Smm	assertFileSize("file", 10);
65238909Smm	failure("%s", target);
66232153Smm	assertFileContents("123456789", 10, "file");
67228753Smm	failure("%s", target);
68228753Smm	assertFileNLinks("file", 2);
69228753Smm
70228753Smm	/* Another name for the same file. */
71238909Smm	failure("%s", target);
72228753Smm	assertIsReg("linkfile", -1);
73238909Smm	failure("%s", target);
74228753Smm	assertFileSize("linkfile", 10);
75232153Smm	assertFileContents("123456789", 10, "linkfile");
76228753Smm	assertFileNLinks("linkfile", 2);
77228753Smm	assertIsHardlink("file", "linkfile");
78228753Smm
79228753Smm	/* Symlink */
80228753Smm	if (canSymlink())
81348607Smm		assertIsSymlink("symlink", "file", 0);
82228753Smm
83228753Smm	/* dir */
84238909Smm	failure("%s", target);
85228753Smm	assertIsDir("dir", 0775);
86228753Smm	assertChdir("..");
87228753Smm}
88228753Smm
89232153Smmstatic void
90232153Smmrun_tar(const char *target, const char *pack_options,
91232153Smm    const char *unpack_options, const char *flist)
92228753Smm{
93232153Smm	int r;
94228753Smm
95232153Smm	assertMakeDir(target, 0775);
96228753Smm
97232153Smm	/* Use the tar program to create an archive. */
98232153Smm	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
99358088Smm	failure("Error invoking %s cf -%s", testprog, pack_options);
100232153Smm	assertEqualInt(r, 0);
101228753Smm
102232153Smm	assertChdir(target);
103228753Smm
104232153Smm	/* Verify that nothing went to stderr. */
105232153Smm	assertEmptyFile("pack.err");
106228753Smm
107232153Smm	/*
108232153Smm	 * Use tar to unpack the archive into another directory.
109232153Smm	 */
110232153Smm	r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
111232153Smm	    testprog, unpack_options);
112232153Smm	failure("Error invoking %s xf archive %s", testprog, unpack_options);
113232153Smm	assertEqualInt(r, 0);
114228753Smm
115232153Smm	/* Verify that nothing went to stderr. */
116232153Smm	assertEmptyFile("unpack.err");
117232153Smm	assertChdir("..");
118232153Smm}
119232153Smm
120232153SmmDEFINE_TEST(test_basic)
121232153Smm{
122232153Smm	const char *flist;
123232153Smm
124232153Smm	assertUmask(0);
125232153Smm	flist = make_files();
126228753Smm	/* Archive/dearchive with a variety of options. */
127232153Smm	run_tar("copy", "", "", flist);
128232153Smm	verify_files("copy");
129232153Smm
130232153Smm	run_tar("copy_ustar", "--format=ustar", "", flist);
131232153Smm	verify_files("copy_ustar");
132232153Smm
133228753Smm	/* tar doesn't handle cpio symlinks correctly */
134232153Smm	/* run_tar("copy_odc", "--format=odc", ""); */
135228753Smm}
136