]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/test_write_format_cpio.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / test_write_format_cpio.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 /* The version stamp macro was introduced after cpio write support. */
29 #if ARCHIVE_VERSION_NUMBER >= 1009000
30 static void
31 test_format(int (*set_format)(struct archive *))
32 {
33         char filedata[64];
34         struct archive_entry *ae;
35         struct archive *a;
36         char *p;
37         size_t used;
38         size_t buffsize = 1000000;
39         char *buff;
40         int damaged = 0;
41
42         buff = malloc(buffsize);
43
44         /* Create a new archive in memory. */
45         assert((a = archive_write_new()) != NULL);
46         assertA(0 == (*set_format)(a));
47         assertA(0 == archive_write_set_compression_none(a));
48         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
49
50         /*
51          * Write a file to it.
52          */
53         assert((ae = archive_entry_new()) != NULL);
54         archive_entry_set_mtime(ae, 1, 10);
55         assert(1 == archive_entry_mtime(ae));
56         assert(10 == archive_entry_mtime_nsec(ae));
57         p = strdup("file");
58         archive_entry_copy_pathname(ae, p);
59         strcpy(p, "XXXX");
60         free(p);
61         assertEqualString("file", archive_entry_pathname(ae));
62         archive_entry_set_mode(ae, S_IFREG | 0755);
63         assert((S_IFREG | 0755) == archive_entry_mode(ae));
64         archive_entry_set_size(ae, 8);
65
66         assertA(0 == archive_write_header(a, ae));
67         archive_entry_free(ae);
68         assertA(8 == archive_write_data(a, "12345678", 9));
69
70         /*
71          * Write another file to it.
72          */
73         assert((ae = archive_entry_new()) != NULL);
74         archive_entry_set_mtime(ae, 1, 10);
75         assert(1 == archive_entry_mtime(ae));
76         assert(10 == archive_entry_mtime_nsec(ae));
77         p = strdup("file2");
78         archive_entry_copy_pathname(ae, p);
79         strcpy(p, "XXXX");
80         free(p);
81         assertEqualString("file2", archive_entry_pathname(ae));
82         archive_entry_set_mode(ae, S_IFREG | 0755);
83         assert((S_IFREG | 0755) == archive_entry_mode(ae));
84         archive_entry_set_size(ae, 4);
85
86         assertA(0 == archive_write_header(a, ae));
87         archive_entry_free(ae);
88         assertA(4 == archive_write_data(a, "1234", 5));
89
90         /*
91          * Write a directory to it.
92          */
93         assert((ae = archive_entry_new()) != NULL);
94         archive_entry_set_mtime(ae, 11, 110);
95         archive_entry_copy_pathname(ae, "dir");
96         archive_entry_set_mode(ae, S_IFDIR | 0755);
97         archive_entry_set_size(ae, 512);
98
99         assertA(0 == archive_write_header(a, ae));
100         assertEqualInt(0, archive_entry_size(ae));
101         archive_entry_free(ae);
102         assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
103
104
105         /* Close out the archive. */
106         assertA(0 == archive_write_close(a));
107 #if ARCHIVE_VERSION_NUMBER < 2000000
108         archive_write_finish(a);
109 #else
110         assertA(0 == archive_write_finish(a));
111 #endif
112
113         /*
114          * Damage the second entry to test the search-ahead recovery.
115          */
116         {
117                 int i;
118                 for (i = 80; i < 150; i++) {
119                         if (memcmp(buff + i, "07070", 5) == 0) {
120                                 damaged = 1;
121                                 buff[i] = 'X';
122                                 break;
123                         }
124                 }
125         }
126         failure("Unable to locate the second header for damage-recovery test.");
127         assert(damaged = 1);
128
129         /*
130          * Now, read the data back.
131          */
132         assert((a = archive_read_new()) != NULL);
133         assertA(0 == archive_read_support_format_all(a));
134         assertA(0 == archive_read_support_compression_all(a));
135         assertA(0 == archive_read_open_memory(a, buff, used));
136
137         if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) {
138                 archive_read_finish(a);
139                 return;
140         }
141
142         assertEqualInt(1, archive_entry_mtime(ae));
143         /* Not the same as above: cpio doesn't store hi-res times. */
144         assert(0 == archive_entry_mtime_nsec(ae));
145         assert(0 == archive_entry_atime(ae));
146         assert(0 == archive_entry_ctime(ae));
147         assertEqualString("file", archive_entry_pathname(ae));
148         assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
149         assertEqualInt(8, archive_entry_size(ae));
150         assertA(8 == archive_read_data(a, filedata, 10));
151         assert(0 == memcmp(filedata, "12345678", 8));
152
153         /*
154          * Read the second file back.
155          */
156         if (!damaged) {
157                 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
158                 assertEqualInt(1, archive_entry_mtime(ae));
159                 /* Not the same as above: cpio doesn't store hi-res times. */
160                 assert(0 == archive_entry_mtime_nsec(ae));
161                 assert(0 == archive_entry_atime(ae));
162                 assert(0 == archive_entry_ctime(ae));
163                 assertEqualString("file2", archive_entry_pathname(ae));
164                 assert((S_IFREG | 0755) == archive_entry_mode(ae));
165                 assertEqualInt(4, archive_entry_size(ae));
166                 assertEqualIntA(a, 4, archive_read_data(a, filedata, 10));
167                 assert(0 == memcmp(filedata, "1234", 4));
168         }
169
170         /*
171          * Read the dir entry back.
172          */
173         assertEqualIntA(a,
174             damaged ? ARCHIVE_WARN : ARCHIVE_OK,
175             archive_read_next_header(a, &ae));
176         assertEqualInt(11, archive_entry_mtime(ae));
177         assert(0 == archive_entry_mtime_nsec(ae));
178         assert(0 == archive_entry_atime(ae));
179         assert(0 == archive_entry_ctime(ae));
180         assertEqualString("dir", archive_entry_pathname(ae));
181         assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
182         assertEqualInt(0, archive_entry_size(ae));
183         assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
184
185         /* Verify the end of the archive. */
186         assertEqualIntA(a, 1, archive_read_next_header(a, &ae));
187         assert(0 == archive_read_close(a));
188 #if ARCHIVE_VERSION_NUMBER < 2000000
189         archive_read_finish(a);
190 #else
191         assert(0 == archive_read_finish(a));
192 #endif
193
194         free(buff);
195 }
196 #endif
197
198 DEFINE_TEST(test_write_format_cpio)
199 {
200 #if ARCHIVE_VERSION_NUMBER >= 1009000
201         test_format(archive_write_set_format_cpio);
202         test_format(archive_write_set_format_cpio_newc);
203 #else
204         skipping("cpio write support");
205 #endif
206 }