| 1 | /* Copyright (c) 2013, 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 | #include <gtest/gtest.h> |
| 25 | #include <stddef.h> |
| 26 | |
| 27 | #include "my_inttypes.h" |
| 28 | #include "my_io.h" |
| 29 | #include "my_sys.h" |
| 30 | |
| 31 | namespace mysys_my_load_path { |
| 32 | |
| 33 | TEST(Mysys, MyLoadPath) { |
| 34 | char dest[FN_REFLEN]; |
| 35 | |
| 36 | static const std::string filename = "filename"; |
| 37 | |
| 38 | // Path with absolute path component. |
| 39 | std::string absolute_path_name = FN_LIBCHAR + filename; |
| 40 | my_load_path(dest, absolute_path_name.c_str(), nullptr); |
| 41 | EXPECT_STREQ(dest, absolute_path_name.c_str()); |
| 42 | |
| 43 | // Path with home directory component. |
| 44 | dest[0] = '\0'; |
| 45 | std::string home_dir_path_name = FN_HOMELIB + (FN_LIBCHAR + filename); |
| 46 | my_load_path(dest, home_dir_path_name.c_str(), nullptr); |
| 47 | EXPECT_STREQ(dest, home_dir_path_name.c_str()); |
| 48 | |
| 49 | // Path with current directory component. |
| 50 | dest[0] = '\0'; |
| 51 | std::string parent_dir_path_name = FN_CURLIB + (FN_LIBCHAR + filename); |
| 52 | my_load_path(dest, parent_dir_path_name.c_str(), nullptr); |
| 53 | char temp_buf[256]; |
| 54 | my_getwd(temp_buf, sizeof(temp_buf), MYF(0)); |
| 55 | EXPECT_STREQ(dest, (temp_buf + filename).c_str()); |
| 56 | |
| 57 | // Path with prefix component appended. |
| 58 | dest[0] = '\0'; |
| 59 | std::string prefix_path_name = "/basedir/"; |
| 60 | my_load_path(dest, filename.c_str(), prefix_path_name.c_str()); |
| 61 | EXPECT_STREQ(dest, (prefix_path_name + filename).c_str()); |
| 62 | |
| 63 | // Path that has length FN_REFLEN-1 |
| 64 | dest[0] = '\0'; |
| 65 | std::string cur_dir_path_name; |
| 66 | for (int i = 0; i < (FN_REFLEN - 3); i++) cur_dir_path_name.append("y"); |
| 67 | cur_dir_path_name = FN_CURLIB + (FN_LIBCHAR + cur_dir_path_name); |
| 68 | my_load_path(dest, cur_dir_path_name.c_str(), nullptr); |
| 69 | EXPECT_STREQ(dest, cur_dir_path_name.c_str()); |
| 70 | |
| 71 | // Path that has length FN_REFLEN. |
| 72 | dest[0] = '\0'; |
| 73 | cur_dir_path_name.append("y"); |
| 74 | my_load_path(dest, cur_dir_path_name.c_str(), nullptr); |
| 75 | EXPECT_STREQ(dest, cur_dir_path_name.substr(0, FN_REFLEN - 1).c_str()); |
| 76 | |
| 77 | // Path that has length exceeding FN_REFLEN |
| 78 | dest[0] = '\0'; |
| 79 | cur_dir_path_name.append("y"); |
| 80 | my_load_path(dest, cur_dir_path_name.c_str(), nullptr); |
| 81 | EXPECT_STREQ(dest, cur_dir_path_name.substr(0, FN_REFLEN - 1).c_str()); |
| 82 | } |
| 83 | } // namespace mysys_my_load_path |