1 /* SPDX-License-Identifier: GPL-2.0-only */
5 #include <lib/lzmadecode.h>
10 #include <tests/test.h>
14 struct lzma_test_state
{
21 static int get_file_size(const char *fname
)
24 if (stat(fname
, &st
) == -1)
29 static int teardown_ulzman_file(void **state
)
31 struct lzma_test_state
*s
= *state
;
33 test_free(s
->raw_filename
);
34 test_free(s
->comp_filename
);
40 /* Set data file with prestate */
41 static int setup_ulzman_file(void **state
)
44 const char *fname_base
= *state
;
45 const char path_prefix
[] = __TEST_DATA_DIR__
"/lib/lzma-test/%s%s";
46 const char raw_file_suffix
[] = ".bin";
47 const char comp_file_suffix
[] = ".lzma.bin";
48 struct lzma_test_state
*s
= test_malloc(sizeof(*s
));
49 memset(s
, 0, sizeof(*s
));
54 const size_t raw_filename_size
=
55 strlen(path_prefix
) + strlen(fname_base
) + ARRAY_SIZE(raw_file_suffix
);
56 s
->raw_filename
= test_malloc(raw_filename_size
);
58 const size_t comp_filename_size
=
59 strlen(path_prefix
) + strlen(fname_base
) + ARRAY_SIZE(comp_file_suffix
);
60 s
->comp_filename
= test_malloc(comp_filename_size
);
62 if (!s
->raw_filename
|| !s
->comp_filename
) {
63 print_error("File path allocation error\n");
68 snprintf(s
->raw_filename
, raw_filename_size
, path_prefix
, fname_base
, raw_file_suffix
);
69 snprintf(s
->comp_filename
, comp_filename_size
, path_prefix
, fname_base
,
72 s
->raw_file_sz
= get_file_size(s
->raw_filename
);
73 s
->comp_file_sz
= get_file_size(s
->comp_filename
);
75 if (s
->raw_file_sz
== -1) {
76 print_error("Unable to open file: %s\n", s
->raw_filename
);
81 if (s
->comp_file_sz
== -1) {
82 print_error("Unable to open file: %s\n", s
->comp_filename
);
90 teardown_ulzman_file((void **)&s
);
94 static int read_file(const char *fname
, uint8_t *buf
, size_t sz
)
96 int f
= open(fname
, O_RDONLY
);
102 read_sz
= read(f
, buf
, sz
);
108 static void test_ulzman_correct_file(void **state
)
110 struct lzma_test_state
*s
= *state
;
111 uint8_t *raw_buf
= test_malloc(s
->raw_file_sz
);
112 uint8_t *decomp_buf
= test_malloc(s
->raw_file_sz
);
113 uint8_t *comp_buf
= test_malloc(s
->comp_file_sz
);
115 assert_non_null(raw_buf
);
116 assert_non_null(decomp_buf
);
117 assert_non_null(comp_buf
);
118 assert_int_equal(s
->raw_file_sz
, read_file(s
->raw_filename
, raw_buf
, s
->raw_file_sz
));
119 assert_int_equal(s
->comp_file_sz
,
120 read_file(s
->comp_filename
, comp_buf
, s
->comp_file_sz
));
122 assert_int_equal(s
->raw_file_sz
,
123 ulzman(comp_buf
, s
->comp_file_sz
, decomp_buf
, s
->raw_file_sz
));
124 assert_memory_equal(raw_buf
, decomp_buf
, s
->raw_file_sz
);
127 test_free(decomp_buf
);
131 static void test_ulzman_input_too_small(void **state
)
133 uint8_t in_buf
[32] = {0};
136 assert_int_equal(0, ulzman(in_buf
, LZMA_PROPERTIES_SIZE
, out_buf
, sizeof(out_buf
)));
139 static void test_ulzman_zero_buffer(void **state
)
141 uint8_t in_buf
[LZMA_PROPERTIES_SIZE
+ 1 * KiB
];
142 uint8_t out_buf
[2 * KiB
];
144 memset(in_buf
, 0, sizeof(in_buf
));
145 memset(out_buf
, 0, sizeof(out_buf
));
147 assert_int_equal(0, ulzman(in_buf
, sizeof(in_buf
), out_buf
, sizeof(out_buf
)));
150 #define ULZMAN_CORRECT_FILE_TEST(_file_prefix) \
152 .name = "test_ulzman_correct_file(" _file_prefix ")", \
153 .test_func = test_ulzman_correct_file, .setup_func = setup_ulzman_file, \
154 .teardown_func = teardown_ulzman_file, .initial_state = (_file_prefix) \
159 const struct CMUnitTest tests
[] = {
160 /* "data.N" in macros below refers to files:
161 - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin
162 - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin.lzma
163 Files data.N.bin suffix are raw data, and data.N.lzma.bin are its
164 LZMA-compressed form. Both are required to exist.
167 /* util/cbfs-compression-tool compressed by itself.
168 To test compression of executable files like payloads. */
169 ULZMAN_CORRECT_FILE_TEST("data.1"),
171 /* README.md compressed by util/cbfs-compression-tool. */
172 ULZMAN_CORRECT_FILE_TEST("data.2"),
174 /* tests/lib/imd-test.c compressed by util/cbfs-compression-tool
175 Structured text file. */
176 ULZMAN_CORRECT_FILE_TEST("data.3"),
178 /* libcmocka.so.0.7.0 compressed by util/cbfs-compression-tool
179 Another binary file, shared object. */
180 ULZMAN_CORRECT_FILE_TEST("data.4"),
182 cmocka_unit_test(test_ulzman_input_too_small
),
184 cmocka_unit_test(test_ulzman_zero_buffer
),
187 return cb_run_group_tests(tests
, NULL
, NULL
);