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 "gtest/gtest.h"
17 #include "inspector/connect_inspector.h"
18 #include "inspector/connect_server.h"
19 #include "websocket/client/websocket_client.h"
20 #include "websocket/server/websocket_server.h"
21
22 using namespace OHOS::ArkCompiler::Toolchain;
23
24 namespace panda::test {
25 class ConnectServerTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "ConnectServerTest::SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "ConnectServerTest::TearDownTestCase";
35 }
36
SetUp()37 void SetUp() override {}
38
TearDown()39 void TearDown() override {}
40
41 #if defined(OHOS_PLATFORM)
42 static constexpr char CONNECTED_MESSAGE_TEST[] = "connected";
43 static constexpr char REQUEST_MESSAGE_TEST[] = "tree";
44 static constexpr char STOPDEBUGGER_MESSAGE_TEST[] = "stopDebugger";
45 static constexpr char OPEN_ARKUI_STATE_PROFILER_TEST[] = "ArkUIStateProfilerOpen";
46 static constexpr char CLOSE_ARKUI_STATE_PROFILER_TEST[] = "ArkUIStateProfilerClose";
47 static constexpr char START_RECORD_MESSAGE_TEST[] = "rsNodeStartRecord";
48 static constexpr char STOP_RECORD_MESSAGE_TEST[] = "rsNodeStopRecord";
49 static constexpr char ARKUI_MESSAGE[] = "{\"method\": \"ArkUI.test\"}";
50 static constexpr char WMS_MESSAGE[] = "{\"method\": \"WMS.test\"}";
51
52 static constexpr char HELLO_INSPECTOR_CLIENT[] = "hello inspector client";
53 static constexpr char INSPECTOR_SERVER_OK[] = "inspector server ok";
54 static constexpr char INSPECTOR_RUN[] = "inspector run";
55 static constexpr char INSPECTOR_QUIT[] = "inspector quit";
56 static constexpr int32_t WAIT_TIME = 2;
57 #endif
58 };
59
60 bool g_profilerFlag = false;
61 bool g_connectFlag = false;
62 int32_t g_createInfoId = 0;
63 int32_t g_instanceId = 1;
64 std::string g_arkUIMsg = "";
65 std::string g_wMSMsg = "";
66
CallbackInit()67 void CallbackInit()
68 {
69 auto profilerCb = [](bool flag) -> void {
70 g_profilerFlag = flag;
71 };
72 SetProfilerCallback(profilerCb);
73
74 auto connectCb = [](bool flag) -> void {
75 g_connectFlag = flag;
76 };
77 SetConnectCallback(connectCb);
78
79 auto debugModeCb = []() -> void {
80 GTEST_LOG_(INFO) << "Execute DebugModeCallBack.";
81 };
82 SetDebugModeCallBack(debugModeCb);
83 auto createInfoCb = [](int32_t id) -> void {
84 g_createInfoId = id;
85 };
86 SetSwitchCallBack(createInfoCb, g_instanceId);
87
88 auto startRecordFunc = []() -> void {};
89 auto stopRecordFunc = []() -> void {};
90 SetRecordCallback(startRecordFunc, stopRecordFunc);
91
92 auto arkUICb = [](const char *msg) -> void {
93 g_arkUIMsg = msg;
94 };
95 SetArkUICallback(arkUICb);
96
97 auto wMSCb = [](const char *msg) -> void {
98 g_wMSMsg = msg;
99 };
100 SetWMSCallback(wMSCb);
101
102 GTEST_LOG_(INFO) << "ConnectServerTest::CallbackInit finished";
103 }
104
105 HWTEST_F(ConnectServerTest, InspectorBasicTest, testing::ext::TestSize.Level0)
106 {
107 ASSERT_TRUE(WaitForConnection());
108 }
109
110 HWTEST_F(ConnectServerTest, InspectorConnectTest, testing::ext::TestSize.Level0)
111 {
112 CallbackInit();
113 #if defined(OHOS_PLATFORM)
114 int appPid = getprocpid();
115 int oldProcessfd = -2;
116 ASSERT_TRUE(StartServerForSocketPair(oldProcessfd));
117 // test ConnectServer is not nullptr
118 ASSERT_TRUE(StartServerForSocketPair(oldProcessfd));
119 StoreMessage(g_instanceId, HELLO_INSPECTOR_CLIENT);
120 // Waiting for the ConnectServer to start running
121 sleep(WAIT_TIME);
122 pid_t pid = fork();
123 if (pid == 0) {
124 WebSocketClient clientSocket;
125 ASSERT_TRUE(clientSocket.InitToolchainWebSocketForSockName(std::to_string(appPid)));
126 ASSERT_TRUE(clientSocket.ClientSendWSUpgradeReq());
127 ASSERT_TRUE(clientSocket.ClientRecvWSUpgradeRsp());
128 EXPECT_TRUE(clientSocket.SendReply(CONNECTED_MESSAGE_TEST));
129 EXPECT_TRUE(clientSocket.SendReply(OPEN_ARKUI_STATE_PROFILER_TEST));
130 EXPECT_TRUE(clientSocket.SendReply(REQUEST_MESSAGE_TEST));
131 EXPECT_TRUE(clientSocket.SendReply(START_RECORD_MESSAGE_TEST));
132 EXPECT_TRUE(clientSocket.SendReply(STOP_RECORD_MESSAGE_TEST));
133 EXPECT_TRUE(clientSocket.SendReply(ARKUI_MESSAGE));
134 EXPECT_TRUE(clientSocket.SendReply(WMS_MESSAGE));
135
136 EXPECT_STREQ(clientSocket.Decode().c_str(), HELLO_INSPECTOR_CLIENT);
137 std::string recv = clientSocket.Decode();
138 EXPECT_STREQ(recv.c_str(), INSPECTOR_SERVER_OK);
139 if (strcmp(recv.c_str(), INSPECTOR_SERVER_OK) == 0) {
140 EXPECT_TRUE(clientSocket.SendReply(STOPDEBUGGER_MESSAGE_TEST));
141 EXPECT_TRUE(clientSocket.SendReply(CLOSE_ARKUI_STATE_PROFILER_TEST));
142 }
143
144 sleep(WAIT_TIME * 2);
145 clientSocket.Close();
146 } else if (pid > 0) {
147 // Waiting for executing the message instruction sent by the client
148 sleep(WAIT_TIME);
149 ASSERT_TRUE(g_profilerFlag);
150 ASSERT_EQ(g_createInfoId, g_instanceId);
151 ASSERT_TRUE(g_connectFlag);
152 EXPECT_STREQ(g_arkUIMsg.c_str(), ARKUI_MESSAGE);
153 EXPECT_STREQ(g_wMSMsg.c_str(), WMS_MESSAGE);
154 ASSERT_FALSE(WaitForConnection());
155
156 SendMessage(INSPECTOR_SERVER_OK);
157
158 // Waiting for executing the message instruction sent by the client
159 sleep(WAIT_TIME);
160 ASSERT_FALSE(g_profilerFlag);
161 ASSERT_FALSE(g_connectFlag);
162 ASSERT_TRUE(WaitForConnection());
163
164 StopServer("InspectorConnectTest");
165 }
166 else {
167 std::cerr << "InspectorConnectTest::fork failed" << std::endl;
168 }
169 RemoveMessage(g_instanceId);
170 #endif
171 }
172
173 } // namespace panda::test