111568Sjkh/*-
211568Sjkh * Copyright (c) 2016 Tim Kientzle
311568Sjkh * All rights reserved.
411568Sjkh *
511568Sjkh * Redistribution and use in source and binary forms, with or without
611568Sjkh * modification, are permitted provided that the following conditions
743685Sjkh * are met:
811568Sjkh * 1. Redistributions of source code must retain the above copyright
911568Sjkh *    notice, this list of conditions and the following disclaimer.
1011568Sjkh * 2. Redistributions in binary form must reproduce the above copyright
1111568Sjkh *    notice, this list of conditions and the following disclaimer in the
1211568Sjkh *    documentation and/or other materials provided with the distribution.
1311568Sjkh *
1411568Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1511568Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1611568Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1711568Sjkh * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1811568Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1911568Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2011568Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2111568Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2211568Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2311568Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2411568Sjkh */
2511568Sjkh#include "test.h"
2611568Sjkh
2711568Sjkh/*
2811568Sjkh * Inspired by Github issue #682, which reported that gnutar filenames
2911568Sjkh * of exactly 512 bytes weren't getting written correctly.
3011568Sjkh *
3111568Sjkh * This writes a filename of every length from 1 to 2000 bytes and
3211568Sjkh * reads back to verify it.
3311568Sjkh */
3411568Sjkh
3511568Sjkhstatic char filename[2048];
3611568Sjkh
3711568SjkhDEFINE_TEST(test_write_format_gnutar_filenames)
3811568Sjkh{
3911568Sjkh	size_t buffsize = 1000000;
4011568Sjkh	char *buff;
4111568Sjkh	struct archive_entry *ae, *template;
4211568Sjkh	struct archive *a;
4311568Sjkh	size_t used;
4411568Sjkh	int i;
4511568Sjkh
4611568Sjkh	buff = malloc(buffsize); /* million bytes of work area */
4711568Sjkh	assert(buff != NULL);
4829222Sjkh
4929222Sjkh	/* Create a template entry. */
5011568Sjkh	assert((template = archive_entry_new()) != NULL);
5111568Sjkh	archive_entry_set_atime(template, 2, 20);
5211568Sjkh	archive_entry_set_birthtime(template, 3, 30);
5311568Sjkh	archive_entry_set_ctime(template, 4, 40);
5411568Sjkh	archive_entry_set_mtime(template, 5, 50);
5511568Sjkh	archive_entry_set_mode(template, S_IFREG | 0755);
5611568Sjkh	archive_entry_set_size(template, 8);
5711568Sjkh
5811568Sjkh	for (i = 0; i < 2000; ++i) {
5911568Sjkh		filename[i] = 'a';
6011568Sjkh		filename[i + 1] = '\0';
6111568Sjkh		archive_entry_copy_pathname(template, filename);
6211568Sjkh
6311568Sjkh		/* Write a one-item gnutar format archive. */
6411568Sjkh		assert((a = archive_write_new()) != NULL);
6511568Sjkh		assertA(0 == archive_write_set_format_gnutar(a));
6611568Sjkh		assertA(0 == archive_write_add_filter_none(a));
6711568Sjkh		assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
6811568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, template));
6911568Sjkh		assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
7011568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
7111568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
7211568Sjkh
7321726Sjkh
7411568Sjkh		/* Read back and verify the filename. */
7524548Sjkh		assert((a = archive_read_new()) != NULL);
7611568Sjkh		assertEqualIntA(a, 0, archive_read_support_format_all(a));
7711568Sjkh		assertEqualIntA(a, 0, archive_read_support_filter_all(a));
7811568Sjkh		assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
7911568Sjkh
8011568Sjkh		assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
8111568Sjkh		assertEqualString(filename, archive_entry_pathname(ae));
8221726Sjkh		assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
8321726Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
8411568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
8511568Sjkh	}
8611568Sjkh
8711568Sjkh	archive_entry_free(template);
8811568Sjkh
8911568Sjkh	free(buff);
9011568Sjkh}
9111568Sjkh
9211568Sjkh
9311568SjkhDEFINE_TEST(test_write_format_gnutar_linknames)
9411568Sjkh{
9511568Sjkh	size_t buffsize = 1000000;
9636320Ssteve	char *buff;
9711568Sjkh	struct archive_entry *ae, *template;
9811568Sjkh	struct archive *a;
9911568Sjkh	size_t used;
10011568Sjkh	int i;
10111568Sjkh
10211568Sjkh#ifdef S_IFLNK
10311568Sjkh	assertEqualInt(S_IFLNK, AE_IFLNK);
10434867Sjkh#endif
10534867Sjkh
10634867Sjkh	buff = malloc(buffsize); /* million bytes of work area */
10711568Sjkh	assert(buff != NULL);
10834867Sjkh
10925251Sjkh	/* Create a template entry. */
11011568Sjkh	assert((template = archive_entry_new()) != NULL);
11111568Sjkh	archive_entry_set_atime(template, 2, 20);
11211568Sjkh	archive_entry_set_birthtime(template, 3, 30);
11311568Sjkh	archive_entry_set_ctime(template, 4, 40);
11411568Sjkh	archive_entry_set_mtime(template, 5, 50);
11521726Sjkh	archive_entry_set_mode(template, AE_IFLNK | 0755);
11611568Sjkh	archive_entry_copy_pathname(template, "link");
11711568Sjkh
11811568Sjkh	for (i = 0; i < 2000; ++i) {
11911568Sjkh		filename[i] = 'a';
12011568Sjkh		filename[i + 1] = '\0';
12111568Sjkh		archive_entry_copy_symlink(template, filename);
12211568Sjkh
12311568Sjkh		/* Write a one-item gnutar format archive. */
12411568Sjkh		assert((a = archive_write_new()) != NULL);
12511568Sjkh		assertA(0 == archive_write_set_format_gnutar(a));
12611568Sjkh		assertA(0 == archive_write_add_filter_none(a));
12711568Sjkh		assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
12811568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, template));
12911568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
13011568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
13121728Sjkh
13221728Sjkh
13311568Sjkh		/* Read back and verify the filename. */
13411568Sjkh		assert((a = archive_read_new()) != NULL);
13515355Sjkh		assertEqualIntA(a, 0, archive_read_support_format_all(a));
13611568Sjkh		assertEqualIntA(a, 0, archive_read_support_filter_all(a));
13721720Sjkh		assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
13811568Sjkh
13911568Sjkh		assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
14011568Sjkh		assertEqualString("link", archive_entry_pathname(ae));
14121726Sjkh		assertEqualString(filename, archive_entry_symlink(ae));
14221728Sjkh		assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
14311568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
14411568Sjkh		assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
14511568Sjkh	}
14611568Sjkh
14715355Sjkh	archive_entry_free(template);
14811568Sjkh
14911568Sjkh	free(buff);
15011568Sjkh}
15111568Sjkh