]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test_write_disk.c
Fix style.
[FreeBSD/FreeBSD.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_NUMBER >= 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_VERSION_NUMBER < 2000000
43         archive_write_finish(ad);
44 #else
45         assertEqualInt(0, 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_VERSION_NUMBER < 2000000
92         archive_write_finish(ad);
93 #else
94         assertEqualInt(0, 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         /* test_write_disk_times has more detailed tests of this area. */
103         assertEqualInt(st.st_mtime, 123456789);
104         failure("No atime was specified, so atime should get set to current time");
105         now = time(NULL);
106         assert(st.st_atime <= now && st.st_atime > now - 5);
107 }
108
109 static void create_reg_file2(struct archive_entry *ae, const char *msg)
110 {
111         const int datasize = 100000;
112         char *data;
113         char *compare;
114         struct archive *ad;
115         struct stat st;
116         int i, fd;
117
118         data = malloc(datasize);
119         for (i = 0; i < datasize; i++)
120                 data[i] = (char)(i % 256);
121
122         /* Write the entry to disk. */
123         assert((ad = archive_write_disk_new()) != NULL);
124         failure("%s", msg);
125         /*
126          * See above for an explanation why this next call
127          * is necessary.
128          */
129         archive_entry_set_size(ae, datasize);
130         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
131         for (i = 0; i < datasize - 999; i += 1000) {
132                 assertEqualIntA(ad, ARCHIVE_OK,
133                     archive_write_data_block(ad, data + i, 1000, i));
134         }
135         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
136 #if ARCHIVE_VERSION_NUMBER < 2000000
137         archive_write_finish(ad);
138 #else
139         assertEqualInt(0, archive_write_finish(ad));
140 #endif
141         /* Test the entries on disk. */
142         assert(0 == stat(archive_entry_pathname(ae), &st));
143         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
144             st.st_mode, archive_entry_mode(ae));
145         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
146         assertEqualInt(st.st_size, i);
147
148         compare = malloc(datasize);
149         fd = open(archive_entry_pathname(ae), O_RDONLY);
150         assertEqualInt(datasize, read(fd, compare, datasize));
151         close(fd);
152         assert(memcmp(compare, data, datasize) == 0);
153         free(compare);
154         free(data);
155 }
156
157 static void create_reg_file3(struct archive_entry *ae, const char *msg)
158 {
159         static const char data[]="abcdefghijklmnopqrstuvwxyz";
160         struct archive *ad;
161         struct stat st;
162
163         /* Write the entry to disk. */
164         assert((ad = archive_write_disk_new()) != NULL);
165         failure("%s", msg);
166         /* Set the size smaller than the data and verify the truncation. */
167         archive_entry_set_size(ae, 5);
168         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
169         assertEqualInt(5, archive_write_data(ad, data, sizeof(data)));
170         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
171 #if ARCHIVE_VERSION_NUMBER < 2000000
172         archive_write_finish(ad);
173 #else
174         assertEqualInt(0, archive_write_finish(ad));
175 #endif
176         /* Test the entry on disk. */
177         assert(0 == stat(archive_entry_pathname(ae), &st));
178         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
179             st.st_mode, archive_entry_mode(ae));
180         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
181         assertEqualInt(st.st_size, 5);
182 }
183
184
185 static void create_reg_file4(struct archive_entry *ae, const char *msg)
186 {
187         static const char data[]="abcdefghijklmnopqrstuvwxyz";
188         struct archive *ad;
189         struct stat st;
190
191         /* Write the entry to disk. */
192         assert((ad = archive_write_disk_new()) != NULL);
193         /* Leave the size unset.  The data should not be truncated. */
194         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
195         assertEqualInt(ARCHIVE_OK,
196             archive_write_data_block(ad, data, sizeof(data), 0));
197         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
198 #if ARCHIVE_VERSION_NUMBER < 2000000
199         archive_write_finish(ad);
200 #else
201         assertEqualInt(0, archive_write_finish(ad));
202 #endif
203         /* Test the entry on disk. */
204         assert(0 == stat(archive_entry_pathname(ae), &st));
205         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
206             st.st_mode, archive_entry_mode(ae));
207         assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
208         failure(msg);
209         assertEqualInt(st.st_size, sizeof(data));
210 }
211 #endif
212
213 DEFINE_TEST(test_write_disk)
214 {
215 #if ARCHIVE_VERSION_NUMBER < 1009000
216         skipping("archive_write_disk interface");
217 #else
218         struct archive_entry *ae;
219
220         /* Force the umask to something predictable. */
221         umask(UMASK);
222
223         /* A regular file. */
224         assert((ae = archive_entry_new()) != NULL);
225         archive_entry_copy_pathname(ae, "file");
226         archive_entry_set_mode(ae, S_IFREG | 0755);
227         create_reg_file(ae, "Test creating a regular file");
228         archive_entry_free(ae);
229
230         /* Another regular file. */
231         assert((ae = archive_entry_new()) != NULL);
232         archive_entry_copy_pathname(ae, "file2");
233         archive_entry_set_mode(ae, S_IFREG | 0755);
234         create_reg_file2(ae, "Test creating another regular file");
235         archive_entry_free(ae);
236
237         /* A regular file with a size restriction */
238         assert((ae = archive_entry_new()) != NULL);
239         archive_entry_copy_pathname(ae, "file3");
240         archive_entry_set_mode(ae, S_IFREG | 0755);
241         create_reg_file3(ae, "Regular file with size restriction");
242         archive_entry_free(ae);
243
244         /* A regular file with an unspecified size */
245         assert((ae = archive_entry_new()) != NULL);
246         archive_entry_copy_pathname(ae, "file3");
247         archive_entry_set_mode(ae, S_IFREG | 0755);
248         create_reg_file4(ae, "Regular file with unspecified size");
249         archive_entry_free(ae);
250
251         /* A regular file over an existing file */
252         assert((ae = archive_entry_new()) != NULL);
253         archive_entry_copy_pathname(ae, "file");
254         archive_entry_set_mode(ae, S_IFREG | 0724);
255         create(ae, "Test creating a file over an existing file.");
256         archive_entry_free(ae);
257
258         /* A directory. */
259         assert((ae = archive_entry_new()) != NULL);
260         archive_entry_copy_pathname(ae, "dir");
261         archive_entry_set_mode(ae, S_IFDIR | 0555);
262         create(ae, "Test creating a regular dir.");
263         archive_entry_free(ae);
264
265         /* A directory over an existing file. */
266         assert((ae = archive_entry_new()) != NULL);
267         archive_entry_copy_pathname(ae, "file");
268         archive_entry_set_mode(ae, S_IFDIR | 0742);
269         create(ae, "Test creating a dir over an existing file.");
270         archive_entry_free(ae);
271
272         /* A file over an existing dir. */
273         assert((ae = archive_entry_new()) != NULL);
274         archive_entry_copy_pathname(ae, "file");
275         archive_entry_set_mode(ae, S_IFREG | 0744);
276         create(ae, "Test creating a file over an existing dir.");
277         archive_entry_free(ae);
278 #endif
279 }