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"
26229592Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753Smm/*
29228753Smm * Exercise symlink recreation.
30228753Smm */
31228753SmmDEFINE_TEST(test_write_disk_symlink)
32228753Smm{
33228753Smm	static const char data[]="abcdefghijklmnopqrstuvwxyz";
34228753Smm	struct archive *ad;
35228753Smm	struct archive_entry *ae;
36228753Smm	int r;
37228753Smm
38228753Smm	if (!canSymlink()) {
39228753Smm		skipping("Symlinks not supported");
40228753Smm		return;
41228753Smm	}
42228753Smm
43228753Smm	/* Write entries to disk. */
44228753Smm	assert((ad = archive_write_disk_new()) != NULL);
45228753Smm
46228753Smm	/*
47228753Smm	 * First, create a regular file then a symlink to that file.
48228753Smm	 */
49228753Smm
50228753Smm	/* Regular file: link1a */
51228753Smm	assert((ae = archive_entry_new()) != NULL);
52228753Smm	archive_entry_copy_pathname(ae, "link1a");
53228753Smm	archive_entry_set_mode(ae, AE_IFREG | 0755);
54228753Smm	archive_entry_set_size(ae, sizeof(data));
55228753Smm	assertEqualIntA(ad, 0, archive_write_header(ad, ae));
56228753Smm	assertEqualInt(sizeof(data),
57228753Smm	    archive_write_data(ad, data, sizeof(data)));
58228753Smm	assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
59228753Smm	archive_entry_free(ae);
60228753Smm
61228753Smm	/* Symbolic Link: link1b -> link1a */
62228753Smm	assert((ae = archive_entry_new()) != NULL);
63228753Smm	archive_entry_copy_pathname(ae, "link1b");
64228753Smm	archive_entry_set_mode(ae, AE_IFLNK | 0642);
65228753Smm	archive_entry_set_size(ae, 0);
66228753Smm	archive_entry_copy_symlink(ae, "link1a");
67228753Smm	assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
68228753Smm	if (r >= ARCHIVE_WARN)
69228753Smm		assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
70228753Smm	archive_entry_free(ae);
71228753Smm
72228753Smm	/*
73228753Smm	 * We should be able to do this in the other order as well,
74228753Smm	 * of course.
75228753Smm	 */
76228753Smm
77228753Smm	/* Symbolic link: link2b -> link2a */
78228753Smm	assert((ae = archive_entry_new()) != NULL);
79228753Smm	archive_entry_copy_pathname(ae, "link2b");
80228753Smm	archive_entry_set_mode(ae, AE_IFLNK | 0642);
81228753Smm	archive_entry_unset_size(ae);
82228753Smm	archive_entry_copy_symlink(ae, "link2a");
83228753Smm	assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
84228753Smm	if (r >= ARCHIVE_WARN) {
85228753Smm		assertEqualInt(ARCHIVE_WARN,
86228753Smm		    archive_write_data(ad, data, sizeof(data)));
87228753Smm		assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
88228753Smm	}
89228753Smm	archive_entry_free(ae);
90228753Smm
91228753Smm	/* File: link2a */
92228753Smm	assert((ae = archive_entry_new()) != NULL);
93228753Smm	archive_entry_copy_pathname(ae, "link2a");
94228753Smm	archive_entry_set_mode(ae, AE_IFREG | 0755);
95228753Smm	archive_entry_set_size(ae, sizeof(data));
96228753Smm	assertEqualIntA(ad, 0, archive_write_header(ad, ae));
97228753Smm	assertEqualInt(sizeof(data),
98228753Smm	    archive_write_data(ad, data, sizeof(data)));
99228753Smm	assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
100228753Smm	archive_entry_free(ae);
101228753Smm
102228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish(ad));
103228753Smm
104228753Smm	/* Test the entries on disk. */
105228753Smm
106228753Smm	/* Test #1 */
107228753Smm	assertIsReg("link1a", -1);
108228753Smm	assertFileSize("link1a", sizeof(data));
109228753Smm	assertFileNLinks("link1a", 1);
110228753Smm	assertIsSymlink("link1b", "link1a");
111228753Smm
112228753Smm	/* Test #2: Should produce identical results to test #1 */
113228753Smm	assertIsReg("link2a", -1);
114228753Smm	assertFileSize("link2a", sizeof(data));
115228753Smm	assertFileNLinks("link2a", 1);
116228753Smm	assertIsSymlink("link2b", "link2a");
117228753Smm}
118