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"
26228763Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753SmmDEFINE_TEST(test_open_file)
29228753Smm{
30228753Smm	char buff[64];
31228753Smm	struct archive_entry *ae;
32228753Smm	struct archive *a;
33228753Smm	FILE *f;
34228753Smm
35228753Smm	f = fopen("test.tar", "wb");
36228753Smm	assert(f != NULL);
37228753Smm	if (f == NULL)
38228753Smm		return;
39228753Smm
40228753Smm	/* Write an archive through this FILE *. */
41228753Smm	assert((a = archive_write_new()) != NULL);
42228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
43248616Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
44228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_FILE(a, f));
45228753Smm
46228753Smm	/*
47228753Smm	 * Write a file to it.
48228753Smm	 */
49228753Smm	assert((ae = archive_entry_new()) != NULL);
50228753Smm	archive_entry_set_mtime(ae, 1, 0);
51228753Smm	archive_entry_copy_pathname(ae, "file");
52228753Smm	archive_entry_set_mode(ae, S_IFREG | 0755);
53228753Smm	archive_entry_set_size(ae, 8);
54228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
55228753Smm	archive_entry_free(ae);
56228753Smm	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
57228753Smm
58228753Smm	/*
59228753Smm	 * Write a second file to it.
60228753Smm	 */
61228753Smm	assert((ae = archive_entry_new()) != NULL);
62228753Smm	archive_entry_copy_pathname(ae, "file2");
63228753Smm	archive_entry_set_mode(ae, S_IFREG | 0755);
64228753Smm	archive_entry_set_size(ae, 819200);
65228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
66228753Smm	archive_entry_free(ae);
67228753Smm
68228753Smm	/* Close out the archive. */
69228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
70232153Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
71228753Smm	fclose(f);
72228753Smm
73228753Smm	/*
74228753Smm	 * Now, read the data back.
75228753Smm	 */
76228753Smm	f = fopen("test.tar", "rb");
77228753Smm	assert(f != NULL);
78228753Smm	if (f == NULL)
79228753Smm		return;
80228753Smm	assert((a = archive_read_new()) != NULL);
81228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
82232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
83228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_FILE(a, f));
84228753Smm
85228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
86228753Smm	assertEqualInt(1, archive_entry_mtime(ae));
87228753Smm	assertEqualInt(0, archive_entry_mtime_nsec(ae));
88228753Smm	assertEqualInt(0, archive_entry_atime(ae));
89228753Smm	assertEqualInt(0, archive_entry_ctime(ae));
90228753Smm	assertEqualString("file", archive_entry_pathname(ae));
91228753Smm	assert((S_IFREG | 0755) == archive_entry_mode(ae));
92228753Smm	assertEqualInt(8, archive_entry_size(ae));
93228753Smm	assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
94228753Smm	assertEqualMem(buff, "12345678", 8);
95228753Smm
96228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
97228753Smm	assertEqualString("file2", archive_entry_pathname(ae));
98228753Smm	assert((S_IFREG | 0755) == archive_entry_mode(ae));
99228753Smm	assertEqualInt(819200, archive_entry_size(ae));
100228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
101228753Smm
102228753Smm	/* Verify the end of the archive. */
103228753Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
104228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
105232153Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
106228753Smm
107228753Smm	fclose(f);
108228753Smm}
109