1 /*-
2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
15 *
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.
26 */
27
28 #include "test.h"
29
30 static char buff[4096];
31 static char buff2[64];
32 static char strtab[] = "abcdefghijklmn.o/\nggghhhjjjrrrttt.o/\niiijjjdddsssppp.o/\n";
33
DEFINE_TEST(test_write_format_ar)34 DEFINE_TEST(test_write_format_ar)
35 {
36 struct archive_entry *ae;
37 struct archive* a;
38 size_t used;
39
40 /*
41 * First we try to create a SVR4/GNU format archive.
42 */
43 assert((a = archive_write_new()) != NULL);
44 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ar_svr4(a));
45 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
46
47 /* write the filename table */
48 assert((ae = archive_entry_new()) != NULL);
49 archive_entry_copy_pathname(ae, "//");
50 archive_entry_set_size(ae, strlen(strtab));
51 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
52 assertA(strlen(strtab) == (size_t)archive_write_data(a, strtab, strlen(strtab)));
53 archive_entry_free(ae);
54
55 /* write entries */
56 assert((ae = archive_entry_new()) != NULL);
57 archive_entry_set_mtime(ae, 1, 0);
58 assert(1 == archive_entry_mtime(ae));
59 archive_entry_set_mode(ae, S_IFREG | 0755);
60 assert((S_IFREG | 0755) == archive_entry_mode(ae));
61 archive_entry_copy_pathname(ae, "abcdefghijklmn.o");
62 archive_entry_set_size(ae, 8);
63 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
64 assertA(8 == archive_write_data(a, "87654321", 15));
65 archive_entry_free(ae);
66
67 assert((ae = archive_entry_new()) != NULL);
68 archive_entry_copy_pathname(ae, "ggghhhjjjrrrttt.o");
69 archive_entry_set_filetype(ae, AE_IFREG);
70 archive_entry_set_size(ae, 7);
71 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
72 assertEqualIntA(a, 7, archive_write_data(a, "7777777", 7));
73 archive_entry_free(ae);
74
75 /* test full pathname */
76 assert((ae = archive_entry_new()) != NULL);
77 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjjdddsssppp.o");
78 archive_entry_set_mode(ae, S_IFREG | 0755);
79 archive_entry_set_size(ae, 8);
80 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
81 assertEqualIntA(a, 8, archive_write_data(a, "88877766", 8));
82 archive_entry_free(ae);
83
84 /* trailing "/" should be rejected */
85 assert((ae = archive_entry_new()) != NULL);
86 archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjj/");
87 archive_entry_set_size(ae, 8);
88 assertA(0 != archive_write_header(a, ae));
89 archive_entry_free(ae);
90
91 /* Non regular file should be rejected */
92 assert((ae = archive_entry_new()) != NULL);
93 archive_entry_copy_pathname(ae, "gfgh.o");
94 archive_entry_set_mode(ae, S_IFDIR | 0755);
95 archive_entry_set_size(ae, 6);
96 assertA(0 != archive_write_header(a, ae));
97 archive_entry_free(ae);
98
99 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
100 assertEqualInt(archive_filter_bytes(a, -1),
101 archive_filter_bytes(a, 0));
102 assertEqualInt(used, archive_filter_bytes(a, 0));
103 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
104
105 /*
106 * Now, read the data back.
107 */
108 assert((a = archive_read_new()) != NULL);
109 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
110 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
111 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
112
113 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
114 assertEqualInt(0, archive_entry_mtime(ae));
115 assertEqualString("//", archive_entry_pathname(ae));
116 assertEqualInt(0, archive_entry_size(ae));
117
118 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
119 assertEqualInt(1, archive_entry_mtime(ae));
120 assertEqualString("abcdefghijklmn.o", archive_entry_pathname(ae));
121 assertEqualInt(8, archive_entry_size(ae));
122 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
123 assertEqualMem(buff2, "87654321", 8);
124
125 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &ae));
126 assertEqualString("ggghhhjjjrrrttt.o", archive_entry_pathname(ae));
127 assertEqualInt(7, archive_entry_size(ae));
128 assertEqualIntA(a, 7, archive_read_data(a, buff2, 11));
129 assertEqualMem(buff2, "7777777", 7);
130
131 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
132 assertEqualString("iiijjjdddsssppp.o", archive_entry_pathname(ae));
133 assertEqualInt(8, archive_entry_size(ae));
134 assertEqualIntA(a, 8, archive_read_data(a, buff2, 17));
135 assertEqualMem(buff2, "88877766", 8);
136
137 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
138 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
139
140 /*
141 * Then, we try to create a BSD format archive.
142 */
143 memset(buff, 0, sizeof(buff));
144 assert((a = archive_write_new()) != NULL);
145 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ar_bsd(a));
146 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
147
148 /* write a entry need long name extension */
149 assert((ae = archive_entry_new()) != NULL);
150 archive_entry_copy_pathname(ae, "ttttyyyyuuuuiiii.o");
151 archive_entry_set_filetype(ae, AE_IFREG);
152 archive_entry_set_size(ae, 5);
153 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
154 assertEqualInt(5, archive_entry_size(ae));
155 assertEqualIntA(a, 5, archive_write_data(a, "12345", 7));
156 archive_entry_free(ae);
157
158 /* write a entry with a short name */
159 assert((ae = archive_entry_new()) != NULL);
160 archive_entry_copy_pathname(ae, "ttyy.o");
161 archive_entry_set_filetype(ae, AE_IFREG);
162 archive_entry_set_size(ae, 6);
163 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
164 assertEqualIntA(a, 6, archive_write_data(a, "555555", 7));
165 archive_entry_free(ae);
166 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
167 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
168
169 /* Now, Read the data back */
170 assert((a = archive_read_new()) != NULL);
171 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
172 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
173 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
174
175 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
176 assertEqualString("ttttyyyyuuuuiiii.o", archive_entry_pathname(ae));
177 assertEqualInt(5, archive_entry_size(ae));
178 assertEqualIntA(a, 5, archive_read_data(a, buff2, 10));
179 assertEqualMem(buff2, "12345", 5);
180
181 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
182 assertEqualString("ttyy.o", archive_entry_pathname(ae));
183 assertEqualInt(6, archive_entry_size(ae));
184 assertEqualIntA(a, 6, archive_read_data(a, buff2, 10));
185 assertEqualMem(buff2, "555555", 6);
186
187 /* Test EOF */
188 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
189 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
190 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
191 }
192