1 /*-
2  * Copyright (c) 2010-2011 Michihiro NAKAJIMA
3  * Copyright (c) 2008 Anselm Strauss
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 
27 /*
28  * Development supported by Google Summer of Code 2008.
29  */
30 
31 #include "test.h"
32 __FBSDID("$FreeBSD$");
33 
34 DEFINE_TEST(test_write_format_xar_empty)
35 {
36 	struct archive *a;
37 	struct archive_entry *ae;
38 	char buff[256];
39 	size_t used;
40 
41 	/* Xar format: Create a new archive in memory. */
42 	assert((a = archive_write_new()) != NULL);
43 	if (archive_write_set_format_xar(a) != ARCHIVE_OK) {
44 		skipping("xar is not supported on this platform");
45 		assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
46 		return;
47 	}
48 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
49 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
50 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
51 	assertEqualIntA(a, ARCHIVE_OK,
52 	    archive_write_open_memory(a, buff, sizeof(buff), &used));
53 
54 	/* Add "." entry which must be ignored. */
55 	assert((ae = archive_entry_new()) != NULL);
56 	archive_entry_set_atime(ae, 2, 0);
57 	archive_entry_set_ctime(ae, 4, 0);
58 	archive_entry_set_mtime(ae, 5, 0);
59 	archive_entry_copy_pathname(ae, ".");
60 	archive_entry_set_mode(ae, S_IFDIR | 0755);
61 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
62 	archive_entry_free(ae);
63 
64 	/* Add ".." entry which must be ignored. */
65 	assert((ae = archive_entry_new()) != NULL);
66 	archive_entry_set_atime(ae, 2, 0);
67 	archive_entry_set_ctime(ae, 4, 0);
68 	archive_entry_set_mtime(ae, 5, 0);
69 	archive_entry_copy_pathname(ae, "..");
70 	archive_entry_set_mode(ae, S_IFDIR | 0755);
71 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
72 	archive_entry_free(ae);
73 
74 	/* Add "/" entry which must be ignored. */
75 	assert((ae = archive_entry_new()) != NULL);
76 	archive_entry_set_atime(ae, 2, 0);
77 	archive_entry_set_ctime(ae, 4, 0);
78 	archive_entry_set_mtime(ae, 5, 0);
79 	archive_entry_copy_pathname(ae, "/");
80 	archive_entry_set_mode(ae, S_IFDIR | 0755);
81 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
82 	archive_entry_free(ae);
83 
84 	/* Add "../" entry which must be ignored. */
85 	assert((ae = archive_entry_new()) != NULL);
86 	archive_entry_set_atime(ae, 2, 0);
87 	archive_entry_set_ctime(ae, 4, 0);
88 	archive_entry_set_mtime(ae, 5, 0);
89 	archive_entry_copy_pathname(ae, "../");
90 	archive_entry_set_mode(ae, S_IFDIR | 0755);
91 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
92 	archive_entry_free(ae);
93 
94 	/* Add "../../." entry which must be ignored. */
95 	assert((ae = archive_entry_new()) != NULL);
96 	archive_entry_set_atime(ae, 2, 0);
97 	archive_entry_set_ctime(ae, 4, 0);
98 	archive_entry_set_mtime(ae, 5, 0);
99 	archive_entry_copy_pathname(ae, "../../.");
100 	archive_entry_set_mode(ae, S_IFDIR | 0755);
101 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
102 	archive_entry_free(ae);
103 
104 	/* Add "..//.././" entry which must be ignored. */
105 	assert((ae = archive_entry_new()) != NULL);
106 	archive_entry_set_atime(ae, 2, 0);
107 	archive_entry_set_ctime(ae, 4, 0);
108 	archive_entry_set_mtime(ae, 5, 0);
109 	archive_entry_copy_pathname(ae, "..//.././");
110 	archive_entry_set_mode(ae, S_IFDIR | 0755);
111 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
112 	archive_entry_free(ae);
113 
114 	/* Close out the archive without writing anything. */
115 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
116 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
117 
118 	/* Verify the correct format for an empty Xar archive. */
119 	assertEqualInt(used, 0);
120 }
121