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 "agent/runtime_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "tooling/protocol_handler.h"
19 
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22 
23 
24 namespace panda::test {
25 class RuntimeImplTest : 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(RuntimeImplTest, DispatcherImplDispatch)53 HWTEST_F_L0(RuntimeImplTest, DispatcherImplDispatch)
54 {
55     std::string result = "";
56     std::function<void(const void*, const std::string &)> callback =
57         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
58     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
59     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
60     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
61     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.test","params":{}})";
62     DispatchRequest request(msg);
63     dispatcherImpl->Dispatch(request);
64     ASSERT_TRUE(result.find("unknown method: test") != std::string::npos);
65 
66     msg = std::string() + R"({"id":0,"method":"Rumtime.enable","params":{}})";
67     DispatchRequest request1(msg);
68     dispatcherImpl->Dispatch(request1);
69     ASSERT_TRUE(result.find("protocols") != std::string::npos);
70     if (channel != nullptr) {
71         delete channel;
72         channel = nullptr;
73     }
74 }
75 
HWTEST_F_L0(RuntimeImplTest, DispatcherImplEnable)76 HWTEST_F_L0(RuntimeImplTest, DispatcherImplEnable)
77 {
78     std::string result = "";
79     std::function<void(const void*, const std::string &)> callback =
80         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
81     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
82     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
83     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
84 
85     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.enable","params":{}})";
86     DispatchRequest request1(msg);
87     dispatcherImpl->Enable(request1);
88     ASSERT_TRUE(result.find("protocols") != std::string::npos);
89     if (channel != nullptr) {
90         delete channel;
91         channel = nullptr;
92     }
93 }
94 
HWTEST_F_L0(RuntimeImplTest, DispatcherImplDisable)95 HWTEST_F_L0(RuntimeImplTest, DispatcherImplDisable)
96 {
97     std::string result = "";
98     std::function<void(const void*, const std::string &)> callback =
99         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
100     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
101     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
102     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
103 
104     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.disable","params":{}})";
105     DispatchRequest request1(msg);
106     dispatcherImpl->Disable(request1);
107     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
108     if (channel != nullptr) {
109         delete channel;
110         channel = nullptr;
111     }
112 }
113 
HWTEST_F_L0(RuntimeImplTest, DispatcherImplRunIfWaitingForDebugger)114 HWTEST_F_L0(RuntimeImplTest, DispatcherImplRunIfWaitingForDebugger)
115 {
116     std::string result = "";
117     std::function<void(const void*, const std::string &)> callback =
118         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
119     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
120     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
121     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
122 
123     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.runIfWaitingForDebugger","params":{}})";
124     DispatchRequest request1(msg);
125     dispatcherImpl->RunIfWaitingForDebugger(request1);
126     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
127     if (channel != nullptr) {
128         delete channel;
129         channel = nullptr;
130     }
131 }
132 
HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetProperties)133 HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetProperties)
134 {
135     std::string result = "";
136     std::function<void(const void*, const std::string &)> callback =
137         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
138     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
139     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
140     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
141 
142     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.getProperties","params":{"objectId":0}})";
143     DispatchRequest request(msg);
144     dispatcherImpl->GetProperties(request);
145     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
146 
147     msg = std::string() + R"({"id":0,"method":"Rumtime.getProperties","params":{"objectId":"0"}})";
148     DispatchRequest request1(msg);
149     dispatcherImpl->GetProperties(request1);
150     ASSERT_TRUE(result.find("Unknown object id") != std::string::npos);
151     if (channel != nullptr) {
152         delete channel;
153         channel = nullptr;
154     }
155 }
156 
HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetHeapUsage)157 HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetHeapUsage)
158 {
159     std::string result = "";
160     std::function<void(const void*, const std::string &)> callback =
161         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
162     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
163     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, channel);
164     auto dispatcherImpl = std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtimeImpl));
165 
166     std::string msg = std::string() + R"({"id":0,"method":"Rumtime.getHeapUsage ","params":{}})";
167     DispatchRequest request(msg);
168     dispatcherImpl->GetHeapUsage(request);
169     ASSERT_TRUE(result.find("usedSize") != std::string::npos);
170     ASSERT_TRUE(result.find("totalSize") != std::string::npos);
171     if (channel != nullptr) {
172         delete channel;
173         channel = nullptr;
174     }
175 }
176 }  // namespace panda::test
177