1219019Sgabor/*-
2219019Sgabor * Copyright (c) 2003-2007 Tim Kientzle
3219019Sgabor * All rights reserved.
4219019Sgabor *
5219019Sgabor * Redistribution and use in source and binary forms, with or without
6219019Sgabor * modification, are permitted provided that the following conditions
7219019Sgabor * are met:
8219019Sgabor * 1. Redistributions of source code must retain the above copyright
9219019Sgabor *    notice, this list of conditions and the following disclaimer.
10219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
11219019Sgabor *    notice, this list of conditions and the following disclaimer in the
12219019Sgabor *    documentation and/or other materials provided with the distribution.
13219019Sgabor *
14219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15219019Sgabor * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16219019Sgabor * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17219019Sgabor * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18219019Sgabor * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19219019Sgabor * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20219019Sgabor * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21219019Sgabor * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22219019Sgabor * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23219019Sgabor * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24219019Sgabor */
25219019Sgabor#include "test.h"
26219019Sgabor__FBSDID("$FreeBSD: head/lib/libarchive/test/test_open_fd.c 201247 2009-12-30 05:59:21Z kientzle $");
27219019Sgabor
28219019Sgabor#if defined(_WIN32) && !defined(__CYGWIN__)
29219019Sgabor#define open _open
30219019Sgabor#if !defined(__BORLANDC__)
31219019Sgabor#define lseek _lseek
32219019Sgabor#endif
33219019Sgabor#define close _close
34219019Sgabor#endif
35219019Sgabor
36219019SgaborDEFINE_TEST(test_open_fd)
37219019Sgabor{
38219019Sgabor	char buff[64];
39219019Sgabor	struct archive_entry *ae;
40219019Sgabor	struct archive *a;
41219019Sgabor	int fd;
42219019Sgabor
43219019Sgabor#if defined(__BORLANDC__)
44219019Sgabor	fd = open("test.tar", O_RDWR | O_CREAT | O_BINARY);
45267829Sdelphij#else
46219019Sgabor	fd = open("test.tar", O_RDWR | O_CREAT | O_BINARY, 0777);
47219019Sgabor#endif
48219019Sgabor	assert(fd >= 0);
49219019Sgabor	if (fd < 0)
50219019Sgabor		return;
51219019Sgabor
52219019Sgabor	/* Write an archive through this fd. */
53219019Sgabor	assert((a = archive_write_new()) != NULL);
54219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
55267829Sdelphij	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_none(a));
56219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_fd(a, fd));
57219019Sgabor
58219019Sgabor	/*
59219019Sgabor	 * Write a file to it.
60219019Sgabor	 */
61219019Sgabor	assert((ae = archive_entry_new()) != NULL);
62219019Sgabor	archive_entry_set_mtime(ae, 1, 0);
63219019Sgabor	archive_entry_copy_pathname(ae, "file");
64219019Sgabor	archive_entry_set_mode(ae, S_IFREG | 0755);
65219019Sgabor	archive_entry_set_size(ae, 8);
66219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
67219019Sgabor	archive_entry_free(ae);
68219019Sgabor	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
69219019Sgabor
70219019Sgabor	/*
71219019Sgabor	 * Write a second file to it.
72219019Sgabor	 */
73219019Sgabor	assert((ae = archive_entry_new()) != NULL);
74219019Sgabor	archive_entry_copy_pathname(ae, "file2");
75219019Sgabor	archive_entry_set_mode(ae, S_IFREG | 0755);
76219019Sgabor	archive_entry_set_size(ae, 819200);
77219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
78219019Sgabor	archive_entry_free(ae);
79219019Sgabor
80219019Sgabor	/* Close out the archive. */
81219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
82219019Sgabor	assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
83219019Sgabor
84219019Sgabor	/*
85219019Sgabor	 * Now, read the data back.
86219019Sgabor	 */
87219019Sgabor	assert(lseek(fd, 0, SEEK_SET) == 0);
88219019Sgabor	assert((a = archive_read_new()) != NULL);
89219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
90219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
91219019Sgabor	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_fd(a, fd, 512));
92219019Sgabor
93	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
94	assertEqualInt(1, archive_entry_mtime(ae));
95	assertEqualInt(0, archive_entry_mtime_nsec(ae));
96	assertEqualInt(0, archive_entry_atime(ae));
97	assertEqualInt(0, archive_entry_ctime(ae));
98	assertEqualString("file", archive_entry_pathname(ae));
99	assert((S_IFREG | 0755) == archive_entry_mode(ae));
100	assertEqualInt(8, archive_entry_size(ae));
101	assertEqualIntA(a, 8, archive_read_data(a, buff, 10));
102	assertEqualMem(buff, "12345678", 8);
103
104	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
105	assertEqualString("file2", archive_entry_pathname(ae));
106	assert((S_IFREG | 0755) == archive_entry_mode(ae));
107	assertEqualInt(819200, archive_entry_size(ae));
108	assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
109
110	/* Verify the end of the archive. */
111	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
112	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
113	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
114	close(fd);
115
116
117	/*
118	 * Verify some of the error handling.
119	 */
120	assert((a = archive_read_new()) != NULL);
121	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
122	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
123	/* FD 100 shouldn't be open. */
124	assertEqualIntA(a, ARCHIVE_FATAL,
125	    archive_read_open_fd(a, 100, 512));
126	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
127	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
128}
129