2 * Copyright (C) 2014 Sebastian Freundt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 __FBSDID("$FreeBSD$");
29 DEFINE_TEST(test_write_format_warc)
33 struct archive_entry *ae;
34 const size_t bsiz = 2048U;
40 /* Create a new archive in memory. */
41 assert((a = archive_write_new()) != NULL);
42 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
43 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
44 assertEqualIntA(a, ARCHIVE_OK,
45 archive_write_open_memory(a, buff, bsiz, &used));
50 assert((ae = archive_entry_new()) != NULL);
51 archive_entry_set_pathname(ae, "test");
52 archive_entry_set_filetype(ae, AE_IFREG);
53 archive_entry_set_size(ae, 9);
54 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
55 archive_entry_free(ae);
56 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
57 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
58 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
63 assert((a = archive_read_new()) != NULL);
64 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_warc(a));
65 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_none(a));
66 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
68 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
69 assertEqualIntA(a, 9, archive_read_data(a, filedata, 10));
70 assertEqualMem(filedata, "12345678", 9);
71 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
72 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
74 /* Create a new archive */
75 assert((a = archive_write_new()) != NULL);
76 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
77 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
78 assertEqualIntA(a, ARCHIVE_OK,
79 archive_write_open_memory(a, buff, bsiz, &used));
81 /* write first file: that should succeed */
82 assert((ae = archive_entry_new()) != NULL);
83 archive_entry_set_pathname(ae, "test");
84 archive_entry_set_filetype(ae, AE_IFREG);
85 archive_entry_set_size(ae, 9);
86 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
87 archive_entry_free(ae);
88 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
90 /* write second file: should succeed as well */
91 assert((ae = archive_entry_new()) != NULL);
92 archive_entry_set_pathname(ae, "test2");
93 archive_entry_set_filetype(ae, AE_IFREG);
94 archive_entry_set_size(ae, 9);
95 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
96 archive_entry_free(ae);
97 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9));
99 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
100 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
102 /* Create a new archive */
103 assert((a = archive_write_new()) != NULL);
104 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a));
105 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
106 assertEqualIntA(a, ARCHIVE_OK,
107 archive_write_open_memory(a, buff, bsiz, &used));
109 /* write a directory: this should fail */
110 assert((ae = archive_entry_new()) != NULL);
111 archive_entry_copy_pathname(ae, "dir");
112 archive_entry_set_filetype(ae, AE_IFDIR);
113 archive_entry_set_size(ae, 512);
114 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
115 archive_entry_free(ae);
117 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
118 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
120 /* test whether last archive is indeed empty */
121 assert((a = archive_read_new()) != NULL);
122 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
123 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
124 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
127 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
128 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
129 assertEqualInt(ARCHIVE_OK, archive_read_free(a));