summaryrefslogtreecommitdiff
path: root/tests/lib/lzma-test.c
blob: 48d629a17923d75478896cdbe6d39f711317fba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* SPDX-License-Identifier: GPL-2.0-only */

#include <fcntl.h>
#include <helpers/file.h>
#include <lib.h>
#include <lib/lzmadecode.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <tests/test.h>
#include <unistd.h>

struct lzma_test_state {
	char *raw_filename;
	size_t raw_file_sz;
	char *comp_filename;
	size_t comp_file_sz;
};

static int teardown_ulzman_file(void **state)
{
	struct lzma_test_state *s = *state;

	test_free(s->raw_filename);
	test_free(s->comp_filename);
	test_free(s);

	return 0;
}

/* Set data file with prestate */
static int setup_ulzman_file(void **state)
{
	int ret = 0;
	const char *fname_base = *state;
	const char path_prefix[] = "lib/lzma-test/%s%s";
	const char raw_file_suffix[] = ".bin";
	const char comp_file_suffix[] = ".lzma.bin";
	struct lzma_test_state *s = test_malloc(sizeof(*s));
	memset(s, 0, sizeof(*s));

	if (!s)
		return 1;

	const size_t raw_filename_size =
		strlen(path_prefix) + strlen(fname_base) + ARRAY_SIZE(raw_file_suffix);
	s->raw_filename = test_malloc(raw_filename_size);

	const size_t comp_filename_size =
		strlen(path_prefix) + strlen(fname_base) + ARRAY_SIZE(comp_file_suffix);
	s->comp_filename = test_malloc(comp_filename_size);

	if (!s->raw_filename || !s->comp_filename) {
		print_error("File path allocation error\n");
		ret = 2;
		goto error;
	}

	snprintf(s->raw_filename, raw_filename_size, path_prefix, fname_base, raw_file_suffix);
	snprintf(s->comp_filename, comp_filename_size, path_prefix, fname_base,
		 comp_file_suffix);

	s->raw_file_sz = test_get_file_size(s->raw_filename);
	s->comp_file_sz = test_get_file_size(s->comp_filename);

	if (s->raw_file_sz == -1) {
		print_error("Unable to open file: %s\n", s->raw_filename);
		ret = 3;
		goto error;
	}

	if (s->comp_file_sz == -1) {
		print_error("Unable to open file: %s\n", s->comp_filename);
		ret = 3;
		goto error;
	}

	*state = s;
	return 0;
error:
	teardown_ulzman_file((void **)&s);
	return ret;
}

static void test_ulzman_correct_file(void **state)
{
	struct lzma_test_state *s = *state;
	uint8_t *raw_buf = test_malloc(s->raw_file_sz);
	uint8_t *decomp_buf = test_malloc(s->raw_file_sz);
	uint8_t *comp_buf = test_malloc(s->comp_file_sz);

	assert_non_null(raw_buf);
	assert_non_null(decomp_buf);
	assert_non_null(comp_buf);
	assert_int_equal(s->raw_file_sz,
			 test_read_file(s->raw_filename, raw_buf, s->raw_file_sz));
	assert_int_equal(s->comp_file_sz,
			 test_read_file(s->comp_filename, comp_buf, s->comp_file_sz));

	assert_int_equal(s->raw_file_sz,
			 ulzman(comp_buf, s->comp_file_sz, decomp_buf, s->raw_file_sz));
	assert_memory_equal(raw_buf, decomp_buf, s->raw_file_sz);

	test_free(raw_buf);
	test_free(decomp_buf);
	test_free(comp_buf);
}

static void test_ulzman_input_too_small(void **state)
{
	uint8_t in_buf[32] = {0};
	uint8_t out_buf[32];

	assert_int_equal(0, ulzman(in_buf, LZMA_PROPERTIES_SIZE, out_buf, sizeof(out_buf)));
}

static void test_ulzman_zero_buffer(void **state)
{
	uint8_t in_buf[LZMA_PROPERTIES_SIZE + 1 * KiB];
	uint8_t out_buf[2 * KiB];

	memset(in_buf, 0, sizeof(in_buf));
	memset(out_buf, 0, sizeof(out_buf));

	assert_int_equal(0, ulzman(in_buf, sizeof(in_buf), out_buf, sizeof(out_buf)));
}

#define ULZMAN_CORRECT_FILE_TEST(_file_prefix)                                                 \
	{                                                                                      \
		.name = "test_ulzman_correct_file(" _file_prefix ")",                          \
		.test_func = test_ulzman_correct_file, .setup_func = setup_ulzman_file,        \
		.teardown_func = teardown_ulzman_file, .initial_state = (_file_prefix)         \
	}

int main(void)
{
	const struct CMUnitTest tests[] = {
		/* "data.N" in macros below refers to files:
		   - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin
		   - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin.lzma
		   Files data.N.bin suffix are raw data, and data.N.lzma.bin are its
		   LZMA-compressed form. Both are required to exist.
		 */

		/* util/cbfs-compression-tool compressed by itself.
		   To test compression of executable files like payloads. */
		ULZMAN_CORRECT_FILE_TEST("data.1"),

		/* README.md compressed by util/cbfs-compression-tool. */
		ULZMAN_CORRECT_FILE_TEST("data.2"),

		/* tests/lib/imd-test.c compressed by util/cbfs-compression-tool
		   Structured text file. */
		ULZMAN_CORRECT_FILE_TEST("data.3"),

		/* libcmocka.so.0.7.0 compressed by util/cbfs-compression-tool
		   Another binary file, shared object. */
		ULZMAN_CORRECT_FILE_TEST("data.4"),

		cmocka_unit_test(test_ulzman_input_too_small),

		cmocka_unit_test(test_ulzman_zero_buffer),
	};

	return cb_run_group_tests(tests, NULL, NULL);
}