1299425Smm/*-
2299425Smm * Copyright (c) 2014 Tim Kientzle
3299425Smm * All rights reserved.
4299425Smm *
5299425Smm * Redistribution and use in source and binary forms, with or without
6299425Smm * modification, are permitted provided that the following conditions
7299425Smm * are met:
8299425Smm * 1. Redistributions of source code must retain the above copyright
9299425Smm *    notice, this list of conditions and the following disclaimer.
10299425Smm * 2. Redistributions in binary form must reproduce the above copyright
11299425Smm *    notice, this list of conditions and the following disclaimer in the
12299425Smm *    documentation and/or other materials provided with the distribution.
13299425Smm *
14299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24299425Smm */
25299425Smm
26299425Smm#include "test.h"
27299425Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/test/test_write_format_zip_zip64.c 313571 2017-02-11 00:56:18Z mm $");
28299425Smm
29299425Smmstatic void
30299425Smmverify_zip_filesize(uint64_t size, int expected)
31299425Smm{
32299425Smm	struct archive *a;
33299425Smm	struct archive_entry *ae;
34299425Smm	char buff[256];
35299425Smm	size_t used;
36299425Smm
37299425Smm	/* Zip format: Create a new archive in memory. */
38299425Smm	assert((a = archive_write_new()) != NULL);
39299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
40299425Smm	/* Disable Zip64 extensions. */
41299425Smm	assertEqualIntA(a, ARCHIVE_OK,
42299425Smm	    archive_write_set_format_option(a, "zip", "zip64", NULL));
43299425Smm	assertEqualIntA(a, ARCHIVE_OK,
44299425Smm	    archive_write_open_memory(a, buff, sizeof(buff), &used));
45299425Smm
46299425Smm	assert((ae = archive_entry_new()) != NULL);
47299425Smm	archive_entry_set_pathname(ae, "test");
48299425Smm	archive_entry_set_mode(ae, AE_IFREG | 0644);
49299425Smm	archive_entry_set_size(ae, size);
50299425Smm	assertEqualInt(expected, archive_write_header(a, ae));
51299425Smm
52313571Smm	archive_entry_free(ae);
53313571Smm
54299425Smm	/* Don't actually write 4GB! ;-) */
55299425Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
56299425Smm}
57299425Smm
58299425SmmDEFINE_TEST(test_write_format_zip_zip64_oversize)
59299425Smm{
60299425Smm	/* With Zip64 extensions disabled, we should be
61299425Smm	 * able to write a file with at most 4G-1 bytes. */
62299425Smm
63299425Smm	/* Note:  Tar writer pads file to declared size when the file
64299425Smm	 * is closed.  If Zip writer is changed to behave the same
65299425Smm	 * way, it will be much harder to test the first case here. */
66299425Smm	verify_zip_filesize(0xffffffffLL, ARCHIVE_OK);
67299425Smm
68299425Smm	verify_zip_filesize(0x100000000LL, ARCHIVE_FAILED);
69299425Smm}
70