xref: /llvm-project/lldb/unittests/tools/lldb-server/tests/LLGSTest.cpp (revision 5d2b3378758b42391e90b1adf936537a66b038b6)
1 //===-- LLGSTest.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 "TestBase.h"
10 #include "lldb/Host/Host.h"
11 #include "llvm/Testing/Support/Error.h"
12 
13 using namespace llgs_tests;
14 using namespace lldb_private;
15 using namespace llvm;
16 
17 // Disable this test on Windows as it appears to have a race condition
18 // that causes lldb-server not to exit after the inferior hangs up.
19 #if !defined(_WIN32)
20 TEST_F(TestBase, LaunchModePreservesEnvironment) {
21   putenv(const_cast<char *>("LLDB_TEST_MAGIC_VARIABLE=LLDB_TEST_MAGIC_VALUE"));
22 
23   auto ClientOr = TestClient::launchCustom(
24       getLogFileName(),
25       /* disable_stdio */ true, {}, {getInferiorPath("environment_check")});
26   ASSERT_THAT_EXPECTED(ClientOr, Succeeded());
27   auto &Client = **ClientOr;
28 
29   ASSERT_THAT_ERROR(Client.ContinueAll(), Succeeded());
30   ASSERT_THAT_EXPECTED(
31       Client.GetLatestStopReplyAs<StopReplyExit>(),
32       HasValue(testing::Property(&StopReply::getKind,
33                                  WaitStatus{WaitStatus::Exit, 0})));
34 }
35 #endif
36 
37 TEST_F(TestBase, DS_TEST(DebugserverEnv)) {
38   // Test that --env takes precedence over inherited environment variables.
39   putenv(const_cast<char *>("LLDB_TEST_MAGIC_VARIABLE=foobar"));
40 
41   auto ClientOr = TestClient::launchCustom(
42       getLogFileName(), /* disable_stdio */ true,
43       {"--env", "LLDB_TEST_MAGIC_VARIABLE=LLDB_TEST_MAGIC_VALUE"},
44       {getInferiorPath("environment_check")});
45   ASSERT_THAT_EXPECTED(ClientOr, Succeeded());
46   auto &Client = **ClientOr;
47 
48   ASSERT_THAT_ERROR(Client.ContinueAll(), Succeeded());
49   ASSERT_THAT_EXPECTED(
50       Client.GetLatestStopReplyAs<StopReplyExit>(),
51       HasValue(testing::Property(&StopReply::getKind,
52                                  WaitStatus{WaitStatus::Exit, 0})));
53 }
54 
55 TEST_F(TestBase, LLGS_TEST(vAttachRichError)) {
56   auto ClientOr = TestClient::launchCustom(
57       getLogFileName(),
58       /* disable_stdio */ true, {}, {getInferiorPath("environment_check")});
59   ASSERT_THAT_EXPECTED(ClientOr, Succeeded());
60   auto &Client = **ClientOr;
61 
62   // Until we enable error strings we should just get the error code.
63   ASSERT_THAT_ERROR(Client.SendMessage("vAttach;1"),
64                     Failed<ErrorInfoBase>(testing::Property(
65                         &ErrorInfoBase::message, "Error 255")));
66 
67   ASSERT_THAT_ERROR(Client.SendMessage("QEnableErrorStrings"), Succeeded());
68 
69   // Now, we expect the full error message.
70   ASSERT_THAT_ERROR(
71       Client.SendMessage("vAttach;1"),
72       Failed<ErrorInfoBase>(testing::Property(
73           &ErrorInfoBase::message,
74           testing::StartsWith(
75               "cannot attach to process 1 when another process with pid"))));
76 }
77