]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_compress_program.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / libarchive / test / test_write_compress_program.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 char buff[1000000];
29 char buff2[64];
30
31 DEFINE_TEST(test_write_compress_program)
32 {
33 #if ARCHIVE_VERSION_NUMBER < 1009000
34         skipping("archive_write_set_compress_program()");
35 #else
36         struct archive_entry *ae;
37         struct archive *a;
38         size_t used;
39         int blocksize = 1024;
40         int r;
41
42         if (!canGzip()) {
43                 skipping("Cannot run 'gzip'");
44                 return;
45         }
46
47         /* Create a new archive in memory. */
48         /* Write it through an external "gzip" program. */
49         assert((a = archive_write_new()) != NULL);
50         assertA(0 == archive_write_set_format_ustar(a));
51         r = archive_write_set_compression_program(a, "gzip");
52         if (r == ARCHIVE_FATAL) {
53                 skipping("Write compression via external "
54                     "program unsupported on this platform");
55                 archive_write_finish(a);
56                 return;
57         }
58         assertA(0 == archive_write_set_bytes_per_block(a, blocksize));
59         assertA(0 == archive_write_set_bytes_in_last_block(a, blocksize));
60         assertA(blocksize == archive_write_get_bytes_in_last_block(a));
61         assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
62         assertA(blocksize == archive_write_get_bytes_in_last_block(a));
63
64         /*
65          * Write a file to it.
66          */
67         assert((ae = archive_entry_new()) != NULL);
68         archive_entry_set_mtime(ae, 1, 10);
69         archive_entry_copy_pathname(ae, "file");
70         archive_entry_set_mode(ae, S_IFREG | 0755);
71         archive_entry_set_size(ae, 8);
72
73         assertA(0 == archive_write_header(a, ae));
74         archive_entry_free(ae);
75         assertA(8 == archive_write_data(a, "12345678", 9));
76
77         /* Close out the archive. */
78         assertA(0 == archive_write_close(a));
79         assertA(0 == archive_write_finish(a));
80
81         /*
82          * Now, read the data back through the built-in gzip support.
83          */
84         assert((a = archive_read_new()) != NULL);
85         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
86         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
87         r = archive_read_support_compression_gzip(a);
88         /* The compression_gzip() handler will fall back to gunzip
89          * automatically, but if we know gunzip isn't available, then
90          * skip the rest. */
91         if (r != ARCHIVE_OK && !canGunzip()) {
92                 skipping("No libz and no gunzip program, "
93                     "unable to verify gzip compression");
94                 assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
95                 return;
96         }
97         assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
98
99         if (!assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae))) {
100                 archive_read_finish(a);
101                 return;
102         }
103
104         assertEqualInt(1, archive_entry_mtime(ae));
105         assertEqualInt(0, archive_entry_atime(ae));
106         assertEqualInt(0, archive_entry_ctime(ae));
107         assertEqualString("file", archive_entry_pathname(ae));
108         assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
109         assertEqualInt(8, archive_entry_size(ae));
110         assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
111         assertEqualMem(buff2, "12345678", 8);
112
113         /* Verify the end of the archive. */
114         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
115         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
116         assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
117 #endif
118 }