File: mysys_pathfuncs-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (259 lines) | stat: -rw-r--r-- 8,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stddef.h>
#include <algorithm>
#include <iterator>

#include "my_inttypes.h"
#include "my_io.h"
#include "my_sys.h"

// Check that various mysys path functions produce a valid
// ('\0'-terminated) c-string, do not write more than FN_REFLEN bytes
// into the destination buffer.

using ::testing::MatchesRegex;
using ::testing::StartsWith;

namespace mysys_pathfuncs {
char dest[FN_REFLEN];
// Assigns values to a T[SZ] array with a value s of type S which must be
// static_cast'able to T. The array argument a is passed as a reference to an
// array of T[SZ]. The default value for argument s is the default
// constructor for S, (0 for integer types).
template <typename T, size_t SZ, typename S>
void aset(T (&a)[SZ], S s = S()) {
  std::fill(std::begin(a), std::end(a), static_cast<T>(s));
}

// Sets up dst array of size SZ and a src array of size SZ + 10 on the stack
// and invokes the testcase tc argument with these arrays (decayed to
// pointers) and their sizes. The type TC must be a callable type
// implementing void operator()(char *, size_t, const char*. size_t),
// (typically a lambda).
// Dst buffer initialized to 0xde. Src buffer set to aaa... terminated with
// '\0'. Verifies that testcase has filled the whole dst buffer and placed a
// '\0' at the end.
template <size_t SZ, typename TC>
void null_term_setup(TC &&tc) {
  char dst[SZ];
  aset(dst, 0xde);
  char src[SZ + 10];
  aset(src, 'a');
  src[sizeof(src) - 1] = '\0';
  tc(dst, sizeof(dst), src, sizeof(src));
  constexpr size_t dlen = sizeof(dst) - 1;
  EXPECT_EQ('\0', dst[dlen]);
  EXPECT_NE(static_cast<char>(0xde), dst[dlen - 1]);
}

TEST(Mysys, CleanupDirnameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { cleanup_dirname(d, s); });
}

TEST(Mysys, NormalizeDirnameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { normalize_dirname(d, s); });
}

TEST(Mysys, UnpackDirnameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { unpack_dirname(d, s); });
}

TEST(Mysys, UnpackFilenameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { unpack_filename(d, s); });
}

TEST(Mysys, SystemFilenameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { system_filename(d, s); });
}

TEST(Mysys, InternFilenameOverflow) {
  null_term_setup<FN_REFLEN>(
      [](char *d, size_t, const char *s, size_t) { intern_filename(d, s); });
}

TEST(Mysys, DirnamePartOverflow) {
  char dst[FN_REFLEN];
  aset(dst, 0xaa);

  char src[FN_REFLEN + 5];
  aset(src, 'a');
  src[sizeof(src) - 1] = '\0';
  src[sizeof(src) - 3] = FN_LIBCHAR;
  size_t arglen = 0;
  constexpr size_t dlen = sizeof(dst) - 1;

  const size_t rlen = dirname_part(dst, src, &arglen);
  EXPECT_EQ('\0', dst[dlen]);
  EXPECT_EQ(dlen, arglen);
  EXPECT_EQ(sizeof(src) - 2, rlen);
}

TEST(Mysys, ConvertDirnameOverflow) {
  null_term_setup<FN_REFLEN>([](char *d, size_t, const char *s, size_t sl) {
    convert_dirname(d, s, s + sl);
  });
}

TEST(Mysys, LoadPathNoPrefixOverflow) {
  null_term_setup<FN_REFLEN>([](char *d, size_t, const char *s, size_t) {
    my_load_path(d, s, nullptr);
  });
}

TEST(Mysys, LoadPathOverflow) {
  char dst[FN_REFLEN];
  aset(dst, 0xaa);

  char prefix[FN_REFLEN + 5];
  aset(prefix, 'a');
  prefix[sizeof(prefix) - 1] = '\0';
  EXPECT_EQ(dst, my_load_path(dst, "123", prefix));
  EXPECT_EQ('\0', dst[FN_REFLEN - 1]);
  EXPECT_EQ('a', dst[FN_REFLEN - 2]);
}

