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//===----------------------------------------------------------------------===//
8
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"
15
16#ifdef _WIN32
17#define setenv(name, var, ignore) _putenv_s(name, var)
18#endif
19
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()); \
28 } else { \
29 }
30
31using namespace llvm;
32
33// Check that the Debuginfod client can find locally cached artifacts.
34TEST(DebuginfodClient, CacheHit) {
35 int FD;
36 SmallString<64> CachedFilePath;
37 sys::fs::createTemporaryFile(Prefix: "llvmcache-key", Suffix: "temp", ResultFD&: FD, ResultPath&: CachedFilePath);
38 StringRef CacheDir = sys::path::parent_path(path: CachedFilePath);
39 StringRef UniqueKey = sys::path::filename(path: CachedFilePath);
40 EXPECT_TRUE(UniqueKey.consume_front("llvmcache-"));
41 raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
42 OF << "contents\n";
43 OF << CacheDir << "\n";
44 OF.close();
45 Expected<std::string> PathOrErr = getCachedOrDownloadArtifact(
46 UniqueKey, /*UrlPath=*/"/null", CacheDirectoryPath: CacheDir,
47 /*DebuginfodUrls=*/{}, /*Timeout=*/std::chrono::milliseconds(1));
48 EXPECT_THAT_EXPECTED(PathOrErr, HasValue(CachedFilePath));
49}
50
51// Check that the Debuginfod client returns an Error when it fails to find an
52// artifact.
53TEST(DebuginfodClient, CacheMiss) {
54 SmallString<32> CacheDir;
55 ASSERT_NO_ERROR(
56 sys::fs::createUniqueDirectory("debuginfod-unittest", CacheDir));
57 sys::path::append(path&: CacheDir, a: "cachedir");
58 ASSERT_FALSE(sys::fs::exists(CacheDir));
59 setenv(name: "DEBUGINFOD_CACHE_PATH", value: CacheDir.c_str(),
60 /*replace=*/1);
61 // Ensure there are no urls to guarantee a cache miss.
62 setenv(name: "DEBUGINFOD_URLS", value: "", /*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));
69}
70

source code of llvm/unittests/Debuginfod/DebuginfodTests.cpp