test_write_disk_symlink.c revision 228753
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: head/lib/libarchive/test/test_write_disk_symlink.c 201247 2009-12-30 05:59:21Z kientzle $");
27
28/*
29 * Exercise symlink recreation.
30 */
31DEFINE_TEST(test_write_disk_symlink)
32{
33	static const char data[]="abcdefghijklmnopqrstuvwxyz";
34	struct archive *ad;
35	struct archive_entry *ae;
36	int r;
37
38	if (!canSymlink()) {
39		skipping("Symlinks not supported");
40		return;
41	}
42
43	/* Write entries to disk. */
44	assert((ad = archive_write_disk_new()) != NULL);
45
46	/*
47	 * First, create a regular file then a symlink to that file.
48	 */
49
50	/* Regular file: link1a */
51	assert((ae = archive_entry_new()) != NULL);
52	archive_entry_copy_pathname(ae, "link1a");
53	archive_entry_set_mode(ae, AE_IFREG | 0755);
54	archive_entry_set_size(ae, sizeof(data));
55	assertEqualIntA(ad, 0, archive_write_header(ad, ae));
56	assertEqualInt(sizeof(data),
57	    archive_write_data(ad, data, sizeof(data)));
58	assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
59	archive_entry_free(ae);
60
61	/* Symbolic Link: link1b -> link1a */
62	assert((ae = archive_entry_new()) != NULL);
63	archive_entry_copy_pathname(ae, "link1b");
64	archive_entry_set_mode(ae, AE_IFLNK | 0642);
65	archive_entry_set_size(ae, 0);
66	archive_entry_copy_symlink(ae, "link1a");
67	assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
68	if (r >= ARCHIVE_WARN)
69		assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
70	archive_entry_free(ae);
71
72	/*
73	 * We should be able to do this in the other order as well,
74	 * of course.
75	 */
76
77	/* Symbolic link: link2b -> link2a */
78	assert((ae = archive_entry_new()) != NULL);
79	archive_entry_copy_pathname(ae, "link2b");
80	archive_entry_set_mode(ae, AE_IFLNK | 0642);
81	archive_entry_unset_size(ae);
82	archive_entry_copy_symlink(ae, "link2a");
83	assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
84	if (r >= ARCHIVE_WARN) {
85		assertEqualInt(ARCHIVE_WARN,
86		    archive_write_data(ad, data, sizeof(data)));
87		assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
88	}
89	archive_entry_free(ae);
90
91	/* File: link2a */
92	assert((ae = archive_entry_new()) != NULL);
93	archive_entry_copy_pathname(ae, "link2a");
94	archive_entry_set_mode(ae, AE_IFREG | 0755);
95	archive_entry_set_size(ae, sizeof(data));
96	assertEqualIntA(ad, 0, archive_write_header(ad, ae));
97	assertEqualInt(sizeof(data),
98	    archive_write_data(ad, data, sizeof(data)));
99	assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
100	archive_entry_free(ae);
101
102	assertEqualInt(ARCHIVE_OK, archive_write_finish(ad));
103
104	/* Test the entries on disk. */
105
106	/* Test #1 */
107	assertIsReg("link1a", -1);
108	assertFileSize("link1a", sizeof(data));
109	assertFileNLinks("link1a", 1);
110	assertIsSymlink("link1b", "link1a");
111
112	/* Test #2: Should produce identical results to test #1 */
113	assertIsReg("link2a", -1);
114	assertFileSize("link2a", sizeof(data));
115	assertFileNLinks("link2a", 1);
116	assertIsSymlink("link2b", "link2a");
117}
118