]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_write_format_ar.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 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(0, archive_entry_size(ae));
123
124         assertA(0 == archive_read_next_header(a, &ae));
125         assert(1 == archive_entry_mtime(ae));
126         assertEqualString("abcdefghijklmn.o", archive_entry_pathname(ae));
127         assert(8 == archive_entry_size(ae));
128         assertA(8 == archive_read_data(a, buff2, 10));
129         assert(0 == memcmp(buff2, "87654321", 8));
130
131         assert(0 == archive_read_next_header(a, &ae));
132         assertEqualString("ggghhhjjjrrrttt.o", archive_entry_pathname(ae));
133         assert(7 == archive_entry_size(ae));
134         assertA(7 == archive_read_data(a, buff2, 11));
135         assert(0 == memcmp(buff2, "7777777", 7));
136
137         assert(0 == archive_read_next_header(a, &ae));
138         assertEqualString("iiijjjdddsssppp.o", archive_entry_pathname(ae));
139         assert(8 == archive_entry_size(ae));
140         assertA(8 == archive_read_data(a, buff2, 17));
141         assert(0 == memcmp(buff2, "88877766", 8));
142
143         assert(0 == archive_read_close(a));
144 #if ARCHIVE_API_VERSION > 1
145         assert(0 == archive_read_finish(a));
146 #else
147         archive_read_finish(a);
148 #endif
149
150         /*
151          * Then, we try to create a BSD format archive.
152          */
153         memset(buff, 0, sizeof(buff));
154         assert((a = archive_write_new()) != NULL);
155         assertA(0 == archive_write_set_format_ar_bsd(a));
156         assertA(0 == archive_write_set_compression_bzip2(a));
157         assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
158
159         /* write a entry need long name extension */
160         assert((ae = archive_entry_new()) != NULL);
161         archive_entry_copy_pathname(ae, "ttttyyyyuuuuiiii.o");
162         archive_entry_set_filetype(ae, AE_IFREG);
163         archive_entry_set_size(ae, 5);
164         assertA(0 == archive_write_header(a, ae));
165         assertA(5 == archive_entry_size(ae));
166         assertA(5 == archive_write_data(a, "12345", 7));
167         archive_entry_free(ae);
168
169         /* write a entry with a short name */
170         assert((ae = archive_entry_new()) != NULL);
171         archive_entry_copy_pathname(ae, "ttyy.o");
172         archive_entry_set_filetype(ae, AE_IFREG);
173         archive_entry_set_size(ae, 6);
174         assertA(0 == archive_write_header(a, ae));
175         assertA(6 == archive_write_data(a, "555555", 7));
176         archive_entry_free(ae);
177         archive_write_close(a);
178 #if ARCHIVE_API_VERSION > 1
179         assert(0 == archive_write_finish(a));
180 #else
181         archive_write_finish(a);
182 #endif
183
184         /* Now, Read the data back */
185         assert((a = archive_read_new()) != NULL);
186         assertA(0 == archive_read_support_format_all(a));
187         assertA(0 == archive_read_support_compression_all(a));
188         assertA(0 == archive_read_open_memory(a, buff, used));
189
190         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
191         assertEqualString("ttttyyyyuuuuiiii.o", archive_entry_pathname(ae));
192         assertEqualInt(5, archive_entry_size(ae));
193         assertA(5 == archive_read_data(a, buff2, 10));
194         assert(0 == memcmp(buff2, "12345", 5));
195
196         assert(0 == archive_read_next_header(a, &ae));
197         assertEqualString("ttyy.o", archive_entry_pathname(ae));
198         assert(6 == archive_entry_size(ae));
199         assertA(6 == archive_read_data(a, buff2, 10));
200         assert(0 == memcmp(buff2, "555555", 6));
201
202         /* Test EOF */
203         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
204         assert(0 == archive_read_close(a));
205 #if ARCHIVE_API_VERSION > 1
206         assert(0 == archive_read_finish(a));
207 #else
208         archive_read_finish(a);
209 #endif
210 #endif
211 }