]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test_read_extract.c
This commit was generated by cvs2svn to compensate for changes in r167612,
[FreeBSD/FreeBSD.git] / lib / 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         struct stat st;
36         ssize_t used;
37         int i;
38         char *buff, *file_buff;
39         int fd;
40         ssize_t bytes_read;
41
42         buff = malloc(BUFF_SIZE);
43         file_buff = malloc(FILE_BUFF_SIZE);
44
45         /* Force the umask to something predictable. */
46         umask(022);
47
48         /* Create a new archive in memory containing various types of entries. */
49         assert((a = archive_write_new()) != NULL);
50         assertA(0 == archive_write_set_format_ustar(a));
51         assertA(0 == archive_write_set_compression_none(a));
52         assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
53         /* A directory to be restored with EXTRACT_PERM. */
54         assert((ae = archive_entry_new()) != NULL);
55         archive_entry_copy_pathname(ae, "dir_0775");
56         archive_entry_set_mode(ae, S_IFDIR | 0775);
57         assertA(0 == archive_write_header(a, ae));
58         /* A regular file. */
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         assert((ae = archive_entry_new()) != NULL);
70         archive_entry_copy_pathname(ae, "dir");
71         archive_entry_set_mode(ae, S_IFDIR | 0777);
72         assertA(0 == archive_write_header(a, ae));
73         /* A file in the directory. */
74         assert((ae = archive_entry_new()) != NULL);
75         archive_entry_copy_pathname(ae, "dir/file");
76         archive_entry_set_mode(ae, S_IFREG | 0700);
77         assertA(0 == archive_write_header(a, ae));
78         /* A file in a dir that is not already in the archive. */
79         assert((ae = archive_entry_new()) != NULL);
80         archive_entry_copy_pathname(ae, "dir2/file");
81         archive_entry_set_mode(ae, S_IFREG | 0000);
82         assertA(0 == archive_write_header(a, ae));
83         /* A dir with a trailing /. */
84         assert((ae = archive_entry_new()) != NULL);
85         archive_entry_copy_pathname(ae, "dir3/.");
86         archive_entry_set_mode(ae, S_IFDIR | 0710);
87         assertA(0 == archive_write_header(a, ae));
88         /* Multiple dirs with a single entry. */
89         assert((ae = archive_entry_new()) != NULL);
90         archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
91         archive_entry_set_mode(ae, S_IFDIR | 0711);
92         assertA(0 == archive_write_header(a, ae));
93         /* A symlink. */
94         assert((ae = archive_entry_new()) != NULL);
95         archive_entry_copy_pathname(ae, "symlink");
96         archive_entry_set_mode(ae, S_IFLNK | 0755);
97         archive_entry_set_symlink(ae, "file");
98         assertA(0 == archive_write_header(a, ae));
99         /* Close out the archive. */
100         assertA(0 == archive_write_close(a));
101 #if ARCHIVE_API_VERSION > 1
102         assertA(0 == archive_write_finish(a));
103 #else
104         archive_write_finish(a);
105 #endif
106
107         /* Extract the entries to disk. */
108         assert((a = archive_read_new()) != NULL);
109         assertA(0 == archive_read_support_format_all(a));
110         assertA(0 == archive_read_support_compression_all(a));
111         assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
112         /* Restore first entry with _EXTRACT_PERM. */
113         failure("Error reading first entry", i);
114         assertA(0 == archive_read_next_header(a, &ae));
115         assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
116         /* Rest of entries get restored with no flags. */
117         for (i = 0; i < 7; i++) {
118                 failure("Error reading entry %d", i+1);
119                 assertA(0 == archive_read_next_header(a, &ae));
120                 assertA(0 == archive_read_extract(a, ae, 0));
121         }
122         assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
123         assert(0 == archive_read_close(a));
124 #if ARCHIVE_API_VERSION > 1
125         assert(0 == archive_read_finish(a));
126 #else
127         archive_read_finish(a);
128 #endif
129
130         /* Test the entries on disk. */
131         assert(0 == stat("dir_0775", &st));
132         failure("This was 0775 in archive, and should be 0775 on disk");
133         assert(st.st_mode == (S_IFDIR | 0775));
134         assert(0 == stat("file", &st));
135         failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755);
136         assert(st.st_mode == (S_IFREG | 0755));
137         failure("The file extracted to disk is the wrong size.");
138         assert(st.st_size == FILE_BUFF_SIZE);
139         fd = open("file", O_RDONLY);
140         failure("The file on disk could not be opened.");
141         assert(fd != 0);
142         bytes_read = read(fd, buff, FILE_BUFF_SIZE);
143         failure("The file contents read from disk are the wrong size");
144         assert(bytes_read == FILE_BUFF_SIZE);
145         failure("The file contents on disk do not match the file contents that were put into the archive.");
146         assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0);
147         assert(0 == stat("dir", &st));
148         failure("This was 0777 in archive, but umask should make it 0755");
149         assert(st.st_mode == (S_IFDIR | 0755));
150         assert(0 == stat("dir/file", &st));
151         assert(st.st_mode == (S_IFREG | 0700));
152         assert(0 == stat("dir2", &st));
153         assert(st.st_mode == (S_IFDIR | 0755));
154         assert(0 == stat("dir2/file", &st));
155         assert(st.st_mode == (S_IFREG | 0000));
156         assert(0 == stat("dir3", &st));
157         assert(st.st_mode == (S_IFDIR | 0710));
158         assert(0 == stat("dir4", &st));
159         assert(st.st_mode == (S_IFDIR | 0755));
160         assert(0 == stat("dir4/a", &st));
161         assert(st.st_mode == (S_IFDIR | 0755));
162         assert(0 == stat("dir4/b", &st));
163         assert(st.st_mode == (S_IFDIR | 0755));
164         assert(0 == stat("dir4/c", &st));
165         assert(st.st_mode == (S_IFDIR | 0711));
166         assert(0 == lstat("symlink", &st));
167         assert(S_ISLNK(st.st_mode));
168 #if HAVE_LCHMOD
169         /* Systems that lack lchmod() can't set symlink perms, so skip this. */
170         assert((st.st_mode & 07777) == 0755);
171 #endif
172         assert(0 == stat("symlink", &st));
173         assert(st.st_mode == (S_IFREG | 0755));
174
175         free(buff);
176         free(file_buff);
177 }