1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_stream_flags.c
4 /// \brief Tests Stream Header and Stream Footer 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];
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)
40 test_header_decoder(lzma_ret expected_ret)
42 memcrap(&decoded_flags, sizeof(decoded_flags));
44 if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
47 if (expected_ret != LZMA_OK)
50 // Header doesn't have Backward Size, so make
51 // lzma_stream_flags_compare() ignore it.
52 decoded_flags.backward_size = LZMA_VLI_UNKNOWN;
60 memcrap(buffer, sizeof(buffer));
61 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
62 succeed(test_header_decoder(LZMA_OK));
67 test_footer_decoder(lzma_ret expected_ret)
69 memcrap(&decoded_flags, sizeof(decoded_flags));
71 if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
74 if (expected_ret != LZMA_OK)
84 memcrap(buffer, sizeof(buffer));
85 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
86 succeed(test_footer_decoder(LZMA_OK));
91 test_encode_invalid(void)
93 known_flags.check = LZMA_CHECK_ID_MAX + 1;
94 known_flags.backward_size = 1024;
96 expect(lzma_stream_header_encode(&known_flags, buffer)
99 expect(lzma_stream_footer_encode(&known_flags, buffer)
102 known_flags.check = (lzma_check)(-1);
104 expect(lzma_stream_header_encode(&known_flags, buffer)
107 expect(lzma_stream_footer_encode(&known_flags, buffer)
110 known_flags.check = LZMA_CHECK_NONE;
111 known_flags.backward_size = 0;
113 // Header encoder ignores backward_size.
114 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
116 expect(lzma_stream_footer_encode(&known_flags, buffer)
119 known_flags.backward_size = LZMA_VLI_MAX;
121 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
123 expect(lzma_stream_footer_encode(&known_flags, buffer)
129 test_decode_invalid(void)
131 known_flags.check = LZMA_CHECK_NONE;
132 known_flags.backward_size = 1024;
134 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
136 // Test 1 (invalid Magic Bytes)
138 succeed(test_header_decoder(LZMA_FORMAT_ERROR));
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));
146 // Test 2b (invalid Stream Flags with valid CRC32)
148 crc = lzma_crc32(buffer + 6, 2, 0);
149 integer_write_32(buffer + 8, crc);
150 succeed(test_header_decoder(LZMA_OPTIONS_ERROR));
152 // Test 3 (invalid CRC32)
153 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
155 succeed(test_header_decoder(LZMA_DATA_ERROR));
157 // Test 4 (invalid Stream Flags with valid CRC32)
158 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
160 crc = lzma_crc32(buffer + 4, 6, 0);
161 integer_write_32(buffer, crc);
162 succeed(test_footer_decoder(LZMA_OPTIONS_ERROR));
164 // Test 5 (invalid Magic Bytes)
165 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
167 succeed(test_footer_decoder(LZMA_FORMAT_ERROR));
177 known_flags.backward_size = 1024;
178 for (lzma_check check = LZMA_CHECK_NONE;
179 check <= LZMA_CHECK_ID_MAX; ++check) {
185 test_encode_invalid();
186 test_decode_invalid();