Debian Codegraph @

unittest/gunit/innodb/fil_path-t.cc

raw symbols SCIP
1 /* Copyright (c) 2020, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 /* See http://code.google.com/p/googletest/wiki/Primer */
25
26 #include <gtest/gtest.h>
27 #include <stddef.h>
28
29 #include "storage/innobase/include/fil0fil.h"
30 extern bool lower_case_file_system;
31
32 namespace innodb_fil_path_unittest {
33
34 static constexpr char SEP = Fil_path::OS_SEPARATOR;
35
36 TEST(fil_path, is_absolute_path) {
37 Fil_path current_dir(".");
38 auto abs_current_dir_str = current_dir.abs_path();
39 EXPECT_FALSE(current_dir.is_absolute_path());
40 EXPECT_EQ(abs_current_dir_str.find_last_of(SEP),
41 abs_current_dir_str.size() - 1);
42
43 Fil_path abs_current_dir(abs_current_dir_str);
44 EXPECT_TRUE(abs_current_dir.is_absolute_path());
45 EXPECT_EQ(abs_current_dir_str, abs_current_dir.abs_path());
46 }
47
48 void get_existing_path_subtest(std::string abs_path, std::string sub_path) {
49 std::string ghost;
50 EXPECT_EQ(Fil_path::get_existing_path(abs_path + sub_path, ghost), abs_path);
51 EXPECT_EQ(ghost, sub_path);
52 EXPECT_EQ(Fil_path::get_existing_path(abs_path + sub_path + SEP, ghost),
53 abs_path);
54 EXPECT_EQ(ghost, sub_path + SEP);
55 EXPECT_EQ(Fil_path::get_existing_path(abs_path + sub_path + SEP + SEP, ghost),
56 abs_path);
57 EXPECT_EQ(ghost, sub_path + SEP + SEP);
58 }
59
60 static std::string ghost = "ghost";
61 static std::string ghost2 = ghost + SEP + "ghost2";
62 static std::string some_ibd = ghost2 + SEP + "some.ibd";
63 static std::string someibd = ghost2 + SEP + "someibd";
64 static std::string some_txt = ghost2 + SEP + "some.txt";
65 static std::string dot_t_dot_t = ghost2 + SEP + "some.t.t";
66 static std::string dot_t_sep_t = ghost2 + SEP + "some.t" + SEP + "t";
67
68 static std::string Ghost = "Ghost";
69 static std::string Ghost2 = Ghost + SEP + "Ghost2";
70 static std::string Some_Ibd = Ghost2 + SEP + "Some.Ibd";
71 static std::string Someibd = Ghost2 + SEP + "Someibd";
72 static std::string Some_Txt = Ghost2 + SEP + "Some.Txt";
73 static std::string Dot_t_dot_t = ghost2 + SEP + "Some.t.t";
74 static std::string Dot_t_sep_t = ghost2 + SEP + "Some.t" + SEP + "t";
75
76 TEST(fil_path, get_existing_path) {
77 Fil_path current_dir(".");
78 auto abs_current_dir_str = current_dir.abs_path();
79 EXPECT_FALSE(current_dir.is_absolute_path());
80 EXPECT_EQ(abs_current_dir_str.find_last_of(SEP),
81 abs_current_dir_str.size() - 1);
82
83 get_existing_path_subtest(abs_current_dir_str, ghost);
84 get_existing_path_subtest(abs_current_dir_str, ghost2);
85 get_existing_path_subtest(abs_current_dir_str, ghost2 + SEP);
86 get_existing_path_subtest(abs_current_dir_str, ghost2 + SEP + SEP);
87
88 get_existing_path_subtest(abs_current_dir_str, some_ibd);
89
90 get_existing_path_subtest(abs_current_dir_str, some_txt);
91 }
92
93 void get_real_path_subsubtest(std::string abs_path, std::string sub_path,
94 std::string expect_path, bool expect_a_file) {
95 std::string expected_suffix = expect_a_file ? "" : std::string{SEP};
96 std::string relative_path = std::string{"."} + SEP;
97
98 EXPECT_EQ(Fil_path::get_real_path(relative_path + sub_path, false),
99 abs_path + expect_path + expected_suffix);
100 EXPECT_EQ(Fil_path::get_real_path(abs_path + sub_path, false),
101 abs_path + expect_path + expected_suffix);
102
103 EXPECT_EQ(Fil_path::get_real_path(relative_path + sub_path + SEP, false),
104 abs_path + expect_path + expected_suffix);
105 EXPECT_EQ(Fil_path::get_real_path(abs_path + sub_path + SEP, false),
106 abs_path + expect_path + expected_suffix);
107 }
108
109 void get_real_path_subtest(std::string abs_path,
110 bool expect_ghost2_file = false,
111 bool expect_ibd_file = true,
112 bool expect_txt_file = true) {
113 get_real_path_subsubtest(abs_path, ghost, ghost, false);
114 get_real_path_subsubtest(abs_path, ghost2, ghost2, expect_ghost2_file);
115 get_real_path_subsubtest(abs_path, some_ibd, some_ibd, expect_ibd_file);
116 get_real_path_subsubtest(abs_path, someibd, someibd, false);
117 get_real_path_subsubtest(abs_path, some_txt, some_txt, expect_txt_file);
118 get_real_path_subsubtest(abs_path, dot_t_dot_t, dot_t_dot_t, false);
119 get_real_path_subsubtest(abs_path, dot_t_sep_t, dot_t_sep_t, false);
120
121 if (lower_case_file_system) {
122 get_real_path_subsubtest(abs_path, Ghost, ghost, false);
123 get_real_path_subsubtest(abs_path, Ghost2, ghost2, expect_ghost2_file);
124 get_real_path_subsubtest(abs_path, Some_Ibd, some_ibd, expect_ibd_file);
125 get_real_path_subsubtest(abs_path, Someibd, someibd, false);
126 get_real_path_subsubtest(abs_path, Some_Txt, some_txt, expect_txt_file);
127 get_real_path_subsubtest(abs_path, Dot_t_dot_t, dot_t_dot_t, false);
128 get_real_path_subsubtest(abs_path, Dot_t_sep_t, dot_t_sep_t, false);
129 }
130 }
131
132 void mkdir(std::string path) {
133 EXPECT_EQ(::my_mkdir(path.c_str(), 0777, MYF(0)), 0);
134 }
135
136 void rmdir(std::string path) { EXPECT_EQ(::rmdir(path.c_str()), 0); }
137 void unlink(std::string path) { EXPECT_EQ(::unlink(path.c_str()), 0); }
138
139 void create_file(std::string path) {
140 auto f = fopen(path.c_str(), "w");
141 EXPECT_NE(f, nullptr);
142 fclose(f);
143 }
144
145 TEST(fil_path, get_real_path) {
146 #ifndef _WIN32
147 std::string root{SEP};
148 EXPECT_EQ(Fil_path::get_real_path(root), root);
149 #endif
150
151 Fil_path current_dir(".");
152 auto abs_current_dir_str = current_dir.abs_path();
153 EXPECT_FALSE(current_dir.is_absolute_path());
154 EXPECT_EQ(abs_current_dir_str.find_last_of(SEP),
155 abs_current_dir_str.size() - 1);
156
157 std::string relative_path = std::string{"."} + SEP;
158
159 EXPECT_EQ(Fil_path::get_real_path("."), abs_current_dir_str);
160 EXPECT_EQ(Fil_path::get_real_path(relative_path), abs_current_dir_str);
161 EXPECT_EQ(Fil_path::get_real_path(abs_current_dir_str), abs_current_dir_str);
162
163 // Run the test where only the current dir exists.
164 get_real_path_subtest(abs_current_dir_str);
165
166 // Make a sub-directory called 'non-existing'
167 mkdir(abs_current_dir_str + ghost);
168 get_real_path_subtest(abs_current_dir_str);
169
170 // Make a file called 'ghost2'
171 create_file(abs_current_dir_str + ghost2);
172 get_real_path_subtest(abs_current_dir_str, true);
173 unlink(abs_current_dir_str + ghost2);
174
175 // Make a sub-directory called 'ghost2'
176 mkdir(abs_current_dir_str + ghost2);
177 get_real_path_subtest(abs_current_dir_str);
178
179 // Make a file called 'some.ibd'
180 create_file(abs_current_dir_str + some_ibd);
181 get_real_path_subtest(abs_current_dir_str);
182 unlink(abs_current_dir_str + some_ibd);
183
184 // Make a sub-directory called 'some.ibd'
185 mkdir(abs_current_dir_str + some_ibd);
186 get_real_path_subtest(abs_current_dir_str, false, false);
187 rmdir(abs_current_dir_str + some_ibd);
188
189 // Make a sub-directory called 'someibd'
190 mkdir(abs_current_dir_str + someibd);
191 get_real_path_subtest(abs_current_dir_str);
192 rmdir(abs_current_dir_str + someibd);
193
194 // Make a file called 'some.txt'
195 create_file(abs_current_dir_str + some_txt);
196 get_real_path_subtest(abs_current_dir_str);
197 unlink(abs_current_dir_str + some_txt);
198
199 // Make a sub-directory called 'some.txt'
200 mkdir(abs_current_dir_str + some_txt);
201 get_real_path_subtest(abs_current_dir_str, false, true, false);
202 rmdir(abs_current_dir_str + some_txt);
203
204 // Make a sub-directory called 'dot.t.t'
205 mkdir(abs_current_dir_str + dot_t_dot_t);
206 get_real_path_subtest(abs_current_dir_str);
207 rmdir(abs_current_dir_str + dot_t_dot_t);
208
209 rmdir(abs_current_dir_str + ghost2);
210 rmdir(abs_current_dir_str + ghost);
211 }
212
213 } // namespace innodb_fil_path_unittest