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: head/lib/libarchive/test/test_read_format_mtree.c 201247 2009-12-30 05:59:21Z kientzle $");
27 
28 static void
29 test_read_format_mtree1(void)
30 {
31 	const char reffile[] = "test_read_format_mtree.mtree";
32 	char buff[16];
33 	struct archive_entry *ae;
34 	struct archive *a;
35 	FILE *f;
36 
37 	extract_reference_file(reffile);
38 
39 	/*
40 	 * An access error occurred on some platform when mtree
41 	 * format handling open a directory. It is for through
42 	 * the routine which open a directory that we create
43 	 * "dir" and "dir2" directories.
44 	 */
45 	assertMakeDir("dir", 0775);
46 	assertMakeDir("dir2", 0775);
47 
48 	assert((a = archive_read_new()) != NULL);
49 	assertEqualIntA(a, ARCHIVE_OK,
50 	    archive_read_support_compression_all(a));
51 	assertEqualIntA(a, ARCHIVE_OK,
52 	    archive_read_support_format_all(a));
53 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_file(a, reffile, 11));
54 
55 	/*
56 	 * Read "file", whose data is available on disk.
57 	 */
58 	f = fopen("file", "wb");
59 	assert(f != NULL);
60 	assertEqualInt(3, fwrite("hi\n", 1, 3, f));
61 	fclose(f);
62 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
63 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_MTREE);
64 	assertEqualString(archive_entry_pathname(ae), "file");
65 	assertEqualInt(archive_entry_uid(ae), 18);
66 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
67 	assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0123);
68 	assertEqualInt(archive_entry_size(ae), 3);
69 	assertEqualInt(3, archive_read_data(a, buff, 3));
70 	assertEqualMem(buff, "hi\n", 3);
71 
72 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
73 	assertEqualString(archive_entry_pathname(ae), "dir");
74 	assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
75 
76 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
77 	assertEqualString(archive_entry_pathname(ae), "dir/file with space");
78 
79 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
80 	assertEqualString(archive_entry_pathname(ae), "file with space");
81 
82 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
83 	assertEqualString(archive_entry_pathname(ae), "dir2");
84 
85 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
86 	assertEqualString(archive_entry_pathname(ae), "dir2/dir3a");
87 
88 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
89 	assertEqualString(archive_entry_pathname(ae), "dir2/dir3a/indir3a");
90 
91 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
92 	assertEqualString(archive_entry_pathname(ae), "dir2/fullindir2");
93 	assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
94 
95 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
96 	assertEqualString(archive_entry_pathname(ae), "dir2/indir2");
97 
98 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
99 	assertEqualString(archive_entry_pathname(ae), "dir2/dir3b");
100 
101 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
102 	assertEqualString(archive_entry_pathname(ae), "dir2/dir3b/indir3b");
103 
104 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
105 	assertEqualString(archive_entry_pathname(ae), "notindir");
106 
107 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
108 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
109 	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
110 }
111 
112 static void
113 test_read_format_mtree2(void)
114 {
115 	static char archive[] =
116 	    "#mtree\n"
117 	    "d type=dir content=.\n";
118 	struct archive_entry *ae;
119 	struct archive *a;
120 
121 	assert((a = archive_read_new()) != NULL);
122 	assertEqualIntA(a, ARCHIVE_OK,
123 	    archive_read_support_compression_all(a));
124 	assertEqualIntA(a, ARCHIVE_OK,
125 	    archive_read_support_format_all(a));
126 	assertEqualIntA(a, ARCHIVE_OK,
127 	    archive_read_open_memory(a, archive, sizeof(archive)));
128 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
129 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_MTREE);
130 	assertEqualString(archive_entry_pathname(ae), "d");
131 	assertEqualInt(archive_entry_filetype(ae), AE_IFDIR);
132 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
133 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
134 	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
135 }
136 
137 
138 
139 DEFINE_TEST(test_read_format_mtree)
140 {
141 	test_read_format_mtree1();
142 	test_read_format_mtree2();
143 }
144