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 *    in this position and unchanged.
11299425Smm * 2. Redistributions in binary form must reproduce the above copyright
12299425Smm *    notice, this list of conditions and the following disclaimer in the
13299425Smm *    documentation and/or other materials provided with the distribution.
14299425Smm *
15299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25299425Smm */
26299425Smm
27299425Smm#include "test.h"
28299425Smm__FBSDID("$FreeBSD$");
29299425Smm
30299425Smm
31299425SmmDEFINE_TEST(test_read_format_warc)
32299425Smm{
33299425Smm	char buff[256U];
34299425Smm	const char reffile[] = "test_read_format_warc.warc";
35299425Smm	struct archive_entry *ae;
36299425Smm	struct archive *a;
37299425Smm
38299425Smm	extract_reference_file(reffile);
39299425Smm	assert((a = archive_read_new()) != NULL);
40299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
41299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
42299425Smm	assertEqualIntA(a, ARCHIVE_OK,
43299425Smm	    archive_read_open_filename(a, reffile, 10240));
44299425Smm
45299425Smm	/* First Entry */
46299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
47299425Smm	assertEqualString("sometest.txt", archive_entry_pathname(ae));
48299425Smm	assertEqualInt(1402399833, archive_entry_mtime(ae));
49299425Smm	assertEqualInt(0, archive_entry_uid(ae));
50299425Smm	assertEqualInt(0, archive_entry_gid(ae));
51299425Smm	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
52299425Smm	assertEqualInt(65, archive_entry_size(ae));
53299425Smm	assertEqualInt(65, archive_read_data(a, buff, sizeof(buff)));
54299425Smm	assertEqualMem(buff, "This is a sample text file for libarchive's WARC reader/writer.\n\n", 65);
55299425Smm	assertEqualInt(archive_entry_is_encrypted(ae), 0);
56299425Smm	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
57299425Smm
58299425Smm	/* Second Entry */
59299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
60299425Smm	assertEqualString("moretest.txt", archive_entry_pathname(ae));
61299425Smm	assertEqualInt(1402399884, archive_entry_mtime(ae));
62299425Smm	assertEqualInt(0, archive_entry_uid(ae));
63299425Smm	assertEqualInt(0, archive_entry_gid(ae));
64299425Smm	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
65299425Smm	assertEqualInt(76, archive_entry_size(ae));
66299425Smm	assertA(76 == archive_read_data(a, buff, sizeof(buff)));
67299425Smm	assertEqualMem(buff, "The beauty is that WARC remains ASCII only iff all contents are ASCII only.\n", 76);
68299425Smm	assertEqualInt(archive_entry_is_encrypted(ae), 0);
69299425Smm	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
70299425Smm
71299425Smm	/* Test EOF */
72299425Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
73299425Smm	assertEqualInt(2, archive_file_count(a));
74299425Smm
75299425Smm	/* Verify archive format. */
76299425Smm	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
77299425Smm	assertEqualIntA(a, ARCHIVE_FORMAT_WARC, archive_format(a));
78299425Smm
79299425Smm	/* Verify closing and resource freeing */
80299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
81299425Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
82299425Smm}
83