1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26229592Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753SmmDEFINE_TEST(test_empty_write)
29228753Smm{
30228753Smm	char buff[32768];
31228753Smm	struct archive_entry *ae;
32228753Smm	struct archive *a;
33228753Smm	size_t used;
34228753Smm	int r;
35228753Smm
36228753Smm	/*
37228753Smm	 * Exercise a zero-byte write to a gzip-compressed archive.
38228753Smm	 */
39228753Smm
40228753Smm	/* Create a new archive in memory. */
41228753Smm	assert((a = archive_write_new()) != NULL);
42228753Smm	assertA(0 == archive_write_set_format_ustar(a));
43228753Smm	r = archive_write_set_compression_gzip(a);
44228753Smm	if (r == ARCHIVE_FATAL) {
45228753Smm		skipping("Empty write to gzip-compressed archive");
46228753Smm	} else {
47228753Smm		assertEqualIntA(a, ARCHIVE_OK, r);
48228753Smm		assertEqualIntA(a, ARCHIVE_OK,
49228753Smm		    archive_write_open_memory(a, buff, sizeof(buff), &used));
50228753Smm		/* Write a file to it. */
51228753Smm		assert((ae = archive_entry_new()) != NULL);
52228753Smm		archive_entry_copy_pathname(ae, "file");
53228753Smm		archive_entry_set_mode(ae, S_IFREG | 0755);
54228753Smm		archive_entry_set_size(ae, 0);
55228753Smm		assertA(0 == archive_write_header(a, ae));
56228753Smm		archive_entry_free(ae);
57228753Smm
58228753Smm		/* THE TEST: write zero bytes to this entry. */
59228753Smm		/* This used to crash. */
60228753Smm		assertEqualIntA(a, 0, archive_write_data(a, "", 0));
61228753Smm
62228753Smm		/* Close out the archive. */
63228753Smm		assertA(0 == archive_write_close(a));
64228753Smm		assertA(0 == archive_write_finish(a));
65228753Smm	}
66228753Smm
67228753Smm	/*
68228753Smm	 * Again, with bzip2 compression.
69228753Smm	 */
70228753Smm
71228753Smm	/* Create a new archive in memory. */
72228753Smm	assert((a = archive_write_new()) != NULL);
73228753Smm	assertA(0 == archive_write_set_format_ustar(a));
74228753Smm	r = archive_write_set_compression_bzip2(a);
75228753Smm	if (r == ARCHIVE_FATAL) {
76228753Smm		skipping("Empty write to bzip2-compressed archive");
77228753Smm	} else {
78228753Smm		assertEqualIntA(a, ARCHIVE_OK, r);
79228753Smm		assertEqualIntA(a, ARCHIVE_OK,
80228753Smm		    archive_write_open_memory(a, buff, sizeof(buff), &used));
81228753Smm		/* Write a file to it. */
82228753Smm		assert((ae = archive_entry_new()) != NULL);
83228753Smm		archive_entry_copy_pathname(ae, "file");
84228753Smm		archive_entry_set_mode(ae, S_IFREG | 0755);
85228753Smm		archive_entry_set_size(ae, 0);
86228753Smm		assertA(0 == archive_write_header(a, ae));
87228753Smm		archive_entry_free(ae);
88228753Smm
89228753Smm		/* THE TEST: write zero bytes to this entry. */
90228753Smm		assertEqualIntA(a, 0, archive_write_data(a, "", 0));
91228753Smm
92228753Smm		/* Close out the archive. */
93228753Smm		assertA(0 == archive_write_close(a));
94228753Smm		assertA(0 == archive_write_finish(a));
95228753Smm	}
96228753Smm
97228753Smm	/*
98228753Smm	 * For good measure, one more time with no compression.
99228753Smm	 */
100228753Smm
101228753Smm	/* Create a new archive in memory. */
102228753Smm	assert((a = archive_write_new()) != NULL);
103228753Smm	assertA(0 == archive_write_set_format_ustar(a));
104228753Smm	assertA(0 == archive_write_set_compression_none(a));
105228753Smm	assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
106228753Smm	/* Write a file to it. */
107228753Smm	assert((ae = archive_entry_new()) != NULL);
108228753Smm	archive_entry_copy_pathname(ae, "file");
109228753Smm	archive_entry_set_mode(ae, S_IFREG | 0755);
110228753Smm	archive_entry_set_size(ae, 0);
111228753Smm	assertA(0 == archive_write_header(a, ae));
112228753Smm	archive_entry_free(ae);
113228753Smm
114228753Smm	/* THE TEST: write zero bytes to this entry. */
115228753Smm	assertEqualIntA(a, 0, archive_write_data(a, "", 0));
116228753Smm
117228753Smm	/* Close out the archive. */
118228753Smm	assertA(0 == archive_write_close(a));
119228753Smm	assertA(0 == archive_write_finish(a));
120228753Smm}
121