[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / unittests / Debuginfod / DebuginfodTests.cpp
blobfc5eb705969e71ba9bd23fa0b511ffee8ed66927
1 //===-- llvm/unittest/Support/DebuginfodTests.cpp - unit tests ------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Debuginfod/Debuginfod.h"
10 #include "llvm/Debuginfod/HTTPClient.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
16 #ifdef _WIN32
17 #define setenv(name, var, ignore) _putenv_s(name, var)
18 #endif
20 using namespace llvm;
22 // Check that the Debuginfod client can find locally cached artifacts.
23 TEST(DebuginfodClient, CacheHit) {
24 int FD;
25 SmallString<64> CachedFilePath;
26 sys::fs::createTemporaryFile("llvmcache-key", "temp", FD, CachedFilePath);
27 StringRef CacheDir = sys::path::parent_path(CachedFilePath);
28 StringRef UniqueKey = sys::path::filename(CachedFilePath);
29 EXPECT_TRUE(UniqueKey.consume_front("llvmcache-"));
30 raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
31 OF << "contents\n";
32 OF << CacheDir << "\n";
33 OF.close();
34 Expected<std::string> PathOrErr = getCachedOrDownloadArtifact(
35 UniqueKey, /*UrlPath=*/"/null", CacheDir,
36 /*DebuginfodUrls=*/{}, /*Timeout=*/std::chrono::milliseconds(1));
37 EXPECT_THAT_EXPECTED(PathOrErr, HasValue(CachedFilePath));
40 // Check that the Debuginfod client returns an Error when it fails to find an
41 // artifact.
42 TEST(DebuginfodClient, CacheMiss) {
43 // Set the cache path to a temp directory to avoid permissions issues if $HOME
44 // is not writable.
45 SmallString<32> TempDir;
46 sys::path::system_temp_directory(true, TempDir);
47 setenv("DEBUGINFOD_CACHE_PATH", TempDir.c_str(),
48 /*replace=*/1);
49 // Ensure there are no urls to guarantee a cache miss.
50 setenv("DEBUGINFOD_URLS", "", /*replace=*/1);
51 HTTPClient::initialize();
52 Expected<std::string> PathOrErr = getCachedOrDownloadArtifact(
53 /*UniqueKey=*/"nonexistent-key", /*UrlPath=*/"/null");
54 EXPECT_THAT_EXPECTED(PathOrErr, Failed<StringError>());