]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_read_extract.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_read_extract.c
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$");
27
28 #define BUFF_SIZE 1000000
29 #define FILE_BUFF_SIZE 100000
30
31 DEFINE_TEST(test_read_extract)
32 {
33         struct archive_entry *ae;
34         struct archive *a;
35         size_t used;
36         int i, numEntries = 0;
37         char *buff, *file_buff;
38
39         buff = malloc(BUFF_SIZE);
40         file_buff = malloc(FILE_BUFF_SIZE);
41
42         /* Force the umask to something predictable. */
43         assertUmask(022);
44
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_add_filter_none(a));
49         assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
50         /* A directory to be restored with EXTRACT_PERM. */
51         ++numEntries;
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);
57         /* A regular file. */
58         ++numEntries;
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. */
69         ++numEntries;
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. */
76         ++numEntries;
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. */
83         ++numEntries;
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 /. */
90         ++numEntries;
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. */
97         ++numEntries;
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);
103         /* A symlink. */
104         if (canSymlink()) {
105                 ++numEntries;
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);
112         }
113         /* Close out the archive. */
114         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
115         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
116
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_filter_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");
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));
133         }
134         assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
135         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
136         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
137
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);
163         if (canSymlink())
164                 assertIsSymlink("symlink", "file", 0);
165
166         free(buff);
167         free(file_buff);
168 }