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)
20 #define ASSERT_NO_ERROR(x) \
21 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
33 // Check that the Debuginfod client can find locally cached artifacts.
34 TEST(DebuginfodClient
, CacheHit
) {
36 SmallString
<64> CachedFilePath
;
37 sys::fs::createTemporaryFile("llvmcache-key", "temp", FD
, CachedFilePath
);
38 StringRef CacheDir
= sys::path::parent_path(CachedFilePath
);
39 StringRef UniqueKey
= sys::path::filename(CachedFilePath
);
40 EXPECT_TRUE(UniqueKey
.consume_front("llvmcache-"));
41 raw_fd_ostream
OF(FD
, true, /*unbuffered=*/true);
43 OF
<< CacheDir
<< "\n";
45 Expected
<std::string
> PathOrErr
= getCachedOrDownloadArtifact(
46 UniqueKey
, /*UrlPath=*/"/null", CacheDir
,
47 /*DebuginfodUrls=*/{}, /*Timeout=*/std::chrono::milliseconds(1));
48 EXPECT_THAT_EXPECTED(PathOrErr
, HasValue(CachedFilePath
));
51 // Check that the Debuginfod client returns an Error when it fails to find an
53 TEST(DebuginfodClient
, CacheMiss
) {
54 SmallString
<32> CacheDir
;
56 sys::fs::createUniqueDirectory("debuginfod-unittest", CacheDir
));
57 sys::path::append(CacheDir
, "cachedir");
58 ASSERT_FALSE(sys::fs::exists(CacheDir
));
59 setenv("DEBUGINFOD_CACHE_PATH", CacheDir
.c_str(),
61 // Ensure there are no urls to guarantee a cache miss.
62 setenv("DEBUGINFOD_URLS", "", /*replace=*/1);
63 HTTPClient::initialize();
64 Expected
<std::string
> PathOrErr
= getCachedOrDownloadArtifact(
65 /*UniqueKey=*/"nonexistent-key", /*UrlPath=*/"/null");
66 EXPECT_THAT_EXPECTED(PathOrErr
, Failed
<StringError
>());
67 // A cache miss with no possible URLs should not create the cache directory.
68 EXPECT_FALSE(sys::fs::exists(CacheDir
));