1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_stream_flags.c
4 /// \brief Tests Stream Header and Stream tail coders
6 // Copyright (C) 2007 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
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;
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)
42 test_header_decoder(size_t expected_size, lzma_ret expected_ret)
44 memcrap(&decoded_flags, sizeof(decoded_flags));
46 if (lzma_stream_header_decoder(&strm, &decoded_flags) != LZMA_OK)
49 if (coder_loop(&strm, buffer, expected_size, NULL, 0,
50 expected_ret, LZMA_RUN))
53 if (expected_ret != LZMA_STREAM_END)
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));
70 test_tail_decoder(size_t expected_size, lzma_ret expected_ret)
72 memcrap(&decoded_flags, sizeof(decoded_flags));
74 if (lzma_stream_tail_decoder(&strm, &decoded_flags) != LZMA_OK)
77 if (coder_loop(&strm, buffer, expected_size, NULL, 0,
78 expected_ret, LZMA_RUN))
81 if (expected_ret == LZMA_STREAM_END && validate())
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));
98 test_encode_invalid(void)
100 known_flags.check = LZMA_CHECK_ID_MAX + 1;
101 known_flags.has_crc32 = false;
102 known_flags.is_multi = false;
104 expect(lzma_stream_header_encode(buffer, &known_flags)
107 expect(lzma_stream_tail_encode(buffer, &known_flags)
110 known_flags.check = (lzma_check_type)(-1);
112 expect(lzma_stream_header_encode(buffer, &known_flags)
115 expect(lzma_stream_tail_encode(buffer, &known_flags)
121 test_decode_invalid(void)
123 known_flags.check = LZMA_CHECK_NONE;
124 known_flags.has_crc32 = false;
125 known_flags.is_multi = false;
127 expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK);
129 // Test 1 (invalid Magic Bytes)
131 succeed(test_header_decoder(6, LZMA_DATA_ERROR));
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));
140 // Test 2b (invalid Stream Flags with valid CRC32)
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));
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));
152 // Test 4 (invalid Stream Flags)
153 expect(lzma_stream_tail_encode(buffer, &known_flags) == LZMA_OK);
155 succeed(test_tail_decoder(1, LZMA_HEADER_ERROR));
158 // Test 5 (invalid Magic Bytes)
160 succeed(test_tail_decoder(3, LZMA_DATA_ERROR));
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;
185 test_encode_invalid();
186 test_decode_invalid();