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  */
HWTEST_F(RegisterTest, CastToRegisterTest001, TestSize.Level1)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  */
HWTEST_F(RegisterTest, CastToRegisterTest002, TestSize.Level1)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  */
HWTEST_F(RegisterTest, CastToRegisterTest003, TestSize.Level1)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         HILOG_FATAL(LOG_CORE, "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  */
HWTEST_F(RegisterTest, CastToRegisterTest005, TestSize.Level1)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     ASSERT_EQ(
142         pthread_create(&tid, nullptr, &HdcConnectRunTest, static_cast<void*>(g_connectManagement.get())),
143         0) << "pthread_create fail!";
144     sleep(3);
145     EXPECT_TRUE(g_threadRunning);
146 
147     /**
148      * @tc.steps: step2. Call  disconnect and delete the  HdcJdwpSimulator
149      * @tc.expected: step2. Disconnect ok, the thread is stopped.
150      */
151     g_hdcJdwpSimulator->Disconnect();
152     sleep(3);
153     delete g_hdcJdwpSimulator;
154     g_hdcJdwpSimulator = nullptr;
155     EXPECT_FALSE(g_threadRunning);
156 }
157 
158 bool g_isCtxPointNull = false;
ConnectJpidTest(void* pkgName)159 void* ConnectJpidTest(void* pkgName)
160 {
161     g_threadRunning = true;
162 
163     std::string name = (char*)pkgName;
164     g_hdcJdwpSimulator = new (std::nothrow) HdcJdwpSimulator(name, name, true, nullptr);
165     if (!g_hdcJdwpSimulator->Connect()) {
166         HILOG_FATAL(LOG_CORE, "Connect fail.");
167     }
168     g_threadRunning = false;
169     return nullptr;
170 }
171 
172 /**
173  * @tc.name: CastToRegisterTest006
174  * @tc.desc: Test cast to Connect.
175  * @tc.type: FUNC
176  */
HWTEST_F(RegisterTest, CastToRegisterTest007, TestSize.Level1)177 HWTEST_F(RegisterTest, CastToRegisterTest007, TestSize.Level1)
178 {
179     /**
180      * @tc.steps: step1. new a ConnectManagement and start the connect thread
181      * @tc.expected: step1. connect ok, the thread is runed.
182      */
183     pthread_t tid;
184     g_hdcJdwpSimulator = nullptr;
185     g_threadRunning = false;
186     string pkgName = "test_pkt_name";
187     ASSERT_EQ(pthread_create(&tid, nullptr, &ConnectJpidTest, (void*)pkgName.c_str()), 0);
188     sleep(3);
189     EXPECT_TRUE(g_threadRunning);
190 
191     /**
192      * @tc.steps: step2. Call  disconnect and delete the  HdcJdwpSimulator
193      * @tc.expected: step2. Disconnect ok, the thread is stopped.
194      */
195     g_hdcJdwpSimulator->Disconnect();
196     delete g_hdcJdwpSimulator;
197     g_hdcJdwpSimulator = nullptr;
198     sleep(2);
199 
200     /**
201      * @tc.steps: step3. new a HdcJdwpSimulator and start the connect thread
202      * @tc.expected: step3. connect failed
203      */
204     pthread_t tid2;
205     g_hdcJdwpSimulator = nullptr;
206     g_threadRunning = false;
207     g_isCtxPointNull = true;
208     ASSERT_EQ(pthread_create(&tid2, nullptr, &ConnectJpidTest, (void*)pkgName.c_str()), 0);
209     sleep(3);
210 
211     g_hdcJdwpSimulator->Disconnect();
212     delete g_hdcJdwpSimulator;
213     g_hdcJdwpSimulator = nullptr;
214 }
215 } // namespace Hdc
216