]> icculus.org git repositories - icculus/xz.git/blob - tests/test_stream_flags.c
Initial changes to change the suffix of the new format to .xz.
[icculus/xz.git] / tests / test_stream_flags.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       test_stream_flags.c
4 /// \brief      Tests Stream Header and Stream Footer 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 lzma_stream_flags known_flags;
24 static lzma_stream_flags decoded_flags;
25 static uint8_t buffer[LZMA_STREAM_HEADER_SIZE];
26
27
28 static bool
29 validate(void)
30 {
31         // TODO: This could require the specific error type as an argument.
32         // We could also test that lzma_stream_flags_compare() gives
33         // the correct return values in different situations.
34         return lzma_stream_flags_compare(&known_flags, &decoded_flags)
35                         != LZMA_OK;
36 }
37
38
39 static bool
40 test_header_decoder(lzma_ret expected_ret)
41 {
42         memcrap(&decoded_flags, sizeof(decoded_flags));
43
44         if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
45                 return true;
46
47         if (expected_ret != LZMA_OK)
48                 return false;
49
50         // Header doesn't have Backward Size, so make
51         // lzma_stream_flags_compare() ignore it.
52         decoded_flags.backward_size = LZMA_VLI_UNKNOWN;
53         return validate();
54 }
55
56
57 static void
58 test_header(void)
59 {
60         memcrap(buffer, sizeof(buffer));
61         expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
62         succeed(test_header_decoder(LZMA_OK));
63 }
64
65
66 static bool
67 test_footer_decoder(lzma_ret expected_ret)
68 {
69         memcrap(&decoded_flags, sizeof(decoded_flags));
70
71         if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
72                 return true;
73
74         if (expected_ret != LZMA_OK)
75                 return false;
76
77         return validate();
78 }
79
80
81 static void
82 test_footer(void)
83 {
84         memcrap(buffer, sizeof(buffer));
85         expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
86         succeed(test_footer_decoder(LZMA_OK));
87 }
88
89
90 static void
91 test_encode_invalid(void)
92 {
93         known_flags.check = LZMA_CHECK_ID_MAX + 1;
94         known_flags.backward_size = 1024;
95
96         expect(lzma_stream_header_encode(&known_flags, buffer)
97                         == LZMA_PROG_ERROR);
98
99         expect(lzma_stream_footer_encode(&known_flags, buffer)
100                         == LZMA_PROG_ERROR);
101
102         known_flags.check = (lzma_check)(-1);
103
104         expect(lzma_stream_header_encode(&known_flags, buffer)
105                         == LZMA_PROG_ERROR);
106
107         expect(lzma_stream_footer_encode(&known_flags, buffer)
108                         == LZMA_PROG_ERROR);
109
110         known_flags.check = LZMA_CHECK_NONE;
111         known_flags.backward_size = 0;
112
113         // Header encoder ignores backward_size.
114         expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
115
116         expect(lzma_stream_footer_encode(&known_flags, buffer)
117                         == LZMA_PROG_ERROR);
118
119         known_flags.backward_size = LZMA_VLI_MAX;
120
121         expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
122
123         expect(lzma_stream_footer_encode(&known_flags, buffer)
124                         == LZMA_PROG_ERROR);
125 }
126
127
128 static void
129 test_decode_invalid(void)
130 {
131         known_flags.check = LZMA_CHECK_NONE;
132         known_flags.backward_size = 1024;
133
134         expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
135
136         // Test 1 (invalid Magic Bytes)
137         buffer[5] ^= 1;
138         succeed(test_header_decoder(LZMA_FORMAT_ERROR));
139         buffer[5] ^= 1;
140
141         // Test 2a (valid CRC32)
142         uint32_t crc = lzma_crc32(buffer + 6, 2, 0);
143         integer_write_32(buffer + 8, crc);
144         succeed(test_header_decoder(LZMA_OK));
145
146         // Test 2b (invalid Stream Flags with valid CRC32)
147         buffer[6] ^= 0x20;
148         crc = lzma_crc32(buffer + 6, 2, 0);
149         integer_write_32(buffer + 8, crc);
150         succeed(test_header_decoder(LZMA_OPTIONS_ERROR));
151
152         // Test 3 (invalid CRC32)
153         expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
154         buffer[9] ^= 1;
155         succeed(test_header_decoder(LZMA_DATA_ERROR));
156
157         // Test 4 (invalid Stream Flags with valid CRC32)
158         expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
159         buffer[9] ^= 0x40;
160         crc = lzma_crc32(buffer + 4, 6, 0);
161         integer_write_32(buffer, crc);
162         succeed(test_footer_decoder(LZMA_OPTIONS_ERROR));
163
164         // Test 5 (invalid Magic Bytes)
165         expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
166         buffer[11] ^= 1;
167         succeed(test_footer_decoder(LZMA_FORMAT_ERROR));
168 }
169
170
171 int
172 main(void)
173 {
174         lzma_init();
175
176         // Valid headers
177         known_flags.backward_size = 1024;
178         for (lzma_check check = LZMA_CHECK_NONE;
179                         check <= LZMA_CHECK_ID_MAX; ++check) {
180                 test_header();
181                 test_footer();
182         }
183
184         // Invalid headers
185         test_encode_invalid();
186         test_decode_invalid();
187
188         return 0;
189 }