]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test_write_format_ar.c
This commit was generated by cvs2svn to compensate for changes in r172767,
[FreeBSD/FreeBSD.git] / lib / libarchive / test / test_write_format_ar.c
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 __FBSDID("$FreeBSD$");
30
31 char buff[4096];
32 char buff2[64];
33 static unsigned char strtab[] = "abcdefghijklmn.o/\nggghhhjjjrrrttt.o/\niiijjjdddsssppp.o/\n";
34
35 DEFINE_TEST(test_write_format_ar)
36 {
37 #if ARCHIVE_VERSION_STAMP < 1009000
38         skipping("ar write support");
39 #else
40         struct archive_entry *ae;
41         struct archive* a;
42         size_t used;
43
44         /*
45          * First we try to create a SVR4/GNU format archive.
46          */
47         assert((a = archive_write_new()) != NULL);
48         assertA(0 == archive_write_set_format_ar_svr4(a));
49         assertA(0 == archive_write_set_compression_gzip(a));
50         assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
51
52         /* write the filename table */
53         assert((ae = archive_entry_new()) != NULL);
54         archive_entry_copy_pathname(ae, "//");
55         archive_entry_set_size(ae, strlen(strtab));
56         assertA(0 == archive_write_header(a, ae));
57         assertA(strlen(strtab) == (size_t)archive_write_data(a, strtab, strlen(strtab)));
58         archive_entry_free(ae);
59
60         /* write entries */
61         assert((ae = archive_entry_new()) != NULL);
62         archive_entry_set_mtime(ae, 1, 0);
63         assert(1 == archive_entry_mtime(ae));
64         archive_entry_set_mode(ae, S_IFREG | 0755);
65         assert((S_IFREG | 0755) == archive_entry_mode(ae));
66         archive_entry_copy_pathname(ae, "abcdefghijklmn.o");
67         archive_entry_set_size(ae, 8);
68         assertA(0 == archive_write_header(a, ae));
69         assertA(8 == archive_write_data(a, "87654321", 15));
70         archive_entry_free(ae);
71
72         assert((ae = archive_entry_new()) != NULL);
73         archive_entry_copy_pathname(ae, "ggghhhjjjrrrttt.o");
74         archive_entry_set_filetype(ae, AE_IFREG);
75         archive_entry_set_size(ae, 7);
76         assertA(0 == archive_write_header(a, ae));
77         assertA(7 == archive_write_data(a, "7777777", 7));
78         archive_entry_free(ae);
79
80         /* test full pathname */
81         assert((ae = archive_entry_new()) != NULL);
82         archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjjdddsssppp.o");
83         archive_entry_set_mode(ae, S_IFREG | 0755);
84         archive_entry_set_size(ae, 8);
85         assertA(0 == archive_write_header(a, ae));
86         assertA(8 == archive_write_data(a, "88877766", 8));
87         archive_entry_free(ae);
88
89         /* trailing "/" should be rejected */
90         assert((ae = archive_entry_new()) != NULL);
91         archive_entry_copy_pathname(ae, "/usr/home/xx/iiijjj/");
92         archive_entry_set_size(ae, 8);
93         assertA(0 != archive_write_header(a, ae));
94         archive_entry_free(ae);
95
96         /* Non regular file should be rejected */
97         assert((ae = archive_entry_new()) != NULL);
98         archive_entry_copy_pathname(ae, "gfgh.o");
99         archive_entry_set_mode(ae, S_IFDIR | 0755);
100         archive_entry_set_size(ae, 6);
101         assertA(0 != archive_write_header(a, ae));
102         archive_entry_free(ae);
103
104         archive_write_close(a);
105 #if ARCHIVE_API_VERSION > 1
106         assert(0 == archive_write_finish(a));
107 #else
108         archive_write_finish(a);
109 #endif
110
111         /*
112          * Now, read the data back.
113          */
114         assert((a = archive_read_new()) != NULL);
115         assertA(0 == archive_read_support_format_all(a));
116         assertA(0 == archive_read_support_compression_all(a));
117         assertA(0 == archive_read_open_memory(a, buff, used));
118
119         assertA(0 == archive_read_next_header(a, &ae));
120         assertEqualInt(0, archive_entry_mtime(ae));
121         assertEqualString("//", archive_entry_pathname(ae));
122         assertEqualInt(strlen(strtab), archive_entry_size(ae));
123         assertEqualIntA(a, strlen(strtab), archive_read_data(a, buff2, 100));
124         assert(0 == memcmp(buff2, strtab, strlen(strtab)));
125
126         assertA(0 == archive_read_next_header(a, &ae));
127         assert(1 == archive_entry_mtime(ae));
128         assertEqualString("abcdefghijklmn.o", archive_entry_pathname(ae));
129         assert(8 == archive_entry_size(ae));
130         assertA(8 == archive_read_data(a, buff2, 10));
131         assert(0 == memcmp(buff2, "87654321", 8));
132
133         assert(0 == archive_read_next_header(a, &ae));
134         assertEqualString("ggghhhjjjrrrttt.o", archive_entry_pathname(ae));
135         assert(7 == archive_entry_size(ae));
136         assertA(7 == archive_read_data(a, buff2, 11));
137         assert(0 == memcmp(buff2, "7777777", 7));
138
139         assert(0 == archive_read_next_header(a, &ae));
140         assertEqualString("iiijjjdddsssppp.o", archive_entry_pathname(ae));
141         assert(8 == archive_entry_size(ae));
142         assertA(8 == archive_read_data(a, buff2, 17));
143         assert(0 == memcmp(buff2, "88877766", 8));
144
145         assert(0 == archive_read_close(a));
146 #if ARCHIVE_API_VERSION > 1
147         assert(0 == archive_read_finish(a));
148 #else
149         archive_read_finish(a);
150 #endif
151
152         /*
153          * Then, we try to create a BSD format archive.
154          */
155         memset(buff, 0, sizeof(buff));
156         assert((a = archive_write_new()) != NULL);
157         assertA(0 == archive_write_set_format_ar_bsd(a));
158         assertA(0 == archive_write_set_compression_bzip2(a));
159         assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
160
161         /* write a entry need long name extension */
162         assert((ae = archive_entry_new()) != NULL);
163         archive_entry_copy_pathname(ae, "ttttyyyyuuuuiiii.o");
164         archive_entry_set_filetype(ae, AE_IFREG);
165         archive_entry_set_size(ae, 5);
166         assertA(0 == archive_write_header(a, ae));
167         assertA(5 == archive_write_data(a, "12345", 7));
168         archive_entry_free(ae);
169
170         /* write a entry with a short name */
171         assert((ae = archive_entry_new()) != NULL);
172         archive_entry_copy_pathname(ae, "ttyy.o");
173         archive_entry_set_filetype(ae, AE_IFREG);
174         archive_entry_set_size(ae, 6);
175         assertA(0 == archive_write_header(a, ae));
176         assertA(6 == archive_write_data(a, "555555", 7));
177         archive_entry_free(ae);
178         archive_write_close(a);
179 #if ARCHIVE_API_VERSION > 1
180         assert(0 == archive_write_finish(a));
181 #else
182         archive_write_finish(a);
183 #endif
184
185         /* Now, Read the data back */
186         assert((a = archive_read_new()) != NULL);
187         assertA(0 == archive_read_support_format_all(a));
188         assertA(0 == archive_read_support_compression_all(a));
189         assertA(0 == archive_read_open_memory(a, buff, used));
190
191         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
192         assertEqualString("ttttyyyyuuuuiiii.o", archive_entry_pathname(ae));
193         assertEqualInt(5, archive_entry_size(ae));
194         assertA(5 == archive_read_data(a, buff2, 10));
195         assert(0 == memcmp(buff2, "12345", 5));
196
197         assert(0 == archive_read_next_header(a, &ae));
198         assertEqualString("ttyy.o", archive_entry_pathname(ae));
199         assert(6 == archive_entry_size(ae));
200         assertA(6 == archive_read_data(a, buff2, 10));
201         assert(0 == memcmp(buff2, "555555", 6));
202
203         /* Test EOF */
204         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
205         assert(0 == archive_read_close(a));
206 #if ARCHIVE_API_VERSION > 1
207         assert(0 == archive_read_finish(a));
208 #else
209         archive_read_finish(a);
210 #endif
211 #endif
212 }