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  *    in this position and unchanged.
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 #include "test.h"
28 
29 DEFINE_TEST(test_read_format_warc)
30 {
31 	char buff[256U];
32 	const char reffile[] = "test_read_format_warc.warc";
33 	struct archive_entry *ae;
34 	struct archive *a;
35 
36 	extract_reference_file(reffile);
37 	assert((a = archive_read_new()) != NULL);
38 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
39 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
40 	assertEqualIntA(a, ARCHIVE_OK,
41 	    archive_read_open_filename(a, reffile, 10240));
42 
43 	/* First Entry */
44 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
45 	assertEqualString("sometest.txt", archive_entry_pathname(ae));
46 	assertEqualInt(1402399833, archive_entry_mtime(ae));
47 	assertEqualInt(0, archive_entry_uid(ae));
48 	assertEqualInt(0, archive_entry_gid(ae));
49 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
50 	assertEqualInt(65, archive_entry_size(ae));
51 	assertEqualInt(65, archive_read_data(a, buff, sizeof(buff)));
52 	assertEqualMem(buff, "This is a sample text file for libarchive's WARC reader/writer.\n\n", 65);
53 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
54 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
55 
56 	/* Second Entry */
57 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
58 	assertEqualString("moretest.txt", archive_entry_pathname(ae));
59 	assertEqualInt(1402399884, archive_entry_mtime(ae));
60 	assertEqualInt(0, archive_entry_uid(ae));
61 	assertEqualInt(0, archive_entry_gid(ae));
62 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
63 	assertEqualInt(76, archive_entry_size(ae));
64 	assertA(76 == archive_read_data(a, buff, sizeof(buff)));
65 	assertEqualMem(buff, "The beauty is that WARC remains ASCII only iff all contents are ASCII only.\n", 76);
66 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
67 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
68 
69 	/* Test EOF */
70 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
71 	assertEqualInt(2, archive_file_count(a));
72 
73 	/* Verify archive format. */
74 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
75 	assertEqualIntA(a, ARCHIVE_FORMAT_WARC, archive_format(a));
76 
77 	/* Verify closing and resource freeing */
78 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
79 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
80 }
81