1/*-
2 * Copyright (c) 2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "test.h"
29__FBSDID("$FreeBSD$");
30
31DEFINE_TEST(test_write_filter_gzip_timestamp)
32{
33	struct archive_entry *ae;
34	struct archive* a;
35	char *buff, *data;
36	size_t buffsize, datasize;
37	size_t used1;
38	int r, use_prog = 0;
39
40	buffsize = 10000;
41	assert(NULL != (buff = (char *)malloc(buffsize)));
42	if (buff == NULL)
43		return;
44
45	datasize = 10000;
46	assert(NULL != (data = (char *)malloc(datasize)));
47	if (data == NULL) {
48		free(buff);
49		return;
50	}
51	memset(data, 0, datasize);
52
53	/* Test1: set "gzip:timestamp" option. */
54	assert((a = archive_write_new()) != NULL);
55	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
56	r = archive_write_add_filter_gzip(a);
57	if (r != ARCHIVE_OK) {
58		if (canGzip() && r == ARCHIVE_WARN)
59			use_prog = 1;
60		else {
61			skipping("gzip writing not supported on this platform");
62			assertEqualInt(ARCHIVE_OK, archive_write_free(a));
63			free(buff);
64			free(data);
65			return;
66		}
67	}
68	assertEqualIntA(a, ARCHIVE_OK,
69	    archive_write_set_options(a, "gzip:timestamp"));
70	assertEqualIntA(a, ARCHIVE_OK,
71	    archive_write_set_bytes_per_block(a, 10));
72	assertEqualInt(ARCHIVE_FILTER_GZIP, archive_filter_code(a, 0));
73	assertEqualString("gzip", archive_filter_name(a, 0));
74	assertEqualIntA(a, ARCHIVE_OK,
75	    archive_write_open_memory(a, buff, buffsize, &used1));
76	assert((ae = archive_entry_new()) != NULL);
77	archive_entry_set_filetype(ae, AE_IFREG);
78	archive_entry_set_size(ae, datasize);
79	archive_entry_copy_pathname(ae, "file");
80	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
81	assertEqualIntA(a, datasize, archive_write_data(a, data, datasize));
82	archive_entry_free(ae);
83	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
84	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
85	failure("Timestamp should be recorded");
86	assert(memcmp(buff + 4, "\x00\x00\x00\x00", 4) != 0);
87
88	/* Test2: set "gzip:!timestamp" option. */
89	assert((a = archive_write_new()) != NULL);
90	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
91	assertEqualIntA(a, (use_prog)?ARCHIVE_WARN:ARCHIVE_OK,
92	    archive_write_add_filter_gzip(a));
93	assertEqualIntA(a, ARCHIVE_OK,
94	    archive_write_set_options(a, "gzip:!timestamp"));
95	assertEqualIntA(a, ARCHIVE_OK,
96	    archive_write_set_bytes_per_block(a, 10));
97	assertEqualInt(ARCHIVE_FILTER_GZIP, archive_filter_code(a, 0));
98	assertEqualString("gzip", archive_filter_name(a, 0));
99	assertEqualIntA(a, ARCHIVE_OK,
100	    archive_write_open_memory(a, buff, buffsize, &used1));
101	assert((ae = archive_entry_new()) != NULL);
102	archive_entry_set_filetype(ae, AE_IFREG);
103	archive_entry_set_size(ae, datasize);
104	archive_entry_copy_pathname(ae, "file");
105	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
106	assertEqualIntA(a, datasize, archive_write_data(a, data, datasize));
107	archive_entry_free(ae);
108	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
109	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
110	failure("Timestamp should not be recorded");
111	assertEqualMem(buff + 4, "\x00\x00\x00\x00", 4);
112
113	/*
114	 * Clean up.
115	 */
116	free(data);
117	free(buff);
118}
119