1 | //===-- ProcessEventDataTest.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 "Plugins/Platform/MacOSX/PlatformMacOSX.h" |
10 | #include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h" |
11 | #include "lldb/Core/Debugger.h" |
12 | #include "lldb/Host/FileSystem.h" |
13 | #include "lldb/Host/HostInfo.h" |
14 | #include "lldb/Interpreter/CommandInterpreter.h" |
15 | #include "lldb/Interpreter/CommandObject.h" |
16 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
17 | #include "lldb/Interpreter/CommandReturnObject.h" |
18 | #include "lldb/Utility/Args.h" |
19 | #include "lldb/Utility/Status.h" |
20 | |
21 | #include "gtest/gtest.h" |
22 | |
23 | using namespace lldb_private; |
24 | using namespace lldb; |
25 | |
26 | namespace { |
27 | class VerifyUserMultiwordCmdPathTest : public ::testing::Test { |
28 | void SetUp() override { |
29 | FileSystem::Initialize(); |
30 | HostInfo::Initialize(); |
31 | PlatformMacOSX::Initialize(); |
32 | } |
33 | void TearDown() override { |
34 | PlatformMacOSX::Terminate(); |
35 | HostInfo::Terminate(); |
36 | FileSystem::Terminate(); |
37 | } |
38 | }; |
39 | } // namespace |
40 | |
41 | class CommandObjectLeaf : public CommandObjectParsed { |
42 | public: |
43 | CommandObjectLeaf(CommandInterpreter &interpreter) |
44 | : CommandObjectParsed(interpreter, "dummy subcommand leaf" , |
45 | "Does nothing" , "dummy subcommand leaf" ) { |
46 | SetIsUserCommand(true); |
47 | } |
48 | |
49 | protected: |
50 | void DoExecute(Args &command, CommandReturnObject &result) override { |
51 | result.SetStatus(eReturnStatusSuccessFinishResult); |
52 | result.AppendMessage(in_string: "I did nothing" ); |
53 | } |
54 | }; |
55 | |
56 | class CommandObjectMultiwordSubDummy : public CommandObjectMultiword { |
57 | public: |
58 | CommandObjectMultiwordSubDummy(CommandInterpreter &interpreter) |
59 | : CommandObjectMultiword(interpreter, "dummy subcommand" , "Does nothing" , |
60 | "dummy subcommand" ) { |
61 | SetIsUserCommand(true); |
62 | LoadSubCommand(cmd_name: "leaf" , command_obj: CommandObjectSP(new CommandObjectLeaf(interpreter))); |
63 | } |
64 | |
65 | ~CommandObjectMultiwordSubDummy() override = default; |
66 | }; |
67 | |
68 | class CommandObjectMultiwordDummy : public CommandObjectMultiword { |
69 | public: |
70 | CommandObjectMultiwordDummy(CommandInterpreter &interpreter) |
71 | : CommandObjectMultiword(interpreter, "dummy" , "Does nothing" , "dummy" ) { |
72 | SetIsUserCommand(true); |
73 | LoadSubCommand( |
74 | cmd_name: "subcommand" , |
75 | command_obj: CommandObjectSP(new CommandObjectMultiwordSubDummy(interpreter))); |
76 | } |
77 | |
78 | ~CommandObjectMultiwordDummy() override = default; |
79 | }; |
80 | |
81 | // Pass in the command path to args. If success is true, we make sure the MWC |
82 | // returned matches the test string. If success is false, we make sure the |
83 | // lookup error matches test_str. |
84 | void RunTest(CommandInterpreter &interp, const char *args, bool is_leaf, |
85 | bool success, const char *test_str) { |
86 | CommandObjectMultiword *multi_word_cmd = nullptr; |
87 | Args test_args(args); |
88 | Status error; |
89 | multi_word_cmd = |
90 | interp.VerifyUserMultiwordCmdPath(path&: test_args, leaf_is_command: is_leaf, result&: error); |
91 | if (success) { |
92 | ASSERT_NE(multi_word_cmd, nullptr); |
93 | ASSERT_TRUE(error.Success()); |
94 | ASSERT_STREQ(multi_word_cmd->GetCommandName().str().c_str(), test_str); |
95 | } else { |
96 | ASSERT_EQ(multi_word_cmd, nullptr); |
97 | ASSERT_TRUE(error.Fail()); |
98 | ASSERT_STREQ(error.AsCString(), test_str); |
99 | } |
100 | } |
101 | |
102 | TEST_F(VerifyUserMultiwordCmdPathTest, TestErrors) { |
103 | ArchSpec arch("x86_64-apple-macosx-" ); |
104 | |
105 | Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(force: true, arch: &arch)); |
106 | |
107 | DebuggerSP debugger_sp = Debugger::CreateInstance(); |
108 | ASSERT_TRUE(debugger_sp); |
109 | |
110 | CommandInterpreter &interp = debugger_sp->GetCommandInterpreter(); |
111 | |
112 | Status error; |
113 | bool success; |
114 | bool is_leaf; |
115 | |
116 | // Test that we reject non-user path components: |
117 | success = false; |
118 | is_leaf = true; |
119 | RunTest(interp, args: "process launch" , is_leaf, success, |
120 | test_str: "Path component: 'process' is not a user command" ); |
121 | |
122 | // Test that we reject non-existent commands: |
123 | is_leaf = true; |
124 | success = false; |
125 | RunTest(interp, args: "wewouldnevernameacommandthis subcommand" , is_leaf, success, |
126 | test_str: "Path component: 'wewouldnevernameacommandthis' not found" ); |
127 | |
128 | // Now we have to add a multiword command, and then probe it. |
129 | error = interp.AddUserCommand( |
130 | name: "dummy" , cmd_sp: CommandObjectSP(new CommandObjectMultiwordDummy(interp)), can_replace: true); |
131 | ASSERT_TRUE(error.Success()); |
132 | |
133 | // Now pass the correct path, and make sure we get back the right MWC. |
134 | is_leaf = false; |
135 | success = true; |
136 | RunTest(interp, args: "dummy subcommand" , is_leaf, success, test_str: "dummy subcommand" ); |
137 | |
138 | is_leaf = true; |
139 | RunTest(interp, args: "dummy subcommand" , is_leaf, success, test_str: "dummy" ); |
140 | |
141 | // If you tell us the last node is a leaf, we don't check that. Make sure |
142 | // that is true: |
143 | is_leaf = true; |
144 | success = true; |
145 | RunTest(interp, args: "dummy subcommand leaf" , is_leaf, success, |
146 | test_str: "dummy subcommand" ); |
147 | // But we should fail if we say the last component is a multiword: |
148 | |
149 | is_leaf = false; |
150 | success = false; |
151 | RunTest(interp, args: "dummy subcommand leaf" , is_leaf, success, |
152 | test_str: "Path component: 'leaf' is not a container command" ); |
153 | |
154 | // We should fail if we get the second path component wrong: |
155 | is_leaf = false; |
156 | success = false; |
157 | RunTest(interp, args: "dummy not-subcommand" , is_leaf, success, |
158 | test_str: "Path component: 'not-subcommand' not found" ); |
159 | } |
160 | |