blob: 9c4e23388cf0c8e6b8ad0d82652098fcf7cc92e9 [file] [log] [blame]
Laszlo Csomord7d4def2017-02-15 13:07:52 +00001// 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 Csomorf0702342017-06-28 16:05:23 +020023#include "src/main/native/windows/file.h"
Laszlo Csomord7d4def2017-02-15 13:07:52 +000024#include "src/test/cpp/util/windows_test_util.h"
25
Loo Rong Jie4022bac2018-06-11 02:04:52 -070026#if !defined(_WIN32) && !defined(__CYGWIN__)
Laszlo Csomord7d4def2017-02-15 13:07:52 +000027#error("This test should only be run on Windows")
Loo Rong Jie4022bac2018-06-11 02:04:52 -070028#endif // !defined(_WIN32) && !defined(__CYGWIN__)
Laszlo Csomord7d4def2017-02-15 13:07:52 +000029
Laszlo Csomorf0702342017-06-28 16:05:23 +020030namespace bazel {
31namespace windows {
Laszlo Csomord7d4def2017-02-15 13:07:52 +000032
33using blaze_util::DeleteAllUnder;
34using blaze_util::GetTestTmpDirW;
Laszlo Csomord7d4def2017-02-15 13:07:52 +000035using std::unique_ptr;
36using std::wstring;
37
38static const wstring kUncPrefix = wstring(L"\\\\?\\");
39
40class WindowsFileOperationsTest : public ::testing::Test {
41 public:
42 void TearDown() override { DeleteAllUnder(GetTestTmpDirW()); }
43};
44
45TEST_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 Csomor6485ec62017-10-19 10:24:13 +020059 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 Csomord7d4def2017-02-15 13:07:52 +000063
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 Csomorf0702342017-06-28 16:05:23 +0200111} // namespace windows
112} // namespace bazel