1/*-
2 * Copyright (c) 2003-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 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "test.h"
26
27/*
28 * Test read/write of a 10M block of data in a single operation.
29 * Uses an in-memory archive with a single 10M entry.  Exercises
30 * archive_read_data() to ensure it can handle large blocks like
31 * this and also exercises archive_read_data_into_fd() (which
32 * had a bug relating to this, fixed in Nov 2006).
33 */
34
35#if defined(_WIN32) && !defined(__CYGWIN__)
36#define open _open
37#define close _close
38#endif
39
40static char buff1[11000000];
41static char buff2[10000000];
42static char buff3[10000000];
43
44DEFINE_TEST(test_read_data_large)
45{
46	struct archive_entry *ae;
47	struct archive *a;
48	char tmpfilename[] = "largefile";
49	int tmpfilefd;
50	FILE *f;
51	size_t used;
52
53	/* Create a new archive in memory. */
54	assert((a = archive_write_new()) != NULL);
55	assertA(0 == archive_write_set_format_ustar(a));
56	assertA(0 == archive_write_add_filter_none(a));
57	assertA(0 == archive_write_open_memory(a, buff1, sizeof(buff1), &used));
58
59	/*
60	 * Write a file (with random contents) to it.
61	 */
62	assert((ae = archive_entry_new()) != NULL);
63	archive_entry_copy_pathname(ae, "file");
64	archive_entry_set_mode(ae, S_IFREG | 0755);
65	fill_with_pseudorandom_data(buff2, sizeof(buff2));
66	archive_entry_set_size(ae, sizeof(buff2));
67	assertA(0 == archive_write_header(a, ae));
68	archive_entry_free(ae);
69	assertA((int)sizeof(buff2) == archive_write_data(a, buff2, sizeof(buff2)));
70
71	/* Close out the archive. */
72	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
73	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
74
75	/* Check that archive_read_data can handle 10*10^6 at a pop. */
76	assert((a = archive_read_new()) != NULL);
77	assertA(0 == archive_read_support_format_all(a));
78	assertA(0 == archive_read_support_filter_all(a));
79	assertA(0 == archive_read_open_memory(a, buff1, sizeof(buff1)));
80	assertA(0 == archive_read_next_header(a, &ae));
81	failure("Wrote 10MB, but didn't read the same amount");
82	assertEqualIntA(a, sizeof(buff2),archive_read_data(a, buff3, sizeof(buff3)));
83	failure("Read expected 10MB, but data read didn't match what was written");
84	assertEqualMem(buff2, buff3, sizeof(buff3));
85	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
86	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
87
88	/* Check archive_read_data_into_fd */
89	assert((a = archive_read_new()) != NULL);
90	assertA(0 == archive_read_support_format_all(a));
91	assertA(0 == archive_read_support_filter_all(a));
92	assertA(0 == archive_read_open_memory(a, buff1, sizeof(buff1)));
93	assertA(0 == archive_read_next_header(a, &ae));
94#if defined(__BORLANDC__)
95	tmpfilefd = open(tmpfilename, O_WRONLY | O_CREAT | O_BINARY);
96#else
97	tmpfilefd = open(tmpfilename, O_WRONLY | O_CREAT | O_BINARY, 0777);
98#endif
99	assert(tmpfilefd != 0);
100	assertEqualIntA(a, 0, archive_read_data_into_fd(a, tmpfilefd));
101	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
102	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
103	close(tmpfilefd);
104
105	f = fopen(tmpfilename, "rb");
106	assert(f != NULL);
107	assertEqualInt(sizeof(buff3), fread(buff3, 1, sizeof(buff3), f));
108	fclose(f);
109	assertEqualMem(buff2, buff3, sizeof(buff3));
110}
111