1 /*
2  * Copyright (c) 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 #include <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 
19 #define private public
20 #define protected public
21 
22 #include "ability_manager_errors.h"
23 #include "connection_data.h"
24 #include "connection_observer_client.h"
25 #include "connection_observer_client_impl.h"
26 #ifdef WITH_DLP
27 #include "dlp_state_data.h"
28 #endif // WITH_DLP
29 #include "hilog_tag_wrapper.h"
30 #include "mock_native_token.h"
31 #include "parcel.h"
32 
33 using namespace testing::ext;
34 using namespace testing;
35 
36 namespace OHOS {
37 namespace AbilityRuntime {
38 namespace {
39 const int32_t TEST_PID = 10001;
40 const int32_t TEST_UID = 10002;
41 const std::string TEST_BUNDLE_NAME = "com.ohos.connnection.test";
42 const std::string TEST_MODULE_NAME = "entry";
43 const std::string TEST_ABILITY_NAME = "TestServiceExtension";
44 const int32_t TEST_CALLER_PID = 10003;
45 const int32_t TEST_CALLER_UID = 10004;
46 const std::string TEST_CALLER_NAME = "test_caller";
47 
48 class MyConnectionObserver : public ConnectionObserver {
49 public:
MyConnectionObserver()50     MyConnectionObserver() {}
~MyConnectionObserver()51     ~MyConnectionObserver() {}
52 
OnExtensionConnected(const ConnectionData & data)53     void OnExtensionConnected(const ConnectionData& data)
54     {
55         isExtensionConnected_ = true;
56     }
57 
OnExtensionDisconnected(const ConnectionData & data)58     void OnExtensionDisconnected(const ConnectionData& data)
59     {
60         isExtensionDisconnected_ = true;
61     }
62 
OnDlpAbilityOpened(const DlpStateData & data)63     void OnDlpAbilityOpened(const DlpStateData& data)
64     {
65         isDlpAbilityOpened_ = true;
66     }
67 
OnDlpAbilityClosed(const DlpStateData & data)68     void OnDlpAbilityClosed(const DlpStateData& data)
69     {
70         isDlpAbilityClosed_ = true;
71     }
72 
OnServiceDied()73     void OnServiceDied()
74     {
75         isServiceDied_ = true;
76     }
77 
IsExtensionConnected() const78     bool IsExtensionConnected() const
79     {
80         return isExtensionConnected_;
81     }
82 
IsExtensionDisconnected() const83     bool IsExtensionDisconnected() const
84     {
85         return isExtensionDisconnected_;
86     }
87 
IsDlpAbilityOpened() const88     bool IsDlpAbilityOpened() const
89     {
90         return isDlpAbilityOpened_;
91     }
92 
IsDlpAbilityClosed() const93     bool IsDlpAbilityClosed() const
94     {
95         return isDlpAbilityClosed_;
96     }
97 
IsServiceDied() const98     bool IsServiceDied() const
99     {
100         return isServiceDied_;
101     }
102 
103 private:
104     bool isExtensionConnected_ = false;
105     bool isExtensionDisconnected_ = false;
106     bool isDlpAbilityOpened_ = false;
107     bool isDlpAbilityClosed_ = false;
108     bool isServiceDied_ = false;
109 };
110 }
111 
112 class ConnectionObserverTest : public testing::Test {
113 public:
114     static void SetUpTestCase(void);
115     static void TearDownTestCase(void);
116     void SetUp();
117     void TearDown();
118 };
119 
SetUpTestCase(void)120 void ConnectionObserverTest::SetUpTestCase(void) {}
121 
TearDownTestCase(void)122 void ConnectionObserverTest::TearDownTestCase(void) {}
123 
SetUp(void)124 void ConnectionObserverTest::SetUp(void) {}
125 
TearDown(void)126 void ConnectionObserverTest::TearDown(void) {}
127 
128 /**
129  * @tc.name: ConnectionObserver_Data_0100
130  * @tc.desc: ConnectionData test.
131  * @tc.type: FUNC
132  * @tc.require: SR000H19UG
133  */
134 HWTEST_F(ConnectionObserverTest, ConnectionObserver_Data_0100, TestSize.Level1)
135 {
136     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0100 start");
137 
138     ConnectionData connectionData;
139     connectionData.extensionPid = TEST_PID;
140     connectionData.extensionUid = TEST_UID;
141     connectionData.extensionBundleName = TEST_BUNDLE_NAME;
142     connectionData.extensionModuleName = TEST_MODULE_NAME;
143     connectionData.extensionName = TEST_ABILITY_NAME;
144     connectionData.extensionType = OHOS::AppExecFwk::ExtensionAbilityType::SERVICE;
145     connectionData.callerPid = TEST_CALLER_PID;
146     connectionData.callerUid = TEST_CALLER_UID;
147     connectionData.callerName = TEST_CALLER_NAME;
148 
149     Parcel data;
150     EXPECT_TRUE(connectionData.Marshalling(data));
151 
152     std::shared_ptr<ConnectionData> readedData(ConnectionData::Unmarshalling(data));
153     EXPECT_TRUE(readedData);
154 
155     EXPECT_EQ(connectionData.extensionPid, readedData->extensionPid);
156     EXPECT_EQ(connectionData.extensionUid, readedData->extensionUid);
157     EXPECT_EQ(connectionData.extensionBundleName, readedData->extensionBundleName);
158     EXPECT_EQ(connectionData.extensionModuleName, readedData->extensionModuleName);
159     EXPECT_EQ(connectionData.extensionName, readedData->extensionName);
160     EXPECT_EQ(connectionData.callerPid, readedData->callerPid);
161     EXPECT_EQ(connectionData.callerUid, readedData->callerUid);
162     EXPECT_EQ(connectionData.callerName, readedData->callerName);
163 
164     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0100 end");
165 }
166 
167 #ifdef WITH_DLP
168 /**
169  * @tc.name: ConnectionObserver_Data_0200
170  * @tc.desc: DlpState data test.
171  * @tc.type: FUNC
172  * @tc.require: AR000H1PGT
173  */
174 HWTEST_F(ConnectionObserverTest, ConnectionObserver_Data_0200, TestSize.Level1)
175 {
176     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0200 start");
177 
178     DlpStateData dlpData;
179     dlpData.targetPid = TEST_PID;
180     dlpData.targetUid = TEST_UID;
181     dlpData.targetBundleName = TEST_BUNDLE_NAME;
182     dlpData.targetModuleName = TEST_MODULE_NAME;
183     dlpData.targetAbilityName = TEST_ABILITY_NAME;
184     dlpData.callerPid = TEST_CALLER_PID;
185     dlpData.callerUid = TEST_CALLER_UID;
186     dlpData.callerName = TEST_CALLER_NAME;
187 
188     Parcel data;
189     EXPECT_TRUE(dlpData.Marshalling(data));
190 
191     std::shared_ptr<DlpStateData> readedData(DlpStateData::Unmarshalling(data));
192     EXPECT_TRUE(readedData);
193 
194     EXPECT_EQ(dlpData.targetPid, readedData->targetPid);
195     EXPECT_EQ(dlpData.targetUid, readedData->targetUid);
196     EXPECT_EQ(dlpData.targetBundleName, readedData->targetBundleName);
197     EXPECT_EQ(dlpData.targetModuleName, readedData->targetModuleName);
198     EXPECT_EQ(dlpData.targetAbilityName, readedData->targetAbilityName);
199     EXPECT_EQ(dlpData.callerUid, readedData->callerUid);
200     EXPECT_EQ(dlpData.callerPid, readedData->callerPid);
201     EXPECT_EQ(dlpData.callerName, readedData->callerName);
202 
203     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0200 end");
204 }
205 
206 /**
207  * @tc.name: ConnectionObserver_Data_0300
208  * @tc.desc: DlpState data test.
209  * @tc.type: FUNC
210  * @tc.require: issueI58213
211  */
212 HWTEST_F(ConnectionObserverTest, ConnectionObserver_Data_0300, TestSize.Level1)
213 {
214     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0300 start");
215 
216     DlpConnectionInfo info;
217     info.dlpUid = TEST_UID;
218     info.openedAbilityCount = 1;
219 
220     Parcel data;
221     EXPECT_TRUE(info.Marshalling(data));
222 
223     std::shared_ptr<DlpConnectionInfo> readedData(DlpConnectionInfo::Unmarshalling(data));
224     EXPECT_TRUE(readedData);
225 
226     EXPECT_EQ(info.dlpUid, readedData->dlpUid);
227     EXPECT_EQ(info.openedAbilityCount, readedData->openedAbilityCount);
228 
229     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Data_0300 end");
230 }
231 
232 /**
233  * @tc.name: ConnectionObserver_Observer_0100
234  * @tc.desc: test observer callback.
235  * @tc.type: FUNC
236  * @tc.require: issueI58213
237  */
238 HWTEST_F(ConnectionObserverTest, ConnectionObserver_Observer_0100, TestSize.Level1)
239 {
240     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Observer_0100 start");
241 
242     auto clientImpl = ConnectionObserverClient::GetInstance().clientImpl_;
243     EXPECT_TRUE(clientImpl);
244 
245     std::vector<DlpConnectionInfo> infos;
246     auto result = ConnectionObserverClient::GetInstance().GetDlpConnectionInfos(infos);
247     EXPECT_EQ(result, AAFwk::CHECK_PERMISSION_FAILED);
248 
249     std::vector<ConnectionData> connectionDatas;
250     result = ConnectionObserverClient::GetInstance().GetConnectionData(connectionDatas);
251     EXPECT_EQ(result, AAFwk::CHECK_PERMISSION_FAILED);
252 
253     std::shared_ptr<MyConnectionObserver> myObserver = std::make_shared<MyConnectionObserver>();
254     clientImpl->userObservers_.emplace(myObserver);
255     ConnectionObserverClient::GetInstance().RegisterObserver(myObserver);
256 
257     ConnectionData connectionData;
258     clientImpl->HandleExtensionConnected(connectionData);
259     EXPECT_TRUE(myObserver->IsExtensionConnected());
260 
261     clientImpl->HandleExtensionDisconnected(connectionData);
262     EXPECT_TRUE(myObserver->IsExtensionDisconnected());
263 
264     DlpStateData dlpData;
265     clientImpl->HandleDlpAbilityOpened(dlpData);
266     EXPECT_TRUE(myObserver->IsDlpAbilityOpened());
267 
268     clientImpl->HandleDlpAbilityClosed(dlpData);
269     EXPECT_TRUE(myObserver->IsDlpAbilityClosed());
270 
271     myObserver->OnServiceDied();
272     ConnectionObserverClient::GetInstance().UnregisterObserver(myObserver);
273 
274     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Observer_0100 end");
275 }
276 
277 /**
278  * @tc.name: ConnectionObserver_Observer_0200
279  * @tc.desc: test observer callback.
280  * @tc.type: FUNC
281  * @tc.require: issueI58213
282  */
283 HWTEST_F(ConnectionObserverTest, ConnectionObserver_Observer_0200, TestSize.Level1)
284 {
285     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Observer_0200 start");
286 
287     auto currentID = GetSelfTokenID();
288     AppExecFwk::MockNativeToken::SetNativeToken();
289 
290     auto clientImpl = ConnectionObserverClient::GetInstance().clientImpl_;
291     EXPECT_TRUE(clientImpl);
292 
293     std::vector<DlpConnectionInfo> infos;
294     auto result = ConnectionObserverClient::GetInstance().GetDlpConnectionInfos(infos);
295     EXPECT_EQ(result, ERR_OK);
296 
297     std::vector<ConnectionData> connectionDatas;
298     result = ConnectionObserverClient::GetInstance().GetConnectionData(connectionDatas);
299     EXPECT_EQ(result, ERR_OK);
300 
301     SetSelfTokenID(currentID);
302     TAG_LOGI(AAFwkTag::TEST, "ConnectionObserver_Observer_0200 end");
303 }
304 #endif // WITH_DLP
305 }  // namespace AbilityRuntime
306 }  // namespace OHOS
307