1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_stream_flags.c
4 /// \brief Tests Stream Header and Stream Footer coders
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
16 static lzma_stream_flags known_flags
;
17 static lzma_stream_flags decoded_flags
;
18 static uint8_t buffer
[LZMA_STREAM_HEADER_SIZE
];
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
)
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
)
40 if (expected_ret
!= LZMA_OK
)
43 // Header doesn't have Backward Size, so make
44 // lzma_stream_flags_compare() ignore it.
45 decoded_flags
.backward_size
= LZMA_VLI_UNKNOWN
;
53 memcrap(buffer
, sizeof(buffer
));
54 expect(lzma_stream_header_encode(&known_flags
, buffer
) == LZMA_OK
);
55 succeed(test_header_decoder(LZMA_OK
));
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
)
67 if (expected_ret
!= LZMA_OK
)
77 memcrap(buffer
, sizeof(buffer
));
78 expect(lzma_stream_footer_encode(&known_flags
, buffer
) == LZMA_OK
);
79 succeed(test_footer_decoder(LZMA_OK
));
84 test_encode_invalid(void)
86 known_flags
.check
= (lzma_check
)(LZMA_CHECK_ID_MAX
+ 1);
87 known_flags
.backward_size
= 1024;
89 expect(lzma_stream_header_encode(&known_flags
, buffer
)
92 expect(lzma_stream_footer_encode(&known_flags
, buffer
)
95 known_flags
.check
= (lzma_check
)(-1);
97 expect(lzma_stream_header_encode(&known_flags
, buffer
)
100 expect(lzma_stream_footer_encode(&known_flags
, buffer
)
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
)
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
)
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)
131 succeed(test_header_decoder(LZMA_FORMAT_ERROR
));
134 // Test 2a (valid CRC32)
135 uint32_t crc
= lzma_crc32(buffer
+ 6, 2, 0);
136 write32le(buffer
+ 8, crc
);
137 succeed(test_header_decoder(LZMA_OK
));
139 // Test 2b (invalid Stream Flags with valid CRC32)
141 crc
= lzma_crc32(buffer
+ 6, 2, 0);
142 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
);
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
);
153 crc
= lzma_crc32(buffer
+ 4, 6, 0);
154 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
);
160 succeed(test_footer_decoder(LZMA_FORMAT_ERROR
));
168 known_flags
.backward_size
= 1024;
169 for (lzma_check check
= LZMA_CHECK_NONE
;
170 check
<= LZMA_CHECK_ID_MAX
; ++check
) {
176 test_encode_invalid();
177 test_decode_invalid();