1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/tests/test_helper.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "debugger_service.h"
19 #include "protocol_handler.h"
20 #include "ecmascript/debugger/js_debugger_manager.h"
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::tooling;
23
24 namespace panda::test {
25 class DebuggerServiceTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
40 }
41
42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
45 }
46
47 protected:
48 EcmaVM *ecmaVm {nullptr};
49 EcmaHandleScope *scope {nullptr};
50 JSThread *thread {nullptr};
51 };
52
HWTEST_F_L0(DebuggerServiceTest, InitializeDebuggerTest)53 HWTEST_F_L0(DebuggerServiceTest, InitializeDebuggerTest)
54 {
55 ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
56 ASSERT_TRUE(handler == nullptr);
57 InitializeDebugger(ecmaVm, nullptr);
58 handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
59 ASSERT_TRUE(handler != nullptr);
60 InitializeDebugger(ecmaVm, nullptr);
61 ASSERT_TRUE(handler != nullptr);
62 EcmaVM *vm = nullptr;
63 InitializeDebugger(vm, nullptr);
64 ASSERT_TRUE(handler != nullptr);
65 }
66
HWTEST_F_L0(DebuggerServiceTest, UninitializeDebuggerTest)67 HWTEST_F_L0(DebuggerServiceTest, UninitializeDebuggerTest)
68 {
69 UninitializeDebugger(ecmaVm);
70 InitializeDebugger(ecmaVm, nullptr);
71 ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
72 ASSERT_TRUE(handler != nullptr);
73 UninitializeDebugger(ecmaVm);
74 handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
75 ASSERT_TRUE(handler == nullptr);
76 EcmaVM *vm = nullptr;
77 UninitializeDebugger(vm);
78 ASSERT_TRUE(handler == nullptr);
79 }
80
HWTEST_F_L0(DebuggerServiceTest, OnMessageTest)81 HWTEST_F_L0(DebuggerServiceTest, OnMessageTest)
82 {
83 ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
84 OnMessage(ecmaVm, "");
85 ASSERT_TRUE(handler == nullptr);
86 std::string result = "";
87 std::function<void(const void*, const std::string &)> callback =
88 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
89 InitializeDebugger(ecmaVm, callback);
90 std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
91 OnMessage(ecmaVm, msg + "");
92 ProcessMessage(ecmaVm);
93 ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
94 EcmaVM *vm = nullptr;
95 OnMessage(vm, "");
96 ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
97 }
98
HWTEST_F_L0(DebuggerServiceTest, WaitForDebuggerTest)99 HWTEST_F_L0(DebuggerServiceTest, WaitForDebuggerTest)
100 {
101 ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
102 WaitForDebugger(ecmaVm);
103 ASSERT_TRUE(handler == nullptr);
104 }
105
HWTEST_F_L0(DebuggerServiceTest, ProcessMessageTest)106 HWTEST_F_L0(DebuggerServiceTest, ProcessMessageTest)
107 {
108 ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
109 ProcessMessage(ecmaVm);
110 ASSERT_TRUE(handler == nullptr);
111 std::string result = "";
112 std::function<void(const void*, const std::string &)> callback =
113 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
114 InitializeDebugger(ecmaVm, callback);
115 std::string msg = std::string() + R"({"id":0,"method":"Tracing.end","params":{}})";
116 OnMessage(ecmaVm, msg + "");
117 ProcessMessage(ecmaVm);
118 ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
119 EcmaVM *vm = nullptr;
120 ProcessMessage(vm);
121 ASSERT_TRUE(result.find("Stop is failure") != std::string::npos);
122 }
123
HWTEST_F_L0(DebuggerServiceTest, GetDispatchStatusTest)124 HWTEST_F_L0(DebuggerServiceTest, GetDispatchStatusTest)
125 {
126 ProtocolHandler::DispatchStatus status = ProtocolHandler::DispatchStatus(GetDispatchStatus(ecmaVm));
127 ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::UNKNOWN);
128 std::string result = "";
129 std::function<void(const void*, const std::string &)> callback =
130 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
131 InitializeDebugger(ecmaVm, callback);
132 status = ProtocolHandler::DispatchStatus(GetDispatchStatus(ecmaVm));
133 ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::DISPATCHED);
134 EcmaVM *vm = nullptr;
135 int32_t GetDispatchStatusTest = GetDispatchStatus(vm);
136 ASSERT_TRUE(GetDispatchStatusTest == 0);
137 }
138 } // namespace panda::test