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 unsigned char strtab[] = "abcdefghijklmn.o/\nggghhhjjjrrrttt.o/\niiijjjdddsssppp.o/\n";
35 DEFINE_TEST(test_write_format_ar)
37 struct archive_entry *ae;
42 * First we try to create a SVR4/GNU format archive.
44 assert((a = archive_write_new()) != NULL);
45 assertA(0 == archive_write_set_format_ar_svr4(a));
46 assertA(0 == archive_write_set_compression_gzip(a));
47 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
49 /* write the filename table */
50 assert((ae = archive_entry_new()) != NULL);
51 archive_entry_copy_pathname(ae, "//");
52 archive_entry_set_size(ae, strlen(strtab));
53 assertA(0 == archive_write_header(a, ae));
54 assertA(strlen(strtab) == (size_t)archive_write_data(a, strtab, strlen(strtab)));
55 archive_entry_free(ae);
58 assert((ae = archive_entry_new()) != NULL);
59 archive_entry_set_mtime(ae, 1, 0);
60 assert(1 == archive_entry_mtime(ae));
61 archive_entry_set_mode(ae, S_IFREG | 0755);
62 assert((S_IFREG | 0755) == archive_entry_mode(ae));
63 archive_entry_copy_pathname(ae, "abcdefghijklmn.o");
64 archive_entry_set_size(ae, 8);
65 assertA(0 == archive_write_header(a, ae));
66 assertA(8 == archive_write_data(a, "87654321", 15));
67 archive_entry_free(ae);
69 assert((ae = archive_entry_new()) != NULL);
70 archive_entry_copy_pathname(ae, "ggghhhjjjrrrttt.o");
71 archive_entry_set_filetype(ae, AE_IFREG);
72 archive_entry_set_size(ae, 7);
73 assertA(0 == archive_write_header(a, ae));
74 assertA(7 == archive_write_data(a, "7777777", 7));
75 archive_entry_free(ae);
77 /* test full pathname */
78 assert((ae = archive_entry_new()) != NULL);
79 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjjdddsssppp.o");
80 archive_entry_set_mode(ae, S_IFREG | 0755);
81 archive_entry_set_size(ae, 8);
82 assertA(0 == archive_write_header(a, ae));
83 assertA(8 == archive_write_data(a, "88877766", 8));
84 archive_entry_free(ae);
86 /* trailing "/" should be rejected */
87 assert((ae = archive_entry_new()) != NULL);
88 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjj/");
89 archive_entry_set_size(ae, 8);
90 assertA(0 != archive_write_header(a, ae));
91 archive_entry_free(ae);
93 /* Non regular file should be rejected */
94 assert((ae = archive_entry_new()) != NULL);
95 archive_entry_copy_pathname(ae, "gfgh.o");
96 archive_entry_set_mode(ae, S_IFDIR | 0755);
97 archive_entry_set_size(ae, 6);
98 assertA(0 != archive_write_header(a, ae));
99 archive_entry_free(ae);
101 archive_write_close(a);
102 #if ARCHIVE_API_VERSION > 1
103 assert(0 == archive_write_finish(a));
105 archive_write_finish(a);
109 * Now, read the data back.
111 assert((a = archive_read_new()) != NULL);
112 assertA(0 == archive_read_support_format_all(a));
113 assertA(0 == archive_read_support_compression_all(a));
114 assertA(0 == archive_read_open_memory(a, buff, used));
116 assertA(0 == archive_read_next_header(a, &ae));
117 assertEqualInt(0, archive_entry_mtime(ae));
118 assertEqualString("//", archive_entry_pathname(ae));
119 assertEqualInt(strlen(strtab), archive_entry_size(ae));
120 assertEqualIntA(a, strlen(strtab), archive_read_data(a, buff2, 100));
121 assert(0 == memcmp(buff2, strtab, strlen(strtab)));
123 assertA(0 == archive_read_next_header(a, &ae));
124 assert(1 == archive_entry_mtime(ae));
125 assertEqualString("abcdefghijklmn.o", archive_entry_pathname(ae));
126 assert(8 == archive_entry_size(ae));
127 assertA(8 == archive_read_data(a, buff2, 10));
128 assert(0 == memcmp(buff2, "87654321", 8));
130 assert(0 == archive_read_next_header(a, &ae));
131 assertEqualString("ggghhhjjjrrrttt.o", archive_entry_pathname(ae));
132 assert(7 == archive_entry_size(ae));
133 assertA(7 == archive_read_data(a, buff2, 11));
134 assert(0 == memcmp(buff2, "7777777", 7));
136 assert(0 == archive_read_next_header(a, &ae));
137 assertEqualString("iiijjjdddsssppp.o", archive_entry_pathname(ae));
138 assert(8 == archive_entry_size(ae));
139 assertA(8 == archive_read_data(a, buff2, 17));
140 assert(0 == memcmp(buff2, "88877766", 8));
142 assert(0 == archive_read_close(a));
143 #if ARCHIVE_API_VERSION > 1
144 assert(0 == archive_read_finish(a));
146 archive_read_finish(a);
150 * Then, we try to create a BSD format archive.
152 memset(buff, 0, sizeof(buff));
153 assert((a = archive_write_new()) != NULL);
154 assertA(0 == archive_write_set_format_ar_bsd(a));
155 assertA(0 == archive_write_set_compression_bzip2(a));
156 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
158 /* write a entry need long name extension */
159 assert((ae = archive_entry_new()) != NULL);
160 archive_entry_copy_pathname(ae, "ttttyyyyuuuuiiii.o");
161 archive_entry_set_filetype(ae, AE_IFREG);
162 archive_entry_set_size(ae, 5);
163 assertA(0 == archive_write_header(a, ae));
164 assertA(5 == archive_write_data(a, "12345", 7));
165 archive_entry_free(ae);
167 /* write a entry with a short name */
168 assert((ae = archive_entry_new()) != NULL);
169 archive_entry_copy_pathname(ae, "ttyy.o");
170 archive_entry_set_filetype(ae, AE_IFREG);
171 archive_entry_set_size(ae, 6);
172 assertA(0 == archive_write_header(a, ae));
173 assertA(6 == archive_write_data(a, "555555", 7));
174 archive_entry_free(ae);
175 archive_write_close(a);
176 #if ARCHIVE_API_VERSION > 1
177 assert(0 == archive_write_finish(a));
179 archive_write_finish(a);
182 /* Now, Read the data back */
183 assert((a = archive_read_new()) != NULL);
184 assertA(0 == archive_read_support_format_all(a));
185 assertA(0 == archive_read_support_compression_all(a));
186 assertA(0 == archive_read_open_memory(a, buff, used));
188 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
189 assertEqualString("ttttyyyyuuuuiiii.o", archive_entry_pathname(ae));
190 assertEqualInt(5, archive_entry_size(ae));
191 assertA(5 == archive_read_data(a, buff2, 10));
192 assert(0 == memcmp(buff2, "12345", 5));
194 assert(0 == archive_read_next_header(a, &ae));
195 assertEqualString("ttyy.o", archive_entry_pathname(ae));
196 assert(6 == archive_entry_size(ae));
197 assertA(6 == archive_read_data(a, buff2, 10));
198 assert(0 == memcmp(buff2, "555555", 6));
201 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
202 assert(0 == archive_read_close(a));
203 #if ARCHIVE_API_VERSION > 1
204 assert(0 == archive_read_finish(a));
206 archive_read_finish(a);