1 /*
2  * Copyright (c) 2023 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 <csignal>
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20 #include "ecmascript/tests/test_helper.h"
21 #include "tooling/test/client_utils/test_list.h"
22 #include "tooling/test/client_utils/test_util.h"
23 
24 namespace panda::ecmascript::tooling::test {
25 using panda::test::TestHelper;
26 static int g_port = 9003;  // 9003: socket port
27 class DebuggerCIntClientTest : public testing::TestWithParam<const char *> {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         GTEST_LOG_(INFO) << "SetUpTestCase";
32         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
33             GTEST_LOG_(ERROR) << "Reset SIGPIPE failed.";
34         }
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         GTEST_LOG_(INFO) << "TearDownCase";
40     }
41 
42     void SetUp() override
43     {
44         g_port += 1;
45         SetCurrentTestName(GetParam());
46         TestHelper::CreateEcmaVMWithScope(instance, thread, scope, false, true);
47         TestUtil::ForkSocketClient(g_port, GetParam());
48         JSNApi::DebugOption debugOption = {DEBUGGER_LIBRARY, true, g_port};
49         JSNApi::StartDebugger(instance, debugOption);
50         if (instance->GetJsDebuggerManager() != nullptr) {
51             instance->GetJsDebuggerManager()->DisableObjectHashDisplay();
52         }
53     }
54 
55     void TearDown() override
56     {
57         std::this_thread::sleep_for(std::chrono::microseconds(500000)); // 500000: 500ms
58         JSNApi::StopDebugger(instance);
59         TestHelper::DestroyEcmaVMWithScope(instance, scope);
60     }
61 
62     EcmaVM *instance {nullptr};
63     EcmaHandleScope *scope {nullptr};
64     JSThread *thread {nullptr};
65 };
66 
HWTEST_P_L0(DebuggerCIntClientTest, DebuggerSuite)67 HWTEST_P_L0(DebuggerCIntClientTest, DebuggerSuite)
68 {
69     std::string testName = GetCurrentTestName();
70     std::cout << "Running " << testName << std::endl;
71     ASSERT_NE(instance, nullptr);
72     auto [pandaFile, entryPoint] = GetTestEntryPoint(testName);
73     auto res = JSNApi::Execute(instance, pandaFile.c_str(), entryPoint.c_str());
74     ASSERT_TRUE(res);
75 }
76 
77 INSTANTIATE_TEST_SUITE_P(DebuggerCIntClientAbcTest, DebuggerCIntClientTest, testing::ValuesIn(GetTestList()));
78 }  // namespace panda::ecmascript::tooling::test
79