1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <helpers/file.h>
6 #include <lib/lzmadecode.h>
10 #include <sys/types.h>
12 #include <tests/test.h>
15 struct lzma_test_state
{
22 static int teardown_ulzman_file(void **state
)
24 struct lzma_test_state
*s
= *state
;
26 test_free(s
->raw_filename
);
27 test_free(s
->comp_filename
);
33 /* Set data file with prestate */
34 static int setup_ulzman_file(void **state
)
37 const char *fname_base
= *state
;
38 const char path_prefix
[] = "lib/lzma-test/%s%s";
39 const char raw_file_suffix
[] = ".bin";
40 const char comp_file_suffix
[] = ".lzma.bin";
41 struct lzma_test_state
*s
= test_malloc(sizeof(*s
));
42 memset(s
, 0, sizeof(*s
));
47 const size_t raw_filename_size
=
48 strlen(path_prefix
) + strlen(fname_base
) + ARRAY_SIZE(raw_file_suffix
);
49 s
->raw_filename
= test_malloc(raw_filename_size
);
51 const size_t comp_filename_size
=
52 strlen(path_prefix
) + strlen(fname_base
) + ARRAY_SIZE(comp_file_suffix
);
53 s
->comp_filename
= test_malloc(comp_filename_size
);
55 if (!s
->raw_filename
|| !s
->comp_filename
) {
56 print_error("File path allocation error\n");
61 snprintf(s
->raw_filename
, raw_filename_size
, path_prefix
, fname_base
, raw_file_suffix
);
62 snprintf(s
->comp_filename
, comp_filename_size
, path_prefix
, fname_base
,
65 s
->raw_file_sz
= test_get_file_size(s
->raw_filename
);
66 s
->comp_file_sz
= test_get_file_size(s
->comp_filename
);
68 if (s
->raw_file_sz
== -1) {
69 print_error("Unable to open file: %s\n", s
->raw_filename
);
74 if (s
->comp_file_sz
== -1) {
75 print_error("Unable to open file: %s\n", s
->comp_filename
);
83 teardown_ulzman_file((void **)&s
);
87 static void test_ulzman_correct_file(void **state
)
89 struct lzma_test_state
*s
= *state
;
90 uint8_t *raw_buf
= test_malloc(s
->raw_file_sz
);
91 uint8_t *decomp_buf
= test_malloc(s
->raw_file_sz
);
92 uint8_t *comp_buf
= test_malloc(s
->comp_file_sz
);
94 assert_non_null(raw_buf
);
95 assert_non_null(decomp_buf
);
96 assert_non_null(comp_buf
);
97 assert_int_equal(s
->raw_file_sz
,
98 test_read_file(s
->raw_filename
, raw_buf
, s
->raw_file_sz
));
99 assert_int_equal(s
->comp_file_sz
,
100 test_read_file(s
->comp_filename
, comp_buf
, s
->comp_file_sz
));
102 assert_int_equal(s
->raw_file_sz
,
103 ulzman(comp_buf
, s
->comp_file_sz
, decomp_buf
, s
->raw_file_sz
));
104 assert_memory_equal(raw_buf
, decomp_buf
, s
->raw_file_sz
);
107 test_free(decomp_buf
);
111 static void test_ulzman_input_too_small(void **state
)
113 uint8_t in_buf
[32] = {0};
116 assert_int_equal(0, ulzman(in_buf
, LZMA_PROPERTIES_SIZE
, out_buf
, sizeof(out_buf
)));
119 static void test_ulzman_zero_buffer(void **state
)
121 uint8_t in_buf
[LZMA_PROPERTIES_SIZE
+ 1 * KiB
];
122 uint8_t out_buf
[2 * KiB
];
124 memset(in_buf
, 0, sizeof(in_buf
));
125 memset(out_buf
, 0, sizeof(out_buf
));
127 assert_int_equal(0, ulzman(in_buf
, sizeof(in_buf
), out_buf
, sizeof(out_buf
)));
130 #define ULZMAN_CORRECT_FILE_TEST(_file_prefix) \
132 .name = "test_ulzman_correct_file(" _file_prefix ")", \
133 .test_func = test_ulzman_correct_file, .setup_func = setup_ulzman_file, \
134 .teardown_func = teardown_ulzman_file, .initial_state = (_file_prefix) \
139 const struct CMUnitTest tests
[] = {
140 /* "data.N" in macros below refers to files:
141 - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin
142 - __TEST_DATA_DIR__ /lib/lzma-test/data.N.bin.lzma
143 Files data.N.bin suffix are raw data, and data.N.lzma.bin are its
144 LZMA-compressed form. Both are required to exist.
147 /* util/cbfs-compression-tool compressed by itself.
148 To test compression of executable files like payloads. */
149 ULZMAN_CORRECT_FILE_TEST("data.1"),
151 /* README.md compressed by util/cbfs-compression-tool. */
152 ULZMAN_CORRECT_FILE_TEST("data.2"),
154 /* tests/lib/imd-test.c compressed by util/cbfs-compression-tool
155 Structured text file. */
156 ULZMAN_CORRECT_FILE_TEST("data.3"),
158 /* libcmocka.so.0.7.0 compressed by util/cbfs-compression-tool
159 Another binary file, shared object. */
160 ULZMAN_CORRECT_FILE_TEST("data.4"),
162 cmocka_unit_test(test_ulzman_input_too_small
),
164 cmocka_unit_test(test_ulzman_zero_buffer
),
167 return cb_run_group_tests(tests
, NULL
, NULL
);