| 1 |
|
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <helpers/file.h> |
| 5 |
#include <lib.h> |
| 6 |
#include <lib/lzmadecode.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <tests/test.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
struct lzma_test_state { |
| 16 |
char *raw_filename; |
| 17 |
size_t raw_file_sz; |
| 18 |
char *comp_filename; |
| 19 |
size_t comp_file_sz; |
| 20 |
}; |
| 21 |
|
| 22 |
static int teardown_ulzman_file(void **state) |
| 23 |
{ |
| 24 |
struct lzma_test_state *s = *state; |
| 25 |
|
| 26 |
test_free(s->raw_filename); |
| 27 |
test_free(s->comp_filename); |
| 28 |
test_free(s); |
| 29 |
|
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
static int setup_ulzman_file(void **state) |
| 35 |
{ |
| 36 |
int ret = 0; |
| 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)); |
| 43 |
|
| 44 |
if (!s) |
| 45 |
return 1; |
| 46 |
|
| 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); |
| 50 |
|
| 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); |
| 54 |
|
| 55 |
if (!s->raw_filename || !s->comp_filename) { |
| 56 |
print_error("File path allocation error\n"); |
| 57 |
ret = 2; |
| 58 |
goto error; |
| 59 |
} |
| 60 |
|
| 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, |
| 63 |
comp_file_suffix); |
| 64 |
|
| 65 |
s->raw_file_sz = test_get_file_size(s->raw_filename); |
| 66 |
s->comp_file_sz = test_get_file_size(s->comp_filename); |
| 67 |
|
| 68 |
if (s->raw_file_sz == -1) { |
| 69 |
print_error("Unable to open file: %s\n", s->raw_filename); |
| 70 |
ret = 3; |
| 71 |
goto error; |
| 72 |
} |
| 73 |
|
| 74 |
if (s->comp_file_sz == -1) { |
| 75 |
print_error("Unable to open file: %s\n", s->comp_filename); |
| 76 |
ret = 3; |
| 77 |
goto error; |
| 78 |
} |
| 79 |
|
| 80 |
*state = s; |
| 81 |
return 0; |
| 82 |
error: |
| 83 |
teardown_ulzman_file((void **)&s); |
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
static void test_ulzman_correct_file(void **state) |
| 88 |
{ |
| 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); |
| 93 |
|
| 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)); |
| 101 |
|
| 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); |
| 105 |
|
| 106 |
test_free(raw_buf); |
| 107 |
test_free(decomp_buf); |
| 108 |
test_free(comp_buf); |
| 109 |
} |
| 110 |
|
| 111 |
static void test_ulzman_input_too_small(void **state) |
| 112 |
{ |
| 113 |
uint8_t in_buf[32] = {0}; |
| 114 |
uint8_t out_buf[32]; |
| 115 |
|
| 116 |
assert_int_equal(0, ulzman(in_buf, LZMA_PROPERTIES_SIZE, out_buf, sizeof(out_buf))); |
| 117 |
} |
| 118 |
|
| 119 |
static void test_ulzman_zero_buffer(void **state) |
| 120 |
{ |
| 121 |
uint8_t in_buf[LZMA_PROPERTIES_SIZE + 1 * KiB]; |
| 122 |
uint8_t out_buf[2 * KiB]; |
| 123 |
|
| 124 |
memset(in_buf, 0, sizeof(in_buf)); |
| 125 |
memset(out_buf, 0, sizeof(out_buf)); |
| 126 |
|
| 127 |
assert_int_equal(0, ulzman(in_buf, sizeof(in_buf), out_buf, sizeof(out_buf))); |
| 128 |
} |
| 129 |
|
| 130 |
#define ULZMAN_CORRECT_FILE_TEST(_file_prefix) \ |
| 131 |
{ \ |
| 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) \ |
| 135 |
} |
| 136 |
|
| 137 |
int main(void) |
| 138 |
{ |
| 139 |
const struct CMUnitTest tests[] = { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
ULZMAN_CORRECT_FILE_TEST("data.1"), |
| 150 |
|
| 151 |
|
| 152 |
ULZMAN_CORRECT_FILE_TEST("data.2"), |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
ULZMAN_CORRECT_FILE_TEST("data.3"), |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
ULZMAN_CORRECT_FILE_TEST("data.4"), |
| 161 |
|
| 162 |
cmocka_unit_test(test_ulzman_input_too_small), |
| 163 |
|
| 164 |
cmocka_unit_test(test_ulzman_zero_buffer), |
| 165 |
}; |
| 166 |
|
| 167 |
return cb_run_group_tests(tests, NULL, NULL); |
| 168 |
} |