1 //===-- llvm/unittest/Support/DebuginfodTests.cpp - unit tests ------------===//
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
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"
17 #define setenv(name, var, ignore) _putenv_s(name, var)
22 // Check that the Debuginfod client can find locally cached artifacts.
23 TEST(DebuginfodClient
, CacheHit
) {
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);
32 OF
<< CacheDir
<< "\n";
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
42 TEST(DebuginfodClient
, CacheMiss
) {
43 // Set the cache path to a temp directory to avoid permissions issues if $HOME
45 SmallString
<32> TempDir
;
46 sys::path::system_temp_directory(true, TempDir
);
47 setenv("DEBUGINFOD_CACHE_PATH", TempDir
.c_str(),
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
>());