]> icculus.org git repositories - icculus/xz.git/blob - tests/test_stream_flags.c
Fixed the tests to build with -Werror.
[icculus/xz.git] / tests / test_stream_flags.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       test_stream_flags.c
4 /// \brief      Tests Stream Header and Stream tail 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 + LZMA_STREAM_TAIL_SIZE];
26 static lzma_stream strm = LZMA_STREAM_INIT;
27
28
29 static bool
30 validate(void)
31 {
32         if (known_flags.check != decoded_flags.check
33                         || known_flags.has_crc32 != decoded_flags.has_crc32
34                         || known_flags.is_multi != decoded_flags.is_multi)
35                 return true;
36
37         return false;
38 }
39
40
41 static bool
42 test_header_decoder(size_t expected_size, lzma_ret expected_ret)
43 {
44         memcrap(&decoded_flags, sizeof(decoded_flags));
45
46         if (lzma_stream_header_decoder(&strm, &decoded_flags) != LZMA_OK)
47                 return true;
48
49         if (coder_loop(&strm, buffer, expected_size, NULL, 0,
50                         expected_ret, LZMA_RUN))
51                 return true;
52
53         if (expected_ret != LZMA_STREAM_END)
54                 return false;
55
56         return validate();
57 }
58
59
60 static void
61 test_header(void)
62 {
63         memcrap(buffer, sizeof(buffer));
64         expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK);
65         succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_STREAM_END));
66 }
67
68
69 static bool
70 test_tail_decoder(size_t expected_size, lzma_ret expected_ret)
71 {
72         memcrap(&decoded_flags, sizeof(decoded_flags));
73
74         if (lzma_stream_tail_decoder(&strm, &decoded_flags) != LZMA_OK)
75                 return true;
76
77         if (coder_loop(&strm, buffer, expected_size, NULL, 0,
78                         expected_ret, LZMA_RUN))
79                 return true;
80
81         if (expected_ret == LZMA_STREAM_END && validate())
82                 return true;
83
84         return false;
85 }
86
87
88 static void
89 test_tail(void)
90 {
91         memcrap(buffer, sizeof(buffer));
92         expect(lzma_stream_tail_encode(buffer, &known_flags) == LZMA_OK);
93         succeed(test_tail_decoder(LZMA_STREAM_TAIL_SIZE, LZMA_STREAM_END));
94 }
95
96
97 static void
98 test_encode_invalid(void)
99 {
100         known_flags.check = LZMA_CHECK_ID_MAX + 1;
101         known_flags.has_crc32 = false;
102         known_flags.is_multi = false;
103
104         expect(lzma_stream_header_encode(buffer, &known_flags)
105                         == LZMA_PROG_ERROR);
106
107         expect(lzma_stream_tail_encode(buffer, &known_flags)
108                         == LZMA_PROG_ERROR);
109
110         known_flags.check = (lzma_check_type)(-1);
111
112         expect(lzma_stream_header_encode(buffer, &known_flags)
113                         == LZMA_PROG_ERROR);
114
115         expect(lzma_stream_tail_encode(buffer, &known_flags)
116                         == LZMA_PROG_ERROR);
117 }
118
119
120 static void
121 test_decode_invalid(void)
122 {
123         known_flags.check = LZMA_CHECK_NONE;
124         known_flags.has_crc32 = false;
125         known_flags.is_multi = false;
126
127         expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK);
128
129         // Test 1 (invalid Magic Bytes)
130         buffer[5] ^= 1;
131         succeed(test_header_decoder(6, LZMA_DATA_ERROR));
132         buffer[5] ^= 1;
133
134         // Test 2a (valid CRC32)
135         uint32_t crc = lzma_crc32(buffer + 6, 1, 0);
136         for (size_t i = 0; i < 4; ++i)
137                 buffer[7 + i] = crc >> (i * 8);
138         succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_STREAM_END));
139
140         // Test 2b (invalid Stream Flags with valid CRC32)
141         buffer[6] ^= 0x20;
142         crc = lzma_crc32(buffer + 6, 1, 0);
143         for (size_t i = 0; i < 4; ++i)
144                 buffer[7 + i] = crc >> (i * 8);
145         succeed(test_header_decoder(7, LZMA_HEADER_ERROR));
146
147         // Test 3 (invalid CRC32)
148         expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK);
149         buffer[LZMA_STREAM_HEADER_SIZE - 1] ^= 1;
150         succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_DATA_ERROR));
151
152         // Test 4 (invalid Stream Flags)
153         expect(lzma_stream_tail_encode(buffer, &known_flags) == LZMA_OK);
154         buffer[0] ^= 0x40;
155         succeed(test_tail_decoder(1, LZMA_HEADER_ERROR));
156         buffer[0] ^= 0x40;
157
158         // Test 5 (invalid Magic Bytes)
159         buffer[2] ^= 1;
160         succeed(test_tail_decoder(3, LZMA_DATA_ERROR));
161 }
162
163
164 int
165 main(void)
166 {
167         lzma_init();
168
169         // Valid headers
170         for (lzma_check_type check = LZMA_CHECK_NONE;
171                         check <= LZMA_CHECK_ID_MAX; ++check) {
172                 for (int has_crc32 = 0; has_crc32 <= 1; ++has_crc32) {
173                         for (int is_multi = 0; is_multi <= 1; ++is_multi) {
174                                 known_flags.check = check;
175                                 known_flags.has_crc32 = has_crc32;
176                                 known_flags.is_multi = is_multi;
177
178                                 test_header();
179                                 test_tail();
180                         }
181                 }
182         }
183
184         // Invalid headers
185         test_encode_invalid();
186         test_decode_invalid();
187
188         lzma_end(&strm);
189
190         return 0;
191 }