From d7d4def945b38133c43e3df3ea7c450b229d2bfb Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Wed, 15 Feb 2017 13:07:52 +0000 Subject: Bazel client, JNI, Windows: impl. CreateJunction Implement a CreateJunction function in the Windows JNI library. Also move a bit of code from file_windows to the JNI library, where it is (also) needed. This implementation is an improved version of `blaze_util::SymlinkDirectories` in blaze_util_windows: this version handles Windows paths as `name` and `target`, and performs more validation (e.g. on the length of `target`), plus has more comments explaining the logic. In a subsequent change I'll start using this new function in blaze_util_windows. This method will also be helpful in tests: we will no longer have to shell out to mklink. See https://github.com/bazelbuild/bazel/issues/2107 -- Change-Id: I7e9b085fdc2ba47be83da5319bded02bd323e71b Reviewed-on: https://cr.bazel.build/8892 PiperOrigin-RevId: 147585207 MOS_MIGRATED_REVID=147585207 --- src/test/native/windows_file_operations_test.cc | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/test/native/windows_file_operations_test.cc (limited to 'src/test/native/windows_file_operations_test.cc') diff --git a/src/test/native/windows_file_operations_test.cc b/src/test/native/windows_file_operations_test.cc new file mode 100644 index 0000000000..f3258a9a86 --- /dev/null +++ b/src/test/native/windows_file_operations_test.cc @@ -0,0 +1,111 @@ +// Copyright 2017 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 +#include +#include + +#include // unique_ptr +#include +#include + +#include "gtest/gtest.h" +#include "src/main/native/windows_file_operations.h" +#include "src/test/cpp/util/windows_test_util.h" + +#if !defined(COMPILER_MSVC) && !defined(__CYGWIN__) +#error("This test should only be run on Windows") +#endif // !defined(COMPILER_MSVC) && !defined(__CYGWIN__) + +namespace windows_util { + +using blaze_util::DeleteAllUnder; +using blaze_util::GetTestTmpDirW; +using std::string; +using std::unique_ptr; +using std::wstring; + +static const wstring kUncPrefix = wstring(L"\\\\?\\"); + +class WindowsFileOperationsTest : public ::testing::Test { + public: + void TearDown() override { DeleteAllUnder(GetTestTmpDirW()); } +}; + +TEST_F(WindowsFileOperationsTest, TestCreateJunction) { + wstring tmp(kUncPrefix + GetTestTmpDirW()); + wstring target(tmp + L"\\junc_target"); + EXPECT_TRUE(::CreateDirectoryW(target.c_str(), NULL)); + wstring file1(target + L"\\foo"); + EXPECT_TRUE(blaze_util::CreateDummyFile(file1)); + + EXPECT_EQ(IS_JUNCTION_NO, IsJunctionOrDirectorySymlink(target.c_str())); + EXPECT_NE(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file1.c_str())); + + wstring name(tmp + L"\\junc_name"); + + // Create junctions from all combinations of UNC-prefixed or non-prefixed name + // and target paths. + ASSERT_EQ("", CreateJunction(name + L"1", target)); + ASSERT_EQ("", CreateJunction(name + L"2", target.substr(4))); + ASSERT_EQ("", CreateJunction(name.substr(4) + L"3", target)); + ASSERT_EQ("", CreateJunction(name.substr(4) + L"4", target.substr(4))); + + // Assert creation of the junctions. + ASSERT_EQ(IS_JUNCTION_YES, + IsJunctionOrDirectorySymlink((name + L"1").c_str())); + ASSERT_EQ(IS_JUNCTION_YES, + IsJunctionOrDirectorySymlink((name + L"2").c_str())); + ASSERT_EQ(IS_JUNCTION_YES, + IsJunctionOrDirectorySymlink((name + L"3").c_str())); + ASSERT_EQ(IS_JUNCTION_YES, + IsJunctionOrDirectorySymlink((name + L"4").c_str())); + + // Assert that the file is visible under all junctions. + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"1\\foo").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"2\\foo").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"3\\foo").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"4\\foo").c_str())); + + // Assert that no other file exists under the junctions. + wstring file2(target + L"\\bar"); + ASSERT_EQ(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file2.c_str())); + ASSERT_EQ(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"1\\bar").c_str())); + ASSERT_EQ(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"2\\bar").c_str())); + ASSERT_EQ(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"3\\bar").c_str())); + ASSERT_EQ(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"4\\bar").c_str())); + + // Create a new file. + EXPECT_TRUE(blaze_util::CreateDummyFile(file2)); + EXPECT_NE(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file2.c_str())); + + // Assert that the newly created file appears under all junctions. + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"1\\bar").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"2\\bar").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"3\\bar").c_str())); + ASSERT_NE(INVALID_FILE_ATTRIBUTES, + ::GetFileAttributesW((name + L"4\\bar").c_str())); +} + +} // namespace windows_util -- cgit v1.2.3