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
30DEFINE_TEST(test_write_filter_gzip_timestamp)
31{
32	struct archive_entry *ae;
33	struct archive* a;
34	char *buff, *data;
35	size_t buffsize, datasize;
36	size_t used1;
37	int r, use_prog = 0;
38
39	buffsize = 10000;
40	assert(NULL != (buff = (char *)malloc(buffsize)));
41	if (buff == NULL)
42		return;
43
44	datasize = 10000;
45	assert(NULL != (data = (char *)malloc(datasize)));
46	if (data == NULL) {
47		free(buff);
48		return;
49	}
50	memset(data, 0, datasize);
51
52	/* Test1: set "gzip:timestamp" option. */
53	assert((a = archive_write_new()) != NULL);
54	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
55	r = archive_write_add_filter_gzip(a);
56	if (r != ARCHIVE_OK) {
57		if (canGzip() && r == ARCHIVE_WARN)
58			use_prog = 1;
59		else {
60			skipping("gzip writing not supported on this platform");
61			assertEqualInt(ARCHIVE_OK, archive_write_free(a));
62			free(buff);
63			free(data);
64			return;
65		}
66	}
67	assertEqualIntA(a, ARCHIVE_OK,
68	    archive_write_set_options(a, "gzip:timestamp"));
69	assertEqualIntA(a, ARCHIVE_OK,
70	    archive_write_set_bytes_per_block(a, 10));
71	assertEqualInt(ARCHIVE_FILTER_GZIP, archive_filter_code(a, 0));
72	assertEqualString("gzip", archive_filter_name(a, 0));
73	assertEqualIntA(a, ARCHIVE_OK,
74	    archive_write_open_memory(a, buff, buffsize, &used1));
75	assert((ae = archive_entry_new()) != NULL);
76	archive_entry_set_filetype(ae, AE_IFREG);
77	archive_entry_set_size(ae, datasize);
78	archive_entry_copy_pathname(ae, "file");
79	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
80	assertEqualIntA(a, datasize, archive_write_data(a, data, datasize));
81	archive_entry_free(ae);
82	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
83	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
84	failure("Timestamp should be recorded");
85	assert(memcmp(buff + 4, "\x00\x00\x00\x00", 4) != 0);
86
87	/* Test2: set "gzip:!timestamp" option. */
88	assert((a = archive_write_new()) != NULL);
89	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
90	assertEqualIntA(a, (use_prog)?ARCHIVE_WARN:ARCHIVE_OK,
91	    archive_write_add_filter_gzip(a));
92	assertEqualIntA(a, ARCHIVE_OK,
93	    archive_write_set_options(a, "gzip:!timestamp"));
94	assertEqualIntA(a, ARCHIVE_OK,
95	    archive_write_set_bytes_per_block(a, 10));
96	assertEqualInt(ARCHIVE_FILTER_GZIP, archive_filter_code(a, 0));
97	assertEqualString("gzip", archive_filter_name(a, 0));
98	assertEqualIntA(a, ARCHIVE_OK,
99	    archive_write_open_memory(a, buff, buffsize, &used1));
100	assert((ae = archive_entry_new()) != NULL);
101	archive_entry_set_filetype(ae, AE_IFREG);
102	archive_entry_set_size(ae, datasize);
103	archive_entry_copy_pathname(ae, "file");
104	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
105	assertEqualIntA(a, datasize, archive_write_data(a, data, datasize));
106	archive_entry_free(ae);
107	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
108	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
109	failure("Timestamp should not be recorded");
110	assertEqualMem(buff + 4, "\x00\x00\x00\x00", 4);
111
112	/*
113	 * Clean up.
114	 */
115	free(data);
116	free(buff);
117}
118