]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_format_mtree.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_mtree.c
1 /*-
2  * Copyright (c) 2009 Michihiro NAKAJIMA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
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 #include "test.h"
28 __FBSDID("$FreeBSD$");
29
30 static char buff[4096];
31 static struct {
32         const char      *path;
33         mode_t           mode;
34         time_t           mtime;
35         uid_t            uid;
36         gid_t            gid;
37 } entries[] = {
38         { "./Makefile",         S_IFREG | 0644, 1233041050, 1001, 1001 },
39         { "./NEWS",             S_IFREG | 0644, 1231975636, 1001, 1001 },
40         { "./PROJECTS",         S_IFREG | 0644, 1231975636, 1001, 1001 },
41         { "./README",           S_IFREG | 0644, 1231975636, 1001, 1001 },
42         { "./COPYING",          S_IFREG | 0644, 1231975636, 1001, 1001 },
43         { "./subdir",           S_IFDIR | 0755, 1233504586, 1001, 1001 },
44         { "./subdir/README",    S_IFREG | 0664, 1231975636, 1002, 1001 },
45         { "./subdir/config",    S_IFREG | 0664, 1232266273, 1003, 1003 },
46         { "./subdir2",          S_IFDIR | 0755, 1233504586, 1001, 1001 },
47         { "./subdir3",          S_IFDIR | 0755, 1233504586, 1001, 1001 },
48         { "./subdir3/mtree",    S_IFREG | 0664, 1232266273, 1003, 1003 },
49         { NULL, 0, 0, 0, 0 }
50 };
51
52 static void
53 test_write_format_mtree_sub(int use_set, int dironly)
54 {
55         struct archive_entry *ae;
56         struct archive* a;
57         size_t used;
58         int i;
59
60         /* Create a mtree format archive. */
61         assert((a = archive_write_new()) != NULL);
62         assertA(0 == archive_write_set_format_mtree(a));
63         if (use_set)
64                 assertA(0 == archive_write_set_options(a, "use-set"));
65         if (dironly)
66                 assertA(0 == archive_write_set_options(a, "dironly"));
67         assertA(0 == archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
68
69         /* Write entries */
70         for (i = 0; entries[i].path != NULL; i++) {
71                 assert((ae = archive_entry_new()) != NULL);
72                 archive_entry_set_mtime(ae, entries[i].mtime, 0);
73                 assert(entries[i].mtime == archive_entry_mtime(ae));
74                 archive_entry_set_mode(ae, entries[i].mode);
75                 assert(entries[i].mode == archive_entry_mode(ae));
76                 archive_entry_set_uid(ae, entries[i].uid);
77                 assert(entries[i].uid == archive_entry_uid(ae));
78                 archive_entry_set_gid(ae, entries[i].gid);
79                 assert(entries[i].gid == archive_entry_gid(ae));
80                 archive_entry_copy_pathname(ae, entries[i].path);
81                 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
82                         archive_entry_set_size(ae, 8);
83                 assertA(0 == archive_write_header(a, ae));
84                 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
85                         assertA(8 == archive_write_data(a, "Hello012", 15));
86                 archive_entry_free(ae);
87         }
88         archive_write_close(a);
89 #if ARCHIVE_VERSION_NUMBER < 2000000
90         archive_write_finish(a);
91 #else
92         assertEqualInt(0, archive_write_finish(a));
93 #endif
94         if (use_set) {
95                 const char *p;
96
97                 buff[used] = '\0';
98                 assert(NULL != (p = strstr(buff, "\n/set ")));
99                 if (p != NULL) {
100                         char *r;
101                         const char *o;
102                         p++;
103                         r = strchr(p, '\n');
104                         if (r != NULL)
105                                 *r = '\0';
106                         if (dironly)
107                                 o = "/set type=dir uid=1001 gid=1001 mode=755";
108                         else
109                                 o = "/set type=file uid=1001 gid=1001 mode=644";
110                         assertEqualString(o, p);
111                         if (r != NULL)
112                                 *r = '\n';
113                 }
114         }
115
116         /*
117          * Read the data and check it.
118          */
119         assert((a = archive_read_new()) != NULL);
120         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
121         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
122         assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
123
124         /* Read entries */
125         for (i = 0; entries[i].path != NULL; i++) {
126                 if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
127                         continue;
128                 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
129                 assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
130                 assertEqualInt(entries[i].mode, archive_entry_mode(ae));
131                 assertEqualInt(entries[i].uid, archive_entry_uid(ae));
132                 assertEqualInt(entries[i].gid, archive_entry_gid(ae));
133                 assertEqualString(entries[i].path, archive_entry_pathname(ae));
134                 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
135                         assertEqualInt(8, archive_entry_size(ae));
136         }
137         assertEqualIntA(a, 0, archive_read_close(a));
138 #if ARCHIVE_VERSION_NUMBER < 2000000
139         archive_read_finish(a);
140 #else
141         assertEqualInt(0, archive_read_finish(a));
142 #endif
143 }
144
145 DEFINE_TEST(test_write_format_mtree)
146 {
147         /* Default setting */
148         test_write_format_mtree_sub(0, 0);
149         /* Directory only */
150         test_write_format_mtree_sub(0, 1);
151         /* Use /set keyword */
152         test_write_format_mtree_sub(1, 0);
153         /* Use /set keyword with directory only */
154         test_write_format_mtree_sub(1, 1);
155 }