Rizin
unix-like reverse engineering framework and cli tools
test_stream_flags.c
Go to the documentation of this file.
1 //
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.
10 //
12 
13 #include "tests.h"
14 
15 
19 
20 
21 static bool
22 validate(void)
23 {
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;
29 }
30 
31 
32 static bool
34 {
36 
37  if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
38  return true;
39 
40  if (expected_ret != LZMA_OK)
41  return false;
42 
43  // Header doesn't have Backward Size, so make
44  // lzma_stream_flags_compare() ignore it.
46  return validate();
47 }
48 
49 
50 static void
52 {
53  memcrap(buffer, sizeof(buffer));
54  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
56 }
57 
58 
59 static bool
61 {
63 
64  if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
65  return true;
66 
67  if (expected_ret != LZMA_OK)
68  return false;
69 
70  return validate();
71 }
72 
73 
74 static void
76 {
77  memcrap(buffer, sizeof(buffer));
78  expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
80 }
81 
82 
83 static void
85 {
88 
89  expect(lzma_stream_header_encode(&known_flags, buffer)
90  == LZMA_PROG_ERROR);
91 
92  expect(lzma_stream_footer_encode(&known_flags, buffer)
93  == LZMA_PROG_ERROR);
94 
96 
97  expect(lzma_stream_header_encode(&known_flags, buffer)
98  == LZMA_PROG_ERROR);
99 
100  expect(lzma_stream_footer_encode(&known_flags, buffer)
101  == LZMA_PROG_ERROR);
102 
105 
106  // Header encoder ignores backward_size.
107  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
108 
109  expect(lzma_stream_footer_encode(&known_flags, buffer)
110  == LZMA_PROG_ERROR);
111 
113 
114  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
115 
116  expect(lzma_stream_footer_encode(&known_flags, buffer)
117  == LZMA_PROG_ERROR);
118 }
119 
120 
121 static void
123 {
125  known_flags.backward_size = 1024;
126 
127  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
128 
129  // Test 1 (invalid Magic Bytes)
130  buffer[5] ^= 1;
132  buffer[5] ^= 1;
133 
134  // Test 2a (valid CRC32)
135  uint32_t crc = lzma_crc32(buffer + 6, 2, 0);
136  write32le(buffer + 8, crc);
138 
139  // Test 2b (invalid Stream Flags with valid CRC32)
140  buffer[6] ^= 0x20;
141  crc = lzma_crc32(buffer + 6, 2, 0);
142  write32le(buffer + 8, crc);
144 
145  // Test 3 (invalid CRC32)
146  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
147  buffer[9] ^= 1;
149 
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  write32le(buffer, crc);
156 
157  // Test 5 (invalid Magic Bytes)
158  expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
159  buffer[11] ^= 1;
161 }
162 
163 
164 int
165 main(void)
166 {
167  // Valid headers
168  known_flags.backward_size = 1024;
170  check <= LZMA_CHECK_ID_MAX; ++check) {
171  test_header();
172  test_footer();
173  }
174 
175  // Invalid headers
178 
179  return 0;
180 }
lzma_check
Type of the integrity check (Check ID)
Definition: check.h:27
@ LZMA_CHECK_NONE
Definition: check.h:28
#define LZMA_CHECK_ID_MAX
Maximum valid Check ID.
Definition: check.h:68
lzma_check check
Definition: container.h:292
#define expect(expr, value)
Definition: lz4.c:170
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
#define LZMA_STREAM_HEADER_SIZE
Size of Stream Header and Stream Footer.
Definition: stream_flags.h:27
Definition: buffer.h:15
Options for encoding/decoding Stream Header and Stream Footer.
Definition: stream_flags.h:33
lzma_vli backward_size
Backward Size.
Definition: stream_flags.h:69
lzma_check check
Check ID.
Definition: stream_flags.h:79
static lzma_stream_flags decoded_flags
int main(void)
static bool test_header_decoder(lzma_ret expected_ret)
static void test_footer(void)
static lzma_stream_flags known_flags
static void test_encode_invalid(void)
static bool test_footer_decoder(lzma_ret expected_ret)
static void test_decode_invalid(void)
static void test_header(void)
static bool validate(void)
Common definitions for test applications.
#define memcrap(buf, size)
Definition: tests.h:22
#define succeed(test)
Definition: tests.h:27
#define write32le(buf, num)
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_FORMAT_ERROR
Memory usage limit was reached.
Definition: base.h:150
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58