| 1 | /* Copyright (c) 2018, 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 <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <stddef.h> |
| 27 | #include <algorithm> |
| 28 | #include <iterator> |
| 29 | |
| 30 | #include "my_inttypes.h" |
| 31 | #include "my_io.h" |
| 32 | #include "my_sys.h" |
| 33 | |
| 34 | // Check that various mysys path functions produce a valid |
| 35 | // ('\0'-terminated) c-string, do not write more than FN_REFLEN bytes |
| 36 | // into the destination buffer. |
| 37 | |
| 38 | using ::testing::MatchesRegex; |
| 39 | using ::testing::StartsWith; |
| 40 | |
| 41 | namespace mysys_pathfuncs { |
| 42 | char dest[FN_REFLEN]; |
| 43 | // Assigns values to a T[SZ] array with a value s of type S which must be |
| 44 | // static_cast'able to T. The array argument a is passed as a reference to an |
| 45 | // array of T[SZ]. The default value for argument s is the default |
| 46 | // constructor for S, (0 for integer types). |
| 47 | template <typename T, size_t SZ, typename S> |
| 48 | void aset(T (&a)[SZ], S s = S()) { |
| 49 | std::fill(std::begin(a), std::end(a), static_cast<T>(s)); |
| 50 | } |
| 51 | |
| 52 | // Sets up dst array of size SZ and a src array of size SZ + 10 on the stack |
| 53 | // and invokes the testcase tc argument with these arrays (decayed to |
| 54 | // pointers) and their sizes. The type TC must be a callable type |
| 55 | // implementing void operator()(char *, size_t, const char*. size_t), |
| 56 | // (typically a lambda). |
| 57 | // Dst buffer initialized to 0xde. Src buffer set to aaa... terminated with |
| 58 | // '\0'. Verifies that testcase has filled the whole dst buffer and placed a |
| 59 | // '\0' at the end. |
| 60 | template <size_t SZ, typename TC> |
| 61 | void null_term_setup(TC &&tc) { |
| 62 | char dst[SZ]; |
| 63 | aset(dst, 0xde); |
| 64 | char src[SZ + 10]; |
| 65 | aset(src, 'a'); |
| 66 | src[sizeof(src) - 1] = '\0'; |
| 67 | tc(dst, sizeof(dst), src, sizeof(src)); |
| 68 | constexpr size_t dlen = sizeof(dst) - 1; |
| 69 | EXPECT_EQ('\0', dst[dlen]); |
| 70 | EXPECT_NE(static_cast<char>(0xde), dst[dlen - 1]); |
| 71 | } |
| 72 | |
| 73 | TEST(Mysys, CleanupDirnameOverflow) { |
| 74 | null_term_setup<FN_REFLEN>( |
| 75 | [](char *d, size_t, const char *s, size_t) { cleanup_dirname(d, s); }); |
| 76 | } |
| 77 | |
| 78 | TEST(Mysys, NormalizeDirnameOverflow) { |
| 79 | null_term_setup<FN_REFLEN>( |
| 80 | [](char *d, size_t, const char *s, size_t) { normalize_dirname(d, s); }); |
| 81 | } |
| 82 | |
| 83 | TEST(Mysys, UnpackDirnameOverflow) { |
| 84 | null_term_setup<FN_REFLEN>( |
| 85 | [](char *d, size_t, const char *s, size_t) { unpack_dirname(d, s); }); |
| 86 | } |
| 87 | |
| 88 | TEST(Mysys, UnpackFilenameOverflow) { |
| 89 | null_term_setup<FN_REFLEN>( |
| 90 | [](char *d, size_t, const char *s, size_t) { unpack_filename(d, s); }); |
| 91 | } |
| 92 | |
| 93 | TEST(Mysys, SystemFilenameOverflow) { |
| 94 | null_term_setup<FN_REFLEN>( |
| 95 | [](char *d, size_t, const char *s, size_t) { system_filename(d, s); }); |
| 96 | } |
| 97 | |
| 98 | TEST(Mysys, InternFilenameOverflow) { |
| 99 | null_term_setup<FN_REFLEN>( |
| 100 | [](char *d, size_t, const char *s, size_t) { intern_filename(d, s); }); |
| 101 | } |
| 102 | |
| 103 | TEST(Mysys, DirnamePartOverflow) { |
| 104 | char dst[FN_REFLEN]; |
| 105 | aset(dst, 0xaa); |
| 106 | |
| 107 | char src[FN_REFLEN + 5]; |
| 108 | aset(src, 'a'); |
| 109 | src[sizeof(src) - 1] = '\0'; |
| 110 | src[sizeof(src) - 3] = FN_LIBCHAR; |
| 111 | size_t arglen = 0; |
| 112 | constexpr size_t dlen = sizeof(dst) - 1; |
| 113 | |
| 114 | const size_t rlen = dirname_part(dst, src, &arglen); |
| 115 | EXPECT_EQ('\0', dst[dlen]); |
| 116 | EXPECT_EQ(dlen, arglen); |
| 117 | EXPECT_EQ(sizeof(src) - 2, rlen); |
| 118 | } |
| 119 | |
| 120 | TEST(Mysys, ConvertDirnameOverflow) { |
| 121 | null_term_setup<FN_REFLEN>([](char *d, size_t, const char *s, size_t sl) { |
| 122 | convert_dirname(d, s, s + sl); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | TEST(Mysys, LoadPathNoPrefixOverflow) { |
| 127 | null_term_setup<FN_REFLEN>([](char *d, size_t, const char *s, size_t) { |
| 128 | my_load_path(d, s, nullptr); |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | TEST(Mysys, LoadPathOverflow) { |
| 133 | char dst[FN_REFLEN]; |
| 134 | aset(dst, 0xaa); |
| 135 | |
| 136 | char prefix[FN_REFLEN + 5]; |
| 137 | aset(prefix, 'a'); |
| 138 | prefix[sizeof(prefix) - 1] = '\0'; |
| 139 | EXPECT_EQ(dst, my_load_path(dst, "123", prefix)); |
| 140 | EXPECT_EQ('\0', dst[FN_REFLEN - 1]); |
| 141 | EXPECT_EQ('a', dst[FN_REFLEN - 2]); |
| 142 | } |
| 143 | |
| 144 | #ifdef HAVE_O_TMPFILE |
| 145 | TEST(Mysys, CreateTempFile) { |
| 146 | char dst[FN_REFLEN]; |
| 147 | aset(dst, 0xaa); |
| 148 | |
| 149 | char prefix[FN_REFLEN + 5]; |
| 150 | aset(prefix, 'a'); |
| 151 | prefix[sizeof(prefix) - 1] = '\0'; |
| 152 | |
| 153 | File fileno = create_temp_file(dst, "/tmp", prefix, 42, UNLINK_FILE, 0); |
| 154 | EXPECT_GE(fileno, 0); |
| 155 | my_close(fileno, 0); |
| 156 | EXPECT_THAT(dst, MatchesRegex("/tmp/[a]+fd=[0-9]+")); |
| 157 | aset(dst, 0xaa); |
| 158 | |
| 159 | char *env_tmpdir = getenv("TMPDIR"); |
| 160 | fileno = create_temp_file(dst, nullptr, prefix, 42, UNLINK_FILE, 0); |
| 161 | EXPECT_GE(fileno, 0); |
| 162 | if (env_tmpdir != nullptr) { |
| 163 | EXPECT_THAT(dst, StartsWith(env_tmpdir)); |
| 164 | } else { |
| 165 | EXPECT_THAT(dst, StartsWith("/tmp")); |
| 166 | } |
| 167 | my_close(fileno, 0); |
| 168 | aset(dst, 0xaa); |
| 169 | |
| 170 | char longdirname[FN_REFLEN]; |
| 171 | aset(longdirname, 'x'); |
| 172 | longdirname[0] = '/'; |
| 173 | fileno = create_temp_file(dst, longdirname, "hello", 42, UNLINK_FILE, 0); |
| 174 | EXPECT_LT(fileno, 0); |
| 175 | EXPECT_EQ(errno, ENAMETOOLONG); |
| 176 | } |
| 177 | #endif // HAVE_O_TMPFILE |
| 178 | |
| 179 | // Verify that unpack_dirname works correctly with ~/ and ~user |
| 180 | TEST(Mysys, UnpackDirname) { |
| 181 | char dst[FN_REFLEN]; |
| 182 | aset(dst, 0xaa); |
| 183 | |
| 184 | char src[FN_REFLEN + 5]; |
| 185 | aset(src, 'a'); |
| 186 | std::fill_n(src + FN_REFLEN, 4, 'b'); |
| 187 | src[FN_REFLEN + 4] = '\0'; |
| 188 | |
| 189 | // Verify that destination array does not overflow when source is larger |
| 190 | unpack_dirname(dst, src); |
| 191 | EXPECT_EQ('\0', dst[FN_REFLEN - 1]); |
| 192 | EXPECT_EQ(FN_LIBCHAR, dst[FN_REFLEN - 2]); |
| 193 | EXPECT_EQ('a', dst[FN_REFLEN - 3]); |
| 194 | |
| 195 | aset(dst, 0xaa); |
| 196 | unpack_dirname(dst, "/an/absolute/path"); |
| 197 | EXPECT_STREQ( |
| 198 | FN_ROOTDIR "an" FN_ROOTDIR "absolute" FN_ROOTDIR "path" FN_ROOTDIR, dst); |
| 199 | |
| 200 | aset(dst, 0xaa); |
| 201 | unpack_dirname(dst, "a/relative/path"); |
| 202 | EXPECT_STREQ("a" FN_ROOTDIR "relative" FN_ROOTDIR "path" FN_ROOTDIR, dst); |
| 203 | |
| 204 | // Verify that ~ is expanded to home_dir+/ |
| 205 | // If home_dir is not set (WIN32) tilde expansion does not happen. |
| 206 | std::string hd{home_dir ? home_dir : "~"}; |
| 207 | if (hd != "/") hd.append(1, FN_LIBCHAR); |
| 208 | aset(dst, 0xaa); |
| 209 | unpack_dirname(dst, "~"); |
| 210 | EXPECT_EQ(hd, std::string{dst}); |
| 211 | |
| 212 | // Verify that /~ is expanded to home_dir+/ |
| 213 | aset(dst, 0xaa); |
| 214 | unpack_dirname(dst, "~/"); |
| 215 | EXPECT_EQ(hd, std::string{dst}); |
| 216 | |
| 217 | // Verify that ~root is expanded to somthing starting with / and which |
| 218 | // contains /dir (exact name of root's home dir varies between platforms) |
| 219 | aset(dst, 0xaa); |
| 220 | unpack_dirname(dst, "~root/dir"); |
| 221 | #ifdef HAVE_GETPWNAM |
| 222 | EXPECT_EQ('/', dst[0]); |
| 223 | EXPECT_LE(dst, strstr(dst, "/dir")); |
| 224 | #else |
| 225 | // On platforms which do not have getpwnam no expansion of ~user is performed |
| 226 | EXPECT_STREQ("~root" FN_ROOTDIR "dir" FN_ROOTDIR, dst); |
| 227 | #endif |
| 228 | // Verify that ~ is not expanded when the user does not exist |
| 229 | unpack_dirname(dst, "~___/dir"); |
| 230 | EXPECT_STREQ("~___" FN_ROOTDIR "dir" FN_ROOTDIR, dst); |
| 231 | } |
| 232 | |
| 233 | TEST(Mysys, DirnameLength) { |
| 234 | EXPECT_EQ(0, dirname_length("")); |
| 235 | EXPECT_EQ(1, dirname_length("/test1")); |
| 236 | EXPECT_EQ(2, dirname_length("a/test2")); |
| 237 | EXPECT_EQ(4, dirname_length("a/b/test3")); |
| 238 | EXPECT_EQ(4, dirname_length("x/y/")); |
| 239 | #ifdef _WIN32 |
| 240 | // \ is path-delimiter on Win |
| 241 | EXPECT_EQ(2, dirname_length("a\\b")); |
| 242 | EXPECT_EQ(4, dirname_length("a\\b\\cc")); |
| 243 | // : goes after drive-letter on Win |
| 244 | EXPECT_EQ(2, dirname_length("X:a")); |
| 245 | EXPECT_EQ(3, dirname_length("X:\\a")); |
| 246 | EXPECT_EQ(5, dirname_length("X:\\a\\b")); |
| 247 | #else |
| 248 | // '\\' has no special meaning on non-Windows |
| 249 | EXPECT_EQ(0, dirname_length("a\\b")); |
| 250 | EXPECT_EQ(0, dirname_length("a\\b\\cc")); |
| 251 | // ':' has no special meaning on non-Windows |
| 252 | EXPECT_EQ(0, dirname_length("X:a")); |
| 253 | EXPECT_EQ(0, dirname_length("X:\\a")); |
| 254 | EXPECT_EQ(0, dirname_length("X:\\a\\b")); |
| 255 | #endif |
| 256 | EXPECT_EQ(5, dirname_length("X:/a/b")); |
| 257 | } |
| 258 | |
| 259 | } // namespace mysys_pathfuncs |