1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include "settings_test.h"
11 #include "settings/settings_file.h"
12 
13 #define EXP_STR_CONTENT_1 "\x10\x00myfoo/mybar16=\x00\x01"\
14 			  "\x0d\x00myfoo/mybar=\x14"\
15 			  "\x16\x00myfoo/mybar64=\x25\x00\x00\x00\x00\x00\x00\x00"
16 
17 #define EXP_STR_CONTENT_2 "\x0d\x00myfoo/mybar=\x14"\
18 			  "\x10\x00myfoo/mybar16=\x01\x01"\
19 			  "\x16\x00myfoo/mybar64=\x13\x00\x00\x00\x00\x00\x00\x00"
20 
21 int file_str_cmp(const char *fname, char const *string, size_t pattern_len);
22 
ZTEST(settings_config_fs,test_config_compress_file)23 ZTEST(settings_config_fs, test_config_compress_file)
24 {
25 	int rc;
26 	struct settings_file cf;
27 
28 	config_wipe_srcs();
29 
30 	rc = fs_mkdir(TEST_CONFIG_DIR);
31 	zassert_true(rc == 0 || rc == -EEXIST, "can't create directory");
32 
33 	cf.cf_name = TEST_CONFIG_DIR "/korwin";
34 	cf.cf_maxlines = 24;
35 	cf.cf_lines = 0; /* required as not start with load settings */
36 
37 	rc = settings_file_src(&cf);
38 	zassert_true(rc == 0, "can't register FS as configuration source");
39 	settings_mount_file_backend(&cf);
40 
41 	rc = settings_file_dst(&cf);
42 	zassert_true(rc == 0,
43 		     "can't register FS as configuration destination");
44 
45 	val64 = 1125U;
46 	val16 = 256U;
47 
48 	for (int i = 0; i < 21; i++) {
49 		val8 = i;
50 		rc = settings_save();
51 		zassert_true(rc == 0, "fs write error");
52 
53 		val8 = 0xff;
54 		settings_load();
55 		zassert_true(val8 == i, "Bad value loaded");
56 	}
57 
58 	val64 = 37U;
59 	rc = settings_save();
60 	zassert_true(rc == 0, "fs write error");
61 
62 	const char exp_content_1[] = EXP_STR_CONTENT_1;
63 
64 	/* check 1st compression */
65 	rc = file_str_cmp(cf.cf_name, exp_content_1, sizeof(exp_content_1)-1);
66 	zassert_true(rc == 0, "bad value read");
67 
68 	val16 = 257U;
69 	for (int i = 0; i < 20; i++) {
70 		val64 = i;
71 		rc = settings_save();
72 		zassert_true(rc == 0, "fs write error");
73 
74 		val64 = 0xff;
75 		settings_load();
76 		zassert_true(val64 == i, "Bad value loaded");
77 	}
78 
79 	const char exp_content_2[] = EXP_STR_CONTENT_2;
80 
81 	/* check subsequent compression */
82 	rc = file_str_cmp(cf.cf_name, exp_content_2, sizeof(exp_content_2)-1);
83 	zassert_true(rc == 0, "bad value read");
84 }
85 
file_str_cmp(const char * fname,char const * string,size_t pattern_len)86 int file_str_cmp(const char *fname, char const *string, size_t pattern_len)
87 {
88 	int rc;
89 	uint32_t len;
90 	size_t rlen;
91 	char *buf;
92 	struct fs_dirent entry;
93 
94 	rc = fs_stat(fname, &entry);
95 	if (rc) {
96 		return rc;
97 	}
98 
99 	if (entry.size != pattern_len) {
100 		return -1;
101 	}
102 
103 	len = entry.size;
104 	buf = (char *)k_malloc(len + 1);
105 	zassert_not_null(buf, "out of memory");
106 
107 	rc = fsutil_read_file(fname, 0, len, buf, &rlen);
108 	zassert_true(rc == 0, "can't access the file\n'");
109 	zassert_true(rc == 0, "not enough data read\n'");
110 	buf[rlen] = '\0';
111 
112 	if (memcmp(buf, string, len)) {
113 		return -1;
114 	}
115 
116 	return -0;
117 }
118