]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_format_cpio.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / 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          * TODO: Move the damage-recovery checking to a separate test;
116          * it doesn't really belong in this write test.
117          */
118         {
119                 int i;
120                 for (i = 80; i < 150; i++) {
121                         if (memcmp(buff + i, "07070", 5) == 0) {
122                                 damaged = 1;
123                                 buff[i] = 'X';
124                                 break;
125                         }
126                 }
127         }
128         failure("Unable to locate the second header for damage-recovery test.");
129         assert(damaged == 1);
130
131         /*
132          * Now, read the data back.
133          */
134         assert((a = archive_read_new()) != NULL);
135         assertA(0 == archive_read_support_format_all(a));
136         assertA(0 == archive_read_support_compression_all(a));
137         assertA(0 == archive_read_open_memory(a, buff, used));
138
139         if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) {
140                 archive_read_finish(a);
141                 return;
142         }
143
144         assertEqualInt(1, archive_entry_mtime(ae));
145         /* Not the same as above: cpio doesn't store hi-res times. */
146         assert(0 == archive_entry_mtime_nsec(ae));
147         assert(0 == archive_entry_atime(ae));
148         assert(0 == archive_entry_ctime(ae));
149         assertEqualString("file", archive_entry_pathname(ae));
150         assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
151         assertEqualInt(8, archive_entry_size(ae));
152         assertA(8 == archive_read_data(a, filedata, 10));
153         assert(0 == memcmp(filedata, "12345678", 8));
154
155         /*
156          * The second file can't be read because we damaged its header.
157          */
158
159         /*
160          * Read the dir entry back.
161          * ARCHIVE_WARN here because the damaged entry was skipped.
162          */
163         assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
164         assertEqualInt(11, archive_entry_mtime(ae));
165         assert(0 == archive_entry_mtime_nsec(ae));
166         assert(0 == archive_entry_atime(ae));
167         assert(0 == archive_entry_ctime(ae));
168         assertEqualString("dir", archive_entry_pathname(ae));
169         assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
170         assertEqualInt(0, archive_entry_size(ae));
171         assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
172
173         /* Verify the end of the archive. */
174         assertEqualIntA(a, 1, archive_read_next_header(a, &ae));
175         assert(0 == archive_read_close(a));
176 #if ARCHIVE_VERSION_NUMBER < 2000000
177         archive_read_finish(a);
178 #else
179         assert(0 == archive_read_finish(a));
180 #endif
181
182         free(buff);
183 }
184 #endif
185
186 DEFINE_TEST(test_write_format_cpio)
187 {
188 #if ARCHIVE_VERSION_NUMBER >= 1009000
189         test_format(archive_write_set_format_cpio);
190         test_format(archive_write_set_format_cpio_newc);
191 #else
192         skipping("cpio write support");
193 #endif
194 }