]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_write_disk.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libarchive / test / test_write_disk.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 #if ARCHIVE_VERSION_STAMP >= 1009000
29
30 #define UMASK 022
31
32 static void create(struct archive_entry *ae, const char *msg)
33 {
34         struct archive *ad;
35         struct stat st;
36
37         /* Write the entry to disk. */
38         assert((ad = archive_write_disk_new()) != NULL);
39         failure("%s", msg);
40         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
41         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
42 #if ARCHIVE_API_VERSION > 1
43         assertEqualInt(0, archive_write_finish(ad));
44 #else
45         archive_write_finish(ad);
46 #endif
47         /* Test the entries on disk. */
48         assert(0 == stat(archive_entry_pathname(ae), &st));
49         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
50             st.st_mode, archive_entry_mode(ae));
51         /* When verifying a dir, ignore the S_ISGID bit, as some systems set
52          * that automatically. */
53         if (archive_entry_filetype(ae) == AE_IFDIR)
54                 st.st_mode &= ~S_ISGID;
55         assertEqualInt(st.st_mode, archive_entry_mode(ae) & ~UMASK);
56 }
57
58 static void create_reg_file(struct archive_entry *ae, const char *msg)
59 {
60         static const char data[]="abcdefghijklmnopqrstuvwxyz";
61         struct archive *ad;
62         struct stat st;
63         time_t now;
64
65         /* Write the entry to disk. */
66         assert((ad = archive_write_disk_new()) != NULL);
67         archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_TIME);
68         failure("%s", msg);
69         /*
70          * A touchy API design issue: archive_write_data() does (as of
71          * 2.4.12) enforce the entry size as a limit on the data
72          * written to the file.  This was not enforced prior to
73          * 2.4.12.  The change was prompted by the refined
74          * hardlink-restore semantics introduced at that time.  In
75          * short, libarchive needs to know whether a "hardlink entry"
76          * is going to overwrite the contents so that it can know
77          * whether or not to open the file for writing.  This implies
78          * that there is a fundamental semantic difference between an
79          * entry with a zero size and one with a non-zero size in the
80          * case of hardlinks and treating the hardlink case
81          * differently from the regular file case is just asking for
82          * trouble.  So, a zero size must always mean that no data
83          * will be accepted, which is consistent with the file size in
84          * the entry being a maximum size.
85          */
86         archive_entry_set_size(ae, sizeof(data));
87         archive_entry_set_mtime(ae, 123456789, 0);
88         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
89         assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
90         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
91 #if ARCHIVE_API_VERSION > 1
92         assertEqualInt(0, archive_write_finish(ad));
93 #else
94         archive_write_finish(ad);
95 #endif
96         /* Test the entries on disk. */
97         assert(0 == stat(archive_entry_pathname(ae), &st));
98         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
99             st.st_mode, archive_entry_mode(ae));
100         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
101         assertEqualInt(st.st_size, sizeof(data));
102         assertEqualInt(st.st_mtime, 123456789);
103         failure("No atime was specified, so atime should get set to current time");
104         now = time(NULL);
105         assert(st.st_atime <= now && st.st_atime > now - 5);
106 }
107
108 static void create_reg_file2(struct archive_entry *ae, const char *msg)
109 {
110         const int datasize = 100000;
111         char *data;
112         char *compare;
113         struct archive *ad;
114         struct stat st;
115         int i, fd;
116
117         data = malloc(datasize);
118         for (i = 0; i < datasize; i++)
119                 data[i] = (char)(i % 256);
120
121         /* Write the entry to disk. */
122         assert((ad = archive_write_disk_new()) != NULL);
123         failure("%s", msg);
124         /*
125          * See above for an explanation why this next call
126          * is necessary.
127          */
128         archive_entry_set_size(ae, datasize);
129         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
130         for (i = 0; i < datasize - 999; i += 1000) {
131                 assertEqualIntA(ad, ARCHIVE_OK,
132                     archive_write_data_block(ad, data + i, 1000, i));
133         }
134         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
135 #if ARCHIVE_API_VERSION > 1
136         assertEqualInt(0, archive_write_finish(ad));
137 #else
138         archive_write_finish(ad);
139 #endif
140         /* Test the entries on disk. */
141         assert(0 == stat(archive_entry_pathname(ae), &st));
142         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
143             st.st_mode, archive_entry_mode(ae));
144         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
145         assertEqualInt(st.st_size, i);
146
147         compare = malloc(datasize);
148         fd = open(archive_entry_pathname(ae), O_RDONLY);
149         assertEqualInt(datasize, read(fd, compare, datasize));
150         close(fd);
151         assert(memcmp(compare, data, datasize) == 0);
152         free(compare);
153         free(data);
154 }
155
156 static void create_reg_file3(struct archive_entry *ae, const char *msg)
157 {
158         static const char data[]="abcdefghijklmnopqrstuvwxyz";
159         struct archive *ad;
160         struct stat st;
161
162         /* Write the entry to disk. */
163         assert((ad = archive_write_disk_new()) != NULL);
164         failure("%s", msg);
165         /* Set the size smaller than the data and verify the truncation. */
166         archive_entry_set_size(ae, 5);
167         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
168         assertEqualInt(5, archive_write_data(ad, data, sizeof(data)));
169         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
170 #if ARCHIVE_VERSION_NUMBER < 2000000
171         archive_write_finish(ad);
172 #else
173         assertEqualInt(0, archive_write_finish(ad));
174 #endif
175         /* Test the entry on disk. */
176         assert(0 == stat(archive_entry_pathname(ae), &st));
177         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
178             st.st_mode, archive_entry_mode(ae));
179         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
180         assertEqualInt(st.st_size, 5);
181 }
182
183
184 static void create_reg_file4(struct archive_entry *ae, const char *msg)
185 {
186         static const char data[]="abcdefghijklmnopqrstuvwxyz";
187         struct archive *ad;
188         struct stat st;
189
190         /* Write the entry to disk. */
191         assert((ad = archive_write_disk_new()) != NULL);
192         /* Leave the size unset.  The data should not be truncated. */
193         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
194         assertEqualInt(ARCHIVE_OK,
195             archive_write_data_block(ad, data, sizeof(data), 0));
196         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
197 #if ARCHIVE_VERSION_NUMBER < 2000000
198         archive_write_finish(ad);
199 #else
200         assertEqualInt(0, archive_write_finish(ad));
201 #endif
202         /* Test the entry on disk. */
203         assert(0 == stat(archive_entry_pathname(ae), &st));
204         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
205             st.st_mode, archive_entry_mode(ae));
206         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
207         failure(msg);
208         assertEqualInt(st.st_size, sizeof(data));
209 }
210 #endif
211
212 DEFINE_TEST(test_write_disk)
213 {
214 #if ARCHIVE_VERSION_STAMP < 1009000
215         skipping("archive_write_disk interface");
216 #else
217         struct archive_entry *ae;
218
219         /* Force the umask to something predictable. */
220         umask(UMASK);
221
222         /* A regular file. */
223         assert((ae = archive_entry_new()) != NULL);
224         archive_entry_copy_pathname(ae, "file");
225         archive_entry_set_mode(ae, S_IFREG | 0755);
226         create_reg_file(ae, "Test creating a regular file");
227         archive_entry_free(ae);
228
229         /* Another regular file. */
230         assert((ae = archive_entry_new()) != NULL);
231         archive_entry_copy_pathname(ae, "file2");
232         archive_entry_set_mode(ae, S_IFREG | 0755);
233         create_reg_file2(ae, "Test creating another regular file");
234         archive_entry_free(ae);
235
236         /* A regular file with a size restriction */
237         assert((ae = archive_entry_new()) != NULL);
238         archive_entry_copy_pathname(ae, "file3");
239         archive_entry_set_mode(ae, S_IFREG | 0755);
240         create_reg_file3(ae, "Regular file with size restriction");
241         archive_entry_free(ae);
242
243         /* A regular file with an unspecified size */
244         assert((ae = archive_entry_new()) != NULL);
245         archive_entry_copy_pathname(ae, "file3");
246         archive_entry_set_mode(ae, S_IFREG | 0755);
247         create_reg_file4(ae, "Regular file with unspecified size");
248         archive_entry_free(ae);
249
250         /* A regular file over an existing file */
251         assert((ae = archive_entry_new()) != NULL);
252         archive_entry_copy_pathname(ae, "file");
253         archive_entry_set_mode(ae, S_IFREG | 0724);
254         create(ae, "Test creating a file over an existing file.");
255         archive_entry_free(ae);
256
257         /* A directory. */
258         assert((ae = archive_entry_new()) != NULL);
259         archive_entry_copy_pathname(ae, "dir");
260         archive_entry_set_mode(ae, S_IFDIR | 0555);
261         create(ae, "Test creating a regular dir.");
262         archive_entry_free(ae);
263
264         /* A directory over an existing file. */
265         assert((ae = archive_entry_new()) != NULL);
266         archive_entry_copy_pathname(ae, "file");
267         archive_entry_set_mode(ae, S_IFDIR | 0742);
268         create(ae, "Test creating a dir over an existing file.");
269         archive_entry_free(ae);
270
271         /* A file over an existing dir. */
272         assert((ae = archive_entry_new()) != NULL);
273         archive_entry_copy_pathname(ae, "file");
274         archive_entry_set_mode(ae, S_IFREG | 0744);
275         create(ae, "Test creating a file over an existing dir.");
276         archive_entry_free(ae);
277 #endif
278 }