]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_write_disk_sparse.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_sparse.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 /*
29  * Write a file using archive_write_data call, read the file
30  * back and verify the contents.  The data written includes large
31  * blocks of nulls, so it should exercise the sparsification logic
32  * if ARCHIVE_EXTRACT_SPARSE is enabled.
33  */
34 static void
35 verify_write_data(struct archive *a, int sparse)
36 {
37         static const char data[]="abcdefghijklmnopqrstuvwxyz";
38         struct stat st;
39         struct archive_entry *ae;
40         size_t buff_size = 64 * 1024;
41         char *buff, *p;
42         const char *msg = sparse ? "sparse" : "non-sparse";
43         int fd;
44
45         buff = malloc(buff_size);
46         assert(buff != NULL);
47
48         ae = archive_entry_new();
49         assert(ae != NULL);
50         archive_entry_set_size(ae, 8 * buff_size);
51         archive_entry_set_pathname(ae, "test_write_data");
52         archive_entry_set_mode(ae, AE_IFREG | 0755);
53         assertEqualIntA(a, 0, archive_write_header(a, ae));
54
55         /* Use archive_write_data() to write three relatively sparse blocks. */
56
57         /* First has non-null data at beginning. */
58         memset(buff, 0, buff_size);
59         memcpy(buff, data, sizeof(data));
60         failure("%s", msg);
61         assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
62
63         /* Second has non-null data in the middle. */
64         memset(buff, 0, buff_size);
65         memcpy(buff + buff_size / 2 - 3, data, sizeof(data));
66         failure("%s", msg);
67         assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
68
69         /* Third has non-null data at the end. */
70         memset(buff, 0, buff_size);
71         memcpy(buff + buff_size - sizeof(data), data, sizeof(data));
72         failure("%s", msg);
73         assertEqualInt(buff_size, archive_write_data(a, buff, buff_size));
74
75         failure("%s", msg);
76         assertEqualIntA(a, 0, archive_write_finish_entry(a));
77
78         /* Test the entry on disk. */
79         assert(0 == stat(archive_entry_pathname(ae), &st));
80         assertEqualInt(st.st_size, 8 * buff_size);
81         fd = open(archive_entry_pathname(ae), O_RDONLY);
82         if (!assert(fd >= 0))
83                 return;
84
85         /* Check first block. */
86         assertEqualInt(buff_size, read(fd, buff, buff_size));
87         failure("%s", msg);
88         assertEqualMem(buff, data, sizeof(data));
89         for (p = buff + sizeof(data); p < buff + buff_size; ++p) {
90                 failure("offset: %d, %s", (int)(p - buff), msg);
91                 if (!assertEqualInt(0, *p))
92                         break;
93         }
94
95         /* Check second block. */
96         assertEqualInt(buff_size, read(fd, buff, buff_size));
97         for (p = buff; p < buff + buff_size; ++p) {
98                 failure("offset: %d, %s", (int)(p - buff), msg);
99                 if (p == buff + buff_size / 2 - 3) {
100                         assertEqualMem(p, data, sizeof(data));
101                         p += sizeof(data);
102                 } else if (!assertEqualInt(0, *p))
103                         break;
104         }
105
106         /* Check third block. */
107         assertEqualInt(buff_size, read(fd, buff, buff_size));
108         for (p = buff; p < buff + buff_size - sizeof(data); ++p) {
109                 failure("offset: %d, %s", (int)(p - buff), msg);
110                 if (!assertEqualInt(0, *p))
111                         break;
112         }
113         failure("%s", msg);
114         assertEqualMem(buff + buff_size - sizeof(data), data, sizeof(data));
115
116         /* XXX more XXX */
117
118         assertEqualInt(0, close(fd));
119         free(buff);
120 }
121
122 /*
123  * As above, but using the archive_write_data_block() call.
124  */
125 static void
126 verify_write_data_block(struct archive *a, int sparse)
127 {
128         static const char data[]="abcdefghijklmnopqrstuvwxyz";
129         struct stat st;
130         struct archive_entry *ae;
131         size_t buff_size = 64 * 1024;
132         char *buff, *p;
133         const char *msg = sparse ? "sparse" : "non-sparse";
134         int fd;
135
136         buff = malloc(buff_size);
137         assert(buff != NULL);
138
139         ae = archive_entry_new();
140         assert(ae != NULL);
141         archive_entry_set_size(ae, 8 * buff_size);
142         archive_entry_set_pathname(ae, "test_write_data_block");
143         archive_entry_set_mode(ae, AE_IFREG | 0755);
144         assertEqualIntA(a, 0, archive_write_header(a, ae));
145
146         /* Use archive_write_data_block() to write three
147            relatively sparse blocks. */
148
149         /* First has non-null data at beginning. */
150         memset(buff, 0, buff_size);
151         memcpy(buff, data, sizeof(data));
152         failure("%s", msg);
153         assertEqualInt(ARCHIVE_OK,
154             archive_write_data_block(a, buff, buff_size, 100));
155
156         /* Second has non-null data in the middle. */
157         memset(buff, 0, buff_size);
158         memcpy(buff + buff_size / 2 - 3, data, sizeof(data));
159         failure("%s", msg);
160         assertEqualInt(ARCHIVE_OK,
161             archive_write_data_block(a, buff, buff_size, buff_size + 200));
162
163         /* Third has non-null data at the end. */
164         memset(buff, 0, buff_size);
165         memcpy(buff + buff_size - sizeof(data), data, sizeof(data));
166         failure("%s", msg);
167         assertEqualInt(ARCHIVE_OK,
168             archive_write_data_block(a, buff, buff_size, buff_size * 2 + 300));
169
170         failure("%s", msg);
171         assertEqualIntA(a, 0, archive_write_finish_entry(a));
172
173         /* Test the entry on disk. */
174         assert(0 == stat(archive_entry_pathname(ae), &st));
175         assertEqualInt(st.st_size, 8 * buff_size);
176         fd = open(archive_entry_pathname(ae), O_RDONLY);
177         if (!assert(fd >= 0))
178                 return;
179
180         /* Check 100-byte gap at beginning */
181         assertEqualInt(100, read(fd, buff, 100));
182         failure("%s", msg);
183         for (p = buff; p < buff + 100; ++p) {
184                 failure("offset: %d, %s", (int)(p - buff), msg);
185                 if (!assertEqualInt(0, *p))
186                         break;
187         }
188
189         /* Check first block. */
190         assertEqualInt(buff_size, read(fd, buff, buff_size));
191         failure("%s", msg);
192         assertEqualMem(buff, data, sizeof(data));
193         for (p = buff + sizeof(data); p < buff + buff_size; ++p) {
194                 failure("offset: %d, %s", (int)(p - buff), msg);
195                 if (!assertEqualInt(0, *p))
196                         break;
197         }
198
199         /* Check 100-byte gap */
200         assertEqualInt(100, read(fd, buff, 100));
201         failure("%s", msg);
202         for (p = buff; p < buff + 100; ++p) {
203                 failure("offset: %d, %s", (int)(p - buff), msg);
204                 if (!assertEqualInt(0, *p))
205                         break;
206         }
207
208         /* Check second block. */
209         assertEqualInt(buff_size, read(fd, buff, buff_size));
210         for (p = buff; p < buff + buff_size; ++p) {
211                 failure("offset: %d, %s", (int)(p - buff), msg);
212                 if (p == buff + buff_size / 2 - 3) {
213                         assertEqualMem(p, data, sizeof(data));
214                         p += sizeof(data);
215                 } else if (!assertEqualInt(0, *p))
216                         break;
217         }
218
219         /* Check 100-byte gap */
220         assertEqualInt(100, read(fd, buff, 100));
221         failure("%s", msg);
222         for (p = buff; p < buff + 100; ++p) {
223                 failure("offset: %d, %s", (int)(p - buff), msg);
224                 if (!assertEqualInt(0, *p))
225                         break;
226         }
227
228         /* Check third block. */
229         assertEqualInt(buff_size, read(fd, buff, buff_size));
230         for (p = buff; p < buff + buff_size - sizeof(data); ++p) {
231                 failure("offset: %d, %s", (int)(p - buff), msg);
232                 if (!assertEqualInt(0, *p))
233                         break;
234         }
235         failure("%s", msg);
236         assertEqualMem(buff + buff_size - sizeof(data), data, sizeof(data));
237
238         /* Check another block size beyond last we wrote. */
239         assertEqualInt(buff_size, read(fd, buff, buff_size));
240         failure("%s", msg);
241         for (p = buff; p < buff + buff_size; ++p) {
242                 failure("offset: %d, %s", (int)(p - buff), msg);
243                 if (!assertEqualInt(0, *p))
244                         break;
245         }
246
247
248         /* XXX more XXX */
249
250         assertEqualInt(0, close(fd));
251         free(buff);
252 }
253
254 DEFINE_TEST(test_write_disk_sparse)
255 {
256         struct archive *ad;
257
258
259         /*
260          * The return values, etc, of the write data functions
261          * shouldn't change regardless of whether we've requested
262          * sparsification.  (The performance and pattern of actual
263          * write calls to the disk should vary, of course, but the
264          * client program shouldn't see any difference.)
265          */
266         assert((ad = archive_write_disk_new()) != NULL);
267         archive_write_disk_set_options(ad, 0);
268         verify_write_data(ad, 0);
269         verify_write_data_block(ad, 0);
270         assertEqualInt(0, archive_write_finish(ad));
271
272         assert((ad = archive_write_disk_new()) != NULL);
273         archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_SPARSE);
274         verify_write_data(ad, 1);
275         verify_write_data_block(ad, 1);
276         assertEqualInt(0, archive_write_finish(ad));
277
278 }