1 /*
2 * Copyright (C) 2021-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
17 #include <memory>
18 #include <string>
19
20 #include "gtest/gtest.h"
21
22 #define private public
23 #define protected public
24 #include "define_register.h"
25 #include "hdc_connect.h"
26 #include "hdc_jdwp.h"
27 #undef private
28 #undef protected
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace Hdc {
34 extern std::unique_ptr<ConnectManagement> g_connectManagement;
35 class RegisterTest : public testing::Test {};
36
37 /**
38 * @tc.name: CastToRegisterTest001
39 * @tc.desc: Test cast to register.
40 * @tc.type: FUNC
41 */
42 HWTEST_F(RegisterTest, CastToRegisterTest001, TestSize.Level1)
43 {
44 /**
45 * @tc.steps: step1. stop connect before start.
46 * @tc.expected: step1. g_connectManagement is null.
47 */
48 StopConnect();
49 EXPECT_EQ(g_connectManagement, nullptr);
50 }
51
52 /**
53 * @tc.name: CastToRegisterTest002
54 * @tc.desc: Test cast to register.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(RegisterTest, CastToRegisterTest002, TestSize.Level1)
58 {
59 /**
60 * @tc.steps: step1. start connect.
61 * @tc.expected: step1. g_connectManagement is not null and the pktName is right.
62 */
63 StartConnect("", "test_pkt_name", true, nullptr);
64 ASSERT_NE(g_connectManagement, nullptr);
65 EXPECT_EQ(g_connectManagement->GetPkgName(), "test_pkt_name");
66
67 /**
68 * @tc.steps: step2. sleep 3 second.
69 */
70 sleep(3);
71
72 /**
73 # @tc.steps: step3. stop connect.
74 * @tc.expected: step3. g_connectManagement is not null
75 */
76 StopConnect();
77 ASSERT_NE(g_connectManagement, nullptr);
78 }
79
80 /**
81 * @tc.name: CastToRegisterTest003
82 * @tc.desc: Test cast to register.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(RegisterTest, CastToRegisterTest003, TestSize.Level1)
86 {
87 /**
88 * @tc.steps: step1. start connect.
89 * @tc.expected: step1. g_connectManagement is not null and the pktName is right.
90 */
91 StartConnect("", "test_pkt_name", true, nullptr);
92 ASSERT_NE(g_connectManagement, nullptr);
93 EXPECT_EQ(g_connectManagement->GetPkgName(), "test_pkt_name");
94
95 /**
96 * @tc.steps: step2. sleep 3 second.
97 */
98 sleep(3);
99
100 /**
101 # @tc.steps: step3. start connect again
102 * @tc.expected: step3. start fail and g_connectManagement is not null and the pktName is same with first start.
103 */
104 StartConnect("", "test_pkt_name_2", true, nullptr);
105 ASSERT_NE(g_connectManagement, nullptr);
106 EXPECT_EQ(g_connectManagement->GetPkgName(), "test_pkt_name");
107 }
108
109 HdcJdwpSimulator* g_hdcJdwpSimulator = nullptr;
110 bool g_threadRunning = false;
HdcConnectRunTest(void * pkgContent)111 void* HdcConnectRunTest(void* pkgContent)
112 {
113 g_threadRunning = true;
114 std::string pkgName = static_cast<ConnectManagement*>(pkgContent)->GetPkgName();
115 std::string processName = static_cast<ConnectManagement*>(pkgContent)->GetProcessName();
116 bool isDebug = static_cast<ConnectManagement*>(pkgContent)->GetDebug();
117 g_hdcJdwpSimulator = new (std::nothrow) HdcJdwpSimulator(processName, pkgName, isDebug, nullptr);
118 if (!g_hdcJdwpSimulator->Connect()) {
119 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "Connect fail.");
120 g_threadRunning = false;
121 return nullptr;
122 }
123 g_threadRunning = false;
124 return nullptr;
125 }
126
127 /**
128 * @tc.name: CastToRegisterTest005
129 * @tc.desc: Test cast to HdcJdwpSimulator.
130 * @tc.type: FUNC
131 */
132 HWTEST_F(RegisterTest, CastToRegisterTest005, TestSize.Level1)
133 {
134 /**
135 * @tc.steps: step1. new a ConnectManagement and start the connect thread
136 * @tc.expected: step1. connect ok, the thread is runed.
137 */
138 pthread_t tid;
139 g_connectManagement = std::make_unique<ConnectManagement>();
140 g_connectManagement->SetPkgName("test_pkt_name");
141 if (pthread_create(&tid, nullptr, &HdcConnectRunTest, static_cast<void*>(g_connectManagement.get())) != 0) {
142 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "pthread_create fail!");
143 return;
144 }
145 sleep(3);
146 EXPECT_FALSE(g_threadRunning);
147
148 /**
149 * @tc.steps: step2. Call disconnect and delete the HdcJdwpSimulator
150 * @tc.expected: step2. Disconnect ok, the thread is stopped.
151 */
152 g_hdcJdwpSimulator->Disconnect();
153 delete g_hdcJdwpSimulator;
154 g_hdcJdwpSimulator = nullptr;
155 sleep(2);
156 EXPECT_FALSE(g_threadRunning);
157 }
158
159 /**
160 * @tc.name: CastToRegisterTest006
161 * @tc.desc: Test cast to ConnectJpid.
162 * @tc.type: FUNC
163 */
164 HWTEST_F(RegisterTest, CastToRegisterTest006, TestSize.Level1)
165 {
166 /**
167 * @tc.steps: step1. new a ConnectManagement and start the connect thread
168 * @tc.expected: step1. connect ok, the thread is runed.
169 */
170 auto hdcJdwpSimulator = std::make_unique<HdcJdwpSimulator>("", "test_pkt_name", true, nullptr);
171 auto retFlag = hdcJdwpSimulator->ConnectJpid(hdcJdwpSimulator.get());
172
173 EXPECT_FALSE(retFlag);
174 }
175
176 bool g_isCtxPointNull = false;
ConnectJpidTest(void * pkgName)177 void* ConnectJpidTest(void* pkgName)
178 {
179 g_threadRunning = true;
180
181 std::string name = (char*)pkgName;
182 g_hdcJdwpSimulator = new (std::nothrow) HdcJdwpSimulator(name, name, true, nullptr);
183 if (g_isCtxPointNull) {
184 delete g_hdcJdwpSimulator->ctxPoint_;
185 g_hdcJdwpSimulator->ctxPoint_ = nullptr;
186 } else {
187 g_hdcJdwpSimulator->ctxPoint_->cfd = 1;
188 }
189
190 if (!g_hdcJdwpSimulator->Connect()) {
191 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "Connect fail.");
192 }
193 g_threadRunning = false;
194 return nullptr;
195 }
196
197 /**
198 * @tc.name: CastToRegisterTest006
199 * @tc.desc: Test cast to Connect.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(RegisterTest, CastToRegisterTest007, TestSize.Level1)
203 {
204 /**
205 * @tc.steps: step1. new a ConnectManagement and start the connect thread
206 * @tc.expected: step1. connect ok, the thread is runed.
207 */
208 pthread_t tid;
209 g_hdcJdwpSimulator = nullptr;
210 g_threadRunning = false;
211 string pkgName = "test_pkt_name";
212 ASSERT_EQ(pthread_create(&tid, nullptr, &ConnectJpidTest, (void*)pkgName.c_str()), 0);
213 sleep(3);
214 EXPECT_FALSE(g_threadRunning);
215
216 /**
217 * @tc.steps: step2. Call disconnect and delete the HdcJdwpSimulator
218 * @tc.expected: step2. Disconnect ok, the thread is stopped.
219 */
220 g_hdcJdwpSimulator->Disconnect();
221 delete g_hdcJdwpSimulator;
222 g_hdcJdwpSimulator = nullptr;
223 sleep(2);
224 EXPECT_FALSE(g_threadRunning);
225
226 /**
227 * @tc.steps: step3. new a HdcJdwpSimulator and start the connect thread
228 * @tc.expected: step3. connect failed
229 */
230 pthread_t tid2;
231 g_hdcJdwpSimulator = nullptr;
232 g_threadRunning = false;
233 g_isCtxPointNull = true;
234 ASSERT_EQ(pthread_create(&tid2, nullptr, &ConnectJpidTest, (void*)pkgName.c_str()), 0);
235 sleep(3);
236 EXPECT_FALSE(g_threadRunning);
237
238 g_hdcJdwpSimulator->Disconnect();
239 delete g_hdcJdwpSimulator;
240 g_hdcJdwpSimulator = nullptr;
241 }
242 } // namespace Hdc
243