2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 __FBSDID("$FreeBSD$");
33 static char strtab[] = "abcdefghijklmn.o/\nggghhhjjjrrrttt.o/\niiijjjdddsssppp.o/\n";
35 DEFINE_TEST(test_write_format_ar)
37 #if ARCHIVE_VERSION_STAMP < 1009000
38 skipping("ar write support");
40 struct archive_entry *ae;
45 * First we try to create a SVR4/GNU format archive.
47 assert((a = archive_write_new()) != NULL);
48 assertA(0 == archive_write_set_format_ar_svr4(a));
49 assertA(0 == archive_write_set_compression_gzip(a));
50 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
52 /* write the filename table */
53 assert((ae = archive_entry_new()) != NULL);
54 archive_entry_copy_pathname(ae, "//");
55 archive_entry_set_size(ae, strlen(strtab));
56 assertA(0 == archive_write_header(a, ae));
57 assertA(strlen(strtab) == (size_t)archive_write_data(a, strtab, strlen(strtab)));
58 archive_entry_free(ae);
61 assert((ae = archive_entry_new()) != NULL);
62 archive_entry_set_mtime(ae, 1, 0);
63 assert(1 == archive_entry_mtime(ae));
64 archive_entry_set_mode(ae, S_IFREG | 0755);
65 assert((S_IFREG | 0755) == archive_entry_mode(ae));
66 archive_entry_copy_pathname(ae, "abcdefghijklmn.o");
67 archive_entry_set_size(ae, 8);
68 assertA(0 == archive_write_header(a, ae));
69 assertA(8 == archive_write_data(a, "87654321", 15));
70 archive_entry_free(ae);
72 assert((ae = archive_entry_new()) != NULL);
73 archive_entry_copy_pathname(ae, "ggghhhjjjrrrttt.o");
74 archive_entry_set_filetype(ae, AE_IFREG);
75 archive_entry_set_size(ae, 7);
76 assertA(0 == archive_write_header(a, ae));
77 assertA(7 == archive_write_data(a, "7777777", 7));
78 archive_entry_free(ae);
80 /* test full pathname */
81 assert((ae = archive_entry_new()) != NULL);
82 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjjdddsssppp.o");
83 archive_entry_set_mode(ae, S_IFREG | 0755);
84 archive_entry_set_size(ae, 8);
85 assertA(0 == archive_write_header(a, ae));
86 assertA(8 == archive_write_data(a, "88877766", 8));
87 archive_entry_free(ae);
89 /* trailing "/" should be rejected */
90 assert((ae = archive_entry_new()) != NULL);
91 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjj/");
92 archive_entry_set_size(ae, 8);
93 assertA(0 != archive_write_header(a, ae));
94 archive_entry_free(ae);
96 /* Non regular file should be rejected */
97 assert((ae = archive_entry_new()) != NULL);
98 archive_entry_copy_pathname(ae, "gfgh.o");
99 archive_entry_set_mode(ae, S_IFDIR | 0755);
100 archive_entry_set_size(ae, 6);
101 assertA(0 != archive_write_header(a, ae));
102 archive_entry_free(ae);
104 archive_write_close(a);
105 #if ARCHIVE_API_VERSION > 1
106 assert(0 == archive_write_finish(a));
108 archive_write_finish(a);
112 * Now, read the data back.
114 assert((a = archive_read_new()) != NULL);
115 assertA(0 == archive_read_support_format_all(a));
116 assertA(0 == archive_read_support_compression_all(a));
117 assertA(0 == archive_read_open_memory(a, buff, used));
119 assertA(0 == archive_read_next_header(a, &ae));
120 assertEqualInt(0, archive_entry_mtime(ae));
121 assertEqualString("//", archive_entry_pathname(ae));
122 assertEqualInt(0, archive_entry_size(ae));
124 assertA(0 == archive_read_next_header(a, &ae));
125 assert(1 == archive_entry_mtime(ae));
126 assertEqualString("abcdefghijklmn.o", archive_entry_pathname(ae));
127 assert(8 == archive_entry_size(ae));
128 assertA(8 == archive_read_data(a, buff2, 10));
129 assert(0 == memcmp(buff2, "87654321", 8));
131 assert(0 == archive_read_next_header(a, &ae));
132 assertEqualString("ggghhhjjjrrrttt.o", archive_entry_pathname(ae));
133 assert(7 == archive_entry_size(ae));
134 assertA(7 == archive_read_data(a, buff2, 11));
135 assert(0 == memcmp(buff2, "7777777", 7));
137 assert(0 == archive_read_next_header(a, &ae));
138 assertEqualString("iiijjjdddsssppp.o", archive_entry_pathname(ae));
139 assert(8 == archive_entry_size(ae));
140 assertA(8 == archive_read_data(a, buff2, 17));
141 assert(0 == memcmp(buff2, "88877766", 8));
143 assert(0 == archive_read_close(a));
144 #if ARCHIVE_API_VERSION > 1
145 assert(0 == archive_read_finish(a));
147 archive_read_finish(a);
151 * Then, we try to create a BSD format archive.
153 memset(buff, 0, sizeof(buff));
154 assert((a = archive_write_new()) != NULL);
155 assertA(0 == archive_write_set_format_ar_bsd(a));
156 assertA(0 == archive_write_set_compression_bzip2(a));
157 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
159 /* write a entry need long name extension */
160 assert((ae = archive_entry_new()) != NULL);
161 archive_entry_copy_pathname(ae, "ttttyyyyuuuuiiii.o");
162 archive_entry_set_filetype(ae, AE_IFREG);
163 archive_entry_set_size(ae, 5);
164 assertA(0 == archive_write_header(a, ae));
165 assertA(5 == archive_entry_size(ae));
166 assertA(5 == archive_write_data(a, "12345", 7));
167 archive_entry_free(ae);
169 /* write a entry with a short name */
170 assert((ae = archive_entry_new()) != NULL);
171 archive_entry_copy_pathname(ae, "ttyy.o");
172 archive_entry_set_filetype(ae, AE_IFREG);
173 archive_entry_set_size(ae, 6);
174 assertA(0 == archive_write_header(a, ae));
175 assertA(6 == archive_write_data(a, "555555", 7));
176 archive_entry_free(ae);
177 archive_write_close(a);
178 #if ARCHIVE_API_VERSION > 1
179 assert(0 == archive_write_finish(a));
181 archive_write_finish(a);
184 /* Now, Read the data back */
185 assert((a = archive_read_new()) != NULL);
186 assertA(0 == archive_read_support_format_all(a));
187 assertA(0 == archive_read_support_compression_all(a));
188 assertA(0 == archive_read_open_memory(a, buff, used));
190 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
191 assertEqualString("ttttyyyyuuuuiiii.o", archive_entry_pathname(ae));
192 assertEqualInt(5, archive_entry_size(ae));
193 assertA(5 == archive_read_data(a, buff2, 10));
194 assert(0 == memcmp(buff2, "12345", 5));
196 assert(0 == archive_read_next_header(a, &ae));
197 assertEqualString("ttyy.o", archive_entry_pathname(ae));
198 assert(6 == archive_entry_size(ae));
199 assertA(6 == archive_read_data(a, buff2, 10));
200 assert(0 == memcmp(buff2, "555555", 6));
203 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
204 assert(0 == archive_read_close(a));
205 #if ARCHIVE_API_VERSION > 1
206 assert(0 == archive_read_finish(a));
208 archive_read_finish(a);