2
* Copyright (c) 2003-2007 Tim Kientzle
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
26
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_extract.c 201247 2009-12-30 05:59:21Z kientzle $");
28
#define BUFF_SIZE 1000000
29
#define FILE_BUFF_SIZE 100000
31
DEFINE_TEST(test_read_extract)
33
struct archive_entry *ae;
36
int i, numEntries = 0;
37
char *buff, *file_buff;
39
buff = malloc(BUFF_SIZE);
40
file_buff = malloc(FILE_BUFF_SIZE);
42
/* Force the umask to something predictable. */
45
/* Create a new archive in memory containing various types of entries. */
46
assert((a = archive_write_new()) != NULL);
47
assertA(0 == archive_write_set_format_ustar(a));
48
assertA(0 == archive_write_set_compression_none(a));
49
assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
50
/* A directory to be restored with EXTRACT_PERM. */
52
assert((ae = archive_entry_new()) != NULL);
53
archive_entry_copy_pathname(ae, "dir_0775");
54
archive_entry_set_mode(ae, S_IFDIR | 0775);
55
assertA(0 == archive_write_header(a, ae));
56
archive_entry_free(ae);
59
assert((ae = archive_entry_new()) != NULL);
60
archive_entry_copy_pathname(ae, "file");
61
archive_entry_set_mode(ae, S_IFREG | 0755);
62
for (i = 0; i < FILE_BUFF_SIZE; i++)
63
file_buff[i] = (unsigned char)rand();
64
archive_entry_set_size(ae, FILE_BUFF_SIZE);
65
assertA(0 == archive_write_header(a, ae));
66
assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE));
67
archive_entry_free(ae);
68
/* A directory that should obey umask when restored. */
70
assert((ae = archive_entry_new()) != NULL);
71
archive_entry_copy_pathname(ae, "dir");
72
archive_entry_set_mode(ae, S_IFDIR | 0777);
73
assertA(0 == archive_write_header(a, ae));
74
archive_entry_free(ae);
75
/* A file in the directory. */
77
assert((ae = archive_entry_new()) != NULL);
78
archive_entry_copy_pathname(ae, "dir/file");
79
archive_entry_set_mode(ae, S_IFREG | 0700);
80
assertA(0 == archive_write_header(a, ae));
81
archive_entry_free(ae);
82
/* A file in a dir that is not already in the archive. */
84
assert((ae = archive_entry_new()) != NULL);
85
archive_entry_copy_pathname(ae, "dir2/file");
86
archive_entry_set_mode(ae, S_IFREG | 0000);
87
assertA(0 == archive_write_header(a, ae));
88
archive_entry_free(ae);
89
/* A dir with a trailing /. */
91
assert((ae = archive_entry_new()) != NULL);
92
archive_entry_copy_pathname(ae, "dir3/.");
93
archive_entry_set_mode(ae, S_IFDIR | 0710);
94
assertA(0 == archive_write_header(a, ae));
95
archive_entry_free(ae);
96
/* Multiple dirs with a single entry. */
98
assert((ae = archive_entry_new()) != NULL);
99
archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
100
archive_entry_set_mode(ae, S_IFDIR | 0711);
101
assertA(0 == archive_write_header(a, ae));
102
archive_entry_free(ae);
106
assert((ae = archive_entry_new()) != NULL);
107
archive_entry_copy_pathname(ae, "symlink");
108
archive_entry_set_mode(ae, AE_IFLNK | 0755);
109
archive_entry_set_symlink(ae, "file");
110
assertA(0 == archive_write_header(a, ae));
111
archive_entry_free(ae);
113
/* Close out the archive. */
114
assertA(0 == archive_write_close(a));
115
assertA(0 == archive_write_finish(a));
117
/* Extract the entries to disk. */
118
assert((a = archive_read_new()) != NULL);
119
assertA(0 == archive_read_support_format_all(a));
120
assertA(0 == archive_read_support_compression_all(a));
121
assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
122
/* Restore first entry with _EXTRACT_PERM. */
123
failure("Error reading first entry", i);
124
assertA(0 == archive_read_next_header(a, &ae));
125
assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
126
/* Rest of entries get restored with no flags. */
127
for (i = 1; i < numEntries; i++) {
128
failure("Error reading entry %d", i);
129
assertA(0 == archive_read_next_header(a, &ae));
130
failure("Failed to extract entry %d: %s", i,
131
archive_entry_pathname(ae));
132
assertA(0 == archive_read_extract(a, ae, 0));
134
assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
135
assert(0 == archive_read_close(a));
136
assert(0 == archive_read_finish(a));
138
/* Test the entries on disk. */
139
/* This first entry was extracted with ARCHIVE_EXTRACT_PERM,
140
* so the permissions should have been restored exactly,
141
* including resetting the gid bit on those platforms
142
* where gid is inherited by subdirs. */
143
failure("This was 0775 in archive, and should be 0775 on disk");
144
assertIsDir("dir_0775", 0775);
145
/* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
146
* so there may be some sloppiness about gid bits on directories. */
147
assertIsReg("file", 0755);
148
assertFileSize("file", FILE_BUFF_SIZE);
149
assertFileContents(file_buff, FILE_BUFF_SIZE, "file");
150
/* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
151
* when checking dir modes, as some systems inherit sgid bit
152
* from the parent dir. */
153
failure("This was 0777 in archive, but umask should make it 0755");
154
assertIsDir("dir", 0755);
155
assertIsReg("dir/file", 0700);
156
assertIsDir("dir2", 0755);
157
assertIsReg("dir2/file", 0000);
158
assertIsDir("dir3", 0710);
159
assertIsDir("dir4", 0755);
160
assertIsDir("dir4/a", 0755);
161
assertIsDir("dir4/b", 0755);
162
assertIsDir("dir4/c", 0711);
164
assertIsSymlink("symlink", "file");