Remove building with NOCRYPTO option
[minix.git] / external / public-domain / xz / dist / tests / test_stream_flags.c
blob9611459e1babd1a54c975fbfb8cd69963e7f9cb4
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file test_stream_flags.c
4 /// \brief Tests Stream Header and Stream Footer coders
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tests.h"
16 static lzma_stream_flags known_flags;
17 static lzma_stream_flags decoded_flags;
18 static uint8_t buffer[LZMA_STREAM_HEADER_SIZE];
21 static bool
22 validate(void)
24 // TODO: This could require the specific error type as an argument.
25 // We could also test that lzma_stream_flags_compare() gives
26 // the correct return values in different situations.
27 return lzma_stream_flags_compare(&known_flags, &decoded_flags)
28 != LZMA_OK;
32 static bool
33 test_header_decoder(lzma_ret expected_ret)
35 memcrap(&decoded_flags, sizeof(decoded_flags));
37 if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
38 return true;
40 if (expected_ret != LZMA_OK)
41 return false;
43 // Header doesn't have Backward Size, so make
44 // lzma_stream_flags_compare() ignore it.
45 decoded_flags.backward_size = LZMA_VLI_UNKNOWN;
46 return validate();
50 static void
51 test_header(void)
53 memcrap(buffer, sizeof(buffer));
54 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
55 succeed(test_header_decoder(LZMA_OK));
59 static bool
60 test_footer_decoder(lzma_ret expected_ret)
62 memcrap(&decoded_flags, sizeof(decoded_flags));
64 if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
65 return true;
67 if (expected_ret != LZMA_OK)
68 return false;
70 return validate();
74 static void
75 test_footer(void)
77 memcrap(buffer, sizeof(buffer));
78 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
79 succeed(test_footer_decoder(LZMA_OK));
83 static void
84 test_encode_invalid(void)
86 known_flags.check = LZMA_CHECK_ID_MAX + 1;
87 known_flags.backward_size = 1024;
89 expect(lzma_stream_header_encode(&known_flags, buffer)
90 == LZMA_PROG_ERROR);
92 expect(lzma_stream_footer_encode(&known_flags, buffer)
93 == LZMA_PROG_ERROR);
95 known_flags.check = (lzma_check)(-1);
97 expect(lzma_stream_header_encode(&known_flags, buffer)
98 == LZMA_PROG_ERROR);
100 expect(lzma_stream_footer_encode(&known_flags, buffer)
101 == LZMA_PROG_ERROR);
103 known_flags.check = LZMA_CHECK_NONE;
104 known_flags.backward_size = 0;
106 // Header encoder ignores backward_size.
107 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
109 expect(lzma_stream_footer_encode(&known_flags, buffer)
110 == LZMA_PROG_ERROR);
112 known_flags.backward_size = LZMA_VLI_MAX;
114 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
116 expect(lzma_stream_footer_encode(&known_flags, buffer)
117 == LZMA_PROG_ERROR);
121 static void
122 test_decode_invalid(void)
124 known_flags.check = LZMA_CHECK_NONE;
125 known_flags.backward_size = 1024;
127 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
129 // Test 1 (invalid Magic Bytes)
130 buffer[5] ^= 1;
131 succeed(test_header_decoder(LZMA_FORMAT_ERROR));
132 buffer[5] ^= 1;
134 // Test 2a (valid CRC32)
135 uint32_t crc = lzma_crc32(buffer + 6, 2, 0);
136 unaligned_write32le(buffer + 8, crc);
137 succeed(test_header_decoder(LZMA_OK));
139 // Test 2b (invalid Stream Flags with valid CRC32)
140 buffer[6] ^= 0x20;
141 crc = lzma_crc32(buffer + 6, 2, 0);
142 unaligned_write32le(buffer + 8, crc);
143 succeed(test_header_decoder(LZMA_OPTIONS_ERROR));
145 // Test 3 (invalid CRC32)
146 expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
147 buffer[9] ^= 1;
148 succeed(test_header_decoder(LZMA_DATA_ERROR));
150 // Test 4 (invalid Stream Flags with valid CRC32)
151 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
152 buffer[9] ^= 0x40;
153 crc = lzma_crc32(buffer + 4, 6, 0);
154 unaligned_write32le(buffer, crc);
155 succeed(test_footer_decoder(LZMA_OPTIONS_ERROR));
157 // Test 5 (invalid Magic Bytes)
158 expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
159 buffer[11] ^= 1;
160 succeed(test_footer_decoder(LZMA_FORMAT_ERROR));
165 main(void)
167 // Valid headers
168 known_flags.backward_size = 1024;
169 for (lzma_check check = LZMA_CHECK_NONE;
170 check <= LZMA_CHECK_ID_MAX; ++check) {
171 test_header();
172 test_footer();
175 // Invalid headers
176 test_encode_invalid();
177 test_decode_invalid();
179 return 0;