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
27 #include <locale.h>
28
29 static void
test_open_filename_mbs(void)30 test_open_filename_mbs(void)
31 {
32 char buff[64];
33 struct archive_entry *ae;
34 struct archive *a;
35
36 /* Write an archive through this FILE *. */
37 assert((a = archive_write_new()) != NULL);
38 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
39 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
40 assertEqualIntA(a, ARCHIVE_OK,
41 archive_write_open_filename(a, "test.tar"));
42
43 /*
44 * Write a file to it.
45 */
46 assert((ae = archive_entry_new()) != NULL);
47 archive_entry_set_mtime(ae, 1, 0);
48 archive_entry_copy_pathname(ae, "file");
49 archive_entry_set_mode(ae, S_IFREG | 0755);
50 archive_entry_set_size(ae, 8);
51 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
52 archive_entry_free(ae);
53 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
54
55 /*
56 * Write a second file to it.
57 */
58 assert((ae = archive_entry_new()) != NULL);
59 archive_entry_copy_pathname(ae, "file2");
60 archive_entry_set_mode(ae, S_IFREG | 0755);
61 archive_entry_set_size(ae, 819200);
62 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
63 archive_entry_free(ae);
64
65 /* Close out the archive. */
66 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
67 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
68
69 /*
70 * Now, read the data back.
71 */
72 assert((a = archive_read_new()) != NULL);
73 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
74 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
75 assertEqualIntA(a, ARCHIVE_OK,
76 archive_read_open_filename(a, "test.tar", 512));
77
78 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
79 assertEqualInt(1, archive_entry_mtime(ae));
80 assertEqualInt(0, archive_entry_mtime_nsec(ae));
81 assertEqualInt(0, archive_entry_atime(ae));
82 assertEqualInt(0, archive_entry_ctime(ae));
83 assertEqualString("file", archive_entry_pathname(ae));
84 assert((S_IFREG | 0755) == archive_entry_mode(ae));
85 assertEqualInt(8, archive_entry_size(ae));
86 assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
87 assertEqualMem(buff, "12345678", 8);
88
89 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
90 assertEqualString("file2", archive_entry_pathname(ae));
91 assert((S_IFREG | 0755) == archive_entry_mode(ae));
92 assertEqualInt(819200, archive_entry_size(ae));
93 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
94
95 /* Verify the end of the archive. */
96 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
97 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
98 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
99
100 /*
101 * Verify some of the error handling.
102 */
103 assert((a = archive_read_new()) != NULL);
104 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
105 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
106 assertEqualIntA(a, ARCHIVE_FATAL,
107 archive_read_open_filename(a, "nonexistent.tar", 512));
108 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
109 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
110
111 }
112
113 static void
test_open_filename_wcs(void)114 test_open_filename_wcs(void)
115 {
116 char buff[64];
117 struct archive_entry *ae;
118 struct archive *a;
119
120 /* Write an archive through this FILE *. */
121 assert((a = archive_write_new()) != NULL);
122 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
123 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
124 assertEqualIntA(a, ARCHIVE_OK,
125 archive_write_open_filename_w(a, L"test.tar"));
126
127 /*
128 * Write a file to it.
129 */
130 assert((ae = archive_entry_new()) != NULL);
131 archive_entry_set_mtime(ae, 1, 0);
132 archive_entry_copy_pathname_w(ae, L"file");
133 archive_entry_set_mode(ae, S_IFREG | 0755);
134 archive_entry_set_size(ae, 8);
135 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
136 archive_entry_free(ae);
137 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
138
139 /*
140 * Write a second file to it.
141 */
142 assert((ae = archive_entry_new()) != NULL);
143 archive_entry_copy_pathname_w(ae, L"file2");
144 archive_entry_set_mode(ae, S_IFREG | 0755);
145 archive_entry_set_size(ae, 819200);
146 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
147 archive_entry_free(ae);
148
149 /* Close out the archive. */
150 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
151 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
152
153 /*
154 * Now, read the data back.
155 */
156 assert((a = archive_read_new()) != NULL);
157 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
158 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
159 assertEqualIntA(a, ARCHIVE_OK,
160 archive_read_open_filename_w(a, L"test.tar", 512));
161
162 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
163 assertEqualInt(1, archive_entry_mtime(ae));
164 assertEqualInt(0, archive_entry_mtime_nsec(ae));
165 assertEqualInt(0, archive_entry_atime(ae));
166 assertEqualInt(0, archive_entry_ctime(ae));
167 assertEqualWString(L"file", archive_entry_pathname_w(ae));
168 assert((S_IFREG | 0755) == archive_entry_mode(ae));
169 assertEqualInt(8, archive_entry_size(ae));
170 assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
171 assertEqualMem(buff, "12345678", 8);
172
173 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
174 assertEqualWString(L"file2", archive_entry_pathname_w(ae));
175 assert((S_IFREG | 0755) == archive_entry_mode(ae));
176 assertEqualInt(819200, archive_entry_size(ae));
177 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
178
179 /* Verify the end of the archive. */
180 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
181 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
182 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
183
184 /*
185 * Verify some of the error handling.
186 */
187 assert((a = archive_read_new()) != NULL);
188 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
189 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
190 assertEqualIntA(a, ARCHIVE_FATAL,
191 archive_read_open_filename_w(a, L"nonexistent.tar", 512));
192 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
193 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
194
195 }
196
DEFINE_TEST(test_open_filename)197 DEFINE_TEST(test_open_filename)
198 {
199 test_open_filename_mbs();
200 test_open_filename_wcs();
201 }
202
DEFINE_TEST(test_open_filename_wcs_oob)203 DEFINE_TEST(test_open_filename_wcs_oob)
204 {
205 struct archive *a;
206
207 /* Continue even if not available for best test coverage. */
208 setlocale(LC_ALL, "en_US.UTF-8");
209
210 /*
211 * Try to open file with filename which might use more bytes in
212 * MBS representation than in wchar_t if platform uses 16 bit wchar_t,
213 * e.g. Cygwin (3 times a Euro sign, i.e. 3*2+2=8 vs 3*3+1=10 bytes).
214 */
215 assert((a = archive_read_new()) != NULL);
216 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
217 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
218 assertEqualIntA(a, ARCHIVE_FATAL,
219 archive_read_open_filename_w(a, L"\u20AC\u20AC\u20AC", 512));
220 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
221 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
222 }
223