2 * Copyright (c) 2003-2008 Tim Kientzle
3 * Copyright (c) 2008 Anselm Strauss
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 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Development supported by Google Summer of Code 2008.
31 /* TODO: reader does not yet restore permissions. */
34 __FBSDID("$FreeBSD$");
36 DEFINE_TEST(test_write_format_zip)
39 struct archive_entry *ae;
42 size_t buffsize = 1000000;
44 const char *compression_type;
46 buff = malloc(buffsize);
48 /* Create a new archive in memory. */
49 assert((a = archive_write_new()) != NULL);
50 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
52 compression_type = "zip:compression=deflate";
54 compression_type = "zip:compression=store";
56 assertEqualIntA(a, ARCHIVE_OK,
57 archive_write_set_format_options(a, compression_type));
58 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_none(a));
59 assertEqualIntA(a, ARCHIVE_OK,
60 archive_write_open_memory(a, buff, buffsize, &used));
65 assert((ae = archive_entry_new()) != NULL);
66 archive_entry_set_mtime(ae, 1, 10);
67 assertEqualInt(1, archive_entry_mtime(ae));
68 assertEqualInt(10, archive_entry_mtime_nsec(ae));
69 archive_entry_copy_pathname(ae, "file");
70 assertEqualString("file", archive_entry_pathname(ae));
71 archive_entry_set_mode(ae, S_IFREG | 0755);
72 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
73 archive_entry_set_size(ae, 8);
75 assertEqualInt(0, archive_write_header(a, ae));
76 archive_entry_free(ae);
77 assertEqualInt(8, archive_write_data(a, "12345678", 9));
78 assertEqualInt(0, archive_write_data(a, "1", 1));
81 * Write another file to it.
83 assert((ae = archive_entry_new()) != NULL);
84 archive_entry_set_mtime(ae, 1, 10);
85 assertEqualInt(1, archive_entry_mtime(ae));
86 assertEqualInt(10, archive_entry_mtime_nsec(ae));
87 archive_entry_copy_pathname(ae, "file2");
88 assertEqualString("file2", archive_entry_pathname(ae));
89 archive_entry_set_mode(ae, S_IFREG | 0755);
90 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
91 archive_entry_set_size(ae, 4);
93 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
94 archive_entry_free(ae);
95 assertEqualInt(4, archive_write_data(a, "1234", 5));
98 * Write a directory to it.
100 assert((ae = archive_entry_new()) != NULL);
101 archive_entry_set_mtime(ae, 11, 110);
102 archive_entry_copy_pathname(ae, "dir");
103 archive_entry_set_mode(ae, S_IFDIR | 0755);
104 archive_entry_set_size(ae, 512);
106 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
107 failure("size should be zero so that applications know not to write");
108 assertEqualInt(0, archive_entry_size(ae));
109 archive_entry_free(ae);
110 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
112 /* Close out the archive. */
113 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
114 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
117 * Now, read the data back.
120 assert((a = archive_read_new()) != NULL);
121 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
122 assertEqualIntA(a, ARCHIVE_OK,
123 archive_read_support_compression_all(a));
124 assertEqualIntA(a, ARCHIVE_OK,
125 archive_read_open_memory(a, buff, used));
128 * Read and verify first file.
130 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
131 assertEqualInt(1, archive_entry_mtime(ae));
132 /* Zip doesn't store high-resolution mtime. */
133 assertEqualInt(0, archive_entry_mtime_nsec(ae));
134 assertEqualInt(0, archive_entry_atime(ae));
135 assertEqualInt(0, archive_entry_ctime(ae));
136 assertEqualString("file", archive_entry_pathname(ae));
137 //assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
138 assertEqualInt(0, archive_entry_size(ae));
139 assertEqualIntA(a, 8,
140 archive_read_data(a, filedata, sizeof(filedata)));
141 assertEqualMem(filedata, "12345678", 8);
145 * Read the second file back.
147 if (!assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae))){
151 assertEqualInt(1, archive_entry_mtime(ae));
152 assertEqualInt(0, archive_entry_mtime_nsec(ae));
153 assertEqualInt(0, archive_entry_atime(ae));
154 assertEqualInt(0, archive_entry_ctime(ae));
155 assertEqualString("file2", archive_entry_pathname(ae));
156 //assert((S_IFREG | 0755) == archive_entry_mode(ae));
157 assertEqualInt(0, archive_entry_size(ae));
158 assertEqualIntA(a, 4,
159 archive_read_data(a, filedata, sizeof(filedata)));
160 assertEqualMem(filedata, "1234", 4);
163 * Read the dir entry back.
165 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
166 assertEqualInt(11, archive_entry_mtime(ae));
167 assertEqualInt(0, archive_entry_mtime_nsec(ae));
168 assertEqualInt(0, archive_entry_atime(ae));
169 assertEqualInt(0, archive_entry_ctime(ae));
170 assertEqualString("dir/", archive_entry_pathname(ae));
171 //assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae));
172 assertEqualInt(0, archive_entry_size(ae));
173 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
175 /* Verify the end of the archive. */
176 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
177 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
178 assertEqualInt(ARCHIVE_OK, archive_read_finish(a));