1/*-
2 * Copyright (C) 2014 Sebastian Freundt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "test.h"
27
28static void test_read(struct archive *a, char *buff, size_t used, char *filedata)
29{
30	struct archive_entry *ae;
31	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_none(a));
32	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
33
34	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
35	assertEqualIntA(a, 9, archive_read_data(a, filedata, 10));
36	assertEqualMem(filedata, "12345678", 9);
37	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
38	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
39}
40
41DEFINE_TEST(test_write_format_warc)
42{
43	char filedata[64];
44	struct archive *a;
45	struct archive_entry *ae;
46	const size_t bsiz = 2048U;
47	char *buff;
48	size_t used;
49
50	buff = malloc(bsiz);
51
52	/* Create a new archive in memory. */
53	assert((a = archive_write_new()) != NULL);
54	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
55	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
56	assertEqualIntA(a, ARCHIVE_OK,
57	    archive_write_open_memory(a, buff, bsiz, &used));
58
59	/*
60	 * Write a file to it.
61	 */
62	assert((ae = archive_entry_new()) != NULL);
63	archive_entry_set_pathname(ae, "test");
64	archive_entry_set_filetype(ae, AE_IFREG);
65	archive_entry_set_size(ae, 9);
66	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
67	archive_entry_free(ae);
68	assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
69	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
70	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
71
72	/*
73	 * Read from it.
74	 */
75	assert((a = archive_read_new()) != NULL);
76	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_warc(a));
77	test_read(a, buff, used, filedata);
78
79	assert((a = archive_read_new()) != NULL);
80	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_by_code(a, ARCHIVE_FORMAT_WARC));
81	test_read(a, buff, used, filedata);
82
83	assert((a = archive_read_new()) != NULL);
84	assertEqualIntA(a, ARCHIVE_OK, archive_read_set_format(a, ARCHIVE_FORMAT_WARC));
85	test_read(a, buff, used, filedata);
86
87	/* Create a new archive */
88	assert((a = archive_write_new()) != NULL);
89	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
90	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
91	assertEqualIntA(a, ARCHIVE_OK,
92		archive_write_open_memory(a, buff, bsiz, &used));
93
94	/* write first file: that should succeed */
95	assert((ae = archive_entry_new()) != NULL);
96	archive_entry_set_pathname(ae, "test");
97	archive_entry_set_filetype(ae, AE_IFREG);
98	archive_entry_set_size(ae, 9);
99	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
100	archive_entry_free(ae);
101	assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
102
103	/* write second file: should succeed as well */
104	assert((ae = archive_entry_new()) != NULL);
105	archive_entry_set_pathname(ae, "test2");
106	archive_entry_set_filetype(ae, AE_IFREG);
107	archive_entry_set_size(ae, 9);
108	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
109	archive_entry_free(ae);
110	assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
111
112	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
113	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
114
115	/* Create a new archive */
116	assert((a = archive_write_new()) != NULL);
117	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
118	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
119	assertEqualIntA(a, ARCHIVE_OK,
120		archive_write_open_memory(a, buff, bsiz, &used));
121
122	/* write a directory: this should fail */
123	assert((ae = archive_entry_new()) != NULL);
124	archive_entry_copy_pathname(ae, "dir");
125	archive_entry_set_filetype(ae, AE_IFDIR);
126	archive_entry_set_size(ae, 512);
127	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
128	archive_entry_free(ae);
129
130	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
131	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
132
133	/* test whether last archive is indeed empty */
134	assert((a = archive_read_new()) != NULL);
135	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
136	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
137	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
138
139	/* Test EOF */
140	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
141	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
142	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
143
144	free(buff);
145}
146