blob: 5e8a177cdb964ece0a4d4caa1b576759611f63e5 [file]
// 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.
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <algorithm>
#include <memory>
#include <string>
#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
#include "src/main/cpp/util/path.h"
#include "src/main/cpp/util/strings.h"
#include "googletest/include/gtest/gtest.h"
#include "absl/strings/ascii.h"
namespace blaze {
using std::string;
using std::unique_ptr;
using std::wstring;
// Asserts that the envvar named `key` is unset.
// Exercises GetEnvironmentVariable{A,W}, both with `key` and its lower case
// version, to make sure that envvar retrieval is case-insensitive (envvar names
// are case-insensitive on Windows).
//
// This is a macro so the assertions will have the correct line number.
#define ASSERT_ENVVAR_UNSET(/* const char* */ key) \
{ \
ASSERT_EQ(::GetEnvironmentVariableA(key, nullptr, 0), (DWORD)0); \
ASSERT_EQ(::GetEnvironmentVariableA(absl::AsciiStrToLower(key).c_str(), \
nullptr, 0), \
(DWORD)0); \
ASSERT_EQ(::GetEnvironmentVariableW( \
blaze_util::CstringToWstring(key).c_str(), nullptr, 0), \
(DWORD)0); \
ASSERT_EQ( \
::GetEnvironmentVariableW( \
blaze_util::CstringToWstring(absl::AsciiStrToLower(key)).c_str(), \
nullptr, 0), \
(DWORD)0); \
}
// Asserts that the envvar named `key` is set to the `expected` value.
// Exercises GetEnvironmentVariable{A,W}, both with `key` and its lower case
// version, to make sure that envvar retrieval is case-insensitive (envvar names
// are case-insensitive on Windows).
//
// This is a macro so the assertions will have the correct line number.
#define ASSERT_ENVVAR(/* const (char* | string&) */ _key, \
/* const (char* | string&) */ _expected) \
{ \
string key(_key); \
string expected(_expected); \
DWORD size = ::GetEnvironmentVariableA(key.c_str(), nullptr, 0); \
ASSERT_GT(size, (DWORD)0); \
unique_ptr<char[]> buf(new char[size]); \
\
/* Assert that GetEnvironmentVariableA can retrieve the value. */ \
ASSERT_EQ(::GetEnvironmentVariableA(key.c_str(), buf.get(), size), \
size - 1); \
ASSERT_EQ(string(buf.get()), expected); \
\
/* Assert that envvar keys are case-insensitive. */ \
string lkey(absl::AsciiStrToLower(key)); \
ASSERT_EQ(::GetEnvironmentVariableA(lkey.c_str(), buf.get(), size), \
size - 1); \
ASSERT_EQ(string(buf.get()), expected); \
\
/* Assert that GetEnvironmentVariableW can retrieve the value. */ \
wstring wkey(blaze_util::CstringToWstring(key)); \
wstring wexpected(blaze_util::CstringToWstring(expected)); \
size = ::GetEnvironmentVariableW(wkey.c_str(), nullptr, 0); \
ASSERT_GT(size, (DWORD)0); \
unique_ptr<WCHAR[]> wbuf(new WCHAR[size]); \
ASSERT_EQ(::GetEnvironmentVariableW(wkey.c_str(), wbuf.get(), size), \
size - 1); \
ASSERT_EQ(wstring(wbuf.get()), wexpected); \
\
/* Assert that widechar envvar keys are case-insensitive. */ \
wstring wlkey(blaze_util::CstringToWstring(lkey)); \
ASSERT_EQ(::GetEnvironmentVariableW(wlkey.c_str(), wbuf.get(), size), \
size - 1); \
ASSERT_EQ(wstring(wbuf.get()), wexpected); \
}
TEST(BlazeUtilWindowsTest, TestGetEnv) {
#define _STR(x) #x
#define STR(x) _STR(x)
const char* envvar = "BAZEL_TEST_" STR(__LINE__);
#undef STR
#undef _STR
ASSERT_TRUE(SetEnvironmentVariableA(envvar, "A\\B c"));
ASSERT_EQ(GetEnv(envvar), "A\\B c");
}
TEST(BlazeUtilWindowsTest, TestGetPathEnv) {
#define _STR(x) #x
#define STR(x) _STR(x)
const char* envvar = "BAZEL_TEST_" STR(__LINE__);
#undef STR
#undef _STR
ASSERT_TRUE(SetEnvironmentVariableA(envvar, "A\\B c"));
ASSERT_EQ(GetPathEnv(envvar), "A/B c");
ASSERT_TRUE(SetEnvironmentVariableA(envvar, "\\\\?\\A:\\B c"));
ASSERT_EQ(GetPathEnv(envvar), "A:/B c");
}
TEST(BlazeUtilWindowsTest, TestSetEnv) {
ASSERT_ENVVAR_UNSET("Bazel_TEST_Key1");
SetEnv("Bazel_TEST_Key1", "some_VALUE");
ASSERT_ENVVAR("Bazel_TEST_Key1", "some_VALUE");
SetEnv("Bazel_TEST_Key1", "");
ASSERT_ENVVAR("Bazel_TEST_Key1", "");
string long_string(MAX_PATH, 'a');
string long_key = string("Bazel_TEST_Key2_") + long_string;
string long_value = string("Bazel_TEST_Value2_") + long_string;
ASSERT_ENVVAR_UNSET(long_key.c_str());
SetEnv(long_key, long_value);
ASSERT_ENVVAR(long_key.c_str(), long_value.c_str());
SetEnv(long_key, "");
ASSERT_ENVVAR(long_key.c_str(), "");
}
TEST(BlazeUtilWindowsTest, TestUnsetEnv) {
ASSERT_ENVVAR_UNSET("Bazel_TEST_Key3");
SetEnv("Bazel_TEST_Key3", "some_VALUE");
ASSERT_ENVVAR("Bazel_TEST_Key3", "some_VALUE");
UnsetEnv("Bazel_TEST_Key3");
ASSERT_ENVVAR_UNSET("Bazel_TEST_Key3");
string long_string(MAX_PATH, 'a');
string long_key = string("Bazel_TEST_Key4_") + long_string;
string long_value = string("Bazel_TEST_Value4_") + long_string;
ASSERT_ENVVAR_UNSET(long_key.c_str());
SetEnv(long_key, long_value);
ASSERT_ENVVAR(long_key.c_str(), long_value.c_str());
UnsetEnv(long_key);
ASSERT_ENVVAR_UNSET(long_key.c_str());
}
class FindBashInPathTest : public ::testing::Test {
protected:
void SetUp() override {
root_ = blaze_util::JoinPath(GetPathEnv("TEST_TMPDIR"), "find_bash");
windir_ = blaze_util::JoinPath(root_, "Windows");
system32_ = blaze_util::JoinPath(windir_, "System32");
git_ = blaze_util::JoinPath(root_, "Git\\usr\\bin");
ASSERT_TRUE(blaze_util::MakeDirectories(system32_, 0777));
ASSERT_TRUE(blaze_util::MakeDirectories(git_, 0777));
}
// Lowercases `path` and uses backslashes as the only separator.
static string Normalize(const string& path) {
string result = absl::AsciiStrToLower(path);
std::replace(result.begin(), result.end(), '/', '\\');
return result;
}
void WriteBash(const string& dir) {
ASSERT_TRUE(
blaze_util::WriteFile("", blaze_util::JoinPath(dir, "bash.exe")));
}
string root_;
string windir_;
string system32_;
string git_;
};
TEST_F(FindBashInPathTest, SkipsBashUnderWindir) {
WriteBash(system32_);
WriteBash(git_);
string result = FindBashInPath(system32_ + ";" + git_, windir_, "");
EXPECT_EQ(Normalize(result),
Normalize(blaze_util::JoinPath(git_, "bash.exe")));
}
TEST_F(FindBashInPathTest, EmptyWhenOnlyBashIsUnderWindir) {
string only = blaze_util::JoinPath(root_, "OnlyWsl");
string only_windir = blaze_util::JoinPath(only, "Windows");
string only_system32 = blaze_util::JoinPath(only_windir, "System32");
ASSERT_TRUE(blaze_util::MakeDirectories(only_system32, 0777));
WriteBash(only_system32);
EXPECT_EQ(FindBashInPath(only_system32, only_windir, ""), "");
}
TEST_F(FindBashInPathTest, KeepsBashWhenWindirIsEmpty) {
WriteBash(system32_);
EXPECT_NE(FindBashInPath(system32_, "", ""), "");
}
TEST_F(FindBashInPathTest, SkipsWindowsAppsAlias) {
string local_app_data = blaze_util::JoinPath(root_, "LocalAppData");
string windows_apps =
blaze_util::JoinPath(local_app_data, "Microsoft\\WindowsApps");
ASSERT_TRUE(blaze_util::MakeDirectories(windows_apps, 0777));
WriteBash(windows_apps);
WriteBash(git_);
string result =
FindBashInPath(windows_apps + ";" + git_, windir_, local_app_data);
EXPECT_EQ(Normalize(result),
Normalize(blaze_util::JoinPath(git_, "bash.exe")));
}
TEST_F(FindBashInPathTest, SkipsWindowsAppsAliasWithTrailingSeparator) {
string local_app_data = blaze_util::JoinPath(root_, "LocalAppData");
string windows_apps =
blaze_util::JoinPath(local_app_data, "Microsoft\\WindowsApps");
ASSERT_TRUE(blaze_util::MakeDirectories(windows_apps, 0777));
WriteBash(windows_apps);
EXPECT_EQ(FindBashInPath(windows_apps, windir_, local_app_data + "\\"), "");
}
TEST_F(FindBashInPathTest, SkipsWindowsAppsAliasUnderNonAsciiPath) {
// "Jörg" in the directory on disk and on PATH, "JÖRG" in LOCALAPPDATA.
string local_app_data =
blaze_util::JoinPath(root_, "J\xc3\xb6rg\\LocalAppData");
string upper_local_app_data =
blaze_util::JoinPath(root_, "J\xc3\x96RG\\LocalAppData");
string windows_apps =
blaze_util::JoinPath(local_app_data, "Microsoft\\WindowsApps");
ASSERT_TRUE(blaze_util::MakeDirectories(windows_apps, 0777));
WriteBash(windows_apps);
WriteBash(git_);
string result =
FindBashInPath(windows_apps + ";" + git_, windir_, upper_local_app_data);
EXPECT_EQ(Normalize(result),
Normalize(blaze_util::JoinPath(git_, "bash.exe")));
}
TEST_F(FindBashInPathTest, FindsBashUnderNonAsciiPath) {
string dir = blaze_util::JoinPath(root_, "J\xc3\xb6rg\\bin");
ASSERT_TRUE(blaze_util::MakeDirectories(dir, 0777));
WriteBash(dir);
EXPECT_EQ(Normalize(FindBashInPath(dir, windir_, "")),
Normalize(blaze_util::JoinPath(dir, "bash.exe")));
}
TEST_F(FindBashInPathTest, KeepsWindowsAppsBashWhenLocalAppDataIsEmpty) {
string windows_apps = blaze_util::JoinPath(root_, "Microsoft\\WindowsApps");
ASSERT_TRUE(blaze_util::MakeDirectories(windows_apps, 0777));
WriteBash(windows_apps);
EXPECT_NE(FindBashInPath(windows_apps, windir_, ""), "");
}
} // namespace blaze