|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +#include "settings_test.h" |
| 10 | +#include "settings_priv.h" |
| 11 | + |
| 12 | + |
| 13 | +u8_t val8; |
| 14 | +u16_t val16; |
| 15 | +u64_t val64; |
| 16 | + |
| 17 | +int test_get_called; |
| 18 | +int test_set_called; |
| 19 | +int test_commit_called; |
| 20 | +int test_export_block; |
| 21 | + |
| 22 | +int c2_var_count = 1; |
| 23 | + |
| 24 | +int c1_handle_get(const char *name, char *val, int val_len_max); |
| 25 | +int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb, |
| 26 | + void *cb_arg); |
| 27 | +int c1_handle_commit(void); |
| 28 | +int c1_handle_export(int (*cb)(const char *name, |
| 29 | + const void *value, size_t val_len)); |
| 30 | + |
| 31 | +struct settings_handler c_test_handlers[] = { |
| 32 | + { |
| 33 | + .name = "myfoo", |
| 34 | + .h_get = c1_handle_get, |
| 35 | + .h_set = c1_handle_set, |
| 36 | + .h_commit = c1_handle_commit, |
| 37 | + .h_export = c1_handle_export |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +int c1_handle_get(const char *name, char *val, int val_len_max) |
| 44 | +{ |
| 45 | + test_get_called = 1; |
| 46 | + const char *next; |
| 47 | + |
| 48 | + if (settings_name_steq(name, "mybar", &next) && !next) { |
| 49 | + val_len_max = MIN(val_len_max, sizeof(val8)); |
| 50 | + memcpy(val, &val8, MIN(val_len_max, sizeof(val8))); |
| 51 | + return val_len_max; |
| 52 | + } |
| 53 | + |
| 54 | + if (settings_name_steq(name, "mybar16", &next) && !next) { |
| 55 | + val_len_max = MIN(val_len_max, sizeof(val16)); |
| 56 | + memcpy(val, &val16, MIN(val_len_max, sizeof(val16))); |
| 57 | + return val_len_max; |
| 58 | + } |
| 59 | + |
| 60 | + if (settings_name_steq(name, "mybar64", &next) && !next) { |
| 61 | + val_len_max = MIN(val_len_max, sizeof(val64)); |
| 62 | + memcpy(val, &val64, MIN(val_len_max, sizeof(val64))); |
| 63 | + return val_len_max; |
| 64 | + } |
| 65 | + |
| 66 | + return -ENOENT; |
| 67 | +} |
| 68 | + |
| 69 | +int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb, |
| 70 | + void *cb_arg) |
| 71 | +{ |
| 72 | + int rc; |
| 73 | + const char *next; |
| 74 | + size_t val_len; |
| 75 | + |
| 76 | + test_set_called = 1; |
| 77 | + if (settings_name_steq(name, "mybar", &next) && !next) { |
| 78 | + val_len = len; |
| 79 | + zassert_true(val_len == 1, "bad set-value size"); |
| 80 | + |
| 81 | + rc = read_cb(cb_arg, &val8, sizeof(val8)); |
| 82 | + zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback"); |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + if (settings_name_steq(name, "mybar16", &next) && !next) { |
| 87 | + val_len = len; |
| 88 | + zassert_true(val_len == 2, "bad set-value size"); |
| 89 | + |
| 90 | + rc = read_cb(cb_arg, &val16, sizeof(val16)); |
| 91 | + zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback"); |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + if (settings_name_steq(name, "mybar64", &next) && !next) { |
| 96 | + val_len = len; |
| 97 | + zassert_true(val_len == 8, "bad set-value size"); |
| 98 | + |
| 99 | + rc = read_cb(cb_arg, &val64, sizeof(val64)); |
| 100 | + zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback"); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + return -ENOENT; |
| 105 | +} |
| 106 | + |
| 107 | +int c1_handle_commit(void) |
| 108 | +{ |
| 109 | + test_commit_called = 1; |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +int c1_handle_export(int (*cb)(const char *name, |
| 114 | + const void *value, size_t val_len)) |
| 115 | +{ |
| 116 | + if (test_export_block) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + (void)cb("myfoo/mybar", &val8, sizeof(val8)); |
| 121 | + |
| 122 | + (void)cb("myfoo/mybar16", &val16, sizeof(val16)); |
| 123 | + |
| 124 | + (void)cb("myfoo/mybar64", &val64, sizeof(val64)); |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
| 129 | +void ctest_clear_call_state(void) |
| 130 | +{ |
| 131 | + test_get_called = 0; |
| 132 | + test_set_called = 0; |
| 133 | + test_commit_called = 0; |
| 134 | +} |
| 135 | + |
| 136 | +int ctest_get_call_state(void) |
| 137 | +{ |
| 138 | + return test_get_called + test_set_called + test_commit_called; |
| 139 | +} |
| 140 | + |
| 141 | +void config_wipe_srcs(void) |
| 142 | +{ |
| 143 | + sys_slist_init(&settings_load_srcs); |
| 144 | + settings_save_dst = NULL; |
| 145 | +} |
| 146 | + |
| 147 | +int fsutil_read_file(const char *path, off_t offset, size_t len, void *dst, |
| 148 | + size_t *out_len) |
| 149 | +{ |
| 150 | + struct fs_file_t file; |
| 151 | + int rc; |
| 152 | + ssize_t r_len = 0; |
| 153 | + |
| 154 | + rc = fs_open(&file, path); |
| 155 | + if (rc != 0) { |
| 156 | + return rc; |
| 157 | + } |
| 158 | + |
| 159 | + r_len = fs_read(&file, dst, len); |
| 160 | + if (r_len < 0) { |
| 161 | + rc = -EIO; |
| 162 | + } else { |
| 163 | + *out_len = r_len; |
| 164 | + } |
| 165 | + |
| 166 | + fs_close(&file); |
| 167 | + return rc; |
| 168 | +} |
| 169 | + |
| 170 | +int fsutil_write_file(const char *path, const void *data, size_t len) |
| 171 | +{ |
| 172 | + struct fs_file_t file; |
| 173 | + int rc; |
| 174 | + |
| 175 | + rc = fs_open(&file, path); |
| 176 | + if (rc != 0) { |
| 177 | + return rc; |
| 178 | + } |
| 179 | + |
| 180 | + if (fs_write(&file, data, len) != len) { |
| 181 | + rc = -EIO; |
| 182 | + } |
| 183 | + |
| 184 | + fs_close(&file); |
| 185 | + return rc; |
| 186 | +} |
| 187 | + |
| 188 | +char const *memmem(char const *mem, size_t mem_len, char const *sub, |
| 189 | + size_t sub_len) |
| 190 | +{ |
| 191 | + int i; |
| 192 | + |
| 193 | + if (sub_len <= mem_len && sub_len > 0) { |
| 194 | + for (i = 0; i <= mem_len - sub_len; i++) { |
| 195 | + if (!memcmp(&mem[i], sub, sub_len)) { |
| 196 | + return &mem[i]; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return NULL; |
| 202 | +} |
| 203 | + |
| 204 | +int settings_test_file_strstr(const char *fname, char const *string, |
| 205 | + size_t str_len) |
| 206 | +{ |
| 207 | + int rc; |
| 208 | + u32_t len; |
| 209 | + size_t rlen; |
| 210 | + char *buf; |
| 211 | + struct fs_dirent entry; |
| 212 | + |
| 213 | + rc = fs_stat(fname, &entry); |
| 214 | + if (rc) { |
| 215 | + return rc; |
| 216 | + } |
| 217 | + |
| 218 | + len = entry.size; |
| 219 | + buf = (char *)k_malloc(len + 1); |
| 220 | + zassert_not_null(buf, "out of memory"); |
| 221 | + |
| 222 | + rc = fsutil_read_file(fname, 0, len, buf, &rlen); |
| 223 | + zassert_true(rc == 0, "can't access the file\n'"); |
| 224 | + zassert_true(rc == 0, "not enough data read\n'"); |
| 225 | + buf[rlen] = '\0'; |
| 226 | + |
| 227 | + if (memmem(buf, len, string, str_len)) { |
| 228 | + return 0; |
| 229 | + } |
| 230 | + |
| 231 | + return -1; |
| 232 | +} |
0 commit comments