]> icculus.org git repositories - icculus/xz.git/blob - tests/test_block_header.c
Delete old code that was supposed to be already deleted
[icculus/xz.git] / tests / test_block_header.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       test_block_header.c
4 /// \brief      Tests Block Header coders
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "tests.h"
21
22
23 static uint8_t buf[LZMA_BLOCK_HEADER_SIZE_MAX];
24 static lzma_options_block known_options;
25 static lzma_options_block decoded_options;
26
27 static lzma_options_filter filters_none[1] = {
28         {
29                 .id = LZMA_VLI_VALUE_UNKNOWN,
30         },
31 };
32
33
34 static lzma_options_filter filters_one[2] = {
35         {
36                 .id = LZMA_FILTER_LZMA,
37                 .options = (void *)(&lzma_preset_lzma[0]),
38         }, {
39                 .id = LZMA_VLI_VALUE_UNKNOWN,
40         }
41 };
42
43
44 static lzma_options_filter filters_four[5] = {
45         {
46                 .id = LZMA_FILTER_X86,
47                 .options = NULL,
48         }, {
49                 .id = LZMA_FILTER_X86,
50                 .options = NULL,
51         }, {
52                 .id = LZMA_FILTER_X86,
53                 .options = NULL,
54         }, {
55                 .id = LZMA_FILTER_LZMA,
56                 .options = (void *)(&lzma_preset_lzma[0]),
57         }, {
58                 .id = LZMA_VLI_VALUE_UNKNOWN,
59         }
60 };
61
62
63 static lzma_options_filter filters_five[6] = {
64         {
65                 .id = LZMA_FILTER_X86,
66                 .options = NULL,
67         }, {
68                 .id = LZMA_FILTER_X86,
69                 .options = NULL,
70         }, {
71                 .id = LZMA_FILTER_X86,
72                 .options = NULL,
73         }, {
74                 .id = LZMA_FILTER_X86,
75                 .options = NULL,
76         }, {
77                 .id = LZMA_FILTER_LZMA,
78                 .options = (void *)(&lzma_preset_lzma[0]),
79         }, {
80                 .id = LZMA_VLI_VALUE_UNKNOWN,
81         }
82 };
83
84
85 static void
86 code(void)
87 {
88         expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
89
90         lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
91         memcrap(filters, sizeof(filters));
92         memcrap(&decoded_options, sizeof(decoded_options));
93
94         decoded_options.header_size = known_options.header_size;
95         decoded_options.check = known_options.check;
96         decoded_options.filters = filters;
97         expect(lzma_block_header_decode(&decoded_options, NULL, buf)
98                         == LZMA_OK);
99
100         expect(known_options.compressed_size
101                         == decoded_options.compressed_size);
102         expect(known_options.uncompressed_size
103                         == decoded_options.uncompressed_size);
104
105         for (size_t i = 0; known_options.filters[i].id
106                         != LZMA_VLI_VALUE_UNKNOWN; ++i)
107                 expect(known_options.filters[i].id == filters[i].id);
108
109         for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
110                 free(decoded_options.filters[i].options);
111 }
112
113
114 static void
115 test1(void)
116 {
117         known_options = (lzma_options_block){
118                 .check = LZMA_CHECK_NONE,
119                 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
120                 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
121                 .filters = NULL,
122         };
123
124         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
125
126         known_options.filters = filters_none;
127         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
128
129         known_options.filters = filters_five;
130         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
131
132         known_options.filters = filters_one;
133         expect(lzma_block_header_size(&known_options) == LZMA_OK);
134
135         known_options.check = 999; // Some invalid value, which gets ignored.
136         expect(lzma_block_header_size(&known_options) == LZMA_OK);
137
138         known_options.compressed_size = 5; // Not a multiple of four.
139         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
140
141         known_options.compressed_size = 0; // Cannot be zero.
142         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
143
144         known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN;
145         known_options.uncompressed_size = 0;
146         expect(lzma_block_header_size(&known_options) == LZMA_OK);
147
148         known_options.uncompressed_size = LZMA_VLI_VALUE_MAX + 1;
149         expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
150 }
151
152
153 static void
154 test2(void)
155 {
156         known_options = (lzma_options_block){
157                 .check = LZMA_CHECK_CRC32,
158                 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
159                 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
160                 .filters = filters_four,
161         };
162
163         expect(lzma_block_header_size(&known_options) == LZMA_OK);
164         code();
165
166         known_options.compressed_size = 123456;
167         known_options.uncompressed_size = 234567;
168         expect(lzma_block_header_size(&known_options) == LZMA_OK);
169         code();
170
171         // We can make the sizes smaller while keeping the header size
172         // the same.
173         known_options.compressed_size = 12;
174         known_options.uncompressed_size = 23;
175         code();
176 }
177
178
179 static void
180 test3(void)
181 {
182         known_options = (lzma_options_block){
183                 .check = LZMA_CHECK_CRC32,
184                 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
185                 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
186                 .filters = filters_one,
187         };
188
189         expect(lzma_block_header_size(&known_options) == LZMA_OK);
190         known_options.header_size += 4;
191         expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
192
193         lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
194         decoded_options.header_size = known_options.header_size;
195         decoded_options.check = known_options.check;
196         decoded_options.filters = filters;
197
198         // Wrong size
199         ++buf[0];
200         expect(lzma_block_header_decode(&decoded_options, NULL, buf)
201                         == LZMA_PROG_ERROR);
202         --buf[0];
203
204         // Wrong CRC32
205         buf[known_options.header_size - 1] ^= 1;
206         expect(lzma_block_header_decode(&decoded_options, NULL, buf)
207                         == LZMA_DATA_ERROR);
208         buf[known_options.header_size - 1] ^= 1;
209
210         // Unsupported filter
211         // NOTE: This may need updating when new IDs become supported.
212         buf[2] ^= 0x1F;
213         integer_write_32(buf + known_options.header_size - 4,
214                         lzma_crc32(buf, known_options.header_size - 4, 0));
215         expect(lzma_block_header_decode(&decoded_options, NULL, buf)
216                         == LZMA_HEADER_ERROR);
217         buf[2] ^= 0x1F;
218
219         // Non-nul Padding
220         buf[known_options.header_size - 4 - 1] ^= 1;
221         integer_write_32(buf + known_options.header_size - 4,
222                         lzma_crc32(buf, known_options.header_size - 4, 0));
223         expect(lzma_block_header_decode(&decoded_options, NULL, buf)
224                         == LZMA_HEADER_ERROR);
225         buf[known_options.header_size - 4 - 1] ^= 1;
226 }
227
228
229 int
230 main(void)
231 {
232         lzma_init();
233
234         test1();
235         test2();
236         test3();
237
238         return 0;
239 }