xref: /freebsd/contrib/libarchive/libarchive/test/test_read_format_raw.c (revision b9128a37faafede823eb456aa65a11ac69997284)
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 
30 DEFINE_TEST(test_read_format_raw)
31 {
32 	char buff[512];
33 	struct archive_entry *ae;
34 	struct archive *a;
35 	const char *reffile1 = "test_read_format_raw.data";
36 	const char *reffile2 = "test_read_format_raw.data.Z";
37 	const char *reffile3 = "test_read_format_raw.bufr";
38 #ifdef HAVE_ZLIB_H
39 	const char *reffile4 = "test_read_format_raw.data.gz";
40 #endif
41 
42 	/* First, try pulling data out of an uninterpretable file. */
43 	extract_reference_file(reffile1);
44 	assert((a = archive_read_new()) != NULL);
45 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
46 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
47 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
48 	assertEqualIntA(a, ARCHIVE_OK,
49 	    archive_read_open_filename(a, reffile1, 512));
50 
51 	/* First (and only!) Entry */
52 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
53 	assertEqualString("data", archive_entry_pathname(ae));
54 	/* Most fields should be unset (unknown) */
55 	assert(!archive_entry_size_is_set(ae));
56 	assert(!archive_entry_atime_is_set(ae));
57 	assert(!archive_entry_ctime_is_set(ae));
58 	assert(!archive_entry_mtime_is_set(ae));
59 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
60 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
61 	assertEqualInt(4, archive_read_data(a, buff, 32));
62 	assertEqualMem(buff, "foo\n", 4);
63 
64 	/* Test EOF */
65 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
66 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
67 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
68 
69 
70 	/* Second, try the same with a compressed file. */
71 	extract_reference_file(reffile2);
72 	assert((a = archive_read_new()) != NULL);
73 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
74 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_by_code(a, ARCHIVE_FORMAT_RAW));
75 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
76 	assertEqualIntA(a, ARCHIVE_OK,
77 	    archive_read_open_filename(a, reffile2, 1));
78 
79 	/* First (and only!) Entry */
80 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
81 	assertEqualString("data", archive_entry_pathname(ae));
82 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
83 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
84 	/* Most fields should be unset (unknown) */
85 	assert(!archive_entry_size_is_set(ae));
86 	assert(!archive_entry_atime_is_set(ae));
87 	assert(!archive_entry_ctime_is_set(ae));
88 	assert(!archive_entry_mtime_is_set(ae));
89 	assertEqualInt(4, archive_read_data(a, buff, 32));
90 	assertEqualMem(buff, "foo\n", 4);
91 
92 	/* Test EOF */
93 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
94 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
95 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
96 
97 
98 	/* Third, try with file which fooled us in past - appeared to be tar. */
99 	extract_reference_file(reffile3);
100 	assert((a = archive_read_new()) != NULL);
101 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
102 	assertEqualIntA(a, ARCHIVE_OK, archive_read_set_format(a, ARCHIVE_FORMAT_RAW));
103 	assertEqualIntA(a, ARCHIVE_OK,
104 	    archive_read_open_filename(a, reffile3, 1));
105 
106 	/* First (and only!) Entry */
107 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
108 	assertEqualString("data", archive_entry_pathname(ae));
109 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
110 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
111 	/* Most fields should be unset (unknown) */
112 	assert(!archive_entry_size_is_set(ae));
113 	assert(!archive_entry_atime_is_set(ae));
114 	assert(!archive_entry_ctime_is_set(ae));
115 	assert(!archive_entry_mtime_is_set(ae));
116 
117 	/* Test EOF */
118 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
119 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
120 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
121 
122 #ifdef HAVE_ZLIB_H
123 	/* Fourth, try with gzip which has metadata. */
124 	extract_reference_file(reffile4);
125 	assert((a = archive_read_new()) != NULL);
126 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
127 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
128 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
129 	assertEqualIntA(a, ARCHIVE_OK,
130 	    archive_read_open_filename(a, reffile4, 1));
131 
132 	/* First (and only!) Entry */
133 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
134 	assertEqualString("test-file-name.data", archive_entry_pathname(ae));
135 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
136 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
137 	assert(archive_entry_mtime_is_set(ae));
138 	assertEqualIntA(a, archive_entry_mtime(ae), 0x5cbafd25);
139 	/* Most fields should be unset (unknown) */
140 	assert(!archive_entry_size_is_set(ae));
141 	assert(!archive_entry_atime_is_set(ae));
142 	assert(!archive_entry_ctime_is_set(ae));
143 
144 	/* Test EOF */
145 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
146 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
147 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
148 #endif
149 }
150