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