• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <securec.h>
18 #include <sys/time.h>
19 
20 #include "auth_common.h"
21 #include "auth_connection.h"
22 #include "auth_interface.h"
23 #include "auth_manager.h"
24 #include "auth_sessionkey.h"
25 #include "message_handler.h"
26 #include "softbus_adapter_mem.h"
27 #include "softbus_errcode.h"
28 #include "softbus_json_utils.h"
29 #include "softbus_log.h"
30 #include "softbus_server_frame.h"
31 
32 namespace OHOS {
33 using namespace testing::ext;
34 constexpr char SERVER_MAC[BT_MAC_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
35 constexpr char CLIENT_MAC[BT_MAC_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x07};
36 constexpr uint8_t ENCRYPT_DATA[] = "auth_encrypt_data_test.";
37 constexpr uint8_t SESSION_KEY[32] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
38     'h', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
39 constexpr uint32_t SESSION_KEY_LEN = 32; // must be 16, 24 or 32, it is limited by the third party lib mbedtls
40 constexpr uint64_t DEFAULT_SEQ = 123456789;
41 constexpr char UUID[] = "B4FE52C465D0A53D5AECE2ED9498F28BEA87C8FE3F1581CFFC673425B11F6608";
42 constexpr char UDID[] = "AE3017B79036A7EE19991538BBE303E8826CEB2B5B6CC5130BD74C83BF63137F";
43 
44 class AuthTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp();
49     void TearDown();
50 };
51 
SetUpTestCase()52 void AuthTest::SetUpTestCase()
53 {
54 }
55 
TearDownTestCase()56 void AuthTest::TearDownTestCase()
57 {
58 }
59 
SetUp()60 void AuthTest::SetUp()
61 {
62     LOG_INFO("AuthTest start.");
63 }
64 
TearDown()65 void AuthTest::TearDown()
66 {
67 }
68 
OnKeyGenerated(int64_t authId,ConnectOption * option,SoftBusVersion peerVersion)69 void OnKeyGenerated(int64_t authId, ConnectOption *option, SoftBusVersion peerVersion)
70 {
71     (void)option;
72     (void)peerVersion;
73     LOG_INFO("%s: authId=%lld", __func__, authId);
74 }
75 
OnDeviceVerifyFail(int64_t authId,int32_t reason)76 void OnDeviceVerifyFail(int64_t authId, int32_t reason)
77 {
78     LOG_INFO("%s: authId=%lld, reason=%d", __func__, authId, reason);
79 }
80 
OnRecvSyncDeviceInfo(int64_t authId,AuthSideFlag side,const char * peerUdid,uint8_t * data,uint32_t len)81 void OnRecvSyncDeviceInfo(int64_t authId, AuthSideFlag side, const char *peerUdid, uint8_t *data, uint32_t len)
82 {
83     (void)side;
84     (void)peerUdid;
85     (void)data;
86     (void)len;
87     LOG_INFO("%s: authId=%lld", __func__, authId);
88 }
89 
OnDeviceVerifyPass(int64_t authId)90 void OnDeviceVerifyPass(int64_t authId)
91 {
92     LOG_INFO("%s: authId=%lld", __func__, authId);
93 }
94 
OnDeviceNotTrusted(const char * peerUdid)95 void OnDeviceNotTrusted(const char *peerUdid)
96 {
97     (void)peerUdid;
98     LOG_INFO("%s", __func__);
99 }
100 
OnDisconnect(int64_t authId)101 void OnDisconnect(int64_t authId)
102 {
103     LOG_INFO("%s: authId=%lld", __func__, authId);
104 }
105 
106 /*
107 * @tc.name: AUTH_INIT_Test_001
108 * @tc.desc: auth init test
109 * @tc.type: FUNC
110 * @tc.require: AR000FK6J4
111 */
112 HWTEST_F(AuthTest, AUTH_INIT_Test_001, TestSize.Level0)
113 {
114     int32_t ret;
115     ret = LooperInit();
116     EXPECT_TRUE(ret == SOFTBUS_OK);
117     ret = ConnServerInit();
118     EXPECT_TRUE(ret == SOFTBUS_OK);
119     ret = AuthInit();
120     EXPECT_TRUE(ret == SOFTBUS_OK);
121 }
122 
123 /*
124 * @tc.name: AUTH_REG_CB_Test_001
125 * @tc.desc: register auth callback test
126 * @tc.type: FUNC
127 * @tc.require: AR000FK6J4
128 */
129 HWTEST_F(AuthTest, AUTH_REG_CB_Test_001, TestSize.Level0)
130 {
131     int32_t ret;
132     VerifyCallback cb = {0};
133     cb.onKeyGenerated = OnKeyGenerated;
134     cb.onDeviceVerifyFail = OnDeviceVerifyFail;
135     cb.onRecvSyncDeviceInfo = OnRecvSyncDeviceInfo;
136     cb.onDeviceNotTrusted = OnDeviceNotTrusted;
137     cb.onDeviceVerifyPass = OnDeviceVerifyPass;
138     cb.onDisconnect = OnDisconnect;
139 
140     ret = AuthRegCallback(LNN, &cb);
141     EXPECT_TRUE(ret == SOFTBUS_OK);
142 }
143 
144 /*
145 * @tc.name: AUTH_SET_SESSIONKEY_Test_001
146 * @tc.desc: set server side sessionkey test
147 * @tc.type: FUNC
148 * @tc.require: AR000FK6J4
149 */
150 HWTEST_F(AuthTest, AUTH_SET_SESSIONKEY_Test_001, TestSize.Level0)
151 {
152     int32_t ret;
153     NecessaryDevInfo devInfo = {0};
154     devInfo.type = CONNECT_BR;
155     devInfo.side = SERVER_SIDE_FLAG;
156     ret = memcpy_s(devInfo.deviceKey, MAX_DEVICE_KEY_LEN, CLIENT_MAC, BT_MAC_LEN);
157     EXPECT_TRUE(ret == EOK);
158     devInfo.deviceKeyLen = BT_MAC_LEN;
159     devInfo.seq = DEFAULT_SEQ;
160     AuthSetLocalSessionKey(&devInfo, "udid_server", SESSION_KEY, SESSION_KEY_LEN);
161 }
162 
163 /*
164 * @tc.name: AUTH_SET_SESSIONKEY_Test_002
165 * @tc.desc: set client side sessionkey test
166 * @tc.type: FUNC
167 * @tc.require: AR000FK6J4
168 */
169 HWTEST_F(AuthTest, AUTH_SET_SESSIONKEY_Test_002, TestSize.Level0)
170 {
171     int32_t ret;
172     NecessaryDevInfo devInfo;
173     devInfo.type = CONNECT_BR;
174     devInfo.side = CLIENT_SIDE_FLAG;
175     ret = memcpy_s(devInfo.deviceKey, MAX_DEVICE_KEY_LEN, SERVER_MAC, BT_MAC_LEN);
176     EXPECT_TRUE(ret == EOK);
177     devInfo.deviceKeyLen = BT_MAC_LEN;
178     devInfo.seq = DEFAULT_SEQ;
179     AuthSetLocalSessionKey(&devInfo, "udid_client", SESSION_KEY, SESSION_KEY_LEN);
180 }
181 
182 /*
183 * @tc.name: AUTH_ENCRYPT_AND_DECRYPT_Test_001
184 * @tc.desc: auth encrypt and decrypt data test
185 * @tc.type: FUNC
186 * @tc.require: AR000FK6J4
187 */
188 HWTEST_F(AuthTest, AUTH_ENCRYPT_AND_DECRYPT_Test_001, TestSize.Level0)
189 {
190     int32_t ret;
191     AuthSideFlag clientSide = AUTH_SIDE_ANY;
192     ConnectOption option;
193     option.type = CONNECT_BR;
194 
195     ret = memcpy_s(option.info.brOption.brMac, BT_MAC_LEN, SERVER_MAC, BT_MAC_LEN);
196     EXPECT_TRUE(ret == EOK);
197     uint32_t totalLen = strlen((char *)ENCRYPT_DATA) + AuthGetEncryptHeadLen();
198     uint8_t *sendBuf = (uint8_t *)SoftBusMalloc(totalLen);
199     ASSERT_TRUE(sendBuf != NULL);
200     (void)memset_s(sendBuf, totalLen, 0, totalLen);
201     OutBuf outBuf;
202     outBuf.buf = sendBuf;
203     outBuf.bufLen = totalLen;
204     ret= AuthEncrypt(&option, &clientSide, (uint8_t *)ENCRYPT_DATA, strlen((char *)ENCRYPT_DATA), &outBuf);
205     EXPECT_TRUE(ret == SOFTBUS_OK);
206 
207     ConnectOption option1;
208     option1.type = CONNECT_BR;
209 
210     ret = memcpy_s(option1.info.brOption.brMac, BT_MAC_LEN, CLIENT_MAC, BT_MAC_LEN);
211     EXPECT_TRUE(ret == EOK);
212     uint8_t *recvBuf = (uint8_t *)SoftBusMalloc(strlen((char *)ENCRYPT_DATA) + 1);
213     if (recvBuf == NULL) {
214         SoftBusFree(sendBuf);
215     }
216     ASSERT_TRUE(recvBuf != NULL);
217     (void)memset_s(recvBuf, strlen((char *)ENCRYPT_DATA) + 1, 0, strlen((char *)ENCRYPT_DATA) + 1);
218     OutBuf outBuf1;
219     outBuf1.buf = recvBuf;
220     outBuf1.bufLen = strlen((char *)ENCRYPT_DATA) + 1;
221     ret = AuthDecrypt(&option1, SERVER_SIDE_FLAG, outBuf.buf, outBuf.outLen, &outBuf1);
222     EXPECT_TRUE(ret == SOFTBUS_OK);
223     SoftBusFree(sendBuf);
224     SoftBusFree(recvBuf);
225 }
226 
AuthPackDeviceInfo(void)227 static cJSON *AuthPackDeviceInfo(void)
228 {
229     cJSON *msg = cJSON_CreateObject();
230     if (msg == NULL) {
231         return NULL;
232     }
233 
234     EXPECT_TRUE(AddStringToJsonObject(msg, CMD_TAG, CMD_RET_AUTH_INFO));
235     EXPECT_TRUE(AddStringToJsonObject(msg, DATA_TAG, UUID));
236     EXPECT_TRUE(AddStringToJsonObject(msg, TE_DEVICE_ID_TAG, UDID));
237     EXPECT_TRUE(AddNumberToJsonObject(msg, DATA_BUF_SIZE_TAG, PACKET_SIZE));
238     EXPECT_TRUE(AddNumberToJsonObject(msg, SOFTBUS_VERSION_INFO, SOFT_BUS_NEW_V1));
239     return msg;
240 }
241 
242 /*
243 * @tc.name: AUTH_PACK_AND_UNPACK_Test_001
244 * @tc.desc: auth pack and unpack data test
245 * @tc.type: FUNC
246 * @tc.require: AR000FK6J4
247 */
248 HWTEST_F(AuthTest, AUTH_PACK_AND_UNPACK_Test_001, TestSize.Level0)
249 {
250     cJSON *obj = AuthPackDeviceInfo();
251     EXPECT_TRUE(obj != NULL);
252     char *msgStr = cJSON_PrintUnformatted(obj);
253     EXPECT_TRUE(msgStr != NULL);
254     cJSON_Delete(obj);
255     cJSON *msg = cJSON_Parse((char*)msgStr);
256     cJSON_free(msgStr);
257     char cmd[CMD_TAG_LEN] = {0};
258     EXPECT_TRUE(GetJsonObjectStringItem(msg, CMD_TAG, cmd, CMD_TAG_LEN));
259     char uuid[UUID_BUF_LEN] = {0};
260     EXPECT_TRUE(GetJsonObjectStringItem(msg, DATA_TAG, uuid, UUID_BUF_LEN));
261     char deviceUdid[UDID_BUF_LEN] = {0};
262     EXPECT_TRUE(GetJsonObjectStringItem(msg, TE_DEVICE_ID_TAG, deviceUdid, UDID_BUF_LEN));
263     int32_t packetSize;
264     EXPECT_TRUE(GetJsonObjectNumberItem(msg, DATA_BUF_SIZE_TAG, &packetSize));
265     int32_t peerVersion;
266     EXPECT_TRUE(GetJsonObjectNumberItem(msg, SOFTBUS_VERSION_INFO, &peerVersion));
267     cJSON_Delete(msg);
268 }
269 
270 /*
271 * @tc.name: AUTH_DEINIT_Test_001
272 * @tc.desc: auth deinit test
273 * @tc.type: FUNC
274 * @tc.require: AR000FK6J4
275 */
276 HWTEST_F(AuthTest, AUTH_DEINIT_Test_001, TestSize.Level0)
277 {
278     ConnServerDeinit();
279     AuthDeinit();
280     LooperDeinit();
281 }
282 }
283