1 /*
2 * Copyright (c) 2019 Peter Bigot Consulting, LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /* Tests of functions in testfs_util */
8
9 #include <string.h>
10 #include <zephyr/ztest.h>
11 #include "testfs_tests.h"
12 #include "testfs_util.h"
13
14 #define MNT "/mnt"
15 #define ELT1 "a"
16 #define ELT2 "b"
17
18 static struct testfs_path path;
19 static const struct fs_mount_t mnt = {
20 .mnt_point = MNT,
21 };
22
reset_path()23 static inline struct testfs_path *reset_path()
24 {
25 testfs_path_init(&path, &mnt, TESTFS_PATH_END);
26 return &path;
27 }
28
ZTEST(littlefs,test_util_path_init_base)29 ZTEST(littlefs, test_util_path_init_base)
30 {
31 zassert_equal(testfs_path_init(&path, NULL, TESTFS_PATH_END),
32 path.path,
33 "bad root init return");
34 zassert_equal(strcmp(path.path, "/"), 0, "bad root init path");
35
36 zassert_equal(testfs_path_init(&path, &mnt, TESTFS_PATH_END),
37 path.path,
38 "bad mnt init return");
39 zassert_equal(strcmp(path.path, mnt.mnt_point), 0, "bad mnt init path");
40
41 if (IS_ENABLED(CONFIG_DEBUG)) {
42 struct fs_mount_t invalid = {
43 .mnt_point = "relative",
44 };
45
46 /* Apparently no way to verify this without panic. */
47 testfs_path_init(&path, &invalid, TESTFS_PATH_END);
48 }
49 }
50
ZTEST(littlefs,test_util_path_init_overrun)51 ZTEST(littlefs, test_util_path_init_overrun)
52 {
53 char overrun[TESTFS_PATH_MAX + 2] = "/";
54 size_t overrun_max = sizeof(overrun) - 1;
55 size_t path_max = sizeof(path.path) - 1;
56 struct fs_mount_t overrun_mnt = {
57 .mnt_point = overrun,
58 };
59
60 zassert_true(path_max < overrun_max,
61 "path length requirements unmet");
62
63 memset(overrun + 1, 'A', overrun_max - 1);
64
65 zassert_equal(testfs_path_init(&path, &overrun_mnt, TESTFS_PATH_END),
66 path.path,
67 "bad overrun init return");
68 zassert_true(strcmp(path.path, overrun) < 0,
69 "bad overrun init");
70 zassert_equal(strncmp(path.path, overrun, path_max),
71 0,
72 "bad overrun path");
73 zassert_equal(path.path[path_max], '\0',
74 "missing overrun EOS");
75 }
76
ZTEST(littlefs,test_util_path_init_extended)77 ZTEST(littlefs, test_util_path_init_extended)
78 {
79 zassert_equal(strcmp(testfs_path_init(&path, &mnt,
80 ELT1,
81 TESTFS_PATH_END),
82 MNT "/" ELT1),
83 0,
84 "bad mnt init elt1");
85
86 zassert_equal(strcmp(testfs_path_init(&path, &mnt,
87 ELT1,
88 ELT2,
89 TESTFS_PATH_END),
90 MNT "/" ELT1 "/" ELT2),
91 0,
92 "bad mnt init elt1 elt2");
93 }
94
ZTEST(littlefs,test_util_path_extend)95 ZTEST(littlefs, test_util_path_extend)
96 {
97 zassert_equal(strcmp(testfs_path_extend(reset_path(),
98 TESTFS_PATH_END),
99 MNT),
100 0,
101 "empty extend failed");
102
103 zassert_equal(strcmp(testfs_path_extend(reset_path(),
104 ELT2,
105 TESTFS_PATH_END),
106 MNT "/" ELT2),
107 0,
108 "elt extend failed");
109
110 zassert_equal(strcmp(testfs_path_extend(reset_path(),
111 ELT1,
112 ELT2,
113 TESTFS_PATH_END),
114 MNT "/" ELT1 "/" ELT2),
115 0,
116 "elt1 elt2 extend failed");
117 }
118
ZTEST(littlefs,test_util_path_extend_up)119 ZTEST(littlefs, test_util_path_extend_up)
120 {
121 zassert_equal(strcmp(testfs_path_extend(reset_path(),
122 ELT2,
123 "..",
124 ELT1,
125 TESTFS_PATH_END),
126 MNT "/" ELT1),
127 0,
128 "elt elt2, up, elt1 failed");
129
130 zassert_equal(strcmp(testfs_path_extend(reset_path(),
131 "..",
132 TESTFS_PATH_END),
133 "/"),
134 0,
135 "up strip mnt failed");
136
137 zassert_equal(strcmp(testfs_path_extend(reset_path(),
138 "..",
139 "..",
140 TESTFS_PATH_END),
141 "/"),
142 0,
143 "up from root failed");
144 }
145
ZTEST(littlefs,test_util_path_extend_overrun)146 ZTEST(littlefs, test_util_path_extend_overrun)
147 {
148 char long_elt[TESTFS_PATH_MAX];
149
150 memset(long_elt, 'a', sizeof(long_elt) - 1);
151 long_elt[sizeof(long_elt) - 1] = '\0';
152
153 zassert_equal(strcmp(testfs_path_extend(reset_path(),
154 long_elt,
155 ELT1,
156 TESTFS_PATH_END),
157 MNT),
158 0,
159 "stop at overrun failed");
160 }
161