#ifdef HAVE_O_TMPFILE
TEST(Mysys, CreateTempFile) {
  char dst[FN_REFLEN];
  aset(dst, 0xaa);

  char prefix[FN_REFLEN + 5];
  aset(prefix, 'a');
  prefix[sizeof(prefix) - 1] = '\0';

  File fileno = create_temp_file(dst, "/tmp", prefix, 42, UNLINK_FILE, 0);
  EXPECT_GE(fileno, 0);
  my_close(fileno, 0);
  EXPECT_THAT(dst, MatchesRegex("/tmp/[a]+fd=[0-9]+"));
  aset(dst, 0xaa);

  char *env_tmpdir = getenv("TMPDIR");
  fileno = create_temp_file(dst, nullptr, prefix, 42, UNLINK_FILE, 0);
  EXPECT_GE(fileno, 0);
  if (env_tmpdir != nullptr) {
    EXPECT_THAT(dst, StartsWith(env_tmpdir));
  } else {
    EXPECT_THAT(dst, StartsWith("/tmp"));
  }
  my_close(fileno, 0);
  aset(dst, 0xaa);

  char longdirname[FN_REFLEN];
  aset(longdirname, 'x');
  longdirname[0] = '/';
  fileno = create_temp_file(dst, longdirname, "hello", 42, UNLINK_FILE, 0);
  EXPECT_LT(fileno, 0);
  EXPECT_EQ(errno, ENAMETOOLONG);
}
#endif  // HAVE_O_TMPFILE

// Verify that unpack_dirname works correctly with ~/ and ~user
TEST(Mysys, UnpackDirname) {
  char dst[FN_REFLEN];
  aset(dst, 0xaa);

  char src[FN_REFLEN + 5];
  aset(src, 'a');
  std::fill_n(src + FN_REFLEN, 4, 'b');
  src[FN_REFLEN + 4] = '\0';

  // Verify that destination array does not overflow when source is larger
  unpack_dirname(dst, src);
  EXPECT_EQ('\0', dst[FN_REFLEN - 1]);
  EXPECT_EQ(FN_LIBCHAR, dst[FN_REFLEN - 2]);
  EXPECT_EQ('a', dst[FN_REFLEN - 3]);

  aset(dst, 0xaa);
  unpack_dirname(dst, "/an/absolute/path");
  EXPECT_STREQ(
      FN_ROOTDIR "an" FN_ROOTDIR "absolute" FN_ROOTDIR "path" FN_ROOTDIR, dst);

  aset(dst, 0xaa);
  unpack_dirname(dst, "a/relative/path");
  EXPECT_STREQ("a" FN_ROOTDIR "relative" FN_ROOTDIR "path" FN_ROOTDIR, dst);

  // Verify that ~ is expanded to home_dir+/
  // If home_dir is not set (WIN32) tilde expansion does not happen.
  std::string hd{home_dir ? home_dir : "~"};
  if (hd != "/") hd.append(1, FN_LIBCHAR);
  aset(dst, 0xaa);
  unpack_dirname(dst, "~");
  EXPECT_EQ(hd, std::string{dst});

  // Verify that /~ is expanded to home_dir+/
  aset(dst, 0xaa);
  unpack_dirname(dst, "~/");
  EXPECT_EQ(hd, std::string{dst});

  // Verify that ~root is expanded to somthing starting with / and which
  // contains /dir (exact name of root's home dir varies between platforms)
  aset(dst, 0xaa);
  unpack_dirname(dst, "~root/dir");
#ifdef HAVE_GETPWNAM
  EXPECT_EQ('/', dst[0]);
  EXPECT_LE(dst, strstr(dst, "/dir"));
#else
  // On platforms which do not have getpwnam no expansion of ~user is performed
  EXPECT_STREQ("~root" FN_ROOTDIR "dir" FN_ROOTDIR, dst);
#endif
  // Verify that ~ is not expanded when the user does not exist
  unpack_dirname(dst, "~___/dir");
  EXPECT_STREQ("~___" FN_ROOTDIR "dir" FN_ROOTDIR, dst);
}

TEST(Mysys, DirnameLength) {
  EXPECT_EQ(0, dirname_length(""));
  EXPECT_EQ(1, dirname_length("/test1"));
  EXPECT_EQ(2, dirname_length("a/test2"));
  EXPECT_EQ(4, dirname_length("a/b/test3"));
  EXPECT_EQ(4, dirname_length("x/y/"));
#ifdef _WIN32
  // \ is path-delimiter on Win
  EXPECT_EQ(2, dirname_length("a\\b"));
  EXPECT_EQ(4, dirname_length("a\\b\\cc"));
  // : goes after drive-letter on Win
  EXPECT_EQ(2, dirname_length("X:a"));
  EXPECT_EQ(3, dirname_length("X:\\a"));
  EXPECT_EQ(5, dirname_length("X:\\a\\b"));
#else
  // '\\' has no special meaning on non-Windows
  EXPECT_EQ(0, dirname_length("a\\b"));
  EXPECT_EQ(0, dirname_length("a\\b\\cc"));
  // ':' has no special meaning on non-Windows
  EXPECT_EQ(0, dirname_length("X:a"));
  EXPECT_EQ(0, dirname_length("X:\\a"));
  EXPECT_EQ(0, dirname_length("X:\\a\\b"));
#endif
  EXPECT_EQ(5, dirname_length("X:/a/b"));
}

}  // namespace mysys_pathfuncs