]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_format_zip.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / libarchive / test / test_write_format_zip.c
1 /*-
2  * Copyright (c) 2003-2008 Tim Kientzle
3  * Copyright (c) 2008 Anselm Strauss
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  * 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.
14  *
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.
25  */
26
27 /*
28  * Development supported by Google Summer of Code 2008.
29  */
30
31 /* TODO: reader does not yet restore permissions. */
32
33 #include "test.h"
34 __FBSDID("$FreeBSD$");
35
36 DEFINE_TEST(test_write_format_zip)
37 {
38         char filedata[64];
39         struct archive_entry *ae;
40         struct archive *a;
41         size_t used;
42         size_t buffsize = 1000000;
43         char *buff;
44         const char *compression_type;
45
46         buff = malloc(buffsize);
47
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));
51 #ifdef HAVE_ZLIB_H
52         compression_type = "zip:compression=deflate";
53 #else
54         compression_type = "zip:compression=store";
55 #endif
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));
61
62         /*
63          * Write a file to it.
64          */
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);
74
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));
79
80         /*
81          * Write another file to it.
82          */
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);
92
93         assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
94         archive_entry_free(ae);
95         assertEqualInt(4, archive_write_data(a, "1234", 5));
96
97         /*
98          * Write a directory to it.
99          */
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);
105
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));
111
112         /* Close out the archive. */
113         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
114         assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
115
116         /*
117          * Now, read the data back.
118          */
119         ae = NULL;
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));
126
127         /*
128          * Read and verify first file.
129          */
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);
142
143
144         /*
145          * Read the second file back.
146          */
147         if (!assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae))){
148                 free(buff);
149                 return;
150         }
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);
161
162         /*
163          * Read the dir entry back.
164          */
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));
174
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));
179         free(buff);
180 }