]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/test_read_format_mtree.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / test_read_format_mtree.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27
28 /* Single entry with a hardlink. */
29 static unsigned char archive[] = {
30         "#mtree\n"
31         "file type=file uid=18 mode=0123 size=3\n"
32         "dir type=dir\n"
33         " file\\040with\\040space type=file uid=18\n"
34         " ..\n"
35         "file\\04with\\040space type=file\n"
36         "dir2 type=dir\n"
37         " dir3a type=dir\n"
38         "  indir3a type=file\n"
39         "dir2/fullindir2 type=file mode=0777\n"
40         "  ..\n"
41         " indir2 type=file\n"
42         " dir3b type=dir\n"
43         "  indir3b type=file\n"
44         "  ..\n"
45         " ..\n"
46         "notindir type=file\n"
47         "dir2/fullindir2 mode=0644\n"
48 };
49
50 DEFINE_TEST(test_read_format_mtree)
51 {
52         char buff[16];
53         struct archive_entry *ae;
54         struct archive *a;
55         int fd;
56
57         /*
58          * An access error occurred on some platform when mtree
59          * format handling open a directory. It is for through
60          * the routine which open a directory that we create
61          * "dir" and "dir2" directories.
62          */
63         assertEqualInt(0, mkdir("dir", 0775));
64         assertEqualInt(0, mkdir("dir2", 0775));
65
66         assert((a = archive_read_new()) != NULL);
67         assertEqualIntA(a, ARCHIVE_OK,
68             archive_read_support_compression_all(a));
69         assertEqualIntA(a, ARCHIVE_OK,
70             archive_read_support_format_all(a));
71         assertEqualIntA(a, ARCHIVE_OK,
72             archive_read_open_memory(a, archive, sizeof(archive)));
73
74         /*
75          * Read "file", whose data is available on disk.
76          */
77         fd = open("file", O_WRONLY | O_CREAT, 0777);
78         assert(fd >= 0);
79         assertEqualInt(3, write(fd, "hi\n", 3));
80         close(fd);
81         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
82         assertEqualInt(archive_format(a), ARCHIVE_FORMAT_MTREE);
83         assertEqualString(archive_entry_pathname(ae), "file");
84         assertEqualInt(archive_entry_uid(ae), 18);
85         assert(S_ISREG(archive_entry_mode(ae)));
86         assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0123);
87         assertEqualInt(archive_entry_size(ae), 3);
88         assertEqualInt(3, archive_read_data(a, buff, 3));
89         assertEqualMem(buff, "hi\n", 3);
90
91         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
92         assertEqualString(archive_entry_pathname(ae), "dir");
93         assert(S_ISDIR(archive_entry_mode(ae)));
94
95         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
96         assertEqualString(archive_entry_pathname(ae), "dir/file with space");
97
98         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
99         assertEqualString(archive_entry_pathname(ae), "file\\04with space");
100
101         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
102         assertEqualString(archive_entry_pathname(ae), "dir2");
103
104         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
105         assertEqualString(archive_entry_pathname(ae), "dir2/dir3a");
106
107         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
108         assertEqualString(archive_entry_pathname(ae), "dir2/dir3a/indir3a");
109
110         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
111         assertEqualString(archive_entry_pathname(ae), "dir2/fullindir2");
112         assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
113
114         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
115         assertEqualString(archive_entry_pathname(ae), "dir2/indir2");
116
117         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
118         assertEqualString(archive_entry_pathname(ae), "dir2/dir3b");
119
120         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
121         assertEqualString(archive_entry_pathname(ae), "dir2/dir3b/indir3b");
122
123         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
124         assertEqualString(archive_entry_pathname(ae), "notindir");
125
126         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
127         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
128 #if ARCHIVE_VERSION_NUMBER < 2000000
129         archive_read_finish(a);
130 #else
131         assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
132 #endif
133 }
134
135