Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <windows.h> |
| 17 | |
| 18 | #include <memory> // unique_ptr |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | |
| 22 | #include "gtest/gtest.h" |
Laszlo Csomor | f070234 | 2017-06-28 16:05:23 +0200 | [diff] [blame] | 23 | #include "src/main/native/windows/file.h" |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 24 | #include "src/test/cpp/util/windows_test_util.h" |
| 25 | |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame^] | 26 | #if !defined(_WIN32) && !defined(__CYGWIN__) |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 27 | #error("This test should only be run on Windows") |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame^] | 28 | #endif // !defined(_WIN32) && !defined(__CYGWIN__) |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 29 | |
Laszlo Csomor | f070234 | 2017-06-28 16:05:23 +0200 | [diff] [blame] | 30 | namespace bazel { |
| 31 | namespace windows { |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 32 | |
| 33 | using blaze_util::DeleteAllUnder; |
| 34 | using blaze_util::GetTestTmpDirW; |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 35 | using std::unique_ptr; |
| 36 | using std::wstring; |
| 37 | |
| 38 | static const wstring kUncPrefix = wstring(L"\\\\?\\"); |
| 39 | |
| 40 | class WindowsFileOperationsTest : public ::testing::Test { |
| 41 | public: |
| 42 | void TearDown() override { DeleteAllUnder(GetTestTmpDirW()); } |
| 43 | }; |
| 44 | |
| 45 | TEST_F(WindowsFileOperationsTest, TestCreateJunction) { |
| 46 | wstring tmp(kUncPrefix + GetTestTmpDirW()); |
| 47 | wstring target(tmp + L"\\junc_target"); |
| 48 | EXPECT_TRUE(::CreateDirectoryW(target.c_str(), NULL)); |
| 49 | wstring file1(target + L"\\foo"); |
| 50 | EXPECT_TRUE(blaze_util::CreateDummyFile(file1)); |
| 51 | |
| 52 | EXPECT_EQ(IS_JUNCTION_NO, IsJunctionOrDirectorySymlink(target.c_str())); |
| 53 | EXPECT_NE(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file1.c_str())); |
| 54 | |
| 55 | wstring name(tmp + L"\\junc_name"); |
| 56 | |
| 57 | // Create junctions from all combinations of UNC-prefixed or non-prefixed name |
| 58 | // and target paths. |
Laszlo Csomor | 6485ec6 | 2017-10-19 10:24:13 +0200 | [diff] [blame] | 59 | ASSERT_EQ(L"", CreateJunction(name + L"1", target)); |
| 60 | ASSERT_EQ(L"", CreateJunction(name + L"2", target.substr(4))); |
| 61 | ASSERT_EQ(L"", CreateJunction(name.substr(4) + L"3", target)); |
| 62 | ASSERT_EQ(L"", CreateJunction(name.substr(4) + L"4", target.substr(4))); |
Laszlo Csomor | d7d4def | 2017-02-15 13:07:52 +0000 | [diff] [blame] | 63 | |
| 64 | // Assert creation of the junctions. |
| 65 | ASSERT_EQ(IS_JUNCTION_YES, |
| 66 | IsJunctionOrDirectorySymlink((name + L"1").c_str())); |
| 67 | ASSERT_EQ(IS_JUNCTION_YES, |
| 68 | IsJunctionOrDirectorySymlink((name + L"2").c_str())); |
| 69 | ASSERT_EQ(IS_JUNCTION_YES, |
| 70 | IsJunctionOrDirectorySymlink((name + L"3").c_str())); |
| 71 | ASSERT_EQ(IS_JUNCTION_YES, |
| 72 | IsJunctionOrDirectorySymlink((name + L"4").c_str())); |
| 73 | |
| 74 | // Assert that the file is visible under all junctions. |
| 75 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 76 | ::GetFileAttributesW((name + L"1\\foo").c_str())); |
| 77 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 78 | ::GetFileAttributesW((name + L"2\\foo").c_str())); |
| 79 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 80 | ::GetFileAttributesW((name + L"3\\foo").c_str())); |
| 81 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 82 | ::GetFileAttributesW((name + L"4\\foo").c_str())); |
| 83 | |
| 84 | // Assert that no other file exists under the junctions. |
| 85 | wstring file2(target + L"\\bar"); |
| 86 | ASSERT_EQ(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file2.c_str())); |
| 87 | ASSERT_EQ(INVALID_FILE_ATTRIBUTES, |
| 88 | ::GetFileAttributesW((name + L"1\\bar").c_str())); |
| 89 | ASSERT_EQ(INVALID_FILE_ATTRIBUTES, |
| 90 | ::GetFileAttributesW((name + L"2\\bar").c_str())); |
| 91 | ASSERT_EQ(INVALID_FILE_ATTRIBUTES, |
| 92 | ::GetFileAttributesW((name + L"3\\bar").c_str())); |
| 93 | ASSERT_EQ(INVALID_FILE_ATTRIBUTES, |
| 94 | ::GetFileAttributesW((name + L"4\\bar").c_str())); |
| 95 | |
| 96 | // Create a new file. |
| 97 | EXPECT_TRUE(blaze_util::CreateDummyFile(file2)); |
| 98 | EXPECT_NE(INVALID_FILE_ATTRIBUTES, ::GetFileAttributesW(file2.c_str())); |
| 99 | |
| 100 | // Assert that the newly created file appears under all junctions. |
| 101 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 102 | ::GetFileAttributesW((name + L"1\\bar").c_str())); |
| 103 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 104 | ::GetFileAttributesW((name + L"2\\bar").c_str())); |
| 105 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 106 | ::GetFileAttributesW((name + L"3\\bar").c_str())); |
| 107 | ASSERT_NE(INVALID_FILE_ATTRIBUTES, |
| 108 | ::GetFileAttributesW((name + L"4\\bar").c_str())); |
| 109 | } |
| 110 | |
Laszlo Csomor | f070234 | 2017-06-28 16:05:23 +0200 | [diff] [blame] | 111 | } // namespace windows |
| 112 | } // namespace bazel |