1299425Smm/*-
2299425Smm * Copyright (C) 2014 Sebastian Freundt
3299425Smm * All rights reserved.
4299425Smm *
5299425Smm * Redistribution and use in source and binary forms, with or without
6299425Smm * modification, are permitted provided that the following conditions
7299425Smm * are met:
8299425Smm * 1. Redistributions of source code must retain the above copyright
9299425Smm *    notice, this list of conditions and the following disclaimer.
10299425Smm * 2. Redistributions in binary form must reproduce the above copyright
11299425Smm *    notice, this list of conditions and the following disclaimer in the
12299425Smm *    documentation and/or other materials provided with the distribution.
13299425Smm *
14299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24299425Smm */
25299425Smm
26299425Smm#include "test.h"
27299425Smm__FBSDID("$FreeBSD$");
28299425Smm
29299425SmmDEFINE_TEST(test_write_format_warc_empty)
30299425Smm{
31299425Smm	struct archive *a;
32299425Smm	struct archive_entry *ae;
33299425Smm	char buff[512U];
34299425Smm	size_t used;
35299425Smm
36299425Smm	/* Create a new archive in memory. */
37299425Smm	assert((a = archive_write_new()) != NULL);
38299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
39299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
40299425Smm	assertEqualIntA(a, ARCHIVE_OK,
41299425Smm	    archive_write_open_memory(a, buff, sizeof(buff), &used));
42299425Smm
43299425Smm	/* Add "." entry which must be ignored. */
44299425Smm	assert((ae = archive_entry_new()) != NULL);
45299425Smm	archive_entry_set_atime(ae, 2, 0);
46299425Smm	archive_entry_set_ctime(ae, 4, 0);
47299425Smm	archive_entry_set_mtime(ae, 5, 0);
48299425Smm	archive_entry_copy_pathname(ae, ".");
49299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
50299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
51299425Smm	archive_entry_free(ae);
52299425Smm
53299425Smm	/* Add ".." entry which must be ignored. */
54299425Smm	assert((ae = archive_entry_new()) != NULL);
55299425Smm	archive_entry_set_atime(ae, 2, 0);
56299425Smm	archive_entry_set_ctime(ae, 4, 0);
57299425Smm	archive_entry_set_mtime(ae, 5, 0);
58299425Smm	archive_entry_copy_pathname(ae, "..");
59299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
60299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
61299425Smm	archive_entry_free(ae);
62299425Smm
63299425Smm	/* Add "/" entry which must be ignored. */
64299425Smm	assert((ae = archive_entry_new()) != NULL);
65299425Smm	archive_entry_set_atime(ae, 2, 0);
66299425Smm	archive_entry_set_ctime(ae, 4, 0);
67299425Smm	archive_entry_set_mtime(ae, 5, 0);
68299425Smm	archive_entry_copy_pathname(ae, "/");
69299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
70299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
71299425Smm	archive_entry_free(ae);
72299425Smm
73299425Smm	/* Add "../" entry which must be ignored. */
74299425Smm	assert((ae = archive_entry_new()) != NULL);
75299425Smm	archive_entry_set_atime(ae, 2, 0);
76299425Smm	archive_entry_set_ctime(ae, 4, 0);
77299425Smm	archive_entry_set_mtime(ae, 5, 0);
78299425Smm	archive_entry_copy_pathname(ae, "../");
79299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
80299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
81299425Smm	archive_entry_free(ae);
82299425Smm
83299425Smm	/* Add "../../." entry which must be ignored. */
84299425Smm	assert((ae = archive_entry_new()) != NULL);
85299425Smm	archive_entry_set_atime(ae, 2, 0);
86299425Smm	archive_entry_set_ctime(ae, 4, 0);
87299425Smm	archive_entry_set_mtime(ae, 5, 0);
88299425Smm	archive_entry_copy_pathname(ae, "../../.");
89299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
90299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
91299425Smm	archive_entry_free(ae);
92299425Smm
93299425Smm	/* Add "..//.././" entry which must be ignored. */
94299425Smm	assert((ae = archive_entry_new()) != NULL);
95299425Smm	archive_entry_set_atime(ae, 2, 0);
96299425Smm	archive_entry_set_ctime(ae, 4, 0);
97299425Smm	archive_entry_set_mtime(ae, 5, 0);
98299425Smm	archive_entry_copy_pathname(ae, "..//.././");
99299425Smm	archive_entry_set_mode(ae, S_IFDIR | 0755);
100299425Smm	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
101299425Smm	archive_entry_free(ae);
102299425Smm
103299425Smm	/* Close out the archive without writing anything. */
104299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
105299425Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
106299425Smm
107299425Smm	/* Test whether last archive is empty indeed. */
108299425Smm	assert((a = archive_read_new()) != NULL);
109299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
110299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
111299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
112299425Smm
113299425Smm	/* Test EOF */
114299425Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
115299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
116299425Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
117299425Smm}
118