xref: /freebsd/contrib/libarchive/libarchive/test/test_read_disk_entry_from_file.c (revision b9128a37faafede823eb456aa65a11ac69997284)
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 static const char *
28 gname_lookup(void *d, int64_t g)
29 {
30 	(void)d; /* UNUSED */
31 	(void)g; /* UNUSED */
32 	return ("FOOGROUP");
33 }
34 
35 static const char *
36 uname_lookup(void *d, int64_t u)
37 {
38 	(void)d; /* UNUSED */
39 	(void)u; /* UNUSED */
40 	return ("FOO");
41 }
42 
43 DEFINE_TEST(test_read_disk_entry_from_file)
44 {
45 	struct archive *a;
46 	struct archive_entry *entry;
47 	FILE *f;
48 
49 	assert((a = archive_read_disk_new()) != NULL);
50 
51 	assertEqualInt(ARCHIVE_OK, archive_read_disk_set_uname_lookup(a,
52 			   NULL, &uname_lookup, NULL));
53 	assertEqualInt(ARCHIVE_OK, archive_read_disk_set_gname_lookup(a,
54 			   NULL, &gname_lookup, NULL));
55 	assertEqualString(archive_read_disk_uname(a, 0), "FOO");
56 	assertEqualString(archive_read_disk_gname(a, 0), "FOOGROUP");
57 
58 	/* Create a file on disk. */
59 	f = fopen("foo", "wb");
60 	assert(f != NULL);
61 	assertEqualInt(4, fwrite("1234", 1, 4, f));
62 	fclose(f);
63 
64 	/* Use archive_read_disk_entry_from_file to get information about it. */
65 	entry = archive_entry_new();
66 	assert(entry != NULL);
67 	archive_entry_copy_pathname(entry, "foo");
68 	assertEqualIntA(a, ARCHIVE_OK,
69 	    archive_read_disk_entry_from_file(a, entry, -1, NULL));
70 
71 	/* Verify the information we got back. */
72 	assertEqualString(archive_entry_uname(entry), "FOO");
73 	assertEqualString(archive_entry_gname(entry), "FOOGROUP");
74 	assertEqualInt(archive_entry_size(entry), 4);
75 
76 	/* Destroy the archive. */
77 	archive_entry_free(entry);
78 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
79 }
80