135 lines · cpp
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-exception6//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/TargetParser/Host.h"15#include "gtest/gtest.h"16 17using namespace lldb_private;18using namespace llvm;19 20namespace {21class HostInfoTest : public ::testing::Test {22 SubsystemRAII<FileSystem, HostInfo> subsystems;23};24} // namespace25 26TEST_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_DEFAULT44 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 51TEST_F(HostInfoTest, GetHostname) {52 // Check non-empty string input works correctly.53 std::string s("abc");54 EXPECT_TRUE(HostInfo::GetHostname(s));55}56 57TEST_F(HostInfoTest, GetProgramFileSpec) {58 FileSpec filespec = HostInfo::GetProgramFileSpec();59 EXPECT_TRUE(FileSystem::Instance().Exists(filespec));60}61 62#if defined(__APPLE__)63TEST_F(HostInfoTest, GetXcodeSDK) {64 auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef {65 auto sdk_path_or_err =66 HostInfo::GetSDKRoot(HostInfo::SDKOptions{XcodeSDK(std::move(sdk))});67 if (!error) {68 EXPECT_TRUE((bool)sdk_path_or_err);69 return *sdk_path_or_err;70 }71 EXPECT_FALSE((bool)sdk_path_or_err);72 llvm::consumeError(sdk_path_or_err.takeError());73 return {};74 };75 EXPECT_FALSE(get_sdk("MacOSX.sdk").empty());76 // These are expected to fall back to an available version.77 EXPECT_FALSE(get_sdk("MacOSX9999.sdk").empty());78 // This is expected to fail.79 EXPECT_TRUE(get_sdk("CeciNestPasUnOS.sdk", true).empty());80}81 82TEST_F(HostInfoTest, FindSDKTool) {83 auto find_tool = [](std::string sdk, llvm::StringRef tool,84 bool error = false) -> llvm::StringRef {85 auto sdk_path_or_err =86 HostInfo::FindSDKTool(XcodeSDK(std::move(sdk)), tool);87 if (!error) {88 EXPECT_TRUE((bool)sdk_path_or_err);89 return *sdk_path_or_err;90 }91 EXPECT_FALSE((bool)sdk_path_or_err);92 llvm::consumeError(sdk_path_or_err.takeError());93 return {};94 };95 EXPECT_FALSE(find_tool("MacOSX.sdk", "clang").empty());96 EXPECT_TRUE(find_tool("MacOSX.sdk", "CeciNestPasUnOutil").empty());97}98#endif99 100TEST(HostInfoTestInitialization, InitTwice) {101 llvm::VersionTuple Version;102 {103 SubsystemRAII<FileSystem, HostInfo> subsystems;104 Version = HostInfo::GetOSVersion();105 }106 107 {108 SubsystemRAII<FileSystem, HostInfo> subsystems;109 EXPECT_EQ(Version, HostInfo::GetOSVersion());110 }111}112 113#ifdef __APPLE__114struct HostInfoTester : public HostInfoMacOSX {115public:116 using HostInfoMacOSX::FindComponentInPath;117};118 119TEST_F(HostInfoTest, FindComponentInPath) {120 EXPECT_EQ("/path/to/foo",121 HostInfoTester::FindComponentInPath("/path/to/foo/", "foo"));122 123 EXPECT_EQ("/path/to/foo",124 HostInfoTester::FindComponentInPath("/path/to/foo", "foo"));125 126 EXPECT_EQ("/path/to/foobar",127 HostInfoTester::FindComponentInPath("/path/to/foobar", "foo"));128 129 EXPECT_EQ("/path/to/foobar",130 HostInfoTester::FindComponentInPath("/path/to/foobar", "bar"));131 132 EXPECT_EQ("", HostInfoTester::FindComponentInPath("/path/to/foo", "bar"));133}134#endif135