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 
32 DEFINE_TEST(test_read_format_ar)
33 {
34 	char buff[64];
35 	const char reffile[] = "test_read_format_ar.ar";
36 	struct archive_entry *ae;
37 	struct archive *a;
38 
39 	extract_reference_file(reffile);
40 	assert((a = archive_read_new()) != NULL);
41 	assertA(0 == archive_read_support_filter_all(a));
42 	assertA(0 == archive_read_support_format_all(a));
43 	assertA(0 == archive_read_open_filename(a, reffile, 7));
44 
45 	/* Filename table.  */
46 	assertA(0 == archive_read_next_header(a, &ae));
47 	assertEqualString("//", archive_entry_pathname(ae));
48 	assertEqualInt(0, archive_entry_mtime(ae));
49 	assertEqualInt(0, archive_entry_uid(ae));
50 	assertEqualInt(0, archive_entry_gid(ae));
51 	assertEqualInt(0, archive_entry_size(ae));
52 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
53 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
54 
55 	/* First Entry */
56 	assertA(0 == archive_read_next_header(a, &ae));
57 	assertEqualString("yyytttsssaaafff.o", archive_entry_pathname(ae));
58 	assertEqualInt(1175465652, archive_entry_mtime(ae));
59 	assertEqualInt(1001, archive_entry_uid(ae));
60 	assertEqualInt(0, archive_entry_gid(ae));
61 	assert(8 == archive_entry_size(ae));
62 	assertA(8 == archive_read_data(a, buff, 10));
63 	assertEqualMem(buff, "55667788", 8);
64 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
65 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
66 
67 	/* Second Entry */
68 	assertA(0 == archive_read_next_header(a, &ae));
69 	assertEqualString("gghh.o", archive_entry_pathname(ae));
70 	assertEqualInt(1175465668, archive_entry_mtime(ae));
71 	assertEqualInt(1001, archive_entry_uid(ae));
72 	assertEqualInt(0, archive_entry_gid(ae));
73 	assert(4 == archive_entry_size(ae));
74 	assertA(4 == archive_read_data(a, buff, 10));
75 	assertEqualMem(buff, "3333", 4);
76 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
77 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
78 
79 	/* Third Entry */
80 	assertA(0 == archive_read_next_header(a, &ae));
81 	assertEqualString("hhhhjjjjkkkkllll.o", archive_entry_pathname(ae));
82 	assertEqualInt(1175465713, archive_entry_mtime(ae));
83 	assertEqualInt(1001, archive_entry_uid(ae));
84 	assertEqualInt(0, archive_entry_gid(ae));
85 	assert(9 == archive_entry_size(ae));
86 	assertA(9 == archive_read_data(a, buff, 9));
87 	assertEqualMem(buff, "987654321", 9);
88 
89 	/* Test EOF */
90 	assertA(1 == archive_read_next_header(a, &ae));
91 	assertEqualInt(4, archive_file_count(a));
92 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
93 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
94 }
95