| 1 |
//===-- HostInfoTest.cpp --------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "lldb/Host/HostInfo.h" |
| 10 |
#include "TestingSupport/SubsystemRAII.h" |
| 11 |
#include "TestingSupport/TestUtilities.h" |
| 12 |
#include "lldb/Host/FileSystem.h" |
| 13 |
#include "lldb/lldb-defines.h" |
| 14 |
#include "llvm/Support/Host.h" |
| 15 |
#include "gtest/gtest.h" |
| 16 |
|
| 17 |
using namespace lldb_private; |
| 18 |
using namespace llvm; |
| 19 |
|
| 20 |
namespace { |
| 21 |
class HostInfoTest : public ::testing::Test { |
| 22 |
SubsystemRAII<FileSystem, HostInfo> subsystems; |
| 23 |
}; |
| 24 |
} // namespace |
| 25 |
|
| 26 |
TEST_F(HostInfoTest, GetAugmentedArchSpec) { |
| 27 |
// Fully specified triple should not be changed. |
| 28 |
ArchSpec spec = HostInfo::GetAugmentedArchSpec("x86_64-pc-linux-gnu"); |
| 29 |
EXPECT_EQ(spec.GetTriple().getTriple(), "x86_64-pc-linux-gnu"); |
| 30 |
|
| 31 |
// Same goes if we specify at least one of (os, vendor, env). |
| 32 |
spec = HostInfo::GetAugmentedArchSpec("x86_64-pc"); |
| 33 |
EXPECT_EQ(spec.GetTriple().getTriple(), "x86_64-pc"); |
| 34 |
|
| 35 |
// But if we specify only an arch, we should fill in the rest from the host. |
| 36 |
spec = HostInfo::GetAugmentedArchSpec("x86_64"); |
| 37 |
Triple triple(sys::getDefaultTargetTriple()); |
| 38 |
EXPECT_EQ(spec.GetTriple().getArch(), Triple::x86_64); |
| 39 |
EXPECT_EQ(spec.GetTriple().getOS(), triple.getOS()); |
| 40 |
EXPECT_EQ(spec.GetTriple().getVendor(), triple.getVendor()); |
| 41 |
EXPECT_EQ(spec.GetTriple().getEnvironment(), triple.getEnvironment()); |
| 42 |
|
| 43 |
// Test LLDB_ARCH_DEFAULT |
| 44 |
EXPECT_EQ(HostInfo::GetAugmentedArchSpec(LLDB_ARCH_DEFAULT).GetTriple(), |
| 45 |
HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple()); |
| 46 |
EXPECT_NE( |
| 47 |
HostInfo::GetAugmentedArchSpec("armv7k").GetTriple().getEnvironmentName(), |
| 48 |
"unknown"); |
| 49 |
} |
| 50 |
|
| 51 |
TEST_F(HostInfoTest, GetHostname) { |
| 52 |
// Check non-empty string input works correctly. |
| 53 |
std::string s("abc"); |
| 54 |
EXPECT_TRUE(HostInfo::GetHostname(s)); |
| 55 |
} |
| 56 |
|
| 57 |
#if defined(__APPLE__) |
| 58 |
TEST_F(HostInfoTest, GetXcodeSDK) { |
| 59 |
auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef { |
| 60 |
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK(std::move(sdk))); |
| 61 |
if (!error) { |
| 62 |
EXPECT_TRUE((bool)sdk_path_or_err); |
| 63 |
return *sdk_path_or_err; |
| 64 |
} |
| 65 |
EXPECT_FALSE((bool)sdk_path_or_err); |
| 66 |
llvm::consumeError(sdk_path_or_err.takeError()); |
| 67 |
return {}; |
| 68 |
}; |
| 69 |
EXPECT_FALSE(get_sdk("MacOSX.sdk").empty()); |
| 70 |
// These are expected to fall back to an available version. |
| 71 |
EXPECT_FALSE(get_sdk("MacOSX9999.sdk").empty()); |
| 72 |
// This is expected to fail. |
| 73 |
EXPECT_TRUE(get_sdk("CeciNestPasUnOS.sdk", true).empty()); |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
TEST(HostInfoTestInitialization, InitTwice) { |
| 78 |
llvm::VersionTuple Version; |
| 79 |
{ |
| 80 |
SubsystemRAII<FileSystem, HostInfo> subsystems; |
| 81 |
Version = HostInfo::GetOSVersion(); |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
SubsystemRAII<FileSystem, HostInfo> subsystems; |
| 86 |
EXPECT_EQ(Version, HostInfo::GetOSVersion()); |
| 87 |
} |
| 88 |
} |