1248590Smm/*-
2248590Smm * Copyright (c) 2007 Tim Kientzle
3248590Smm * All rights reserved.
4248590Smm *
5248590Smm * Redistribution and use in source and binary forms, with or without
6248590Smm * modification, are permitted provided that the following conditions
7248590Smm * are met:
8248590Smm * 1. Redistributions of source code must retain the above copyright
9248590Smm *    notice, this list of conditions and the following disclaimer
10248590Smm *    in this position and unchanged.
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
27248590Smm#include "test.h"
28248590Smm__FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_compress.c 189308 2009-03-03 17:02:51Z kientzle $");
29248590Smm
30248590Smm/*
31248590Smm * A basic exercise of compress reading and writing.
32248590Smm *
33248590Smm * TODO: Add a reference file and make sure we can decompress that.
34248590Smm */
35248590Smm
36248590SmmDEFINE_TEST(test_write_filter_compress)
37248590Smm{
38248590Smm	struct archive_entry *ae;
39248590Smm	struct archive* a;
40248590Smm	char *buff, *data;
41248590Smm	size_t buffsize, datasize;
42248590Smm	char path[16];
43248590Smm	size_t used;
44248590Smm	int i;
45248590Smm
46248590Smm	buffsize = 1000000;
47248590Smm	assert(NULL != (buff = (char *)malloc(buffsize)));
48248590Smm
49248590Smm	datasize = 10000;
50248590Smm	assert(NULL != (data = (char *)malloc(datasize)));
51248590Smm	memset(data, 0, datasize);
52248590Smm
53248590Smm	assert((a = archive_write_new()) != NULL);
54248590Smm	assertEqualIntA(a, ARCHIVE_OK,
55248590Smm	    archive_write_set_format_ustar(a));
56248590Smm	assertEqualIntA(a, ARCHIVE_OK,
57248590Smm	    archive_write_add_filter_compress(a));
58248590Smm	assertEqualIntA(a, ARCHIVE_OK,
59248590Smm	    archive_write_open_memory(a, buff, buffsize, &used));
60248590Smm
61248590Smm	for (i = 0; i < 100; i++) {
62248590Smm		sprintf(path, "file%03d", i);
63248590Smm		assert((ae = archive_entry_new()) != NULL);
64248590Smm		archive_entry_copy_pathname(ae, path);
65248590Smm		archive_entry_set_size(ae, datasize);
66248590Smm		archive_entry_set_filetype(ae, AE_IFREG);
67248590Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
68248590Smm		assertEqualInt(datasize,
69248590Smm		    archive_write_data(a, data, datasize));
70248590Smm		archive_entry_free(ae);
71248590Smm	}
72248590Smm
73248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
74248590Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
75248590Smm
76248590Smm	/*
77248590Smm	 * Now, read the data back.
78248590Smm	 */
79248590Smm	assert((a = archive_read_new()) != NULL);
80248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
81248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
82248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
83248590Smm
84248590Smm
85248590Smm	for (i = 0; i < 100; i++) {
86248590Smm		sprintf(path, "file%03d", i);
87248590Smm		if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
88248590Smm			break;
89248590Smm		assertEqualString(path, archive_entry_pathname(ae));
90248590Smm		assertEqualInt((int)datasize, archive_entry_size(ae));
91248590Smm	}
92248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
93248590Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
94248590Smm
95248590Smm	free(data);
96248590Smm	free(buff);
97248590Smm}
98