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