1 /*
2  * Copyright (c) 2024 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/overlay_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_handler.h"
19 
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22 
23 namespace panda::test {
24 class OverlayImplTest : public testing::Test {
25 public:
SetUpTestCase()26     static void SetUpTestCase()
27     {
28         GTEST_LOG_(INFO) << "SetUpTestCase";
29     }
30 
TearDownTestCase()31     static void TearDownTestCase()
32     {
33         GTEST_LOG_(INFO) << "TearDownCase";
34     }
35 
36     void SetUp() override
37     {
38         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39     }
40 
41     void TearDown() override
42     {
43         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44     }
45 
46 protected:
47     EcmaVM *ecmaVm {nullptr};
48     EcmaHandleScope *scope {nullptr};
49     JSThread *thread {nullptr};
50 };
51 
HWTEST_F_L0(OverlayImplTest, DispatchTest_001)52 HWTEST_F_L0(OverlayImplTest, DispatchTest_001)
53 {
54     std::string outStrForCallbackCheck = "";
55     std::function<void(const void*, const std::string &)> callback =
56         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
57             outStrForCallbackCheck = inStrOfReply;};
58     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
59 
60     auto overlayImpl = std::make_unique<OverlayImpl>();
61     auto dispatcherImpl = std::make_unique<OverlayImpl::DispatcherImpl>(protocolChannel, std::move(overlayImpl));
62     std::string msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
63     DispatchRequest request(msg);
64     dispatcherImpl->Dispatch(request);
65     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
66 }
67 
HWTEST_F_L0(OverlayImplTest, DispatchTest_002)68 HWTEST_F_L0(OverlayImplTest, DispatchTest_002)
69 {
70     std::string outStrForCallbackCheck = "";
71     std::function<void(const void*, const std::string &)> callback =
72         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
73             outStrForCallbackCheck = inStrOfReply;};
74     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
75 
76     auto overlayImpl = std::make_unique<OverlayImpl>();
77     auto dispatcherImpl = std::make_unique<OverlayImpl::DispatcherImpl>(protocolChannel, std::move(overlayImpl));
78     std::string msg = "OverlayImplTestCommand";
79     DispatchRequest request(msg);
80     dispatcherImpl->Dispatch(request);
81     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":-1,"result":{"code":1,"message":"Unknown method: "}})");
82 }
83 }  // namespace panda::test