]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test_write_disk.c
This commit was generated by cvs2svn to compensate for changes in r171164,
[FreeBSD/FreeBSD.git] / lib / libarchive / test / test_write_disk.c
1 /*-
2  * Copyright (c) 2003-2007 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 __FBSDID("$FreeBSD$");
27
28 #define UMASK 022
29
30 static void create(struct archive_entry *ae, const char *msg)
31 {
32         struct archive *ad;
33         struct stat st;
34
35         /* Write the entry to disk. */
36         assert((ad = archive_write_disk_new()) != NULL);
37         failure("%s", msg);
38         assertEqualIntA(ad, 0, archive_write_header(ad, ae));
39         assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
40 #if ARCHIVE_API_VERSION > 1
41         assertEqualInt(0, archive_write_finish(ad));
42 #else
43         archive_write_finish(ad);
44 #endif
45         /* Test the entries on disk. */
46         assert(0 == stat(archive_entry_pathname(ae), &st));
47         failure("st.st_mode=%o archive_entry_mode(ae)=%o",
48             st.st_mode, archive_entry_mode(ae));
49         assert(st.st_mode == (archive_entry_mode(ae) & ~UMASK));
50 }
51
52 DEFINE_TEST(test_write_disk)
53 {
54         struct archive_entry *ae;
55
56         /* Force the umask to something predictable. */
57         umask(UMASK);
58
59         /* A regular file. */
60         assert((ae = archive_entry_new()) != NULL);
61         archive_entry_copy_pathname(ae, "file");
62         archive_entry_set_mode(ae, S_IFREG | 0755);
63         create(ae, "Test creating a regular file");
64         archive_entry_free(ae);
65
66         /* A regular file over an existing file */
67         assert((ae = archive_entry_new()) != NULL);
68         archive_entry_copy_pathname(ae, "file");
69         archive_entry_set_mode(ae, S_IFREG | 0724);
70         create(ae, "Test creating a file over an existing file.");
71         archive_entry_free(ae);
72
73         /* A directory. */
74         assert((ae = archive_entry_new()) != NULL);
75         archive_entry_copy_pathname(ae, "dir");
76         archive_entry_set_mode(ae, S_IFDIR | 0555);
77         create(ae, "Test creating a regular dir.");
78         archive_entry_free(ae);
79
80         /* A directory over an existing file. */
81         assert((ae = archive_entry_new()) != NULL);
82         archive_entry_copy_pathname(ae, "file");
83         archive_entry_set_mode(ae, S_IFDIR | 0742);
84         create(ae, "Test creating a dir over an existing file.");
85         archive_entry_free(ae);
86
87         /* A file over an existing dir. */
88         assert((ae = archive_entry_new()) != NULL);
89         archive_entry_copy_pathname(ae, "file");
90         archive_entry_set_mode(ae, S_IFREG | 0744);
91         create(ae, "Test creating a file over an existing dir.");
92         archive_entry_free(ae);
93 }