Rizin
unix-like reverse engineering framework and cli tools
test_stream_flags.c File Reference

Tests Stream Header and Stream Footer coders. More...

#include "tests.h"

Go to the source code of this file.

Functions

static bool validate (void)
 
static bool test_header_decoder (lzma_ret expected_ret)
 
static void test_header (void)
 
static bool test_footer_decoder (lzma_ret expected_ret)
 
static void test_footer (void)
 
static void test_encode_invalid (void)
 
static void test_decode_invalid (void)
 
int main (void)
 

Variables

static lzma_stream_flags known_flags
 
static lzma_stream_flags decoded_flags
 
static uint8_t buffer [LZMA_STREAM_HEADER_SIZE]
 

Detailed Description

Tests Stream Header and Stream Footer coders.

Definition in file test_stream_flags.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 165 of file test_stream_flags.c.

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
lzma_vli backward_size
Backward Size.
Definition: stream_flags.h:69
static void test_footer(void)
static lzma_stream_flags known_flags
static void test_encode_invalid(void)
static void test_decode_invalid(void)
static void test_header(void)

References lzma_stream_flags::backward_size, check, known_flags, LZMA_CHECK_ID_MAX, LZMA_CHECK_NONE, test_decode_invalid(), test_encode_invalid(), test_footer(), and test_header().

◆ test_decode_invalid()

static void test_decode_invalid ( void  )
static

Definition at line 122 of file test_stream_flags.c.

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 }
#define expect(expr, value)
Definition: lz4.c:170
unsigned int uint32_t
Definition: sftypes.h:29
Definition: buffer.h:15
lzma_check check
Check ID.
Definition: stream_flags.h:79
static bool test_header_decoder(lzma_ret expected_ret)
static bool test_footer_decoder(lzma_ret expected_ret)
#define succeed(test)
Definition: tests.h:27
#define write32le(buf, num)
@ 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

References lzma_stream_flags::backward_size, lzma_stream_flags::check, expect, known_flags, LZMA_CHECK_NONE, LZMA_DATA_ERROR, LZMA_FORMAT_ERROR, LZMA_OK, LZMA_OPTIONS_ERROR, succeed, test_footer_decoder(), test_header_decoder(), and write32le.

Referenced by main().

◆ test_encode_invalid()

static void test_encode_invalid ( void  )
static

Definition at line 84 of file test_stream_flags.c.

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 }
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218

References lzma_stream_flags::backward_size, lzma_stream_flags::check, expect, known_flags, LZMA_CHECK_ID_MAX, LZMA_CHECK_NONE, LZMA_OK, LZMA_PROG_ERROR, and LZMA_VLI_MAX.

Referenced by main().

◆ test_footer()

static void test_footer ( void  )
static

Definition at line 75 of file test_stream_flags.c.

76 {
77  memcrap(buffer, sizeof(buffer));
78  expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
80 }
#define memcrap(buf, size)
Definition: tests.h:22

References expect, known_flags, LZMA_OK, memcrap, succeed, and test_footer_decoder().

Referenced by main().

◆ test_footer_decoder()

static bool test_footer_decoder ( lzma_ret  expected_ret)
static

Definition at line 60 of file test_stream_flags.c.

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 }
static lzma_stream_flags decoded_flags
static bool validate(void)

References decoded_flags, LZMA_OK, memcrap, and validate().

Referenced by test_decode_invalid(), and test_footer().

◆ test_header()

static void test_header ( void  )
static

Definition at line 51 of file test_stream_flags.c.

52 {
53  memcrap(buffer, sizeof(buffer));
54  expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
56 }

References expect, known_flags, LZMA_OK, memcrap, succeed, and test_header_decoder().

Referenced by main().

◆ test_header_decoder()

static bool test_header_decoder ( lzma_ret  expected_ret)
static

Definition at line 33 of file test_stream_flags.c.

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 }
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39

References lzma_stream_flags::backward_size, decoded_flags, LZMA_OK, LZMA_VLI_UNKNOWN, memcrap, and validate().

Referenced by test_decode_invalid(), and test_header().

◆ validate()

static bool validate ( void  )
static

Definition at line 22 of file test_stream_flags.c.

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 }

References decoded_flags, known_flags, and LZMA_OK.

Referenced by test_footer_decoder(), and test_header_decoder().

Variable Documentation

◆ buffer

Definition at line 18 of file test_stream_flags.c.

◆ decoded_flags

lzma_stream_flags decoded_flags
static

Definition at line 17 of file test_stream_flags.c.

Referenced by test_footer_decoder(), test_header_decoder(), and validate().

◆ known_flags