• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <sys/time.h>
20 
21 #include "auth_common_mock.h"
22 #include "auth_hichain.c"
23 #include "auth_hichain.h"
24 #include "auth_hichain_adapter.h"
25 #include "auth_log.h"
26 #include "auth_net_ledger_mock.h"
27 #include "lnn_hichain_mock.h"
28 #include "softbus_app_info.h"
29 #include "softbus_error_code.h"
30 #include "softbus_socket.h"
31 
32 namespace OHOS {
33 using namespace testing;
34 using namespace testing::ext;
35 
36 class AuthHichainTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void AuthHichainTest::SetUpTestCase() { }
45 
TearDownTestCase()46 void AuthHichainTest::TearDownTestCase() { }
47 
SetUp()48 void AuthHichainTest::SetUp()
49 {
50     AUTH_LOGI(AUTH_TEST, "AuthHichainTest start");
51 }
52 
TearDown()53 void AuthHichainTest::TearDown() { }
54 
OnDeviceNotTrustedTest(const char * peerUdid,int32_t localUserId)55 void OnDeviceNotTrustedTest(const char *peerUdid, int32_t localUserId)
56 {
57     (void)localUserId;
58     (void)peerUdid;
59 }
60 
OnGroupCreatedTest(const char * groupId,int32_t groupType)61 void OnGroupCreatedTest(const char *groupId, int32_t groupType)
62 {
63     (void)groupId;
64     (void)groupType;
65 }
66 
OnGroupDeletedTest(const char * groupId,int32_t groupType)67 void OnGroupDeletedTest(const char *groupId, int32_t groupType)
68 {
69     (void)groupId;
70     (void)groupType;
71 }
72 
OnDeviceBound(const char * udid,const char * groupInfo)73 void OnDeviceBound(const char *udid, const char *groupInfo)
74 {
75     (void)udid;
76     (void)groupInfo;
77 }
78 /*
79  * @tc.name: ON_DEVICE_NOT_TRUSTED_TEST_001
80  * @tc.desc: on device not trusted test
81  * @tc.type: FUNC
82  * @tc.require:
83  */
84 HWTEST_F(AuthHichainTest, ON_DEVICE_NOT_TRUSTED_TEST_001, TestSize.Level1)
85 {
86     const char *groupInfo = "testdata";
87     const char *groupInfoStr = "{\"groupId\":\"1111\", \"groupType\":1}";
88     const char *udid = "000";
89     GroupInfo info;
90     AuthNetLedgertInterfaceMock ledgermock;
91     EXPECT_CALL(ledgermock, GetJsonObjectStringItem).WillOnce(Return(false)).WillRepeatedly(Return(true));
92     OnGroupCreated(nullptr);
93     OnGroupCreated(groupInfo);
94     g_dataChangeListener.onGroupCreated = nullptr;
95     OnGroupCreated(groupInfoStr);
96     g_dataChangeListener.onGroupCreated = OnGroupCreatedTest;
97     OnGroupCreated(groupInfoStr);
98 
99     OnGroupDeleted(nullptr);
100     OnGroupDeleted(groupInfo);
101     g_dataChangeListener.onGroupDeleted = nullptr;
102     OnGroupDeleted(groupInfoStr);
103     g_dataChangeListener.onGroupDeleted = OnGroupDeletedTest;
104     OnGroupDeleted(groupInfoStr);
105 
106     OnDeviceNotTrusted(nullptr);
107     g_dataChangeListener.onDeviceNotTrusted = nullptr;
108     OnDeviceNotTrusted(udid);
109     g_dataChangeListener.onDeviceNotTrusted = OnDeviceNotTrustedTest;
110     OnDeviceNotTrusted(udid);
111 
112     (void)memset_s(&info, sizeof(GroupInfo), 0, sizeof(GroupInfo));
113     int32_t ret = ParseGroupInfo(nullptr, &info);
114     EXPECT_TRUE(ret != SOFTBUS_OK);
115     ret = ParseGroupInfo(groupInfoStr, &info);
116     EXPECT_TRUE(ret == SOFTBUS_OK);
117     ret = ParseGroupInfo(groupInfoStr, &info);
118     EXPECT_TRUE(ret == SOFTBUS_OK);
119 
120     int64_t authSeq = 0;
121     uint8_t sessionKey[SESSION_KEY_LENGTH] = { 0 };
122     OnSessionKeyReturned(authSeq, nullptr, SESSION_KEY_LENGTH + 1);
123     OnSessionKeyReturned(authSeq, nullptr, SESSION_KEY_LENGTH);
124     OnSessionKeyReturned(authSeq, sessionKey, SESSION_KEY_LENGTH + 1);
125 
126     uint32_t softbusErrCode = 0;
127     GetSoftbusHichainAuthErrorCode(HICHAIN_DAS_ERRCODE_MIN, &softbusErrCode);
128     GetSoftbusHichainAuthErrorCode(0, &softbusErrCode);
129 }
130 
131 /*
132  * @tc.name: ON_REQUEST_TEST_001
133  * @tc.desc: on request test
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(AuthHichainTest, ON_REQUEST_TEST_001, TestSize.Level1)
138 {
139     int64_t authSeq = 0;
140     int32_t operationCode = 0;
141     const char *reqParams = "testdata";
142 
143     char *msgStr = OnRequest(authSeq, operationCode, reqParams);
144     EXPECT_TRUE(msgStr == nullptr);
145 
146     const char *udid = "111";
147     const char *groupInfo = "{\"groupId\":\"1111\", \"groupType\":1}";
148     OnDeviceBound(udid, nullptr);
149     OnDeviceBound(nullptr, groupInfo);
150     OnDeviceBound(nullptr, nullptr);
151     g_dataChangeListener.onDeviceBound = OnDeviceBound;
152     OnDeviceBound(udid, groupInfo);
153 
154     DfxRecordLnnExchangekeyEnd(authSeq, SOFTBUS_OK);
155     DfxRecordLnnEndHichainEnd(authSeq, SOFTBUS_OK);
156 }
157 
158 /*
159  * @tc.name: IS_POTENTIAL_TRUSTED_DEVICE_TEST_001
160  * @tc.desc: is potential trusted device test
161  * @tc.type: FUNC
162  * @tc.require:
163  */
164 HWTEST_F(AuthHichainTest, IS_POTENTIAL_TRUSTED_DEVICE_TEST_001, TestSize.Level1)
165 {
166     TrustedRelationIdType idType = ID_TYPE_UID;
167     const char *deviceId = "test123456";
168     bool isPrecise = false;
169     DeviceGroupManager grounpManager;
170     LnnHichainInterfaceMock hichainMock;
171     grounpManager.regDataChangeListener = LnnHichainInterfaceMock::InvokeDataChangeListener;
172     grounpManager.unRegDataChangeListener = LnnHichainInterfaceMock::ActionofunRegDataChangeListener;
173     grounpManager.getRelatedGroups = LnnHichainInterfaceMock::getRelatedGroups;
174     grounpManager.destroyInfo = LnnHichainInterfaceMock::destroyInfo;
175     EXPECT_CALL(hichainMock, GetGmInstance).WillOnce(Return(nullptr)).WillRepeatedly(Return(&grounpManager));
176     AuthNetLedgertInterfaceMock ledgermock;
177     EXPECT_CALL(ledgermock, LnnGetLocalStrInfo)
178         .WillOnce(Return(SOFTBUS_NETWORK_NOT_FOUND))
179         .WillRepeatedly(Return(SOFTBUS_OK));
180     bool ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
181     EXPECT_TRUE(ret == false);
182     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
183     EXPECT_TRUE(ret == false);
184     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, true);
185     EXPECT_TRUE(ret == false);
186     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, true);
187     EXPECT_TRUE(ret == false);
188     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, true);
189     EXPECT_TRUE(ret == false);
190 }
191 
192 /*
193  * @tc.name: IS_POTENTIAL_TRUSTED_DEVICE_TEST_002
194  * @tc.desc: is potential trusted device test
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(AuthHichainTest, IS_POTENTIAL_TRUSTED_DEVICE_TEST_002, TestSize.Level1)
199 {
200     TrustedRelationIdType idType = ID_TYPE_UID;
201     const char *deviceId = "1122";
202     bool isPrecise = false;
203     DeviceGroupManager grounpManager;
204     NiceMock<LnnHichainInterfaceMock> hichainMock;
205     grounpManager.regDataChangeListener = LnnHichainInterfaceMock::InvokeDataChangeListener;
206     grounpManager.unRegDataChangeListener = LnnHichainInterfaceMock::ActionofunRegDataChangeListener;
207     grounpManager.getRelatedGroups = LnnHichainInterfaceMock::getRelatedGroups1;
208     grounpManager.destroyInfo = LnnHichainInterfaceMock::destroyInfo;
209     grounpManager.getTrustedDevices = LnnHichainInterfaceMock::getTrustedDevices;
210     EXPECT_CALL(hichainMock, GetGmInstance).WillRepeatedly(Return(&grounpManager));
211     NiceMock<AuthNetLedgertInterfaceMock> ledgermock;
212     EXPECT_CALL(ledgermock, LnnGetLocalStrInfo).WillRepeatedly(Return(SOFTBUS_OK));
213     EXPECT_CALL(ledgermock, GetJsonObjectStringItem).WillOnce(Return(false)).WillRepeatedly(Return(true));
214     bool ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, true);
215     EXPECT_TRUE(ret == false);
216     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, true);
217     EXPECT_TRUE(ret == false);
218     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
219     EXPECT_TRUE(ret == false);
220     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
221     EXPECT_TRUE(ret == false);
222     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
223     EXPECT_TRUE(ret == false);
224     grounpManager.getTrustedDevices = LnnHichainInterfaceMock::getTrustedDevices1;
225     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
226     EXPECT_TRUE(ret == false);
227     ret = IsPotentialTrustedDevice(idType, deviceId, isPrecise, false);
228     EXPECT_TRUE(ret == false);
229 }
230 
231 /*
232  * @tc.name: HI_CHAIN_GET_JOINED_GROUPS_TEST_001
233  * @tc.desc: hichain get joined groups test
234  * @tc.type: FUNC
235  * @tc.require:
236  */
237 HWTEST_F(AuthHichainTest, HI_CHAIN_GET_JOINED_GROUPS_TEST_001, TestSize.Level1)
238 {
239     NiceMock<LnnHichainInterfaceMock> hichainMock;
240     DeviceGroupManager grounpManager;
241     grounpManager.regDataChangeListener = LnnHichainInterfaceMock::InvokeDataChangeListener;
242     grounpManager.unRegDataChangeListener = LnnHichainInterfaceMock::ActionofunRegDataChangeListener;
243     grounpManager.getJoinedGroups = LnnHichainInterfaceMock::InvokeGetJoinedGroups1;
244     EXPECT_CALL(hichainMock, GetGmInstance).WillRepeatedly(Return(&grounpManager));
245     int32_t groupType = 0;
246     uint32_t ret = HichainGetJoinedGroups(groupType);
247     EXPECT_TRUE(ret == 1);
248     groupType = 99;
249     grounpManager.getJoinedGroups = LnnHichainInterfaceMock::InvokeGetJoinedGroups2;
250     ret = HichainGetJoinedGroups(groupType);
251     EXPECT_TRUE(ret == 0);
252     int64_t authReqId = 32;
253     const char *appId = "111";
254     CancelRequest(authReqId, appId);
255 }
256 
257 /*
258  * @tc.name: IS_SAME_ACCOUNT_GROUP_DEVICE_TEST_001
259  * @tc.desc: is potential trusted device test
260  * @tc.type: FUNC
261  * @tc.require:
262  */
263 HWTEST_F(AuthHichainTest, IS_SAME_ACCOUNT_GROUP_DEVICE_TEST_001, TestSize.Level1)
264 {
265     NiceMock<LnnHichainInterfaceMock> hichainMock;
266     DeviceGroupManager grounpManager;
267     grounpManager.regDataChangeListener = LnnHichainInterfaceMock::InvokeDataChangeListener;
268     grounpManager.unRegDataChangeListener = LnnHichainInterfaceMock::ActionofunRegDataChangeListener;
269     grounpManager.getJoinedGroups = LnnHichainInterfaceMock::InvokeGetJoinedGroups2;
270     grounpManager.destroyInfo = LnnHichainInterfaceMock::destroyInfo;
271     EXPECT_CALL(hichainMock, GetGmInstance).WillOnce(Return(nullptr)).WillRepeatedly(Return(&grounpManager));
272     bool ret = IsSameAccountGroupDevice();
273     EXPECT_TRUE(ret == false);
274     ret = IsSameAccountGroupDevice();
275     EXPECT_TRUE(ret == false);
276     grounpManager.getJoinedGroups = LnnHichainInterfaceMock::InvokeGetJoinedGroups3;
277     EXPECT_CALL(hichainMock, GetGmInstance).WillRepeatedly(Return(&grounpManager));
278     ret = IsSameAccountGroupDevice();
279     EXPECT_TRUE(ret == false);
280     grounpManager.getJoinedGroups = LnnHichainInterfaceMock::InvokeGetJoinedGroups1;
281     EXPECT_CALL(hichainMock, GetGmInstance).WillRepeatedly(Return(&grounpManager));
282     ret = IsSameAccountGroupDevice();
283     EXPECT_TRUE(ret == true);
284 }
285 } // namespace OHOS
286