17
17
#include <stddef.h>
18
18
#include <setjmp.h>
19
19
#include <string.h>
20
-
21
- #include <cmocka.h>
22
20
#include <stdio.h>
23
21
#include <stdlib.h>
24
22
23
+ #ifdef UNIT_TESTING
24
+ // When defined cmocka redefine malloc/free which does not work well with
25
+ // address-sanitizer
26
+ #undef UNIT_TESTING
27
+ #include <cmocka.h>
28
+ #define UNIT_TESTING
29
+ #else
30
+ #include <cmocka.h>
31
+ #endif
32
+
25
33
#include "app_storage.h"
26
34
#include "app_storage_internal.h"
27
35
#include "app_storage_stubs.h"
36
+ #include "os_nvm.h"
28
37
29
38
/* Defines */
30
39
#define INITIAL_SIZE 20
@@ -68,6 +77,10 @@ _Static_assert(sizeof(app_storage_data_t) <= APP_STORAGE_SIZE,
68
77
app_storage_read( \
69
78
dst_buf, sizeof(((app_storage_data_t *) 0)->field), offsetof(app_storage_data_t, field))
70
79
80
+ // app_storage.h private
81
+ extern app_storage_t app_storage_real ;
82
+ bool app_storage_is_initalized (void );
83
+
71
84
/* Local prototypes */
72
85
static void test_write_read_from_empty (void * * state __attribute__((unused )));
73
86
static void test_app_style_from_empty (void * * state __attribute__((unused )));
@@ -119,6 +132,55 @@ static void test_getters_from_empty(void **state __attribute__((unused)))
119
132
APP_STORAGE_PROP_SETTINGS | APP_STORAGE_PROP_DATA );
120
133
}
121
134
135
+ /* Test that corruption from empty storage is detected */
136
+ static void test_corrupted_storage_from_empty (void * * state __attribute__((unused )))
137
+ {
138
+ assert_true (app_storage_is_initalized ());
139
+ // --- Simulate corrupted header
140
+ app_storage_header_t header = app_storage_real .header ;
141
+ header .data_version += 1 ;
142
+ // Change header with no CRC update
143
+ nvm_write ((void * ) & app_storage_real .header , & header , sizeof (header ));
144
+ // Ensure invalid CRC
145
+ assert_false (app_storage_is_initalized ());
146
+
147
+ // --- Simulate corrupted data
148
+ setup_from_empty (NULL );
149
+ assert_true (app_storage_is_initalized ());
150
+ uint8_t buf [20 ] = {0 };
151
+ memset (buf , 0xAA , sizeof (buf ));
152
+ assert_int_equal (app_storage_write (buf , sizeof (buf ), 0 ), sizeof (buf ));
153
+ // Change data with no CRC update
154
+ buf [sizeof (buf ) - 1 ] = 0xAB ;
155
+ nvm_write ((void * ) & app_storage_real .data , buf , sizeof (buf ));
156
+ // Ensure invalid CRC
157
+ assert_false (app_storage_is_initalized ());
158
+ }
159
+
160
+ /* Test that corruption from prepared storage is detected */
161
+ static void test_corrupted_storage_from_prepared (void * * state __attribute__((unused )))
162
+ {
163
+ assert_true (app_storage_is_initalized ());
164
+ // --- Simulate corrupted header
165
+ app_storage_header_t header = app_storage_real .header ;
166
+ header .data_version += 1 ;
167
+ // Change header with no CRC update
168
+ nvm_write ((void * ) & app_storage_real .header , & header , sizeof (header ));
169
+ // Ensure invalid CRC
170
+ assert_false (app_storage_is_initalized ());
171
+
172
+ // --- Simulate corrupted data
173
+ setup_from_prepared (NULL );
174
+ assert_true (app_storage_is_initalized ());
175
+ uint8_t data [INITIAL_SIZE + ADDITIONALL_SIZE ] = {0 };
176
+ app_storage_read (data , INITIAL_SIZE + ADDITIONALL_SIZE , 0 );
177
+ // Change data with no CRC update
178
+ data [INITIAL_SIZE + ADDITIONALL_SIZE - 1 ]++ ;
179
+ nvm_write ((void * ) & app_storage_real .data , data , INITIAL_SIZE + ADDITIONALL_SIZE );
180
+ // Ensure invalid CRC
181
+ assert_false (app_storage_is_initalized ());
182
+ }
183
+
122
184
/* Read error cases with initially empty storage */
123
185
static void test_read_error_from_empty (void * * state __attribute__((unused )))
124
186
{
@@ -448,6 +510,8 @@ int main(int argc, char **argv)
448
510
{
449
511
const struct CMUnitTest tests [] = {
450
512
cmocka_unit_test_setup_teardown (test_getters_from_empty , setup_from_empty , teardown ),
513
+ cmocka_unit_test_setup_teardown (
514
+ test_corrupted_storage_from_empty , setup_from_empty , teardown ),
451
515
cmocka_unit_test_setup_teardown (test_read_error_from_empty , setup_from_empty , teardown ),
452
516
cmocka_unit_test_setup_teardown (test_write_error_from_empty , setup_from_empty , teardown ),
453
517
cmocka_unit_test_setup_teardown (test_data_version_from_empty , setup_from_empty , teardown ),
@@ -456,6 +520,8 @@ int main(int argc, char **argv)
456
520
test_write_big_reset_from_empty , setup_from_empty , teardown ),
457
521
cmocka_unit_test_setup_teardown (
458
522
test_write_read_from_prepared , setup_from_prepared , teardown ),
523
+ cmocka_unit_test_setup_teardown (
524
+ test_corrupted_storage_from_prepared , setup_from_prepared , teardown ),
459
525
cmocka_unit_test_setup_teardown (test_app_style_from_empty , setup_from_empty , teardown ),
460
526
cmocka_unit_test_setup_teardown (
461
527
test_app_style_from_prepared , setup_from_prepared_app_style , teardown ),
0 commit comments