1248590Smm/*-
2248590Smm * Copyright (c) 2003-2007 Tim Kientzle
3248590Smm * Copyright (c) 2012 Michihiro NAKAJIMA
4248590Smm * All rights reserved.
5248590Smm *
6248590Smm * Redistribution and use in source and binary forms, with or without
7248590Smm * modification, are permitted provided that the following conditions
8248590Smm * are met:
9248590Smm * 1. Redistributions of source code must retain the above copyright
10248590Smm *    notice, this list of conditions and the following disclaimer.
11248590Smm * 2. Redistributions in binary form must reproduce the above copyright
12248590Smm *    notice, this list of conditions and the following disclaimer in the
13248590Smm *    documentation and/or other materials provided with the distribution.
14248590Smm *
15248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25248590Smm */
26248590Smm#include "test.h"
27248590Smm__FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_compress_program.c 201247 2009-12-30 05:59:21Z kientzle $");
28248590Smm
29299529Smmstatic char buff[1000000];
30299529Smmstatic char buff2[64];
31248590Smm
32248590SmmDEFINE_TEST(test_write_filter_program)
33248590Smm{
34248590Smm	struct archive_entry *ae;
35248590Smm	struct archive *a;
36248590Smm	size_t used;
37248590Smm	int blocksize = 1024;
38248590Smm	int r;
39248590Smm
40248590Smm	if (!canGzip()) {
41248590Smm		skipping("Cannot run 'gzip'");
42248590Smm		return;
43248590Smm	}
44248590Smm	/* NOTE: Setting blocksize=1024 will cause gunzip failure because
45248590Smm	 * it add extra bytes that gunzip ignores with its warning and
46248590Smm	 * exit code 1. So we should set blocksize=1 in order not to
47248590Smm	 * yield the extra bytes when using gunzip. */
48248590Smm	assert((a = archive_read_new()) != NULL);
49248590Smm	r = archive_read_support_filter_gzip(a);
50248590Smm	if (r != ARCHIVE_OK && canGzip())
51248590Smm		blocksize = 1;
52248590Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
53248590Smm
54248590Smm	/* Create a new archive in memory. */
55248590Smm	/* Write it through an external "gzip" program. */
56248590Smm	assert((a = archive_write_new()) != NULL);
57248590Smm	assertA(0 == archive_write_set_format_ustar(a));
58248590Smm	r = archive_write_add_filter_program(a, "gzip -6");
59248590Smm	if (r == ARCHIVE_FATAL) {
60248590Smm		skipping("Write compression via external "
61248590Smm		    "program unsupported on this platform");
62248590Smm		archive_write_free(a);
63248590Smm		return;
64248590Smm	}
65248590Smm	assertA(0 == archive_write_set_bytes_per_block(a, blocksize));
66248590Smm	assertA(0 == archive_write_set_bytes_in_last_block(a, blocksize));
67248590Smm	assertA(blocksize == archive_write_get_bytes_in_last_block(a));
68248590Smm	assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
69248590Smm	assertA(blocksize == archive_write_get_bytes_in_last_block(a));
70248590Smm
71248590Smm	/*
72248590Smm	 * Write a file to it.
73248590Smm	 */
74248590Smm	assert((ae = archive_entry_new()) != NULL);
75248590Smm	archive_entry_set_mtime(ae, 1, 10);
76248590Smm	archive_entry_copy_pathname(ae, "file");
77248590Smm	archive_entry_set_mode(ae, S_IFREG | 0755);
78248590Smm	archive_entry_set_size(ae, 8);
79248590Smm
80248590Smm	assertA(0 == archive_write_header(a, ae));
81248590Smm	archive_entry_free(ae);
82248590Smm	assertA(8 == archive_write_data(a, "12345678", 9));
83248590Smm
84248590Smm	/* Close out the archive. */
85248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
86248590Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
87248590Smm
88248590Smm	/*
89248590Smm	 * Now, read the data back through the built-in gzip support.
90248590Smm	 */
91248590Smm	assert((a = archive_read_new()) != NULL);
92248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
93248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
94248590Smm	r = archive_read_support_filter_gzip(a);
95248590Smm	/* The compression_gzip() handler will fall back to gunzip
96248590Smm	 * automatically, but if we know gunzip isn't available, then
97248590Smm	 * skip the rest. */
98248590Smm	if (r != ARCHIVE_OK && !canGzip()) {
99248590Smm		skipping("No libz and no gunzip program, "
100248590Smm		    "unable to verify gzip compression");
101248590Smm		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
102248590Smm		return;
103248590Smm	}
104248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
105248590Smm
106248590Smm	if (!assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae))) {
107248590Smm		archive_read_free(a);
108248590Smm		return;
109248590Smm	}
110248590Smm
111248590Smm	assertEqualInt(1, archive_entry_mtime(ae));
112248590Smm	assertEqualInt(0, archive_entry_atime(ae));
113248590Smm	assertEqualInt(0, archive_entry_ctime(ae));
114248590Smm	assertEqualString("file", archive_entry_pathname(ae));
115248590Smm	assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
116248590Smm	assertEqualInt(8, archive_entry_size(ae));
117248590Smm	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
118248590Smm	assertEqualMem(buff2, "12345678", 8);
119248590Smm
120248590Smm	/* Verify the end of the archive. */
121248590Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
122248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
123248590Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
124248590Smm}
125