]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_filter_lrzip.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_filter_lrzip.c
1 /*-
2  * Copyright (c) 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "test.h"
28
29 /*
30  * A basic exercise of lrzip reading and writing.
31  */
32
33 DEFINE_TEST(test_write_filter_lrzip)
34 {
35         struct archive_entry *ae;
36         struct archive* a;
37         char *buff, *data;
38         size_t buffsize, datasize;
39         char path[16];
40         size_t used1, used2;
41         int i;
42
43         if (!canLrzip()) {
44                 skipping("lrzip command-line program not found");
45                 return;
46         }
47
48         buffsize = 2000000;
49         assert(NULL != (buff = (char *)malloc(buffsize)));
50
51         datasize = 10000;
52         assert(NULL != (data = (char *)malloc(datasize)));
53         memset(data, 0, datasize);
54
55         /*
56          * Write 100 files and read them all back.
57          */
58         assert((a = archive_write_new()) != NULL);
59         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_gnutar(a));
60         assertEqualIntA(a, ARCHIVE_WARN, archive_write_add_filter_lrzip(a));
61         assertEqualIntA(a, ARCHIVE_OK,
62                 archive_write_set_bytes_per_block(a, 10));
63         assertEqualInt(ARCHIVE_FILTER_LRZIP, archive_filter_code(a, 0));
64         assertEqualString("lrzip", archive_filter_name(a, 0));
65         assertEqualIntA(a, ARCHIVE_OK,
66                 archive_write_open_memory(a, buff, buffsize, &used1));
67         assert((ae = archive_entry_new()) != NULL);
68         archive_entry_set_filetype(ae, AE_IFREG);
69         archive_entry_set_size(ae, datasize);
70         for (i = 0; i < 100; i++) {
71                 sprintf(path, "file%03d", i);
72                 archive_entry_copy_pathname(ae, path);
73                 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
74                 assertA(datasize
75                     == (size_t)archive_write_data(a, data, datasize));
76         }
77         archive_entry_free(ae);
78         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
79         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
80
81         assert((a = archive_read_new()) != NULL);
82         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
83         assertEqualIntA(a, ARCHIVE_WARN, archive_read_support_filter_lrzip(a));
84         assertEqualIntA(a, ARCHIVE_OK,
85             archive_read_open_memory(a, buff, used1));
86         for (i = 0; i < 100; i++) {
87                 sprintf(path, "file%03d", i);
88                 if (!assertEqualInt(ARCHIVE_OK,
89                         archive_read_next_header(a, &ae)))
90                         break;
91                 assertEqualString(path, archive_entry_pathname(ae));
92                 assertEqualInt((int)datasize, archive_entry_size(ae));
93         }
94         assertEqualInt(ARCHIVE_FILTER_LRZIP, archive_filter_code(a, 0));
95         assertEqualString("lrzip", archive_filter_name(a, 0));
96         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
97         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
98
99         /*
100          * Test various premature shutdown scenarios to make sure we
101          * don't crash or leak memory.
102          */
103         assert((a = archive_write_new()) != NULL);
104         assertEqualIntA(a, ARCHIVE_WARN, archive_write_add_filter_lrzip(a));
105         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
106
107         assert((a = archive_write_new()) != NULL);
108         assertEqualIntA(a, ARCHIVE_WARN, archive_write_add_filter_lrzip(a));
109         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
110         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
111
112         assert((a = archive_write_new()) != NULL);
113         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
114         assertEqualIntA(a, ARCHIVE_WARN, archive_write_add_filter_lrzip(a));
115         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
116         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
117
118         assert((a = archive_write_new()) != NULL);
119         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
120         assertEqualIntA(a, ARCHIVE_WARN, archive_write_add_filter_lrzip(a));
121         assertEqualIntA(a, ARCHIVE_OK,
122                 archive_write_open_memory(a, buff, buffsize, &used2));
123         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
124         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
125
126         /*
127          * Clean up.
128          */
129         free(data);
130         free(buff);
131 }