1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2011 Michihiro NAKAJIMA
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  * 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 #include "test.h"
27 
28 /*
29  * Test archive contains the following entries with only MSDOS attributes:
30  *   'abc' -- zero-length file
31  *   'def' -- directory without trailing slash and without streaming extension
32  *   'def/foo' -- file in def
33  *   'ghi/' -- directory with trailing slash and without streaming extension
34  *   'jkl'  -- directory without trailing slash and with streaming extension
35  *   'mno/' -- directory with trailing slash and streaming extension
36  *
37  * Seeking reader should identify all of these correctly using the
38  * central directory information.
39  * Streaming reader should correctly identify everything except 'def';
40  * since the standard Zip local file header does not include any file
41  * type information, it will be mis-identified as a zero-length file.
42  */
43 
44 static void verify(struct archive *a, int streaming) {
45 	struct archive_entry *ae;
46 
47 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
48 	assertEqualString("abc", archive_entry_pathname(ae));
49 	assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae));
50 
51 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
52 	if (streaming) {
53 		/* Streaming reader has no basis for making this a dir */
54 		assertEqualString("def", archive_entry_pathname(ae));
55 		assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae));
56 	} else {
57 		/* Since 'def' is a dir, '/' should be added */
58 		assertEqualString("def/", archive_entry_pathname(ae));
59 		assertEqualInt(AE_IFDIR | 0775, archive_entry_mode(ae));
60 	}
61 
62 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
63 	assertEqualString("def/foo", archive_entry_pathname(ae));
64 	assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae));
65 
66 	/* Streaming reader can tell this is a dir because it ends in '/' */
67 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
68 	assertEqualString("ghi/", archive_entry_pathname(ae));
69 	assertEqualInt(AE_IFDIR | 0775, archive_entry_mode(ae));
70 
71 	/* Streaming reader can tell this is a dir because it has xl
72 	 * extension */
73 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
74 	/* '/' gets added because this is a dir */
75 	assertEqualString("jkl/", archive_entry_pathname(ae));
76 	assertEqualInt(AE_IFDIR | 0775, archive_entry_mode(ae));
77 
78 	/* Streaming reader can tell this is a dir because it ends in
79 	 * '/' and has xl extension */
80 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
81 	assertEqualString("mno/", archive_entry_pathname(ae));
82 	assertEqualInt(AE_IFDIR | 0775, archive_entry_mode(ae));
83 
84 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
85 }
86 
87 DEFINE_TEST(test_read_format_zip_msdos)
88 {
89 	const char *refname = "test_read_format_zip_msdos.zip";
90 	struct archive *a;
91 	char *p;
92 	size_t s;
93 
94 	extract_reference_file(refname);
95 
96 	/* Verify with seeking reader. */
97 	assert((a = archive_read_new()) != NULL);
98 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
99 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
100 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 17));
101 	verify(a, 0);
102 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
103 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
104 
105 	/* Verify with streaming reader. */
106 	p = slurpfile(&s, refname);
107 	assert((a = archive_read_new()) != NULL);
108 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
109 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
110 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 31));
111 	verify(a, 1);
112 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
113 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
114 
115 	free(p);
116 }
117