1/*-
2 * Copyright (c) 2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "test.h"
28__FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_compress.c 189308 2009-03-03 17:02:51Z kientzle $");
29
30/*
31 * A basic exercise of compress reading and writing.
32 *
33 * TODO: Add a reference file and make sure we can decompress that.
34 */
35
36DEFINE_TEST(test_write_filter_compress)
37{
38	struct archive_entry *ae;
39	struct archive* a;
40	char *buff, *data;
41	size_t buffsize, datasize;
42	char path[16];
43	size_t used;
44	int i;
45
46	buffsize = 1000000;
47	assert(NULL != (buff = (char *)malloc(buffsize)));
48
49	datasize = 10000;
50	assert(NULL != (data = (char *)malloc(datasize)));
51	memset(data, 0, datasize);
52
53	assert((a = archive_write_new()) != NULL);
54	assertEqualIntA(a, ARCHIVE_OK,
55	    archive_write_set_format_ustar(a));
56	assertEqualIntA(a, ARCHIVE_OK,
57	    archive_write_add_filter_compress(a));
58	assertEqualIntA(a, ARCHIVE_OK,
59	    archive_write_open_memory(a, buff, buffsize, &used));
60
61	for (i = 0; i < 100; i++) {
62		sprintf(path, "file%03d", i);
63		assert((ae = archive_entry_new()) != NULL);
64		archive_entry_copy_pathname(ae, path);
65		archive_entry_set_size(ae, datasize);
66		archive_entry_set_filetype(ae, AE_IFREG);
67		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
68		assertEqualInt(datasize,
69		    archive_write_data(a, data, datasize));
70		archive_entry_free(ae);
71	}
72
73	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
74	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
75
76	/*
77	 * Now, read the data back.
78	 */
79	assert((a = archive_read_new()) != NULL);
80	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
81	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
82	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
83
84
85	for (i = 0; i < 100; i++) {
86		sprintf(path, "file%03d", i);
87		if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
88			break;
89		assertEqualString(path, archive_entry_pathname(ae));
90		assertEqualInt((int)datasize, archive_entry_size(ae));
91	}
92	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
93	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
94
95	free(data);
96	free(buff);
97}
98