1 /*
2 * Copyright (c) 2024 BayLibre SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "settings_priv.h"
10 #include "settings_test.h"
11
12 uint8_t val8;
13 uint8_t val8_un;
14 uint32_t val32;
15 uint64_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 c1_handle_get(const char *name, char *val, int val_len_max);
23 int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
24 int c1_handle_commit(void);
25 int c1_handle_export(int (*cb)(const char *name, const void *value, size_t val_len));
26
27 struct settings_handler c_test_handlers[] = {
28 {.name = "myfoo",
29 .h_get = c1_handle_get,
30 .h_set = c1_handle_set,
31 .h_commit = c1_handle_commit,
32 .h_export = c1_handle_export},
33 };
34
c1_handle_get(const char * name,char * val,int val_len_max)35 int c1_handle_get(const char *name, char *val, int val_len_max)
36 {
37 const char *next;
38
39 test_get_called = 1;
40
41 if (settings_name_steq(name, "mybar", &next) && !next) {
42 val_len_max = MIN(val_len_max, sizeof(val8));
43 memcpy(val, &val8, MIN(val_len_max, sizeof(val8)));
44 return val_len_max;
45 }
46
47 if (settings_name_steq(name, "mybar64", &next) && !next) {
48 val_len_max = MIN(val_len_max, sizeof(val64));
49 memcpy(val, &val64, MIN(val_len_max, sizeof(val64)));
50 return val_len_max;
51 }
52
53 return -ENOENT;
54 }
55
c1_handle_set(const char * name,size_t len,settings_read_cb read_cb,void * cb_arg)56 int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
57 {
58 size_t val_len;
59 int rc;
60 const char *next;
61
62 test_set_called = 1;
63 if (settings_name_steq(name, "mybar", &next) && !next) {
64 rc = read_cb(cb_arg, &val8, sizeof(val8));
65 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
66 return 0;
67 }
68
69 if (settings_name_steq(name, "mybar64", &next) && !next) {
70 rc = read_cb(cb_arg, &val64, sizeof(val64));
71 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
72 return 0;
73 }
74
75 if (settings_name_steq(name, "unaligned", &next) && !next) {
76 val_len = len;
77 zassert_equal(val_len, sizeof(val8_un), "value length: %d, ought equal 1", val_len);
78 rc = read_cb(cb_arg, &val8_un, sizeof(val8_un));
79 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
80 return 0;
81 }
82
83 return -ENOENT;
84 }
85
c1_handle_commit(void)86 int c1_handle_commit(void)
87 {
88 test_commit_called = 1;
89 return 0;
90 }
91
c1_handle_export(int (* cb)(const char * name,const void * value,size_t val_len))92 int c1_handle_export(int (*cb)(const char *name, const void *value, size_t val_len))
93 {
94 if (test_export_block) {
95 return 0;
96 }
97
98 (void)cb("myfoo/mybar", &val8, sizeof(val8));
99
100 (void)cb("myfoo/mybar64", &val64, sizeof(val64));
101
102 (void)cb("myfoo/unaligned", &val8_un, sizeof(val8_un));
103
104 return 0;
105 }
106
ctest_clear_call_state(void)107 void ctest_clear_call_state(void)
108 {
109 test_get_called = 0;
110 test_set_called = 0;
111 test_commit_called = 0;
112 }
113
ctest_get_call_state(void)114 int ctest_get_call_state(void)
115 {
116 return test_get_called + test_set_called + test_commit_called;
117 }
118
config_wipe_srcs(void)119 void config_wipe_srcs(void)
120 {
121 sys_slist_init(&settings_load_srcs);
122 settings_save_dst = NULL;
123 }
124
125 ZTEST_SUITE(settings_config, NULL, settings_config_setup, NULL, NULL, settings_config_teardown);
126