From d8fe78e3932ca5dde6b6ab3372e5a0bc2f5342cc Mon Sep 17 00:00:00 2001 From: RichardHuang9527 Date: Fri, 14 Jun 2024 11:13:44 +0800 Subject: [PATCH] fix: authentication code optimization Signed-off-by: RichardHuang9527 --- .../unittest/auth_session_fsm_test.cpp | 579 +++---- .../unittest/auth_session_key_test.cpp | 468 +++--- .../unittest/auth_session_message_test.cpp | 1448 +++++++++-------- 3 files changed, 1249 insertions(+), 1246 deletions(-) diff --git a/tests/core/authentication/unittest/auth_session_fsm_test.cpp b/tests/core/authentication/unittest/auth_session_fsm_test.cpp index 51de5526d..7b24a692f 100644 --- a/tests/core/authentication/unittest/auth_session_fsm_test.cpp +++ b/tests/core/authentication/unittest/auth_session_fsm_test.cpp @@ -1,289 +1,290 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include "auth_request.h" -#include "auth_session_fsm.h" -#include "auth_session_fsm.c" -#include "ble_mock.h" -#include "softbus_adapter_mem.h" - -namespace OHOS { -using namespace testing; -using namespace testing::ext; -constexpr int64_t AUTH_SEQ = 1; -constexpr int64_t AUTH_SEQ_1 = 2; -constexpr uint64_t CONN_ID = 10; -constexpr uint64_t CONN_ID_1 = 11; -constexpr int32_t DEVICE_ID_HASH_LEN = 9; -constexpr uint32_t REQUEST_ID = 1000; -constexpr uint32_t REQUEST_ID_1 = 1001; -constexpr int32_t TMP_DATA_LEN = 10; -constexpr char UDID_TEST[UDID_BUF_LEN] = "123456789udidtest"; -constexpr char INVALID_UDID_TEST[UDID_BUF_LEN] = "nullptr"; -constexpr char BR_MAC[BT_MAC_LEN] = "00:15:5d:de:d4:23"; -constexpr char BLE_MAC[BT_MAC_LEN] = "00:15:5d:de:d4:23"; -constexpr uint8_t DEVICE_ID_HASH[UDID_HASH_LEN] = "123456789"; -constexpr uint8_t TMP_IN_DATA[TMP_DATA_LEN] = "tmpInData"; - -class AuthSessionFsmTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void AuthSessionFsmTest::SetUpTestCase() -{ - GTEST_LOG_(INFO) << "AuthSessionFsmTest start"; - AuthCommonInit(); -} - -void AuthSessionFsmTest::TearDownTestCase() -{ - AuthCommonDeinit(); - GTEST_LOG_(INFO) << "AuthSessionFsmTest end"; -} - -void AuthSessionFsmTest::SetUp() {} - -void AuthSessionFsmTest::TearDown() {} - -/* - * @tc.name: TRANSLATE_TO_AUTH_FSM_TEST_001 - * @tc.desc: TranslateToAuthFsm test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, TRANSLATE_TO_AUTH_FSM_TEST_001, TestSize.Level1) -{ - EXPECT_TRUE(strcmp(FsmMsgTypeToStr(-1), "UNKNOWN MSG!!") == EOK); - EXPECT_TRUE(strcmp(FsmMsgTypeToStr(FSM_MSG_UNKNOWN), "UNKNOWN MSG!!") == EOK); - EXPECT_TRUE(strcmp(FsmMsgTypeToStr(FSM_MSG_AUTH_FINISH), "AUTH_FINISH") == EOK); - AuthConnInfo connInfo; - (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); - connInfo.type = AUTH_LINK_TYPE_BR; - ASSERT_TRUE(memcpy_s(connInfo.info.brInfo.brMac, BT_MAC_LEN, BR_MAC, strlen(BR_MAC)) == EOK); - AuthFsm *authFsm = CreateAuthFsm(AUTH_SEQ, REQUEST_ID, CONN_ID, &connInfo, true); - EXPECT_TRUE(authFsm == nullptr); - authFsm = TranslateToAuthFsm(nullptr, FSM_MSG_AUTH_TIMEOUT, nullptr); - EXPECT_TRUE(authFsm == nullptr); - authFsm = TranslateToAuthFsm(&authFsm->fsm, FSM_MSG_AUTH_TIMEOUT, nullptr); - EXPECT_TRUE(authFsm == nullptr); - authFsm = TranslateToAuthFsm(&authFsm->fsm, FSM_MSG_RECV_DEVICE_ID, nullptr); - EXPECT_TRUE(authFsm == nullptr); - AuthFsmDeinitCallback(nullptr); -} - -/* - * @tc.name: PROC_AUTH_FSM_TEST_001 - * @tc.desc: ProcAuthFsm test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, PROC_AUTH_FSM_TEST_001, TestSize.Level1) -{ - ClearAuthRequest(); - AuthRequest request; - (void)memset_s(&request, sizeof(AuthRequest), 0, sizeof(AuthRequest)); - request.authId = REQUEST_ID; - request.type = REQUEST_TYPE_RECONNECT; - EXPECT_TRUE(SoftBusGenerateStrHash(DEVICE_ID_HASH, DEVICE_ID_HASH_LEN, - request.connInfo.info.bleInfo.deviceIdHash) == SOFTBUS_OK); - EXPECT_TRUE(AddAuthRequest(&request) == 0); - AuthConnInfo connInfo; - (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); - connInfo.type = AUTH_LINK_TYPE_BR; - AddUdidInfo(REQUEST_ID, true, &connInfo); - AddUdidInfo(REQUEST_ID, false, &connInfo); - connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; - AddUdidInfo(REQUEST_ID, false, &connInfo); - AuthFsm authFsm; - (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - authFsm.info.connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(ProcAuthFsm(REQUEST_ID_1, true, &authFsm) == SOFTBUS_ERR); - EXPECT_TRUE(ProcAuthFsm(REQUEST_ID, true, &authFsm) == SOFTBUS_ERR); - authFsm.info.connInfo.type = AUTH_LINK_TYPE_WIFI; - EXPECT_TRUE(ProcAuthFsm(REQUEST_ID, true, &authFsm) == SOFTBUS_OK); - authFsm.authSeq = AUTH_SEQ_1; - authFsm.info.isServer = false; - authFsm.info.isSupportFastAuth = false; - authFsm.info.connInfo.type = AUTH_LINK_TYPE_BLE; - CompleteAuthSession(&authFsm, SOFTBUS_OK); - authFsm.info.isSupportFastAuth = true; - CompleteAuthSession(&authFsm, SOFTBUS_OK); -} - -/* - * @tc.name: RECOVERY_DEVICE_KEY_TEST_001 - * @tc.desc: RecoveryFastAuthKey test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, RECOVERY_DEVICE_KEY_TEST_001, TestSize.Level1) -{ - AuthFsm authFsm; - (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - authFsm.info.isNodeInfoReceived = true; - authFsm.info.isCloseAckReceived = true; - HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_NOT_TRUSTED, nullptr); - HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); - authFsm.info.isCloseAckReceived = false; - HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); - authFsm.info.isNodeInfoReceived = false; - HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); - HandleCommonMsg(&authFsm, SOFTBUS_AUTH_INNER_ERR, nullptr); - ASSERT_TRUE(memcpy_s(authFsm.info.udid, UDID_BUF_LEN, UDID_TEST, strlen(UDID_TEST)) == EOK); - authFsm.info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; - EXPECT_TRUE(RecoveryFastAuthKey(&authFsm) == SOFTBUS_ERR); - authFsm.info.connInfo.type = AUTH_LINK_TYPE_WIFI; - EXPECT_TRUE(RecoveryFastAuthKey(&authFsm) == SOFTBUS_ERR); - SyncDevIdStateEnter(nullptr); -} - -/* - * @tc.name: CLIENT_SET_EXCHANGE_ID_TYPE_TEST_001 - * @tc.desc: ClientSetExchangeIdType test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, CLIENT_SET_EXCHANGE_ID_TYPE_TEST_001, TestSize.Level1) -{ - LnnAuditExtra *auditData = reinterpret_cast(SoftBusMalloc(sizeof(LnnAuditExtra))); - EXPECT_TRUE(auditData != nullptr); - AuthSessionInfo info; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - (void)memset_s(auditData, sizeof(LnnAuditExtra), 0, sizeof(LnnAuditExtra)); - AuditReportSetPeerDevInfo(nullptr, &info); - AuditReportSetPeerDevInfo(auditData, nullptr); - info.connInfo.type = AUTH_LINK_TYPE_BR; - ASSERT_TRUE(memcpy_s(info.connInfo.info.brInfo.brMac, BT_MAC_LEN, BR_MAC, strlen(BR_MAC)) == EOK); - AuditReportSetPeerDevInfo(auditData, &info); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - ASSERT_TRUE(memcpy_s(info.connInfo.info.bleInfo.bleMac, BT_MAC_LEN, BLE_MAC, strlen(BLE_MAC)) == EOK); - AuditReportSetPeerDevInfo(auditData, &info); - info.connInfo.type = AUTH_LINK_TYPE_MAX; - AuditReportSetPeerDevInfo(auditData, &info); - AuditReportSetLocalDevInfo(nullptr); - BuildLnnAuditEvent(nullptr, &info, SOFTBUS_OK, SOFTBUS_OK, AUDIT_EVENT_PACKETS_ERROR); - BuildLnnAuditEvent(auditData, nullptr, SOFTBUS_OK, SOFTBUS_OK, AUDIT_EVENT_PACKETS_ERROR); - SoftBusFree(auditData); - AuthFsm authFsm; - (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - authFsm.info.idType = EXCHANGE_FAIL; - EXPECT_TRUE(ClientSetExchangeIdType(&authFsm) == SOFTBUS_ERR); - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - info.isServer = true; - EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_OK); - info.isServer = false; - EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_ENCRYPT_ERR); - info.connInfo.type = AUTH_LINK_TYPE_MAX; - EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_ERR); -} - -/* - * @tc.name: GET_AUTH_FSM_TEST_001 - * @tc.desc: GetAuthFsmByConnId test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, GET_AUTH_FSM_TEST_001, TestSize.Level1) -{ - AuthFsm *authFsm = (AuthFsm *)SoftBusCalloc(sizeof(AuthFsm)); - ASSERT_TRUE(authFsm != nullptr); - (void)memset_s(authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - ASSERT_TRUE(memcpy_s(authFsm->info.udid, UDID_BUF_LEN, UDID_TEST, strlen(UDID_TEST)) == EOK); - authFsm->authSeq = AUTH_SEQ; - authFsm->info.connId = CONN_ID; - authFsm->info.isServer = true; - authFsm->isDead = true; - ListNodeInsert(&g_authFsmList, &authFsm->node); - EXPECT_TRUE(GetAuthFsmByAuthSeq(AUTH_SEQ) == nullptr); - EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID_1, false, false) == nullptr); - EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID, false, false) == nullptr); - EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID, true, false) == nullptr); -} - -/* - * @tc.name: AUTH_SESSION_HANDLE_TEST_001 - * @tc.desc: AuthSessionHandle test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, AUTH_SESSION_HANDLE_TEST_001, TestSize.Level1) -{ - AuthFsm authFsm; - (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - authFsm.info.deviceInfoData = nullptr; - MessagePara para; - (void)memset_s(¶, sizeof(MessagePara), 0, sizeof(MessagePara)); - ASSERT_TRUE(memcpy_s(para.data, TMP_DATA_LEN, TMP_IN_DATA, TMP_DATA_LEN) == EOK); - para.len = TMP_DATA_LEN; - HandleMsgRecvDevInfoEarly(&authFsm, ¶); - authFsm.info.deviceInfoData = reinterpret_cast(SoftBusMalloc(TMP_DATA_LEN)); - EXPECT_TRUE(authFsm.info.deviceInfoData != nullptr); - para.len = 0; - HandleMsgRecvDevInfoEarly(&authFsm, ¶); - TryFinishAuthSession(&authFsm); - authFsm.info.isNodeInfoReceived = true; - TryFinishAuthSession(&authFsm); - authFsm.info.isCloseAckReceived = true; - TryFinishAuthSession(&authFsm); - authFsm.info.isAuthFinished = true; - TryFinishAuthSession(&authFsm); - EXPECT_TRUE(AuthSessionHandleDeviceNotTrusted(INVALID_UDID_TEST) == SOFTBUS_OK); - EXPECT_TRUE(AuthSessionHandleDeviceNotTrusted(UDID_TEST) == SOFTBUS_OK); - EXPECT_TRUE(AuthSessionHandleDeviceDisconnected(CONN_ID_1) == SOFTBUS_OK); - EXPECT_TRUE(AuthSessionHandleDeviceDisconnected(CONN_ID) == SOFTBUS_OK); - AuthSessionFsmExit(); -} - -/* - * @tc.name: HANDLE_CLOSE_ACK_TEST_001 - * @tc.desc: handle close ack base remote info - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionFsmTest, HANDLE_CLOSE_ACK_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info = {0}; - info.nodeInfo.feature = 0xF7CA; - BleMock bleMock; - AuthFsm authFsm; - (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); - - EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_ENABLE)); - int32_t ret = HandleCloseAckMessage(&authFsm, &info); - EXPECT_TRUE(ret == SOFTBUS_ERR); - - EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); - ret = HandleCloseAckMessage(&authFsm, &info); - EXPECT_TRUE(ret == SOFTBUS_ERR); - - EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); - ret = HandleCloseAckMessage(&authFsm, &info); - EXPECT_TRUE(ret == SOFTBUS_ERR); - - info.nodeInfo.feature = 0x1F7CA; - EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); - ret = HandleCloseAckMessage(&authFsm, &info); - EXPECT_TRUE(ret == SOFTBUS_ERR); -} -} // namespace OHOS +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "auth_request.h" +#include "auth_session_fsm.h" +#include "auth_session_fsm.c" +#include "ble_mock.h" +#include "softbus_adapter_mem.h" + +namespace OHOS { +using namespace testing; +using namespace testing::ext; +constexpr int64_t AUTH_SEQ = 1; +constexpr int64_t AUTH_SEQ_1 = 2; +constexpr uint64_t CONN_ID = 10; +constexpr uint64_t CONN_ID_1 = 11; +constexpr int32_t DEVICE_ID_HASH_LEN = 9; +constexpr uint32_t REQUEST_ID = 1000; +constexpr uint32_t REQUEST_ID_1 = 1001; +constexpr int32_t TMP_DATA_LEN = 10; +constexpr char UDID_TEST[UDID_BUF_LEN] = "123456789udidtest"; +constexpr char INVALID_UDID_TEST[UDID_BUF_LEN] = "nullptr"; +constexpr char BR_MAC[BT_MAC_LEN] = "00:15:5d:de:d4:23"; +constexpr char BLE_MAC[BT_MAC_LEN] = "00:15:5d:de:d4:23"; +constexpr uint8_t DEVICE_ID_HASH[UDID_HASH_LEN] = "123456789"; +constexpr uint8_t TMP_IN_DATA[TMP_DATA_LEN] = "tmpInData"; + +class AuthSessionFsmTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AuthSessionFsmTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "AuthSessionFsmTest start"; + AuthCommonInit(); +} + +void AuthSessionFsmTest::TearDownTestCase() +{ + AuthCommonDeinit(); + GTEST_LOG_(INFO) << "AuthSessionFsmTest end"; +} + +void AuthSessionFsmTest::SetUp() {} + +void AuthSessionFsmTest::TearDown() {} + +/* + * @tc.name: TRANSLATE_TO_AUTH_FSM_TEST_001 + * @tc.desc: TranslateToAuthFsm test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, TRANSLATE_TO_AUTH_FSM_TEST_001, TestSize.Level1) +{ + EXPECT_TRUE(strcmp(FsmMsgTypeToStr(-1), "UNKNOWN MSG!!") == EOK); + EXPECT_TRUE(strcmp(FsmMsgTypeToStr(FSM_MSG_UNKNOWN), "UNKNOWN MSG!!") == EOK); + EXPECT_TRUE(strcmp(FsmMsgTypeToStr(FSM_MSG_AUTH_FINISH), "AUTH_FINISH") == EOK); + AuthConnInfo connInfo; + (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); + connInfo.type = AUTH_LINK_TYPE_BR; + ASSERT_TRUE(memcpy_s(connInfo.info.brInfo.brMac, BT_MAC_LEN, BR_MAC, strlen(BR_MAC)) == EOK); + AuthFsm *authFsm = CreateAuthFsm(AUTH_SEQ, REQUEST_ID, CONN_ID, &connInfo, true); + EXPECT_TRUE(authFsm == nullptr); + authFsm = TranslateToAuthFsm(nullptr, FSM_MSG_AUTH_TIMEOUT, nullptr); + EXPECT_TRUE(authFsm == nullptr); + authFsm = TranslateToAuthFsm(&authFsm->fsm, FSM_MSG_AUTH_TIMEOUT, nullptr); + EXPECT_TRUE(authFsm == nullptr); + authFsm = TranslateToAuthFsm(&authFsm->fsm, FSM_MSG_RECV_DEVICE_ID, nullptr); + EXPECT_TRUE(authFsm == nullptr); + AuthFsmDeinitCallback(nullptr); +} + +/* + * @tc.name: PROC_AUTH_FSM_TEST_001 + * @tc.desc: ProcAuthFsm test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, PROC_AUTH_FSM_TEST_001, TestSize.Level1) +{ + ClearAuthRequest(); + AuthRequest request; + (void)memset_s(&request, sizeof(AuthRequest), 0, sizeof(AuthRequest)); + request.authId = REQUEST_ID; + request.type = REQUEST_TYPE_RECONNECT; + EXPECT_TRUE(SoftBusGenerateStrHash(DEVICE_ID_HASH, DEVICE_ID_HASH_LEN, + request.connInfo.info.bleInfo.deviceIdHash) == SOFTBUS_OK); + EXPECT_TRUE(AddAuthRequest(&request) == 0); + AuthConnInfo connInfo; + (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); + connInfo.type = AUTH_LINK_TYPE_BR; + AddUdidInfo(REQUEST_ID, true, &connInfo); + AddUdidInfo(REQUEST_ID, false, &connInfo); + connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; + AddUdidInfo(REQUEST_ID, false, &connInfo); + AuthFsm authFsm; + (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + authFsm.info.connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(ProcAuthFsm(REQUEST_ID_1, true, &authFsm) == SOFTBUS_ERR); + EXPECT_TRUE(ProcAuthFsm(REQUEST_ID, true, &authFsm) == SOFTBUS_ERR); + authFsm.info.connInfo.type = AUTH_LINK_TYPE_WIFI; + EXPECT_TRUE(ProcAuthFsm(REQUEST_ID, true, &authFsm) == SOFTBUS_OK); + authFsm.authSeq = AUTH_SEQ_1; + authFsm.info.isServer = false; + authFsm.info.isSupportFastAuth = false; + authFsm.info.connInfo.type = AUTH_LINK_TYPE_BLE; + CompleteAuthSession(&authFsm, SOFTBUS_OK); + authFsm.info.isSupportFastAuth = true; + CompleteAuthSession(&authFsm, SOFTBUS_OK); +} + +/* + * @tc.name: RECOVERY_DEVICE_KEY_TEST_001 + * @tc.desc: RecoveryFastAuthKey test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, RECOVERY_DEVICE_KEY_TEST_001, TestSize.Level1) +{ + AuthFsm authFsm; + (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + authFsm.info.isNodeInfoReceived = true; + authFsm.info.isCloseAckReceived = true; + HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_NOT_TRUSTED, nullptr); + HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); + authFsm.info.isCloseAckReceived = false; + HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); + authFsm.info.isNodeInfoReceived = false; + HandleCommonMsg(&authFsm, FSM_MSG_DEVICE_DISCONNECTED, nullptr); + HandleCommonMsg(&authFsm, SOFTBUS_AUTH_INNER_ERR, nullptr); + ASSERT_TRUE(memcpy_s(authFsm.info.udid, UDID_BUF_LEN, UDID_TEST, strlen(UDID_TEST)) == EOK); + authFsm.info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; + EXPECT_TRUE(RecoveryFastAuthKey(&authFsm) == SOFTBUS_ERR); + authFsm.info.connInfo.type = AUTH_LINK_TYPE_WIFI; + EXPECT_TRUE(RecoveryFastAuthKey(&authFsm) == SOFTBUS_ERR); + SyncDevIdStateEnter(nullptr); +} + +/* + * @tc.name: CLIENT_SET_EXCHANGE_ID_TYPE_TEST_001 + * @tc.desc: ClientSetExchangeIdType test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, CLIENT_SET_EXCHANGE_ID_TYPE_TEST_001, TestSize.Level1) +{ + LnnAuditExtra *auditData = reinterpret_cast(SoftBusMalloc(sizeof(LnnAuditExtra))); + EXPECT_TRUE(auditData != nullptr); + AuthSessionInfo info; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + (void)memset_s(auditData, sizeof(LnnAuditExtra), 0, sizeof(LnnAuditExtra)); + AuditReportSetPeerDevInfo(nullptr, &info); + AuditReportSetPeerDevInfo(auditData, nullptr); + info.connInfo.type = AUTH_LINK_TYPE_BR; + ASSERT_TRUE(memcpy_s(info.connInfo.info.brInfo.brMac, BT_MAC_LEN, BR_MAC, strlen(BR_MAC)) == EOK); + AuditReportSetPeerDevInfo(auditData, &info); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + ASSERT_TRUE(memcpy_s(info.connInfo.info.bleInfo.bleMac, BT_MAC_LEN, BLE_MAC, strlen(BLE_MAC)) == EOK); + AuditReportSetPeerDevInfo(auditData, &info); + info.connInfo.type = AUTH_LINK_TYPE_MAX; + AuditReportSetPeerDevInfo(auditData, &info); + AuditReportSetLocalDevInfo(nullptr); + BuildLnnAuditEvent(nullptr, &info, SOFTBUS_OK, SOFTBUS_OK, AUDIT_EVENT_PACKETS_ERROR); + BuildLnnAuditEvent(auditData, nullptr, SOFTBUS_OK, SOFTBUS_OK, AUDIT_EVENT_PACKETS_ERROR); + SoftBusFree(auditData); + AuthFsm authFsm; + (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + authFsm.info.idType = EXCHANGE_FAIL; + EXPECT_TRUE(ClientSetExchangeIdType(&authFsm) == SOFTBUS_ERR); + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + info.isServer = true; + EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_OK); + info.isServer = false; + EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_ENCRYPT_ERR); + info.connInfo.type = AUTH_LINK_TYPE_MAX; + EXPECT_TRUE(TrySyncDeviceInfo(AUTH_SEQ_1, &info) == SOFTBUS_ERR); +} + +/* + * @tc.name: GET_AUTH_FSM_TEST_001 + * @tc.desc: GetAuthFsmByConnId test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, GET_AUTH_FSM_TEST_001, TestSize.Level1) +{ + AuthFsm *authFsm = (AuthFsm *)SoftBusCalloc(sizeof(AuthFsm)); + ASSERT_TRUE(authFsm != nullptr); + (void)memset_s(authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + ASSERT_TRUE(memcpy_s(authFsm->info.udid, UDID_BUF_LEN, UDID_TEST, strlen(UDID_TEST)) == EOK); + authFsm->authSeq = AUTH_SEQ; + authFsm->info.connId = CONN_ID; + authFsm->info.isServer = true; + authFsm->isDead = true; + ListNodeInsert(&g_authFsmList, &authFsm->node); + EXPECT_TRUE(GetAuthFsmByAuthSeq(AUTH_SEQ) == nullptr); + EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID_1, false, false) == nullptr); + EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID, false, false) == nullptr); + EXPECT_TRUE(GetAuthFsmByConnId(CONN_ID, true, false) == nullptr); +} + +/* + * @tc.name: AUTH_SESSION_HANDLE_TEST_001 + * @tc.desc: AuthSessionHandle test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, AUTH_SESSION_HANDLE_TEST_001, TestSize.Level1) +{ + AuthFsm authFsm; + (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + authFsm.info.deviceInfoData = nullptr; + MessagePara para; + (void)memset_s(¶, sizeof(MessagePara), 0, sizeof(MessagePara)); + ASSERT_TRUE(memcpy_s(para.data, TMP_DATA_LEN, TMP_IN_DATA, TMP_DATA_LEN) == EOK); + para.len = TMP_DATA_LEN; + HandleMsgRecvDevInfoEarly(&authFsm, ¶); + authFsm.info.deviceInfoData = reinterpret_cast(SoftBusMalloc(TMP_DATA_LEN)); + EXPECT_TRUE(authFsm.info.deviceInfoData != nullptr); + para.len = 0; + HandleMsgRecvDevInfoEarly(&authFsm, ¶); + TryFinishAuthSession(&authFsm); + authFsm.info.isNodeInfoReceived = true; + TryFinishAuthSession(&authFsm); + authFsm.info.isCloseAckReceived = true; + TryFinishAuthSession(&authFsm); + authFsm.info.isAuthFinished = true; + TryFinishAuthSession(&authFsm); + EXPECT_TRUE(AuthSessionHandleDeviceNotTrusted(INVALID_UDID_TEST) == SOFTBUS_OK); + EXPECT_TRUE(AuthSessionHandleDeviceNotTrusted(UDID_TEST) == SOFTBUS_OK); + EXPECT_TRUE(AuthSessionHandleDeviceDisconnected(CONN_ID_1) == SOFTBUS_OK); + EXPECT_TRUE(AuthSessionHandleDeviceDisconnected(CONN_ID) == SOFTBUS_OK); + AuthSessionFsmExit(); +} + +/* + * @tc.name: HANDLE_CLOSE_ACK_TEST_001 + * @tc.desc: handle close ack base remote info + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionFsmTest, HANDLE_CLOSE_ACK_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info = {0}; + info.nodeInfo.feature = 0xF7CA; + BleMock bleMock; + AuthFsm authFsm; + (void)memset_s(&authFsm, sizeof(AuthFsm), 0, sizeof(AuthFsm)); + + EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_ENABLE)); + int32_t ret = HandleCloseAckMessage(&authFsm, &info); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); + ret = HandleCloseAckMessage(&authFsm, &info); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); + ret = HandleCloseAckMessage(&authFsm, &info); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + info.nodeInfo.feature = 0x1F7CA; + EXPECT_CALL(bleMock, SoftBusGetBrState()).WillRepeatedly(Return(BR_DISABLE)); + ret = HandleCloseAckMessage(&authFsm, &info); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} +} // namespace OHOS + diff --git a/tests/core/authentication/unittest/auth_session_key_test.cpp b/tests/core/authentication/unittest/auth_session_key_test.cpp index 93df29a6f..9ab850d73 100644 --- a/tests/core/authentication/unittest/auth_session_key_test.cpp +++ b/tests/core/authentication/unittest/auth_session_key_test.cpp @@ -1,234 +1,234 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include "auth_common.h" -#include "auth_session_key.h" -#include "softbus_adapter_mem.h" - -namespace OHOS { -using namespace testing::ext; -constexpr uint32_t SESSIONKEY_LEN = 32; -constexpr int32_t SESSIONKEY_INDEX = 1; -constexpr int32_t SESSIONKEY_INDEX2 = 2; - -class AuthSessionKeyTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void AuthSessionKeyTest::SetUpTestCase() -{ - AuthCommonInit(); -} - -void AuthSessionKeyTest::TearDownTestCase() -{ - AuthCommonDeinit(); -} - -void AuthSessionKeyTest::SetUp() {} - -void AuthSessionKeyTest::TearDown() {} - -/* - * @tc.name: SESSIONKEY_USE_TIME_TEST_001 - * @tc.desc: sessionkey item useTime test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_001, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_WIFI, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - - uint64_t time = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BLE); - EXPECT_EQ(time, 0); - time = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_WIFI); - EXPECT_NE(time, 0); - DestroySessionKeyList(&clientList); -} - -/* - * @tc.name: SESSIONKEY_USE_TIME_TEST_002 - * @tc.desc: GetLatestSessionKey test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_002, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_WIFI, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); - EXPECT_EQ(ret, SOFTBUS_OK); - - int32_t index = -1; - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BR, &index, &sessionKey); - EXPECT_NE(ret, SOFTBUS_OK); - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); - EXPECT_EQ(ret, SOFTBUS_OK); - EXPECT_TRUE(index == SESSIONKEY_INDEX); - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); - EXPECT_EQ(ret, SOFTBUS_OK); - EXPECT_EQ(index, SESSIONKEY_INDEX2); - DestroySessionKeyList(&clientList); -} - -/* - * @tc.name: SESSIONKEY_USE_TIME_TEST_003 - * @tc.desc: sessionkey item useTime test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_003, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKeyList serverList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - ListInit(&serverList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = AddSessionKey(&serverList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&serverList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_BR); - EXPECT_EQ(ret, SOFTBUS_OK); - - uint64_t clientTime = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BR); - uint64_t serverTime = GetLatestAvailableSessionKeyTime(&serverList, AUTH_LINK_TYPE_BLE); - EXPECT_LE(serverTime, clientTime); - clientTime = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BLE); - EXPECT_GE(serverTime, clientTime); - DestroySessionKeyList(&clientList); - DestroySessionKeyList(&serverList); -} - -/* - * @tc.name: UPDATE_LATEST_USE_TIME_TEST_001 - * @tc.desc: UpdateLatestUseTime test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, UPDATE_LATEST_USE_TIME_TEST_001, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_WIFI); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX2, AUTH_LINK_TYPE_WIFI); - EXPECT_EQ(ret, SOFTBUS_OK); - - int32_t index = 0; - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); - EXPECT_EQ(index, SESSIONKEY_INDEX2); - RemoveSessionkeyByIndex(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_BLE); - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); - EXPECT_EQ(index, SESSIONKEY_INDEX2); - DestroySessionKeyList(&clientList); -} - -/* - * @tc.name: UPDATE_LATEST_USE_TIME_TEST_002 - * @tc.desc: UpdateLatestUseTime test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, UPDATE_LATEST_USE_TIME_TEST_002, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); - EXPECT_EQ(ret, SOFTBUS_OK); - ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX2, AUTH_LINK_TYPE_WIFI); - EXPECT_EQ(ret, SOFTBUS_OK); - SoftBusSleepMs(100); - ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_WIFI); - EXPECT_EQ(ret, SOFTBUS_OK); - - int32_t index = 0; - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); - EXPECT_EQ(index, SESSIONKEY_INDEX); - ClearSessionkeyByAuthLinkType(0, &clientList, AUTH_LINK_TYPE_WIFI); - ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); - EXPECT_EQ(index, SESSIONKEY_INDEX2); - DestroySessionKeyList(&clientList); -} - -/* - * @tc.name: OLD_SESSION_KEY_TEST_001 - * @tc.desc: CheckSessionKeyListHasOldKey and ClearOldKey test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionKeyTest, OLD_SESSION_KEY_TEST_001, TestSize.Level1) -{ - SessionKeyList clientList = { 0 }; - SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; - ListInit(&clientList); - int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, true); - EXPECT_EQ(ret, SOFTBUS_OK); - - EXPECT_EQ(CheckSessionKeyListHasOldKey(&clientList, AUTH_LINK_TYPE_BLE), true); - EXPECT_EQ(ClearOldKey(&clientList, AUTH_LINK_TYPE_BLE), SOFTBUS_OK); - EXPECT_EQ(CheckSessionKeyListHasOldKey(&clientList, AUTH_LINK_TYPE_BLE), false); - DestroySessionKeyList(&clientList); -} -} // namespace OHOS +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "auth_common.h" +#include "auth_session_key.h" +#include "softbus_adapter_mem.h" + +namespace OHOS { +using namespace testing::ext; +constexpr uint32_t SESSIONKEY_LEN = 32; +constexpr int32_t SESSIONKEY_INDEX = 1; +constexpr int32_t SESSIONKEY_INDEX2 = 2; + +class AuthSessionKeyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AuthSessionKeyTest::SetUpTestCase() +{ + AuthCommonInit(); +} + +void AuthSessionKeyTest::TearDownTestCase() +{ + AuthCommonDeinit(); +} + +void AuthSessionKeyTest::SetUp() {} + +void AuthSessionKeyTest::TearDown() {} + +/* + * @tc.name: SESSIONKEY_USE_TIME_TEST_001 + * @tc.desc: sessionkey item useTime test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_001, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_WIFI, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + + uint64_t time = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BLE); + EXPECT_EQ(time, 0); + time = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_WIFI); + EXPECT_NE(time, 0); + DestroySessionKeyList(&clientList); +} + +/* + * @tc.name: SESSIONKEY_USE_TIME_TEST_002 + * @tc.desc: GetLatestSessionKey test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_002, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_WIFI, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); + EXPECT_EQ(ret, SOFTBUS_OK); + + int32_t index = -1; + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BR, &index, &sessionKey); + EXPECT_NE(ret, SOFTBUS_OK); + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); + EXPECT_EQ(ret, SOFTBUS_OK); + EXPECT_TRUE(index == SESSIONKEY_INDEX); + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); + EXPECT_EQ(ret, SOFTBUS_OK); + EXPECT_EQ(index, SESSIONKEY_INDEX2); + DestroySessionKeyList(&clientList); +} + +/* + * @tc.name: SESSIONKEY_USE_TIME_TEST_003 + * @tc.desc: sessionkey item useTime test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, SESSIONKEY_USE_TIME_TEST_003, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKeyList serverList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + ListInit(&serverList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = AddSessionKey(&serverList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&serverList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_BR); + EXPECT_EQ(ret, SOFTBUS_OK); + + uint64_t clientTime = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BR); + uint64_t serverTime = GetLatestAvailableSessionKeyTime(&serverList, AUTH_LINK_TYPE_BLE); + EXPECT_LE(serverTime, clientTime); + clientTime = GetLatestAvailableSessionKeyTime(&clientList, AUTH_LINK_TYPE_BLE); + EXPECT_GE(serverTime, clientTime); + DestroySessionKeyList(&clientList); + DestroySessionKeyList(&serverList); +} + +/* + * @tc.name: UPDATE_LATEST_USE_TIME_TEST_001 + * @tc.desc: UpdateLatestUseTime test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, UPDATE_LATEST_USE_TIME_TEST_001, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_WIFI); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX2, AUTH_LINK_TYPE_WIFI); + EXPECT_EQ(ret, SOFTBUS_OK); + + int32_t index = 0; + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); + EXPECT_EQ(index, SESSIONKEY_INDEX2); + RemoveSessionkeyByIndex(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_BLE); + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); + EXPECT_EQ(index, SESSIONKEY_INDEX2); + DestroySessionKeyList(&clientList); +} + +/* + * @tc.name: UPDATE_LATEST_USE_TIME_TEST_002 + * @tc.desc: UpdateLatestUseTime test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, UPDATE_LATEST_USE_TIME_TEST_002, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = AddSessionKey(&clientList, SESSIONKEY_INDEX2, &sessionKey, AUTH_LINK_TYPE_BLE, false); + EXPECT_EQ(ret, SOFTBUS_OK); + ret = SetSessionKeyAvailable(&clientList, SESSIONKEY_INDEX2); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX2, AUTH_LINK_TYPE_WIFI); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusSleepMs(100); + ret = SetSessionKeyAuthLinkType(&clientList, SESSIONKEY_INDEX, AUTH_LINK_TYPE_WIFI); + EXPECT_EQ(ret, SOFTBUS_OK); + + int32_t index = 0; + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_WIFI, &index, &sessionKey); + EXPECT_EQ(index, SESSIONKEY_INDEX); + ClearSessionkeyByAuthLinkType(0, &clientList, AUTH_LINK_TYPE_WIFI); + ret = GetLatestSessionKey(&clientList, AUTH_LINK_TYPE_BLE, &index, &sessionKey); + EXPECT_EQ(index, SESSIONKEY_INDEX2); + DestroySessionKeyList(&clientList); +} + +/* + * @tc.name: OLD_SESSION_KEY_TEST_001 + * @tc.desc: CheckSessionKeyListHasOldKey and ClearOldKey test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionKeyTest, OLD_SESSION_KEY_TEST_001, TestSize.Level1) +{ + SessionKeyList clientList = { 0 }; + SessionKey sessionKey = { { 0 }, SESSIONKEY_LEN }; + ListInit(&clientList); + int32_t ret = AddSessionKey(&clientList, SESSIONKEY_INDEX, &sessionKey, AUTH_LINK_TYPE_BLE, true); + EXPECT_EQ(ret, SOFTBUS_OK); + EXPECT_EQ(CheckSessionKeyListHasOldKey(&clientList, AUTH_LINK_TYPE_BLE), true); + EXPECT_EQ(ClearOldKey(&clientList, AUTH_LINK_TYPE_BLE), SOFTBUS_OK); + EXPECT_EQ(CheckSessionKeyListHasOldKey(&clientList, AUTH_LINK_TYPE_BLE), false); + DestroySessionKeyList(&clientList); +} +} // namespace OHOS + diff --git a/tests/core/authentication/unittest/auth_session_message_test.cpp b/tests/core/authentication/unittest/auth_session_message_test.cpp index 80158f608..e7e7fe24f 100644 --- a/tests/core/authentication/unittest/auth_session_message_test.cpp +++ b/tests/core/authentication/unittest/auth_session_message_test.cpp @@ -1,723 +1,725 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include "auth_session_json.c" -#include "auth_session_message.h" -#include "auth_session_message.c" -#include "softbus_adapter_json.h" -#include "softbus_errcode.h" - -namespace OHOS { -using namespace testing::ext; -constexpr int32_t PEER_IRK_LEN = 13; -constexpr int32_t PUBLIC_ADDRESS_LEN = 4; -constexpr int32_t DATA_TEST_LEN = 7; -constexpr int32_t CAPABILITY_LEN = 16; -constexpr char DEVICE_KEY[SESSION_KEY_LENGTH] = "11111"; -constexpr char UDID[UDID_BUF_LEN] = "123456789udidtest"; -constexpr char UUID_TEST[UUID_BUF_LEN] = "123456789uuidtest"; -constexpr char INVALID_UDID[UDID_BUF_LEN] = "\0"; -constexpr char NETWORK_ID_TEST[NETWORK_ID_BUF_LEN] = "987654321"; -constexpr uint8_t DATA_TEST[UDID_BUF_LEN] = "1111111"; -constexpr char UNIFIED_NAME[DEVICE_NAME_BUF_LEN] = "unifiedName"; -constexpr char INVALID_UNIFIED_NAME[DEVICE_NAME_BUF_LEN] = "\0"; -constexpr char DEVICE_NAME_TEST[DEVICE_NAME_BUF_LEN] = "deviceName"; -constexpr char KEY_TEST[SESSION_KEY_LENGTH] = "123456keytest"; -constexpr char IV_TEST[BROADCAST_IV_LEN] = "123456ivtest"; -constexpr uint8_t PEER_IRK[LFINDER_IRK_LEN] = "123456irktest"; -constexpr unsigned char PUBLIC_ADDRESS[LFINDER_MAC_ADDR_LEN] = "addr"; -constexpr uint8_t STATIC_CAPABILITY[STATIC_CAP_LEN] = "staticCapability"; -constexpr char REMOTE_PTK[PTK_DEFAULT_LEN] = "remotePtktest"; -class AuthSessionMessageTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void AuthSessionMessageTest::SetUpTestCase() -{ - int32_t ret = AuthCommonInit(); - EXPECT_TRUE(ret == SOFTBUS_OK); -} - -void AuthSessionMessageTest::TearDownTestCase() -{ - AuthCommonDeinit(); -} - -void AuthSessionMessageTest::SetUp() {} - -void AuthSessionMessageTest::TearDown() {} - -/* - * @tc.name: PackFastAuthValue_TEST_001 - * @tc.desc: PackFastAuthValue test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, PackFastAuthValue_TEST_001, TestSize.Level1) -{ - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - char target[10] = {0}; - JSON_AddStringToObject(json, FAST_AUTH, "jsontest"); - OptString(json, DEVICE_ID, target, 10, ""); - OptString(json, FAST_AUTH, target, 10, ""); - int val = 0; - OptInt(json, P2P_ROLE, &val, 0); - JSON_AddInt32ToObject(json, SOFTBUS_VERSION_TAG, 123); - OptInt(json, SOFTBUS_VERSION_TAG, &val, 0); - int64_t value = 0; - OptInt64(json, NEW_CONN_CAP, &value, 1); - JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); - OptInt64(json, NEW_CONN_CAP, &value, 1); - bool result; - OptBool(json, BLE_P2P, &result, false); - JSON_AddBoolToObject(json, BLE_P2P, true); - OptBool(json, BLE_P2P, &result, false); - JSON_Delete(json); - JsonObj *obj = JSON_CreateObject(); - EXPECT_TRUE(obj != nullptr); - AuthDeviceKeyInfo deviceCommKey; - (void)memset_s(&deviceCommKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo)); - ASSERT_TRUE(memcpy_s(deviceCommKey.deviceKey, SESSION_KEY_LENGTH, - DEVICE_KEY, strlen(DEVICE_KEY)) == EOK); - deviceCommKey.keyLen = 5; - deviceCommKey.keyIndex = 12345; - EXPECT_TRUE(PackFastAuthValue(obj, &deviceCommKey) == SOFTBUS_ERR); - JSON_Delete(obj); -} - -/* - * @tc.name: GenerateUdidShortHash_TEST_001 - * @tc.desc: GenerateUdidShortHash test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, GenerateUdidShortHash_TEST_001, TestSize.Level1) -{ - const char *udid = "123456"; - char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0}; - EXPECT_TRUE(GenerateUdidShortHash(udid, udidHashHexStr, SHA_256_HEX_HASH_LEN)); - EXPECT_TRUE(!GenerateUdidShortHash(udid, udidHashHexStr, 10)); -} - -/* - * @tc.name: GetUdidOrShortHash_TEST_001 - * @tc.desc: GetUdidOrShortHash test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, GetUdidOrShortHash_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - info.isServer = false; - info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; - ASSERT_TRUE(memcpy_s(info.connInfo.info.ipInfo.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); - char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0}; - EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); - info.isServer = true; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); - EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, INVALID_UDID, strlen(INVALID_UDID)) == EOK); - EXPECT_TRUE(!GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); -} - -/* - * @tc.name: GetEnhancedP2pAuthKey_TEST_001 - * @tc.desc: GetEnhancedP2pAuthKey test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, GetEnhancedP2pAuthKey_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); - EXPECT_TRUE(GetEnhancedP2pAuthKey(nullptr, &info, nullptr) == SOFTBUS_ERR); - info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; - EXPECT_TRUE(GetFastAuthKey("hashtest", &info, nullptr) == SOFTBUS_ERR); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(GetFastAuthKey("hashtest", &info, nullptr) == SOFTBUS_ERR); - info.isServer = true; - info.isSupportFastAuth = true; - info.isNeedFastAuth = false; - PackFastAuth(nullptr, &info); - info.isNeedFastAuth = true; - PackFastAuth(nullptr, &info); - info.isSupportFastAuth = false; - PackFastAuth(nullptr, &info); - info.isServer = false; - info.isNeedFastAuth = true; - PackFastAuth(nullptr, &info); -} - -/* - * @tc.name: VerifySessionInfoIdType_TEST_001 - * @tc.desc: VerifySessionInfoIdType test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, VerifySessionInfoIdType_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - const char *encryptedFastAuth = "encryptedFastAuth"; - AuthDeviceKeyInfo deviceKey; - (void)memset_s(&deviceKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo)); - ASSERT_TRUE(memcpy_s(deviceKey.deviceKey, SESSION_KEY_LENGTH, - DEVICE_KEY, strlen(DEVICE_KEY)) == EOK); - deviceKey.keyLen = 5; - deviceKey.keyIndex = 12345; - ParseFastAuthValue(&info, encryptedFastAuth, &deviceKey); - JsonObj *obj = JSON_CreateObject(); - EXPECT_TRUE(obj != nullptr); - JSON_AddStringToObject(obj, FAST_AUTH, encryptedFastAuth); - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - UnpackFastAuth(obj, &info); - info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; - UnpackFastAuth(obj, &info); - NodeInfo nodeInfo; - nodeInfo.feature = 127; - PackCompressInfo(obj, &nodeInfo); - nodeInfo.feature = 0; - PackCompressInfo(obj, &nodeInfo); - PackWifiSinglePassInfo(obj, &info); - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - PackWifiSinglePassInfo(obj, &info); - info.idType = EXCHANGE_NETWORKID; - EXPECT_TRUE(VerifySessionInfoIdType(&info, obj, - const_cast(NETWORK_ID_TEST), const_cast(UDID))); - info.idType = EXCHANHE_UDID; - EXPECT_TRUE(VerifySessionInfoIdType(&info, obj, - const_cast(NETWORK_ID_TEST), const_cast(UDID))); - JSON_Delete(obj); -} - -/* - * @tc.name: PackDeviceIdJson_TEST_001 - * @tc.desc: PackDeviceIdJson test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, PackDeviceIdJson_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - info.version = SOFTBUS_OLD_V1; - info.idType = EXCHANHE_UDID; - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - info.isServer = true; - EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); - info.isServer = false; - EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); - info.connInfo.type = AUTH_LINK_TYPE_BR; - EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); -} - -/* - * @tc.name: UnpackWifiSinglePassInfo_TEST_001 - * @tc.desc: UnpackWifiSinglePassInfo test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnpackWifiSinglePassInfo_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - info.connInfo.type = AUTH_LINK_TYPE_BR; - info.connId = 12; - JsonObj *obj = JSON_CreateObject(); - EXPECT_TRUE(obj != nullptr); - JSON_AddStringToObject(obj, FAST_AUTH, "encryptedFastAuth"); - EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); - JSON_AddStringToObject(obj, DEV_IP_HASH_TAG, "12345678"); - EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); - JSON_Delete(obj); -} - -/* - * @tc.name: UnPackBtDeviceIdV1_TEST_001 - * @tc.desc: UnPackBtDeviceIdV1 test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnPackBtDeviceIdV1_TEST_001, TestSize.Level1) -{ - bool sessionSupportFlag = false; - SetCompressFlag("true", &sessionSupportFlag); - SetCompressFlag("false", &sessionSupportFlag); - AuthSessionInfo info; - info.isServer = false; - EXPECT_TRUE(UnPackBtDeviceIdV1(&info, DATA_TEST, DATA_TEST_LEN) == SOFTBUS_INVALID_PARAM); - info.isServer = true; - EXPECT_TRUE(UnPackBtDeviceIdV1(&info, DATA_TEST, DATA_TEST_LEN) == SOFTBUS_OK); -} - -/* - * @tc.name: SetExchangeIdTypeAndValue_TEST_001 - * @tc.desc: SetExchangeIdTypeAndValue test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, SetExchangeIdTypeAndValue_TEST_001, TestSize.Level1) -{ - JsonObj *obj1 = JSON_CreateObject(); - EXPECT_TRUE(obj1 != nullptr); - AuthSessionInfo info; - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - info.idType = EXCHANHE_UDID; - JSON_AddInt32ToObject(obj1, SOFTBUS_VERSION_TAG, 123); - EXPECT_TRUE(SetExchangeIdTypeAndValue(nullptr, &info) == SOFTBUS_INVALID_PARAM); - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, nullptr) == SOFTBUS_INVALID_PARAM); - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, &info) == SOFTBUS_OK); - JSON_AddInt32ToObject(obj1, EXCHANGE_ID_TYPE, EXCHANHE_UDID); - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, &info) == SOFTBUS_OK); - JSON_Delete(obj1); - JsonObj *obj2 = JSON_CreateObject(); - EXPECT_TRUE(obj2 != nullptr); - JSON_AddInt32ToObject(obj2, EXCHANGE_ID_TYPE, EXCHANGE_NETWORKID); - info.isServer = true; - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); - info.isServer = false; - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); - info.idType = EXCHANGE_NETWORKID; - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); - JSON_Delete(obj2); - JsonObj *obj3 = JSON_CreateObject(); - EXPECT_TRUE(obj3 != nullptr); - JSON_AddInt32ToObject(obj3, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); - EXPECT_TRUE(SetExchangeIdTypeAndValue(obj3, &info) == SOFTBUS_OK); - JSON_Delete(obj3); -} - -/* - * @tc.name: UnpackDeviceIdJson_TEST_001 - * @tc.desc: UnpackDeviceIdJson test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnpackDeviceIdJson_TEST_001, TestSize.Level1) -{ - JsonObj *obj = JSON_CreateObject(); - EXPECT_TRUE(obj != nullptr); - AuthSessionInfo info; - EXPECT_TRUE(UnpackDeviceIdJson(nullptr, 0, &info) == SOFTBUS_INVALID_PARAM); - JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); - char *msg = JSON_PrintUnformatted(obj); - EXPECT_TRUE(UnpackDeviceIdJson(msg, strlen(msg), &info) == SOFTBUS_NOT_FIND); - if (msg != nullptr) { - JSON_Free(msg); - } - JSON_Delete(obj); - JsonObj *obj1 = JSON_CreateObject(); - EXPECT_TRUE(obj1 != nullptr); - JSON_AddStringToObject(obj1, CMD_TAG, CMD_GET_AUTH_INFO); - JSON_AddStringToObject(obj1, DATA_TAG, "123456"); - JSON_AddStringToObject(obj1, DEVICE_ID_TAG, "654321"); - JSON_AddInt32ToObject(obj1, DATA_BUF_SIZE_TAG, PACKET_SIZE); - JSON_AddInt32ToObject(obj1, SOFTBUS_VERSION_TAG, 123); - JSON_AddInt32ToObject(obj1, EXCHANGE_ID_TYPE, EXCHANHE_UDID); - JSON_AddStringToObject(obj1, SUPPORT_INFO_COMPRESS, TRUE_STRING_TAG); - char *msg1 = JSON_PrintUnformatted(obj1); - info.connInfo.type = AUTH_LINK_TYPE_BR; - EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_CMP_FAIL); - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - info.isServer = false; - EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_CMP_FAIL); - info.isConnectServer = true; - EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_OK); - if (msg1 != nullptr) { - JSON_Free(msg1); - } - JSON_Delete(obj1); -} - -/* - * @tc.name: PackCommonDevInfo_TEST_001 - * @tc.desc: PackCommonDevInfo test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, PackCommonDevInfo_TEST_001, TestSize.Level1) -{ - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - NodeInfo info; - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - ASSERT_TRUE(memcpy_s(info.deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, - INVALID_UNIFIED_NAME, strlen(INVALID_UNIFIED_NAME)) == EOK); - ASSERT_TRUE(memcpy_s(info.uuid, UUID_BUF_LEN, INVALID_UDID, strlen(INVALID_UDID)) == EOK); - EXPECT_TRUE(PackCommonDevInfo(json, &info, true) == SOFTBUS_OK); - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - ASSERT_TRUE(memcpy_s(info.deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, - UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); - ASSERT_TRUE(memcpy_s(info.networkId, NETWORK_ID_BUF_LEN, - NETWORK_ID_TEST, strlen(NETWORK_ID_TEST)) == EOK); - ASSERT_TRUE(memcpy_s(info.deviceInfo.deviceName, DEVICE_NAME_BUF_LEN, - DEVICE_NAME_TEST, strlen(DEVICE_NAME_TEST)) == EOK); - info.deviceInfo.deviceTypeId = 12; - ASSERT_TRUE(memcpy_s(info.uuid, UUID_BUF_LEN, UUID_TEST, strlen(UUID_TEST)) == EOK); - EXPECT_TRUE(PackCommonDevInfo(json, &info, false) == SOFTBUS_OK); - JSON_Delete(json); -} - -/* - * @tc.name: UnpackCipherRpaInfo_TEST_001 - * @tc.desc: UnpackCipherRpaInfo test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnpackCipherRpaInfo_TEST_001, TestSize.Level1) -{ - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - (void)JSON_AddStringToObject(json, BROADCAST_CIPHER_KEY, "cipherKeyTest"); - (void)JSON_AddStringToObject(json, BROADCAST_CIPHER_IV, "cipherIv"); - (void)JSON_AddStringToObject(json, IRK, "peerIrk"); - (void)JSON_AddStringToObject(json, PUB_MAC, "pubMac"); - NodeInfo info; - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - ASSERT_TRUE(memcpy_s(info.cipherInfo.key, SESSION_KEY_LENGTH, KEY_TEST, strlen(KEY_TEST)) == EOK); - ASSERT_TRUE(memcpy_s(info.cipherInfo.iv, BROADCAST_IV_LEN, IV_TEST, strlen(IV_TEST)) == EOK); - ASSERT_TRUE(memcpy_s(info.rpaInfo.peerIrk, LFINDER_IRK_LEN, PEER_IRK, PEER_IRK_LEN) == EOK); - ASSERT_TRUE(memcpy_s(info.rpaInfo.publicAddress, LFINDER_MAC_ADDR_LEN, - PUBLIC_ADDRESS, PUBLIC_ADDRESS_LEN) == EOK); - UnpackCipherRpaInfo(json, &info); - JSON_Delete(json); - JsonObj *json1 = JSON_CreateObject(); - EXPECT_TRUE(json1 != nullptr); - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - EXPECT_TRUE(PackCommon(json1, &info, SOFTBUS_OLD_V2, false) == SOFTBUS_OK); - EXPECT_TRUE(PackCommon(json1, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); - JSON_Delete(json1); -} - -/* - * @tc.name: UnpackWifiDirectInfo_TEST_001 - * @tc.desc: UnpackWifiDirectInfo test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnpackWifiDirectInfo_TEST_001, TestSize.Level1) -{ - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - (void)JSON_AddStringToObject(json, IRK, "peerIrk"); - NodeInfo info; - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - ASSERT_TRUE(memcpy_s(info.staticCapability, STATIC_CAP_LEN, STATIC_CAPABILITY, CAPABILITY_LEN) == EOK); - UnpackWifiDirectInfo(json, &info); - JSON_AddInt32ToObject(json, STATIC_CAP_LENGTH, 10); - UnpackWifiDirectInfo(json, &info); - JSON_AddStringToObject(json, STATIC_CAP, "staticCap"); - UnpackWifiDirectInfo(json, &info); - JSON_AddStringToObject(json, PTK, "encodePtk"); - UnpackWifiDirectInfo(json, &info); - ASSERT_TRUE(memcpy_s(info.remotePtk, PTK_DEFAULT_LEN, REMOTE_PTK, strlen(REMOTE_PTK)) == EOK); - UnpackWifiDirectInfo(json, &info); - UnpackCommon(json, &info, SOFTBUS_OLD_V1, true); - JSON_AddStringToObject(json, DEVICE_TYPE, "TV"); - JSON_AddStringToObject(json, DEVICE_UUID, "123456"); - JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); - UnpackCommon(json, &info, SOFTBUS_OLD_V1, true); - UnpackCommon(json, &info, SOFTBUS_NEW_V1, false); - JSON_AddStringToObject(json, NODE_ADDR, "127.0.0.0"); - UnpackCommon(json, &info, SOFTBUS_NEW_V1, false); - char buf[10] = {0}; - EXPECT_TRUE(GetBtDiscTypeString(nullptr, buf, 10) == SOFTBUS_OK); - info.discoveryType = 11; - EXPECT_TRUE(GetBtDiscTypeString(&info, buf, 10) == SOFTBUS_OK); - info.discoveryType = 15; - EXPECT_TRUE(UnpackBt(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); - JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, 3); - EXPECT_TRUE(UnpackBt(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); - EXPECT_TRUE(PackWiFi(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); - JSON_Delete(json); -} - -/* - * @tc.name: CheckBusVersion_TEST_001 - * @tc.desc: CheckBusVersion test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, CheckBusVersion_TEST_001, TestSize.Level1) -{ - JsonObj *json1 = JSON_CreateObject(); - EXPECT_TRUE(json1 != nullptr); - JSON_AddInt32ToObject(json1, BUS_MAX_VERSION, -1); - JSON_AddInt32ToObject(json1, BUS_MIN_VERSION, -1); - EXPECT_TRUE(CheckBusVersion(json1) == SOFTBUS_ERR); - NodeInfo info; - (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - EXPECT_TRUE(UnpackWiFi(json1, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_ERR); - JSON_Delete(json1); - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - JSON_AddInt32ToObject(json, BUS_MAX_VERSION, 3); - JSON_AddInt32ToObject(json, BUS_MIN_VERSION, 0);; - EXPECT_TRUE(CheckBusVersion(json) == 2); - EXPECT_TRUE(UnpackWiFi(json, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_OK); - JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, 63); - JSON_AddStringToObject(json, BLE_OFFLINE_CODE, "123"); - EXPECT_TRUE(UnpackWiFi(json, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_OK); - JSON_Delete(json); -} - -/* - * @tc.name: PackDeviceInfoBtV1_TEST_001 - * @tc.desc: PackDeviceInfoBtV1 test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, PackDeviceInfoBtV1_TEST_001, TestSize.Level1) -{ - NodeInfo info; - EXPECT_TRUE(PackDeviceInfoBtV1(nullptr, &info, false) == SOFTBUS_AUTH_REG_DATA_FAIL); - JsonObj *json = JSON_CreateObject(); - EXPECT_TRUE(json != nullptr); - EXPECT_TRUE(PackDeviceInfoBtV1(json, &info, false) == SOFTBUS_OK); - EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); - JSON_AddStringToObject(json, DEVICE_NAME, "testname"); - EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); - JSON_AddStringToObject(json, DEVICE_TYPE, "TV"); - EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); - JSON_AddStringToObject(json, DEVICE_UDID, "123456"); - JSON_AddStringToObject(json, UUID, "123456"); - JSON_AddStringToObject(json, BR_MAC_ADDR, "11:22:33:44:55"); - JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); - EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); - JSON_Delete(json); -} - -/* - * @tc.name: UpdatePeerDeviceName_TEST_001 - * @tc.desc: UpdatePeerDeviceName test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UpdatePeerDeviceName_TEST_001, TestSize.Level1) -{ - NodeInfo peerNodeInfo; - (void)memset_s(&peerNodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - UpdatePeerDeviceName(&peerNodeInfo); - ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedName, - DEVICE_NAME_BUF_LEN, UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); - ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedDefaultName, - DEVICE_NAME_BUF_LEN, DEVICE_NAME_TEST, strlen(DEVICE_NAME_TEST)) == EOK); - UpdatePeerDeviceName(&peerNodeInfo); - ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedDefaultName, - DEVICE_NAME_BUF_LEN, UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); - UpdatePeerDeviceName(&peerNodeInfo); - AuthSessionInfo info; - EXPECT_TRUE(PostDeviceIdData(123, &info, nullptr, 0) == SOFTBUS_AUTH_SEND_FAIL); - info.isServer = false; - EXPECT_TRUE(PostBtV1DevId(123, &info) == SOFTBUS_ERR); - EXPECT_TRUE(PostWifiV1DevId(123, &info) == SOFTBUS_ERR); - info.isServer = true; - EXPECT_TRUE(PostBtV1DevId(123, &info) == SOFTBUS_ERR); - EXPECT_TRUE(PostWifiV1DevId(123, &info) == SOFTBUS_ERR); - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - EXPECT_TRUE(PostDeviceIdV1(123, &info) == SOFTBUS_ERR); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(PostDeviceIdV1(123, &info) == SOFTBUS_ERR); - EXPECT_TRUE(PostDeviceIdNew(123, &info) == SOFTBUS_ERR); - EXPECT_TRUE(PostCloseAckMessage(123, &info) == SOFTBUS_AUTH_SEND_FAIL); -} - -/* - * @tc.name: ProcessDeviceIdMessage_TEST_001 - * @tc.desc: ProcessDeviceIdMessage test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, ProcessDeviceIdMessage_TEST_001, TestSize.Level1) -{ - AuthSessionInfo info; - info.connInfo.type = AUTH_LINK_TYPE_WIFI; - uint8_t data[] = "test"; - EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_INVALID_PARAM); - info.connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN + 1) == SOFTBUS_INVALID_PARAM); - info.isServer = false; - EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_INVALID_PARAM); - info.isServer = true; - EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_OK); -} - -/* - * @tc.name: UnpackDeviceInfoMessage_TEST_001 - * @tc.desc: UnpackDeviceInfoMessage test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, UnpackDeviceInfoMessage_TEST_001, TestSize.Level1) -{ - DevInfoData devInfo; - NodeInfo nodeInfo; - AuthSessionInfo info; - (void)memset_s(&devInfo, sizeof(DevInfoData), 0, sizeof(DevInfoData)); - (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); - JsonObj *obj = JSON_CreateObject(); - EXPECT_TRUE(obj != nullptr); - JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); - char *msg = JSON_PrintUnformatted(obj); - devInfo.msg = msg; - devInfo.len = strlen(msg); - devInfo.linkType = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(UnpackDeviceInfoMessage(&devInfo, &nodeInfo, false, &info) == SOFTBUS_OK); - devInfo.linkType = AUTH_LINK_TYPE_WIFI; - nodeInfo.feature = 511; - EXPECT_TRUE(UnpackDeviceInfoMessage(&devInfo, &nodeInfo, false, &info) == SOFTBUS_ERR); - JSON_Delete(obj); -} - -/* - * @tc.name: IsDeviceMessagePacket_TEST_001 - * @tc.desc: IsDeviceMessagePacket test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, IsDeviceMessagePacket_TEST_001, TestSize.Level1) -{ - AuthConnInfo connInfo; - (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); - DeviceMessageParse messageParse = { 0 }; - connInfo.type = AUTH_LINK_TYPE_BLE; - EXPECT_TRUE(!IsDeviceMessagePacket(&connInfo, nullptr, nullptr, true, &messageParse)); - connInfo.type = AUTH_LINK_TYPE_WIFI; - EXPECT_TRUE(!IsDeviceMessagePacket(&connInfo, nullptr, nullptr, true, &messageParse)); -} - -/* - * @tc.name: PACK_FAST_AUTH_VALUE_TEST_001 - * @tc.desc: Pack fast auth value test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, PACK_FAST_AUTH_VALUE_TEST_001, TestSize.Level1) -{ - AuthDeviceKeyInfo deviceCommKey = {0}; - JsonObj *obj = JSON_CreateObject(); - ASSERT_NE(obj, nullptr); - uint32_t keyLen = 0; - deviceCommKey.keyLen = keyLen; - uint64_t ret = PackFastAuthValue(obj, &deviceCommKey); - EXPECT_EQ(ret, SOFTBUS_ERR); - JSON_Delete(obj); -} - -/* - * @tc.name: CHECK_BUS_VERSION_TEST_001 - * @tc.desc: check bus version test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, CHECK_BUS_VERSION_TEST_001, TestSize.Level1) -{ - JsonObj *obj = JSON_CreateObject(); - if (obj == NULL) { - return; - } - - NodeInfo *info = (NodeInfo*)SoftBusCalloc(sizeof(NodeInfo)); - if (info == NULL) { - JSON_Delete(obj); - return; - } - (void)memset_s(info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); - - SoftBusVersion version = SOFTBUS_NEW_V1; - ASSERT_NE(obj, NULL); - if (!JSON_AddInt32ToObject(obj, "CODE", (int32_t)1) || - !JSON_AddInt32ToObject(obj, "BUS_MAX_VERSION", (int32_t)2) || - !JSON_AddInt32ToObject(obj, "BUS_MIN_VERSION", (int32_t)1) || - !JSON_AddInt32ToObject(obj, "AUTH_PORT", (int32_t)8710) || - !JSON_AddInt32ToObject(obj, "SESSION_PORT", (int32_t)26) || - !JSON_AddInt32ToObject(obj, "PROXY_PORT", (int32_t)80) || - !JSON_AddStringToObject(obj, "DEV_IP", "127.0.0.1")) { - JSON_Delete(obj); - return; - } - JSON_AddStringToObject(obj, BLE_OFFLINE_CODE, "10244"); - - info->connectInfo.authPort = 8710; - info->connectInfo.sessionPort = 26; - info->connectInfo.proxyPort = 80; - info->supportedProtocols = LNN_PROTOCOL_BR; - - int32_t ret = UnpackWiFi(obj, info, version, false); - EXPECT_TRUE(ret == SOFTBUS_OK); - JSON_AddInt32ToObject(obj, "BUS_MAX_VERSION", (int32_t)-1); - ret = UnpackWiFi(obj, info, version, false); - EXPECT_TRUE(ret == SOFTBUS_ERR); - - (void)JSON_AddStringToObject(obj, "BROADCAST_CIPHER_KEY", "1222222222"); - (void)JSON_AddStringToObject(obj, "BROADCAST_CIPHER_IV", "1222222222"); - (void)JSON_AddStringToObject(obj, "IRK", "1222222222"); - (void)JSON_AddStringToObject(obj, "PUB_MAC", "1222222222"); - - JSON_AddStringToObject(obj, "MASTER_UDID", "1122334554444"); - JSON_AddStringToObject(obj, "NODE_ADDR", "1122334554444"); - UnpackCommon(obj, info, version, false); - version = SOFTBUS_OLD_V1; - JSON_AddInt32ToObject(obj, "MASTER_WEIGHT", (int32_t)10); - UnpackCommon(obj, info, version, true); - UnpackCipherRpaInfo(obj, info); - JSON_Delete(obj); - SoftBusFree(info); -} - -/* - * @tc.name: POST_BT_V1_DEVID_TEST_001 - * @tc.desc: post bt v1 devid test - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AuthSessionMessageTest, POST_BT_V1_DEVID_TEST_001, TestSize.Level1) -{ - int64_t authSeq = 0; - AuthSessionInfo *info = (AuthSessionInfo*)SoftBusCalloc(sizeof(AuthSessionInfo)); - if (info == NULL) { - return; - } - info->requestId = 1; - info->connId = 1; - info->isServer = false; - info->version = SOFTBUS_NEW_V1; - info->connInfo.type = AUTH_LINK_TYPE_WIFI; - int32_t ret = PostDeviceIdV1(authSeq, info); - EXPECT_TRUE(ret == SOFTBUS_ERR); - SoftBusFree(info); -} -} // namespace OHOS +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "auth_session_json.c" +#include "auth_session_message.h" +#include "auth_session_message.c" +#include "softbus_adapter_json.h" +#include "softbus_errcode.h" + +namespace OHOS { +using namespace testing::ext; +constexpr int32_t PEER_IRK_LEN = 13; +constexpr int32_t PUBLIC_ADDRESS_LEN = 4; +constexpr int32_t DATA_TEST_LEN = 7; +constexpr int32_t CAPABILITY_LEN = 16; +constexpr char DEVICE_KEY[SESSION_KEY_LENGTH] = "11111"; +constexpr char UDID[UDID_BUF_LEN] = "123456789udidtest"; +constexpr char UUID_TEST[UUID_BUF_LEN] = "123456789uuidtest"; +constexpr char INVALID_UDID[UDID_BUF_LEN] = "\0"; +constexpr char NETWORK_ID_TEST[NETWORK_ID_BUF_LEN] = "987654321"; +constexpr uint8_t DATA_TEST[UDID_BUF_LEN] = "1111111"; +constexpr char UNIFIED_NAME[DEVICE_NAME_BUF_LEN] = "unifiedName"; +constexpr char INVALID_UNIFIED_NAME[DEVICE_NAME_BUF_LEN] = "\0"; +constexpr char DEVICE_NAME_TEST[DEVICE_NAME_BUF_LEN] = "deviceName"; +constexpr char KEY_TEST[SESSION_KEY_LENGTH] = "123456keytest"; +constexpr char IV_TEST[BROADCAST_IV_LEN] = "123456ivtest"; +constexpr uint8_t PEER_IRK[LFINDER_IRK_LEN] = "123456irktest"; +constexpr unsigned char PUBLIC_ADDRESS[LFINDER_MAC_ADDR_LEN] = "addr"; +constexpr uint8_t STATIC_CAPABILITY[STATIC_CAP_LEN] = "staticCapability"; +constexpr char REMOTE_PTK[PTK_DEFAULT_LEN] = "remotePtktest"; + +class AuthSessionMessageTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AuthSessionMessageTest::SetUpTestCase() +{ + int32_t ret = AuthCommonInit(); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +void AuthSessionMessageTest::TearDownTestCase() +{ + AuthCommonDeinit(); +} + +void AuthSessionMessageTest::SetUp() {} + +void AuthSessionMessageTest::TearDown() {} + +/* + * @tc.name: PackFastAuthValue_TEST_001 + * @tc.desc: PackFastAuthValue test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, PackFastAuthValue_TEST_001, TestSize.Level1) +{ + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + char target[10] = {0}; + JSON_AddStringToObject(json, FAST_AUTH, "jsontest"); + OptString(json, DEVICE_ID, target, 10, ""); + OptString(json, FAST_AUTH, target, 10, ""); + int val = 0; + OptInt(json, P2P_ROLE, &val, 0); + JSON_AddInt32ToObject(json, SOFTBUS_VERSION_TAG, 123); + OptInt(json, SOFTBUS_VERSION_TAG, &val, 0); + int64_t value = 0; + OptInt64(json, NEW_CONN_CAP, &value, 1); + JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); + OptInt64(json, NEW_CONN_CAP, &value, 1); + bool result; + OptBool(json, BLE_P2P, &result, false); + JSON_AddBoolToObject(json, BLE_P2P, true); + OptBool(json, BLE_P2P, &result, false); + JSON_Delete(json); + JsonObj *obj = JSON_CreateObject(); + EXPECT_TRUE(obj != nullptr); + AuthDeviceKeyInfo deviceCommKey; + (void)memset_s(&deviceCommKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo)); + ASSERT_TRUE(memcpy_s(deviceCommKey.deviceKey, SESSION_KEY_LENGTH, + DEVICE_KEY, strlen(DEVICE_KEY)) == EOK); + deviceCommKey.keyLen = 5; + deviceCommKey.keyIndex = 12345; + EXPECT_TRUE(PackFastAuthValue(obj, &deviceCommKey) == SOFTBUS_ERR); + JSON_Delete(obj); +} + +/* + * @tc.name: GenerateUdidShortHash_TEST_001 + * @tc.desc: GenerateUdidShortHash test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, GenerateUdidShortHash_TEST_001, TestSize.Level1) +{ + const char *udid = "123456"; + char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0}; + EXPECT_TRUE(GenerateUdidShortHash(udid, udidHashHexStr, SHA_256_HEX_HASH_LEN)); + EXPECT_TRUE(!GenerateUdidShortHash(udid, udidHashHexStr, 10)); +} + +/* + * @tc.name: GetUdidOrShortHash_TEST_001 + * @tc.desc: GetUdidOrShortHash test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, GetUdidOrShortHash_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + info.isServer = false; + info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; + ASSERT_TRUE(memcpy_s(info.connInfo.info.ipInfo.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); + char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0}; + EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); + info.isServer = true; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); + EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, INVALID_UDID, strlen(INVALID_UDID)) == EOK); + EXPECT_TRUE(!GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(GetUdidOrShortHash(&info, udidHashHexStr, SHA_256_HEX_HASH_LEN)); +} + +/* + * @tc.name: GetEnhancedP2pAuthKey_TEST_001 + * @tc.desc: GetEnhancedP2pAuthKey test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, GetEnhancedP2pAuthKey_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); + EXPECT_TRUE(GetEnhancedP2pAuthKey(nullptr, &info, nullptr) == SOFTBUS_ERR); + info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; + EXPECT_TRUE(GetFastAuthKey("hashtest", &info, nullptr) == SOFTBUS_ERR); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(GetFastAuthKey("hashtest", &info, nullptr) == SOFTBUS_ERR); + info.isServer = true; + info.isSupportFastAuth = true; + info.isNeedFastAuth = false; + PackFastAuth(nullptr, &info); + info.isNeedFastAuth = true; + PackFastAuth(nullptr, &info); + info.isSupportFastAuth = false; + PackFastAuth(nullptr, &info); + info.isServer = false; + info.isNeedFastAuth = true; + PackFastAuth(nullptr, &info); +} + +/* + * @tc.name: VerifySessionInfoIdType_TEST_001 + * @tc.desc: VerifySessionInfoIdType test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, VerifySessionInfoIdType_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + const char *encryptedFastAuth = "encryptedFastAuth"; + AuthDeviceKeyInfo deviceKey; + (void)memset_s(&deviceKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo)); + ASSERT_TRUE(memcpy_s(deviceKey.deviceKey, SESSION_KEY_LENGTH, + DEVICE_KEY, strlen(DEVICE_KEY)) == EOK); + deviceKey.keyLen = 5; + deviceKey.keyIndex = 12345; + ParseFastAuthValue(&info, encryptedFastAuth, &deviceKey); + JsonObj *obj = JSON_CreateObject(); + EXPECT_TRUE(obj != nullptr); + JSON_AddStringToObject(obj, FAST_AUTH, encryptedFastAuth); + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + ASSERT_TRUE(memcpy_s(info.udid, UDID_BUF_LEN, UDID, strlen(UDID)) == EOK); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + UnpackFastAuth(obj, &info); + info.connInfo.type = AUTH_LINK_TYPE_ENHANCED_P2P; + UnpackFastAuth(obj, &info); + NodeInfo nodeInfo; + nodeInfo.feature = 127; + PackCompressInfo(obj, &nodeInfo); + nodeInfo.feature = 0; + PackCompressInfo(obj, &nodeInfo); + PackWifiSinglePassInfo(obj, &info); + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + PackWifiSinglePassInfo(obj, &info); + info.idType = EXCHANGE_NETWORKID; + EXPECT_TRUE(VerifySessionInfoIdType(&info, obj, + const_cast(NETWORK_ID_TEST), const_cast(UDID))); + info.idType = EXCHANHE_UDID; + EXPECT_TRUE(VerifySessionInfoIdType(&info, obj, + const_cast(NETWORK_ID_TEST), const_cast(UDID))); + JSON_Delete(obj); +} + +/* + * @tc.name: PackDeviceIdJson_TEST_001 + * @tc.desc: PackDeviceIdJson test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, PackDeviceIdJson_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + info.version = SOFTBUS_OLD_V1; + info.idType = EXCHANHE_UDID; + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + info.isServer = true; + EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); + info.isServer = false; + EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); + info.connInfo.type = AUTH_LINK_TYPE_BR; + EXPECT_TRUE(PackDeviceIdJson(&info) == nullptr); +} + +/* + * @tc.name: UnpackWifiSinglePassInfo_TEST_001 + * @tc.desc: UnpackWifiSinglePassInfo test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnpackWifiSinglePassInfo_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + info.connInfo.type = AUTH_LINK_TYPE_BR; + info.connId = 12; + JsonObj *obj = JSON_CreateObject(); + EXPECT_TRUE(obj != nullptr); + JSON_AddStringToObject(obj, FAST_AUTH, "encryptedFastAuth"); + EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); + JSON_AddStringToObject(obj, DEV_IP_HASH_TAG, "12345678"); + EXPECT_TRUE(UnpackWifiSinglePassInfo(obj, &info)); + JSON_Delete(obj); +} + +/* + * @tc.name: UnPackBtDeviceIdV1_TEST_001 + * @tc.desc: UnPackBtDeviceIdV1 test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnPackBtDeviceIdV1_TEST_001, TestSize.Level1) +{ + bool sessionSupportFlag = false; + SetCompressFlag("true", &sessionSupportFlag); + SetCompressFlag("false", &sessionSupportFlag); + AuthSessionInfo info; + info.isServer = false; + EXPECT_TRUE(UnPackBtDeviceIdV1(&info, DATA_TEST, DATA_TEST_LEN) == SOFTBUS_INVALID_PARAM); + info.isServer = true; + EXPECT_TRUE(UnPackBtDeviceIdV1(&info, DATA_TEST, DATA_TEST_LEN) == SOFTBUS_OK); +} + +/* + * @tc.name: SetExchangeIdTypeAndValue_TEST_001 + * @tc.desc: SetExchangeIdTypeAndValue test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, SetExchangeIdTypeAndValue_TEST_001, TestSize.Level1) +{ + JsonObj *obj1 = JSON_CreateObject(); + EXPECT_TRUE(obj1 != nullptr); + AuthSessionInfo info; + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + info.idType = EXCHANHE_UDID; + JSON_AddInt32ToObject(obj1, SOFTBUS_VERSION_TAG, 123); + EXPECT_TRUE(SetExchangeIdTypeAndValue(nullptr, &info) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, &info) == SOFTBUS_OK); + JSON_AddInt32ToObject(obj1, EXCHANGE_ID_TYPE, EXCHANHE_UDID); + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj1, &info) == SOFTBUS_OK); + JSON_Delete(obj1); + JsonObj *obj2 = JSON_CreateObject(); + EXPECT_TRUE(obj2 != nullptr); + JSON_AddInt32ToObject(obj2, EXCHANGE_ID_TYPE, EXCHANGE_NETWORKID); + info.isServer = true; + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); + info.isServer = false; + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); + info.idType = EXCHANGE_NETWORKID; + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj2, &info) == SOFTBUS_OK); + JSON_Delete(obj2); + JsonObj *obj3 = JSON_CreateObject(); + EXPECT_TRUE(obj3 != nullptr); + JSON_AddInt32ToObject(obj3, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); + EXPECT_TRUE(SetExchangeIdTypeAndValue(obj3, &info) == SOFTBUS_OK); + JSON_Delete(obj3); +} + +/* + * @tc.name: UnpackDeviceIdJson_TEST_001 + * @tc.desc: UnpackDeviceIdJson test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnpackDeviceIdJson_TEST_001, TestSize.Level1) +{ + JsonObj *obj = JSON_CreateObject(); + EXPECT_TRUE(obj != nullptr); + AuthSessionInfo info; + EXPECT_TRUE(UnpackDeviceIdJson(nullptr, 0, &info) == SOFTBUS_INVALID_PARAM); + JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); + char *msg = JSON_PrintUnformatted(obj); + EXPECT_TRUE(UnpackDeviceIdJson(msg, strlen(msg), &info) == SOFTBUS_NOT_FIND); + if (msg != nullptr) { + JSON_Free(msg); + } + JSON_Delete(obj); + JsonObj *obj1 = JSON_CreateObject(); + EXPECT_TRUE(obj1 != nullptr); + JSON_AddStringToObject(obj1, CMD_TAG, CMD_GET_AUTH_INFO); + JSON_AddStringToObject(obj1, DATA_TAG, "123456"); + JSON_AddStringToObject(obj1, DEVICE_ID_TAG, "654321"); + JSON_AddInt32ToObject(obj1, DATA_BUF_SIZE_TAG, PACKET_SIZE); + JSON_AddInt32ToObject(obj1, SOFTBUS_VERSION_TAG, 123); + JSON_AddInt32ToObject(obj1, EXCHANGE_ID_TYPE, EXCHANHE_UDID); + JSON_AddStringToObject(obj1, SUPPORT_INFO_COMPRESS, TRUE_STRING_TAG); + char *msg1 = JSON_PrintUnformatted(obj1); + info.connInfo.type = AUTH_LINK_TYPE_BR; + EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_CMP_FAIL); + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + info.isServer = false; + EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_CMP_FAIL); + info.isConnectServer = true; + EXPECT_TRUE(UnpackDeviceIdJson(msg1, strlen(msg1), &info) == SOFTBUS_OK); + if (msg1 != nullptr) { + JSON_Free(msg1); + } + JSON_Delete(obj1); +} + +/* + * @tc.name: PackCommonDevInfo_TEST_001 + * @tc.desc: PackCommonDevInfo test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, PackCommonDevInfo_TEST_001, TestSize.Level1) +{ + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + ASSERT_TRUE(memcpy_s(info.deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, + INVALID_UNIFIED_NAME, strlen(INVALID_UNIFIED_NAME)) == EOK); + ASSERT_TRUE(memcpy_s(info.uuid, UUID_BUF_LEN, INVALID_UDID, strlen(INVALID_UDID)) == EOK); + EXPECT_TRUE(PackCommonDevInfo(json, &info, true) == SOFTBUS_OK); + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + ASSERT_TRUE(memcpy_s(info.deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, + UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); + ASSERT_TRUE(memcpy_s(info.networkId, NETWORK_ID_BUF_LEN, + NETWORK_ID_TEST, strlen(NETWORK_ID_TEST)) == EOK); + ASSERT_TRUE(memcpy_s(info.deviceInfo.deviceName, DEVICE_NAME_BUF_LEN, + DEVICE_NAME_TEST, strlen(DEVICE_NAME_TEST)) == EOK); + info.deviceInfo.deviceTypeId = 12; + ASSERT_TRUE(memcpy_s(info.uuid, UUID_BUF_LEN, UUID_TEST, strlen(UUID_TEST)) == EOK); + EXPECT_TRUE(PackCommonDevInfo(json, &info, false) == SOFTBUS_OK); + JSON_Delete(json); +} + +/* + * @tc.name: UnpackCipherRpaInfo_TEST_001 + * @tc.desc: UnpackCipherRpaInfo test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnpackCipherRpaInfo_TEST_001, TestSize.Level1) +{ + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + (void)JSON_AddStringToObject(json, BROADCAST_CIPHER_KEY, "cipherKeyTest"); + (void)JSON_AddStringToObject(json, BROADCAST_CIPHER_IV, "cipherIv"); + (void)JSON_AddStringToObject(json, IRK, "peerIrk"); + (void)JSON_AddStringToObject(json, PUB_MAC, "pubMac"); + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + ASSERT_TRUE(memcpy_s(info.cipherInfo.key, SESSION_KEY_LENGTH, KEY_TEST, strlen(KEY_TEST)) == EOK); + ASSERT_TRUE(memcpy_s(info.cipherInfo.iv, BROADCAST_IV_LEN, IV_TEST, strlen(IV_TEST)) == EOK); + ASSERT_TRUE(memcpy_s(info.rpaInfo.peerIrk, LFINDER_IRK_LEN, PEER_IRK, PEER_IRK_LEN) == EOK); + ASSERT_TRUE(memcpy_s(info.rpaInfo.publicAddress, LFINDER_MAC_ADDR_LEN, + PUBLIC_ADDRESS, PUBLIC_ADDRESS_LEN) == EOK); + UnpackCipherRpaInfo(json, &info); + JSON_Delete(json); + JsonObj *json1 = JSON_CreateObject(); + EXPECT_TRUE(json1 != nullptr); + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + EXPECT_TRUE(PackCommon(json1, &info, SOFTBUS_OLD_V2, false) == SOFTBUS_OK); + EXPECT_TRUE(PackCommon(json1, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); + JSON_Delete(json1); +} + +/* + * @tc.name: UnpackWifiDirectInfo_TEST_001 + * @tc.desc: UnpackWifiDirectInfo test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnpackWifiDirectInfo_TEST_001, TestSize.Level1) +{ + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + (void)JSON_AddStringToObject(json, IRK, "peerIrk"); + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + ASSERT_TRUE(memcpy_s(info.staticCapability, STATIC_CAP_LEN, STATIC_CAPABILITY, CAPABILITY_LEN) == EOK); + UnpackWifiDirectInfo(json, &info); + JSON_AddInt32ToObject(json, STATIC_CAP_LENGTH, 10); + UnpackWifiDirectInfo(json, &info); + JSON_AddStringToObject(json, STATIC_CAP, "staticCap"); + UnpackWifiDirectInfo(json, &info); + JSON_AddStringToObject(json, PTK, "encodePtk"); + UnpackWifiDirectInfo(json, &info); + ASSERT_TRUE(memcpy_s(info.remotePtk, PTK_DEFAULT_LEN, REMOTE_PTK, strlen(REMOTE_PTK)) == EOK); + UnpackWifiDirectInfo(json, &info); + UnpackCommon(json, &info, SOFTBUS_OLD_V1, true); + JSON_AddStringToObject(json, DEVICE_TYPE, "TV"); + JSON_AddStringToObject(json, DEVICE_UUID, "123456"); + JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); + UnpackCommon(json, &info, SOFTBUS_OLD_V1, true); + UnpackCommon(json, &info, SOFTBUS_NEW_V1, false); + JSON_AddStringToObject(json, NODE_ADDR, "127.0.0.0"); + UnpackCommon(json, &info, SOFTBUS_NEW_V1, false); + char buf[10] = {0}; + EXPECT_TRUE(GetBtDiscTypeString(nullptr, buf, 10) == SOFTBUS_OK); + info.discoveryType = 11; + EXPECT_TRUE(GetBtDiscTypeString(&info, buf, 10) == SOFTBUS_OK); + info.discoveryType = 15; + EXPECT_TRUE(UnpackBt(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); + JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, 3); + EXPECT_TRUE(UnpackBt(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); + EXPECT_TRUE(PackWiFi(json, &info, SOFTBUS_NEW_V1, false) == SOFTBUS_OK); + JSON_Delete(json); +} + +/* + * @tc.name: CheckBusVersion_TEST_001 + * @tc.desc: CheckBusVersion test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, CheckBusVersion_TEST_001, TestSize.Level1) +{ + JsonObj *json1 = JSON_CreateObject(); + EXPECT_TRUE(json1 != nullptr); + JSON_AddInt32ToObject(json1, BUS_MAX_VERSION, -1); + JSON_AddInt32ToObject(json1, BUS_MIN_VERSION, -1); + EXPECT_TRUE(CheckBusVersion(json1) == SOFTBUS_ERR); + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + EXPECT_TRUE(UnpackWiFi(json1, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_ERR); + JSON_Delete(json1); + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + JSON_AddInt32ToObject(json, BUS_MAX_VERSION, 3); + JSON_AddInt32ToObject(json, BUS_MIN_VERSION, 0);; + EXPECT_TRUE(CheckBusVersion(json) == 2); + EXPECT_TRUE(UnpackWiFi(json, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_OK); + JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, 63); + JSON_AddStringToObject(json, BLE_OFFLINE_CODE, "123"); + EXPECT_TRUE(UnpackWiFi(json, &info, SOFTBUS_OLD_V1, false) == SOFTBUS_OK); + JSON_Delete(json); +} + +/* + * @tc.name: PackDeviceInfoBtV1_TEST_001 + * @tc.desc: PackDeviceInfoBtV1 test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, PackDeviceInfoBtV1_TEST_001, TestSize.Level1) +{ + NodeInfo info; + EXPECT_TRUE(PackDeviceInfoBtV1(nullptr, &info, false) == SOFTBUS_AUTH_REG_DATA_FAIL); + JsonObj *json = JSON_CreateObject(); + EXPECT_TRUE(json != nullptr); + EXPECT_TRUE(PackDeviceInfoBtV1(json, &info, false) == SOFTBUS_OK); + EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); + JSON_AddStringToObject(json, DEVICE_NAME, "testname"); + EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); + JSON_AddStringToObject(json, DEVICE_TYPE, "TV"); + EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); + JSON_AddStringToObject(json, DEVICE_UDID, "123456"); + JSON_AddStringToObject(json, UUID, "123456"); + JSON_AddStringToObject(json, BR_MAC_ADDR, "11:22:33:44:55"); + JSON_AddInt64ToObject(json, NEW_CONN_CAP, -1); + EXPECT_TRUE(UnpackDeviceInfoBtV1(json, &info) == SOFTBUS_OK); + JSON_Delete(json); +} + +/* + * @tc.name: UpdatePeerDeviceName_TEST_001 + * @tc.desc: UpdatePeerDeviceName test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UpdatePeerDeviceName_TEST_001, TestSize.Level1) +{ + NodeInfo peerNodeInfo; + (void)memset_s(&peerNodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + UpdatePeerDeviceName(&peerNodeInfo); + ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedName, + DEVICE_NAME_BUF_LEN, UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); + ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedDefaultName, + DEVICE_NAME_BUF_LEN, DEVICE_NAME_TEST, strlen(DEVICE_NAME_TEST)) == EOK); + UpdatePeerDeviceName(&peerNodeInfo); + ASSERT_TRUE(memcpy_s(peerNodeInfo.deviceInfo.unifiedDefaultName, + DEVICE_NAME_BUF_LEN, UNIFIED_NAME, strlen(UNIFIED_NAME)) == EOK); + UpdatePeerDeviceName(&peerNodeInfo); + AuthSessionInfo info; + EXPECT_TRUE(PostDeviceIdData(123, &info, nullptr, 0) == SOFTBUS_AUTH_SEND_FAIL); + info.isServer = false; + EXPECT_TRUE(PostBtV1DevId(123, &info) == SOFTBUS_ERR); + EXPECT_TRUE(PostWifiV1DevId(123, &info) == SOFTBUS_ERR); + info.isServer = true; + EXPECT_TRUE(PostBtV1DevId(123, &info) == SOFTBUS_ERR); + EXPECT_TRUE(PostWifiV1DevId(123, &info) == SOFTBUS_ERR); + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + EXPECT_TRUE(PostDeviceIdV1(123, &info) == SOFTBUS_ERR); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(PostDeviceIdV1(123, &info) == SOFTBUS_ERR); + EXPECT_TRUE(PostDeviceIdNew(123, &info) == SOFTBUS_ERR); + EXPECT_TRUE(PostCloseAckMessage(123, &info) == SOFTBUS_AUTH_SEND_FAIL); +} + +/* + * @tc.name: ProcessDeviceIdMessage_TEST_001 + * @tc.desc: ProcessDeviceIdMessage test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, ProcessDeviceIdMessage_TEST_001, TestSize.Level1) +{ + AuthSessionInfo info; + info.connInfo.type = AUTH_LINK_TYPE_WIFI; + uint8_t data[] = "test"; + EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_INVALID_PARAM); + info.connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN + 1) == SOFTBUS_INVALID_PARAM); + info.isServer = false; + EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_INVALID_PARAM); + info.isServer = true; + EXPECT_TRUE(ProcessDeviceIdMessage(&info, data, DEVICE_ID_STR_LEN) == SOFTBUS_OK); +} + +/* + * @tc.name: UnpackDeviceInfoMessage_TEST_001 + * @tc.desc: UnpackDeviceInfoMessage test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, UnpackDeviceInfoMessage_TEST_001, TestSize.Level1) +{ + DevInfoData devInfo; + NodeInfo nodeInfo; + AuthSessionInfo info; + (void)memset_s(&devInfo, sizeof(DevInfoData), 0, sizeof(DevInfoData)); + (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + (void)memset_s(&info, sizeof(AuthSessionInfo), 0, sizeof(AuthSessionInfo)); + JsonObj *obj = JSON_CreateObject(); + EXPECT_TRUE(obj != nullptr); + JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, EXCHANGE_FAIL); + char *msg = JSON_PrintUnformatted(obj); + devInfo.msg = msg; + devInfo.len = strlen(msg); + devInfo.linkType = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(UnpackDeviceInfoMessage(&devInfo, &nodeInfo, false, &info) == SOFTBUS_OK); + devInfo.linkType = AUTH_LINK_TYPE_WIFI; + nodeInfo.feature = 511; + EXPECT_TRUE(UnpackDeviceInfoMessage(&devInfo, &nodeInfo, false, &info) == SOFTBUS_ERR); + JSON_Delete(obj); +} + +/* + * @tc.name: IsDeviceMessagePacket_TEST_001 + * @tc.desc: IsDeviceMessagePacket test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, IsDeviceMessagePacket_TEST_001, TestSize.Level1) +{ + AuthConnInfo connInfo; + (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo)); + DeviceMessageParse messageParse = { 0 }; + connInfo.type = AUTH_LINK_TYPE_BLE; + EXPECT_TRUE(!IsDeviceMessagePacket(&connInfo, nullptr, nullptr, true, &messageParse)); + connInfo.type = AUTH_LINK_TYPE_WIFI; + EXPECT_TRUE(!IsDeviceMessagePacket(&connInfo, nullptr, nullptr, true, &messageParse)); +} + +/* + * @tc.name: PACK_FAST_AUTH_VALUE_TEST_001 + * @tc.desc: Pack fast auth value test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, PACK_FAST_AUTH_VALUE_TEST_001, TestSize.Level1) +{ + AuthDeviceKeyInfo deviceCommKey = {0}; + JsonObj *obj = JSON_CreateObject(); + ASSERT_NE(obj, nullptr); + uint32_t keyLen = 0; + deviceCommKey.keyLen = keyLen; + uint64_t ret = PackFastAuthValue(obj, &deviceCommKey); + EXPECT_EQ(ret, SOFTBUS_ERR); + JSON_Delete(obj); +} + +/* + * @tc.name: CHECK_BUS_VERSION_TEST_001 + * @tc.desc: check bus version test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, CHECK_BUS_VERSION_TEST_001, TestSize.Level1) +{ + JsonObj *obj = JSON_CreateObject(); + if (obj == NULL) { + return; + } + + NodeInfo *info = (NodeInfo*)SoftBusCalloc(sizeof(NodeInfo)); + if (info == NULL) { + JSON_Delete(obj); + return; + } + (void)memset_s(info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + + SoftBusVersion version = SOFTBUS_NEW_V1; + ASSERT_NE(obj, NULL); + if (!JSON_AddInt32ToObject(obj, "CODE", (int32_t)1) || + !JSON_AddInt32ToObject(obj, "BUS_MAX_VERSION", (int32_t)2) || + !JSON_AddInt32ToObject(obj, "BUS_MIN_VERSION", (int32_t)1) || + !JSON_AddInt32ToObject(obj, "AUTH_PORT", (int32_t)8710) || + !JSON_AddInt32ToObject(obj, "SESSION_PORT", (int32_t)26) || + !JSON_AddInt32ToObject(obj, "PROXY_PORT", (int32_t)80) || + !JSON_AddStringToObject(obj, "DEV_IP", "127.0.0.1")) { + JSON_Delete(obj); + return; + } + JSON_AddStringToObject(obj, BLE_OFFLINE_CODE, "10244"); + + info->connectInfo.authPort = 8710; + info->connectInfo.sessionPort = 26; + info->connectInfo.proxyPort = 80; + info->supportedProtocols = LNN_PROTOCOL_BR; + + int32_t ret = UnpackWiFi(obj, info, version, false); + EXPECT_TRUE(ret == SOFTBUS_OK); + JSON_AddInt32ToObject(obj, "BUS_MAX_VERSION", (int32_t)-1); + ret = UnpackWiFi(obj, info, version, false); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + (void)JSON_AddStringToObject(obj, "BROADCAST_CIPHER_KEY", "1222222222"); + (void)JSON_AddStringToObject(obj, "BROADCAST_CIPHER_IV", "1222222222"); + (void)JSON_AddStringToObject(obj, "IRK", "1222222222"); + (void)JSON_AddStringToObject(obj, "PUB_MAC", "1222222222"); + + JSON_AddStringToObject(obj, "MASTER_UDID", "1122334554444"); + JSON_AddStringToObject(obj, "NODE_ADDR", "1122334554444"); + UnpackCommon(obj, info, version, false); + version = SOFTBUS_OLD_V1; + JSON_AddInt32ToObject(obj, "MASTER_WEIGHT", (int32_t)10); + UnpackCommon(obj, info, version, true); + UnpackCipherRpaInfo(obj, info); + JSON_Delete(obj); + SoftBusFree(info); +} + +/* + * @tc.name: POST_BT_V1_DEVID_TEST_001 + * @tc.desc: post bt v1 devid test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(AuthSessionMessageTest, POST_BT_V1_DEVID_TEST_001, TestSize.Level1) +{ + int64_t authSeq = 0; + AuthSessionInfo *info = (AuthSessionInfo*)SoftBusCalloc(sizeof(AuthSessionInfo)); + if (info == NULL) { + return; + } + info->requestId = 1; + info->connId = 1; + info->isServer = false; + info->version = SOFTBUS_NEW_V1; + info->connInfo.type = AUTH_LINK_TYPE_WIFI; + int32_t ret = PostDeviceIdV1(authSeq, info); + EXPECT_TRUE(ret == SOFTBUS_ERR); + SoftBusFree(info); +} +} // namespace OHOS +