1 | //===-- StatusTest.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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Utility/Status.h" |
10 | #include "gtest/gtest.h" |
11 | |
12 | #ifdef _WIN32 |
13 | #include <windows.h> |
14 | #endif |
15 | |
16 | using namespace lldb_private; |
17 | using namespace lldb; |
18 | |
19 | TEST(StatusTest, Formatv) { |
20 | EXPECT_EQ("" , llvm::formatv("{0}" , Status()).str()); |
21 | EXPECT_EQ("Hello Status" , llvm::formatv("{0}" , Status("Hello Status" )).str()); |
22 | EXPECT_EQ("Hello" , llvm::formatv("{0:5}" , Status("Hello Error" )).str()); |
23 | } |
24 | |
25 | TEST(StatusTest, ErrorConstructor) { |
26 | EXPECT_TRUE(Status(llvm::Error::success()).Success()); |
27 | |
28 | Status eagain( |
29 | llvm::errorCodeToError(EC: std::error_code(EAGAIN, std::generic_category()))); |
30 | EXPECT_TRUE(eagain.Fail()); |
31 | EXPECT_EQ(eErrorTypePOSIX, eagain.GetType()); |
32 | EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError()); |
33 | |
34 | Status foo(llvm::make_error<llvm::StringError>( |
35 | Args: "foo" , Args: llvm::inconvertibleErrorCode())); |
36 | EXPECT_TRUE(foo.Fail()); |
37 | EXPECT_EQ(eErrorTypeGeneric, foo.GetType()); |
38 | EXPECT_STREQ("foo" , foo.AsCString()); |
39 | |
40 | foo = llvm::Error::success(); |
41 | EXPECT_TRUE(foo.Success()); |
42 | } |
43 | |
44 | TEST(StatusTest, ErrorCodeConstructor) { |
45 | EXPECT_TRUE(Status(std::error_code()).Success()); |
46 | |
47 | Status eagain = std::error_code(EAGAIN, std::generic_category()); |
48 | EXPECT_TRUE(eagain.Fail()); |
49 | EXPECT_EQ(eErrorTypePOSIX, eagain.GetType()); |
50 | EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError()); |
51 | } |
52 | |
53 | TEST(StatusTest, ErrorConversion) { |
54 | EXPECT_FALSE(bool(Status().ToError())); |
55 | |
56 | llvm::Error eagain = Status(EAGAIN, ErrorType::eErrorTypePOSIX).ToError(); |
57 | EXPECT_TRUE(bool(eagain)); |
58 | std::error_code ec = llvm::errorToErrorCode(Err: std::move(eagain)); |
59 | EXPECT_EQ(EAGAIN, ec.value()); |
60 | EXPECT_EQ(std::generic_category(), ec.category()); |
61 | |
62 | llvm::Error foo = Status("foo" ).ToError(); |
63 | EXPECT_TRUE(bool(foo)); |
64 | EXPECT_EQ("foo" , llvm::toString(std::move(foo))); |
65 | } |
66 | |
67 | #ifdef _WIN32 |
68 | TEST(StatusTest, ErrorWin32) { |
69 | auto success = Status(NO_ERROR, ErrorType::eErrorTypeWin32); |
70 | EXPECT_STREQ(NULL, success.AsCString()); |
71 | EXPECT_FALSE(success.ToError()); |
72 | EXPECT_TRUE(success.Success()); |
73 | |
74 | WCHAR name[128]{}; |
75 | ULONG nameLen = std::size(name); |
76 | ULONG langs = 0; |
77 | GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &langs, |
78 | reinterpret_cast<PZZWSTR>(&name), &nameLen); |
79 | // Skip the following tests on non-English, non-US, locales because the |
80 | // formatted messages will be different. |
81 | bool skip = wcscmp(L"en-US" , name) != 0; |
82 | |
83 | auto s = Status(ERROR_ACCESS_DENIED, ErrorType::eErrorTypeWin32); |
84 | EXPECT_TRUE(s.Fail()); |
85 | if (!skip) |
86 | EXPECT_STREQ("Access is denied. " , s.AsCString()); |
87 | |
88 | s.SetError(ERROR_IPSEC_IKE_TIMED_OUT, ErrorType::eErrorTypeWin32); |
89 | if (!skip) |
90 | EXPECT_STREQ("Negotiation timed out " , s.AsCString()); |
91 | |
92 | s.SetError(16000, ErrorType::eErrorTypeWin32); |
93 | if (!skip) |
94 | EXPECT_STREQ("unknown error" , s.AsCString()); |
95 | } |
96 | #endif |
97 | |