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$");
27228753Smm
28228753Smm/*
29228753Smm * Background:  There are two written standards for the tar file format.
30228753Smm * The first is the POSIX 1988 "ustar" format, the second is the 2001
31228753Smm * "pax extended" format that builds on the "ustar" format by adding
32228753Smm * support for generic additional attributes.  Buried in the details
33228753Smm * is one frustrating incompatibility:  The 1988 standard says that
34228753Smm * tar readers MUST ignore the size field on hardlink entries; the
35228753Smm * 2001 standard says that tar readers MUST obey the size field on
36228753Smm * hardlink entries.  libarchive tries to navigate this particular
37228753Smm * minefield by using auto-detect logic to guess whether it should
38228753Smm * or should not obey the size field.
39228753Smm *
40228753Smm * This test tries to probe the boundaries of such handling; the test
41228753Smm * archives here were adapted from real archives created by real
42228753Smm * tar implementations that are (as of early 2008) apparently still
43228753Smm * in use.
44228753Smm */
45228753Smm
46228753Smmstatic void
47228753Smmtest_compat_tar_hardlink_1(void)
48228753Smm{
49228753Smm	char name[] = "test_compat_tar_hardlink_1.tar";
50228753Smm	struct archive_entry *ae;
51228753Smm	struct archive *a;
52228753Smm
53228753Smm	assert((a = archive_read_new()) != NULL);
54232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
55228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
56228753Smm	extract_reference_file(name);
57228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
58228753Smm
59228753Smm	/* Read first entry, which is a regular file. */
60228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
61228753Smm	assertEqualString("xmcd-3.3.2/docs_d/READMf",
62228753Smm		archive_entry_pathname(ae));
63228753Smm	assertEqualString(NULL, archive_entry_hardlink(ae));
64228753Smm	assertEqualInt(321, archive_entry_size(ae));
65228753Smm	assertEqualInt(1082575645, archive_entry_mtime(ae));
66228753Smm	assertEqualInt(1851, archive_entry_uid(ae));
67228753Smm	assertEqualInt(3, archive_entry_gid(ae));
68228753Smm	assertEqualInt(0100444, archive_entry_mode(ae));
69228753Smm
70228753Smm	/* Read second entry, which is a hard link at the end of archive. */
71228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
72228753Smm	assertEqualString("xmcd-3.3.2/README",
73228753Smm		archive_entry_pathname(ae));
74228753Smm	assertEqualString(
75228753Smm		"xmcd-3.3.2/docs_d/READMf",
76228753Smm		archive_entry_hardlink(ae));
77228753Smm	assertEqualInt(0, archive_entry_size(ae));
78228753Smm	assertEqualInt(1082575645, archive_entry_mtime(ae));
79228753Smm	assertEqualInt(1851, archive_entry_uid(ae));
80228753Smm	assertEqualInt(3, archive_entry_gid(ae));
81228753Smm	assertEqualInt(0100444, archive_entry_mode(ae));
82228753Smm
83228753Smm	/* Verify the end-of-archive. */
84228753Smm	/*
85228753Smm	 * This failed in libarchive 2.4.12 because the tar reader
86228753Smm	 * tried to obey the size field for the hard link and ended
87228753Smm	 * up running past the end of the file.
88228753Smm	 */
89228753Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
90228753Smm
91228753Smm	/* Verify that the format detection worked. */
92248616Smm	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_NONE);
93228753Smm	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR);
94228753Smm
95228753Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
96232153Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
97228753Smm}
98228753Smm
99228753SmmDEFINE_TEST(test_compat_tar_hardlink)
100228753Smm{
101228753Smm	test_compat_tar_hardlink_1();
102228753Smm}
103228753Smm
104228753Smm
105