Skip to content

Commit

Permalink
Bazel client, Windows, tests: use CreateJuction
Browse files Browse the repository at this point in the history
Use the JNI library's CreateJuction in
file_windows_test.

See #2107

--
Change-Id: I4ef1536d43691fe7a2ae3ee457064d4e8f4ac6d7
Reviewed-on: https://cr.bazel.build/8895
PiperOrigin-RevId: 147594365
MOS_MIGRATED_REVID=147594365
  • Loading branch information
laszlocsomor authored and hermione521 committed Feb 15, 2017
1 parent ac29b78 commit 3635539
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
10 changes: 8 additions & 2 deletions src/test/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ cc_test(
"//src/main/cpp/util:file",
"//third_party:gtest",
] + select({
"//src:windows": [":windows_test_util"],
"//src:windows_msvc": [":windows_test_util"],
"//src:windows": [
":windows_test_util",
"//src/main/native:windows_jni_lib",
],
"//src:windows_msvc": [
":windows_test_util",
"//src/main/native:windows_jni_lib",
],
"//conditions:default": [],
}),
)
Expand Down
54 changes: 15 additions & 39 deletions src/test/cpp/util/file_windows_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "gtest/gtest.h"
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
#include "src/main/native/windows_file_operations.h"
#include "src/test/cpp/util/windows_test_util.h"

#if !defined(COMPILER_MSVC) && !defined(__CYGWIN__)
Expand Down Expand Up @@ -53,6 +54,15 @@ class FileWindowsTest : public ::testing::Test {
ASSERT_GT(result.size(), 0); \
}

#define CREATE_JUNCTION(/* const string& */ name, /* const string& */ target) \
{ \
wstring wname; \
wstring wtarget; \
EXPECT_TRUE(AsWindowsPath(name, &wname)); \
EXPECT_TRUE(AsWindowsPath(target, &wtarget)); \
EXPECT_EQ("", windows_util::CreateJunction(wname, wtarget)); \
}

// Asserts that dir1 can be created with some content, and dir2 doesn't exist.
static void AssertTearDown(const WCHAR* dir1, const WCHAR* dir2) {
wstring wtmpdir(GetTestTmpDirW());
Expand Down Expand Up @@ -267,35 +277,6 @@ TEST_F(FileWindowsTest, TestMsysRootRetrieval) {
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_F(FileWindowsTest, TestPathExistsWindows) {
ASSERT_FALSE(PathExists(""));
ASSERT_TRUE(PathExists("."));
Expand All @@ -322,14 +303,12 @@ TEST_F(FileWindowsTest, TestPathExistsWindows) {
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");
CREATE_JUNCTION(tmpdir + "/junc1", fake_msys_root);
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");
CREATE_JUNCTION(tmpdir + "/junc2", fake_msys_root + "/i.dont.exist");
ASSERT_FALSE(PathExists(JoinPath(fake_msys_root, "i.dont.exist")));
ASSERT_FALSE(PathExists(JoinPath(tmpdir, "junc2")));
}
Expand Down Expand Up @@ -361,8 +340,7 @@ TEST_F(FileWindowsTest, TestIsDirectory) {

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

ASSERT_EQ(0, rmdir(dir1.c_str()));
Expand All @@ -386,8 +364,7 @@ TEST_F(FileWindowsTest, TestUnlinkPath) {
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");
CREATE_JUNCTION(junc1, dir1);
ASSERT_TRUE(PathExists(junc1));
ASSERT_TRUE(PathExists(JoinPath(junc1, "foo.txt")));

Expand Down Expand Up @@ -444,8 +421,7 @@ TEST_F(FileWindowsTest, CanAccess) {
ASSERT_TRUE(CanAccessDirectory(dir));

string junc(JoinPath(tmpdir, "junc1"));
RunCommand(string("cmd.exe /C mklink /J \"") + junc + "\" \"" + dir +
"\" >NUL 2>NUL");
CREATE_JUNCTION(junc, dir);
ASSERT_FALSE(CanReadFile(junc));
ASSERT_FALSE(CanExecuteFile(junc));
ASSERT_TRUE(CanAccessDirectory(junc));
Expand Down

0 comments on commit 3635539

Please sign in to comment.