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 __FBSDID("$FreeBSD$");
30 
31 DEFINE_TEST(test_read_format_raw)
32 {
33 	char buff[512];
34 	struct archive_entry *ae;
35 	struct archive *a;
36 	const char *reffile1 = "test_read_format_raw.data";
37 	const char *reffile2 = "test_read_format_raw.data.Z";
38 	const char *reffile3 = "test_read_format_raw.bufr";
39 
40 	/* First, try pulling data out of an uninterpretable file. */
41 	extract_reference_file(reffile1);
42 	assert((a = archive_read_new()) != NULL);
43 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
44 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
45 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
46 	assertEqualIntA(a, ARCHIVE_OK,
47 	    archive_read_open_filename(a, reffile1, 512));
48 
49 	/* First (and only!) Entry */
50 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
51 	assertEqualString("data", archive_entry_pathname(ae));
52 	/* Most fields should be unset (unknown) */
53 	assert(!archive_entry_size_is_set(ae));
54 	assert(!archive_entry_atime_is_set(ae));
55 	assert(!archive_entry_ctime_is_set(ae));
56 	assert(!archive_entry_mtime_is_set(ae));
57 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
58 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
59 	assertEqualInt(4, archive_read_data(a, buff, 32));
60 	assertEqualMem(buff, "foo\n", 4);
61 
62 	/* Test EOF */
63 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
64 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
65 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
66 
67 
68 	/* Second, try the same with a compressed file. */
69 	extract_reference_file(reffile2);
70 	assert((a = archive_read_new()) != NULL);
71 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
72 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
73 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
74 	assertEqualIntA(a, ARCHIVE_OK,
75 	    archive_read_open_filename(a, reffile2, 1));
76 
77 	/* First (and only!) Entry */
78 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
79 	assertEqualString("data", archive_entry_pathname(ae));
80 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
81 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
82 	/* Most fields should be unset (unknown) */
83 	assert(!archive_entry_size_is_set(ae));
84 	assert(!archive_entry_atime_is_set(ae));
85 	assert(!archive_entry_ctime_is_set(ae));
86 	assert(!archive_entry_mtime_is_set(ae));
87 	assertEqualInt(4, archive_read_data(a, buff, 32));
88 	assertEqualMem(buff, "foo\n", 4);
89 
90 	/* Test EOF */
91 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
92 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
93 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
94 
95 
96 	/* Third, try with file which fooled us in past - appeared to be tar. */
97 	extract_reference_file(reffile3);
98 	assert((a = archive_read_new()) != NULL);
99 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
100 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a));
101 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
102 	assertEqualIntA(a, ARCHIVE_OK,
103 	    archive_read_open_filename(a, reffile3, 1));
104 
105 	/* First (and only!) Entry */
106 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
107 	assertEqualString("data", archive_entry_pathname(ae));
108 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
109 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
110 	/* Most fields should be unset (unknown) */
111 	assert(!archive_entry_size_is_set(ae));
112 	assert(!archive_entry_atime_is_set(ae));
113 	assert(!archive_entry_ctime_is_set(ae));
114 	assert(!archive_entry_mtime_is_set(ae));
115 
116 	/* Test EOF */
117 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
118 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
119 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
120 }
121