test_open_filename.c revision 232153
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: head/contrib/libarchive/libarchive/test/test_open_filename.c 232153 2012-02-25 10:58:02Z mm $");
27
28static void
29test_open_filename_mbs(void)
30{
31	char buff[64];
32	struct archive_entry *ae;
33	struct archive *a;
34
35	/* Write an archive through this FILE *. */
36	assert((a = archive_write_new()) != NULL);
37	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
38	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_none(a));
39	assertEqualIntA(a, ARCHIVE_OK,
40	    archive_write_open_filename(a, "test.tar"));
41
42	/*
43	 * Write a file to it.
44	 */
45	assert((ae = archive_entry_new()) != NULL);
46	archive_entry_set_mtime(ae, 1, 0);
47	archive_entry_copy_pathname(ae, "file");
48	archive_entry_set_mode(ae, S_IFREG | 0755);
49	archive_entry_set_size(ae, 8);
50	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
51	archive_entry_free(ae);
52	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
53
54	/*
55	 * Write a second file to it.
56	 */
57	assert((ae = archive_entry_new()) != NULL);
58	archive_entry_copy_pathname(ae, "file2");
59	archive_entry_set_mode(ae, S_IFREG | 0755);
60	archive_entry_set_size(ae, 819200);
61	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
62	archive_entry_free(ae);
63
64	/* Close out the archive. */
65	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
66	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
67
68	/*
69	 * Now, read the data back.
70	 */
71	assert((a = archive_read_new()) != NULL);
72	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
73	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
74	assertEqualIntA(a, ARCHIVE_OK,
75	    archive_read_open_filename(a, "test.tar", 512));
76
77	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
78	assertEqualInt(1, archive_entry_mtime(ae));
79	assertEqualInt(0, archive_entry_mtime_nsec(ae));
80	assertEqualInt(0, archive_entry_atime(ae));
81	assertEqualInt(0, archive_entry_ctime(ae));
82	assertEqualString("file", archive_entry_pathname(ae));
83	assert((S_IFREG | 0755) == archive_entry_mode(ae));
84	assertEqualInt(8, archive_entry_size(ae));
85	assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
86	assertEqualMem(buff, "12345678", 8);
87
88	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
89	assertEqualString("file2", archive_entry_pathname(ae));
90	assert((S_IFREG | 0755) == archive_entry_mode(ae));
91	assertEqualInt(819200, archive_entry_size(ae));
92	assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
93
94	/* Verify the end of the archive. */
95	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
96	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
97	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
98
99	/*
100	 * Verify some of the error handling.
101	 */
102	assert((a = archive_read_new()) != NULL);
103	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
104	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
105	assertEqualIntA(a, ARCHIVE_FATAL,
106	    archive_read_open_filename(a, "nonexistent.tar", 512));
107	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
108	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
109
110}
111
112static void
113test_open_filename_wcs(void)
114{
115	char buff[64];
116	struct archive_entry *ae;
117	struct archive *a;
118
119	/* Write an archive through this FILE *. */
120	assert((a = archive_write_new()) != NULL);
121	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
122	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_none(a));
123	assertEqualIntA(a, ARCHIVE_OK,
124	    archive_write_open_filename_w(a, L"test.tar"));
125
126	/*
127	 * Write a file to it.
128	 */
129	assert((ae = archive_entry_new()) != NULL);
130	archive_entry_set_mtime(ae, 1, 0);
131	archive_entry_copy_pathname_w(ae, L"file");
132	archive_entry_set_mode(ae, S_IFREG | 0755);
133	archive_entry_set_size(ae, 8);
134	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
135	archive_entry_free(ae);
136	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
137
138	/*
139	 * Write a second file to it.
140	 */
141	assert((ae = archive_entry_new()) != NULL);
142	archive_entry_copy_pathname_w(ae, L"file2");
143	archive_entry_set_mode(ae, S_IFREG | 0755);
144	archive_entry_set_size(ae, 819200);
145	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
146	archive_entry_free(ae);
147
148	/* Close out the archive. */
149	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
150	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
151
152	/*
153	 * Now, read the data back.
154	 */
155	assert((a = archive_read_new()) != NULL);
156	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
157	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
158	assertEqualIntA(a, ARCHIVE_OK,
159	    archive_read_open_filename_w(a, L"test.tar", 512));
160
161	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
162	assertEqualInt(1, archive_entry_mtime(ae));
163	assertEqualInt(0, archive_entry_mtime_nsec(ae));
164	assertEqualInt(0, archive_entry_atime(ae));
165	assertEqualInt(0, archive_entry_ctime(ae));
166	assertEqualWString(L"file", archive_entry_pathname_w(ae));
167	assert((S_IFREG | 0755) == archive_entry_mode(ae));
168	assertEqualInt(8, archive_entry_size(ae));
169	assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
170	assertEqualMem(buff, "12345678", 8);
171
172	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
173	assertEqualWString(L"file2", archive_entry_pathname_w(ae));
174	assert((S_IFREG | 0755) == archive_entry_mode(ae));
175	assertEqualInt(819200, archive_entry_size(ae));
176	assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
177
178	/* Verify the end of the archive. */
179	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
180	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
181	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
182
183	/*
184	 * Verify some of the error handling.
185	 */
186	assert((a = archive_read_new()) != NULL);
187	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
188	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
189	assertEqualIntA(a, ARCHIVE_FATAL,
190	    archive_read_open_filename_w(a, L"nonexistent.tar", 512));
191	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
192	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
193
194}
195
196DEFINE_TEST(test_open_filename)
197{
198	test_open_filename_mbs();
199	test_open_filename_wcs();
200}
201