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 "../../common/test_fs_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_str_equal(path.path, "/", "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_str_equal(path.path, mnt.mnt_point, "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_str_equal(testfs_path_init(&path, &mnt, ELT1, TESTFS_PATH_END),
80 			  MNT "/" ELT1, "bad mnt init elt1");
81 
82 	zassert_str_equal(testfs_path_init(&path, &mnt, ELT1, ELT2, TESTFS_PATH_END),
83 			  MNT "/" ELT1 "/" ELT2, "bad mnt init elt1 elt2");
84 }
85 
ZTEST(littlefs,test_util_path_extend)86 ZTEST(littlefs, test_util_path_extend)
87 {
88 	zassert_str_equal(testfs_path_extend(reset_path(), TESTFS_PATH_END),
89 			  MNT, "empty extend failed");
90 
91 	zassert_str_equal(testfs_path_extend(reset_path(), ELT2, TESTFS_PATH_END),
92 			  MNT "/" ELT2, "elt extend failed");
93 
94 	zassert_str_equal(testfs_path_extend(reset_path(), ELT1, ELT2, TESTFS_PATH_END),
95 			  MNT "/" ELT1 "/" ELT2, "elt1 elt2 extend failed");
96 }
97 
ZTEST(littlefs,test_util_path_extend_up)98 ZTEST(littlefs, test_util_path_extend_up)
99 {
100 	zassert_str_equal(testfs_path_extend(reset_path(), ELT2, "..", ELT1, TESTFS_PATH_END),
101 			  MNT "/" ELT1, "elt elt2, up, elt1 failed");
102 
103 	zassert_str_equal(testfs_path_extend(reset_path(), "..", TESTFS_PATH_END),
104 			  "/", "up strip mnt failed");
105 
106 	zassert_str_equal(testfs_path_extend(reset_path(), "..", "..", TESTFS_PATH_END),
107 			  "/", "up from root failed");
108 }
109 
ZTEST(littlefs,test_util_path_extend_overrun)110 ZTEST(littlefs, test_util_path_extend_overrun)
111 {
112 	char long_elt[TESTFS_PATH_MAX];
113 
114 	memset(long_elt, 'a', sizeof(long_elt) - 1);
115 	long_elt[sizeof(long_elt) - 1] = '\0';
116 
117 	zassert_str_equal(testfs_path_extend(reset_path(), long_elt, ELT1, TESTFS_PATH_END),
118 			  MNT, "stop at overrun failed");
119 }
120