aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/cpp/util/file_windows_test.cc
blob: 1d73d65e535baa3230ec30366ee0f7ca99386563 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <string.h>
#include <windows.h>

#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
#include "gtest/gtest.h"

#if !defined(COMPILER_MSVC) && !defined(__CYGWIN__)
#error("This test should only be run on Windows")
#endif  // !defined(COMPILER_MSVC) && !defined(__CYGWIN__)

namespace blaze_util {

using std::string;
using std::wstring;

// Methods defined in file_windows.cc that are only visible for testing.
void ResetMsysRootForTesting();
string NormalizeWindowsPath(string path);

static string GetTestTmpDir() {
  char buf[MAX_PATH] = {0};
  DWORD len = GetEnvironmentVariableA("TEST_TMPDIR", buf, MAX_PATH);
  return string(buf);
}

TEST(FileTest, TestNormalizeWindowsPath) {
  ASSERT_EQ(string(""), NormalizeWindowsPath(""));
  ASSERT_EQ(string(""), NormalizeWindowsPath("."));
  ASSERT_EQ(string("foo"), NormalizeWindowsPath("foo"));
  ASSERT_EQ(string("foo"), NormalizeWindowsPath("foo/"));
  ASSERT_EQ(string("foo\\bar"), NormalizeWindowsPath("foo//bar"));
  ASSERT_EQ(string("foo\\bar"), NormalizeWindowsPath("../..//foo/./bar"));
  ASSERT_EQ(string("foo\\bar"), NormalizeWindowsPath("../foo/baz/../bar"));
  ASSERT_EQ(string("c:\\"), NormalizeWindowsPath("c:"));
  ASSERT_EQ(string("c:\\"), NormalizeWindowsPath("c:/"));
  ASSERT_EQ(string("c:\\"), NormalizeWindowsPath("c:\\"));
  ASSERT_EQ(string("c:\\foo\\bar"), NormalizeWindowsPath("c:\\..//foo/./bar/"));
}

TEST(FileTest, TestDirname) {
  ASSERT_EQ("", Dirname(""));
  ASSERT_EQ("/", Dirname("/"));
  ASSERT_EQ("", Dirname("foo"));
  ASSERT_EQ("/", Dirname("/foo"));
  ASSERT_EQ("/foo", Dirname("/foo/"));
  ASSERT_EQ("foo", Dirname("foo/bar"));
  ASSERT_EQ("foo/bar", Dirname("foo/bar/baz"));
  ASSERT_EQ("\\", Dirname("\\foo"));
  ASSERT_EQ("\\foo", Dirname("\\foo\\"));
  ASSERT_EQ("foo", Dirname("foo\\bar"));
  ASSERT_EQ("foo\\bar", Dirname("foo\\bar\\baz"));
  ASSERT_EQ("foo\\bar/baz", Dirname("foo\\bar/baz\\qux"));
  ASSERT_EQ("c:/", Dirname("c:/"));
  ASSERT_EQ("c:\\", Dirname("c:\\"));
  ASSERT_EQ("c:/", Dirname("c:/foo"));
  ASSERT_EQ("c:\\", Dirname("c:\\foo"));
  ASSERT_EQ("\\\\?\\c:\\", Dirname("\\\\?\\c:\\"));
  ASSERT_EQ("\\\\?\\c:\\", Dirname("\\\\?\\c:\\foo"));
}

TEST(FileTest, TestBasename) {
  ASSERT_EQ("", Basename(""));
  ASSERT_EQ("", Basename("/"));
  ASSERT_EQ("foo", Basename("foo"));
  ASSERT_EQ("foo", Basename("/foo"));
  ASSERT_EQ("", Basename("/foo/"));
  ASSERT_EQ("bar", Basename("foo/bar"));
  ASSERT_EQ("baz", Basename("foo/bar/baz"));
  ASSERT_EQ("foo", Basename("\\foo"));
  ASSERT_EQ("", Basename("\\foo\\"));
  ASSERT_EQ("bar", Basename("foo\\bar"));
  ASSERT_EQ("baz", Basename("foo\\bar\\baz"));
  ASSERT_EQ("qux", Basename("foo\\bar/baz\\qux"));
  ASSERT_EQ("", Basename("c:/"));
  ASSERT_EQ("", Basename("c:\\"));
  ASSERT_EQ("foo", Basename("c:/foo"));
  ASSERT_EQ("foo", Basename("c:\\foo"));
  ASSERT_EQ("", Basename("\\\\?\\c:\\"));
  ASSERT_EQ("foo", Basename("\\\\?\\c:\\foo"));
}

TEST(FileTest, IsAbsolute) {
  ASSERT_FALSE(IsAbsolute(""));
  ASSERT_TRUE(IsAbsolute("/"));
  ASSERT_TRUE(IsAbsolute("/foo"));
  ASSERT_TRUE(IsAbsolute("\\"));
  ASSERT_TRUE(IsAbsolute("\\foo"));
  ASSERT_FALSE(IsAbsolute("c:"));
  ASSERT_TRUE(IsAbsolute("c:/"));
  ASSERT_TRUE(IsAbsolute("c:\\"));
  ASSERT_TRUE(IsAbsolute("c:\\foo"));
  ASSERT_TRUE(IsAbsolute("\\\\?\\c:\\"));
  ASSERT_TRUE(IsAbsolute("\\\\?\\c:\\foo"));
}

TEST(FileTest, IsRootDirectory) {
  ASSERT_FALSE(IsRootDirectory(""));
  ASSERT_TRUE(IsRootDirectory("/"));
  ASSERT_FALSE(IsRootDirectory("/foo"));
  ASSERT_TRUE(IsRootDirectory("\\"));
  ASSERT_FALSE(IsRootDirectory("\\foo"));
  ASSERT_FALSE(IsRootDirectory("c:"));
  ASSERT_TRUE(IsRootDirectory("c:/"));
  ASSERT_TRUE(IsRootDirectory("c:\\"));
  ASSERT_FALSE(IsRootDirectory("c:\\foo"));
  ASSERT_TRUE(IsRootDirectory("\\\\?\\c:\\"));
  ASSERT_FALSE(IsRootDirectory("\\\\?\\c:\\foo"));
}

TEST(FileTest, TestAsWindowsPath) {
  SetEnvironmentVariableA("BAZEL_SH", "c:\\msys\\some\\long\\path\\bash.exe");
  ResetMsysRootForTesting();
  wstring actual;

  ASSERT_TRUE(AsWindowsPath("", &actual));
  ASSERT_EQ(wstring(L""), actual);

  ASSERT_TRUE(AsWindowsPath("", &actual));
  ASSERT_EQ(wstring(L""), actual);

  ASSERT_TRUE(AsWindowsPath("foo/bar", &actual));
  ASSERT_EQ(wstring(L"foo\\bar"), actual);

  ASSERT_TRUE(AsWindowsPath("c:", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("c:/", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("c:\\", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("\\\\?\\c:\\", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("\\\\?\\c://../foo", &actual));
  ASSERT_EQ(wstring(L"c:\\foo"), actual);

  ASSERT_TRUE(AsWindowsPath("/c", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("/c/", &actual));
  ASSERT_EQ(wstring(L"c:\\"), actual);

  ASSERT_TRUE(AsWindowsPath("/c/blah", &actual));
  ASSERT_EQ(wstring(L"c:\\blah"), actual);

  ASSERT_TRUE(AsWindowsPath("/d/progra~1/micros~1", &actual));
  ASSERT_EQ(wstring(L"d:\\progra~1\\micros~1"), actual);

  ASSERT_TRUE(AsWindowsPath("/foo", &actual));
  ASSERT_EQ(wstring(L"c:\\msys\\foo"), actual);

  wstring wlongpath(L"\\dummy_long_path");
  string longpath("dummy_long_path/");
  while (longpath.size() <= MAX_PATH) {
    wlongpath += wlongpath;
    longpath += longpath;
  }
  wlongpath = wstring(L"c:") + wlongpath;
  longpath = string("/c/") + longpath;
  ASSERT_TRUE(AsWindowsPath(longpath, &actual));
  ASSERT_EQ(wlongpath, actual);
}

TEST(FileTest, TestAsShortWindowsPath) {
  string tmpdir(GetTestTmpDir());
  ASSERT_LT(0, tmpdir.size());

  string short_tmpdir;
  ASSERT_TRUE(AsShortWindowsPath(tmpdir, &short_tmpdir));
  ASSERT_LT(0, short_tmpdir.size());
  ASSERT_TRUE(PathExists(short_tmpdir));

  string dirname(JoinPath(short_tmpdir, "LONGpathNAME"));
  ASSERT_EQ(0, mkdir(dirname.c_str()));
  ASSERT_TRUE(PathExists(dirname));

  string actual;
  ASSERT_TRUE(AsShortWindowsPath(dirname, &actual));
  ASSERT_EQ(short_tmpdir + "\\longpa~1", actual);
  ASSERT_EQ(0, rmdir(dirname.c_str()));
}

TEST(FileTest, TestMsysRootRetrieval) {
  wstring actual;

  SetEnvironmentVariableA("BAZEL_SH", "c:/foo/msys/bar/qux.exe");
  ResetMsysRootForTesting();
  ASSERT_TRUE(AsWindowsPath("/blah", &actual));
  ASSERT_EQ(wstring(L"c:\\foo\\msys\\blah"), actual);

  SetEnvironmentVariableA("BAZEL_SH", "c:/foo/MSYS64/bar/qux.exe");
  ResetMsysRootForTesting();
  ASSERT_TRUE(AsWindowsPath("/blah", &actual));
  ASSERT_EQ(wstring(L"c:\\foo\\msys64\\blah"), actual);

  SetEnvironmentVariableA("BAZEL_SH", "c:/qux.exe");
  ResetMsysRootForTesting();
  ASSERT_FALSE(AsWindowsPath("/blah", &actual));

  SetEnvironmentVariableA("BAZEL_SH", nullptr);
  ResetMsysRootForTesting();
}

static void RunCommand(const string& cmdline) {
  STARTUPINFOA startupInfo = {sizeof(STARTUPINFO)};
  PROCESS_INFORMATION processInfo;
  // command line maximum size is 32K
  // Source (on 2017-01-04):
  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
  char mutable_cmdline[0x8000];
  strncpy(mutable_cmdline, cmdline.c_str(), 0x8000);
  BOOL ok = CreateProcessA(
      /* lpApplicationName */ NULL,
      /* lpCommandLine */ mutable_cmdline,
      /* lpProcessAttributes */ NULL,
      /* lpThreadAttributes */ NULL,
      /* bInheritHandles */ TRUE,
      /* dwCreationFlags */ 0,
      /* lpEnvironment */ NULL,
      /* lpCurrentDirectory */ NULL,
      /* lpStartupInfo */ &startupInfo,
      /* lpProcessInformation */ &processInfo);
  ASSERT_TRUE(ok);

  // Wait 1 second for the process to finish.
  ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(processInfo.hProcess, 1000));

  DWORD exit_code = 1;
  ASSERT_TRUE(GetExitCodeProcess(processInfo.hProcess, &exit_code));
  ASSERT_EQ(0, exit_code);
}

TEST(FileTest, TestPathExistsWindows) {
  ASSERT_FALSE(PathExists(""));
  ASSERT_TRUE(PathExists("."));
  ASSERT_FALSE(PathExists("non.existent"));

  string tmpdir(GetTestTmpDir());
  ASSERT_LT(0, tmpdir.size());
  ASSERT_TRUE(PathExists(tmpdir));

  // Create a fake msys root. We'll also use it as a junction target.
  string fake_msys_root(tmpdir + "/fake_msys");
  ASSERT_EQ(0, mkdir(fake_msys_root.c_str()));
  ASSERT_TRUE(PathExists(fake_msys_root));

  // Set the BAZEL_SH root so we can resolve MSYS paths.
  SetEnvironmentVariableA("BAZEL_SH",
                          (fake_msys_root + "/fake_bash.exe").c_str());
  ResetMsysRootForTesting();

  // Assert existence check for MSYS paths.
  ASSERT_FALSE(PathExists("/this/should/not/exist/mkay"));
  ASSERT_TRUE(PathExists("/"));

  // Create a junction pointing to an existing directory.
  RunCommand(string("cmd.exe /C mklink /J \"") + tmpdir + "/junc1\" \"" +
             fake_msys_root + "\" >NUL 2>NUL");
  ASSERT_TRUE(PathExists(fake_msys_root));
  ASSERT_TRUE(PathExists(JoinPath(tmpdir, "junc1")));

  // Create a junction pointing to a non-existent directory.
  RunCommand(string("cmd.exe /C mklink /J \"") + tmpdir + "/junc2\" \"" +
             fake_msys_root + "/i.dont.exist\" >NUL 2>NUL");
  ASSERT_FALSE(PathExists(JoinPath(fake_msys_root, "i.dont.exist")));
  ASSERT_FALSE(PathExists(JoinPath(tmpdir, "junc2")));

  // Clean up.
  ASSERT_EQ(0, rmdir(JoinPath(tmpdir, "junc1").c_str()));
  ASSERT_EQ(0, rmdir(JoinPath(tmpdir, "junc2").c_str()));
  ASSERT_EQ(0, rmdir(fake_msys_root.c_str()));
  ASSERT_FALSE(PathExists(JoinPath(tmpdir, "junc1")));
  ASSERT_FALSE(PathExists(JoinPath(tmpdir, "junc2")));
}

TEST(FileTest, TestUnlinkPath) {
  string tmpdir(GetTestTmpDir());
  ASSERT_LT(0, tmpdir.size());
  ASSERT_TRUE(PathExists(tmpdir));

  // Create a directory under `tempdir`, a file inside it, and a junction
  // pointing to it.
  string dir1(JoinPath(tmpdir, "dir1"));
  ASSERT_EQ(0, mkdir(dir1.c_str()));
  FILE* fh = fopen(JoinPath(dir1, "foo.txt").c_str(), "wt");
  ASSERT_NE(nullptr, fh);
  ASSERT_LT(0, fprintf(fh, "hello\n"));
  fclose(fh);
  string junc1(JoinPath(tmpdir, "junc1"));
  RunCommand(string("cmd.exe /C mklink /J \"") + junc1 + "\" \"" + dir1 +
             "\" >NUL 2>NUL");
  ASSERT_TRUE(PathExists(junc1));
  ASSERT_TRUE(PathExists(JoinPath(junc1, "foo.txt")));

  // Non-existent files cannot be unlinked.
  ASSERT_FALSE(UnlinkPath("does.not.exist"));
  // Directories cannot be unlinked.
  ASSERT_FALSE(UnlinkPath(dir1));
  // Junctions can be unlinked, even if the pointed directory is not empty.
  ASSERT_TRUE(UnlinkPath(JoinPath(junc1, "foo.txt")));
  // Files can be unlinked.
  ASSERT_TRUE(UnlinkPath(junc1));
  // Clean up the now empty directory.
  ASSERT_EQ(0, rmdir(dir1.c_str()));
}

TEST(FileTest, TestIsDirectory) {
  ASSERT_FALSE(IsDirectory(""));

  string tmpdir(GetTestTmpDir());
  ASSERT_LT(0, tmpdir.size());
  ASSERT_TRUE(IsDirectory(tmpdir));
  ASSERT_TRUE(IsDirectory("C:\\"));
  ASSERT_TRUE(IsDirectory("C:/"));
  ASSERT_TRUE(IsDirectory("/c"));

  ASSERT_FALSE(IsDirectory("non.existent"));
  // Create a directory under `tempdir`, verify that IsDirectory reports true.
  // Call it msys_dir1 so we can also use it as a mock msys root.
  string dir1(JoinPath(tmpdir, "msys_dir1"));
  ASSERT_EQ(0, mkdir(dir1.c_str()));
  ASSERT_TRUE(IsDirectory(dir1));

  // Use dir1 as the mock msys root, verify that IsDirectory works for a MSYS
  // path.
  SetEnvironmentVariableA("BAZEL_SH", JoinPath(dir1, "bash.exe").c_str());
  ResetMsysRootForTesting();
  ASSERT_TRUE(IsDirectory("/"));

  // Verify that IsDirectory works for a junction.
  string junc1(JoinPath(tmpdir, "junc1"));
  RunCommand(string("cmd.exe /C mklink /J \"") + junc1 + "\" \"" + dir1 +
             "\" >NUL 2>NUL");
  ASSERT_TRUE(IsDirectory(junc1));

  ASSERT_EQ(0, rmdir(dir1.c_str()));
  ASSERT_FALSE(IsDirectory(dir1));
  ASSERT_FALSE(IsDirectory(junc1));

  ASSERT_EQ(0, rmdir(junc1.c_str()));
}

}  // namespace blaze_util