diff --git a/test/authenticationunittest/UTTest_auth_manager_new.cpp b/test/authenticationunittest/UTTest_auth_manager_new.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7afac906794e94f38448501d04133fa11b00293d --- /dev/null +++ b/test/authenticationunittest/UTTest_auth_manager_new.cpp @@ -0,0 +1,500 @@ +/* + * Copyright (c) 2025 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 "UTTest_auth_manager_new.h" +#include "dm_error_type.h" +#include "dm_constants.h" +#include "dm_ability_manager.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void AuthManagerTest::SetUp() +{ + authManager_->context_ = std::make_shared(); + authManager_->context_->listener = std::make_shared(); + authManager_->context_->hiChainAuthConnector = std::make_shared(); + authManager_->context_->authUiStateMgr = std::make_shared(authManager_->context_->listener); + authManager_->context_->authStateMachine = std::make_shared(authManager_->context_); +} + +void AuthManagerTest::TearDown() +{ + +} + +void AuthManagerTest::SetUpTestCase() +{ + appManagerMock_ = std::make_shared(); + DmAppManager::dmAppManager = appManagerMock_; +} + +void AuthManagerTest::TearDownTestCase() +{ + DmAppManager::dmAppManager = nullptr; + appManagerMock_ = nullptr; +} + +namespace { + +const int32_t DM_AUTH_TYPE_MAX = 10; +HWTEST_F(AuthManagerTest, ParseAuthType_001, testing::ext::TestSize.Level1) +{ + std::map bindParam; + int32_t authType = 1; + int32_t ret = authManager_->ParseAuthType(bindParam, authType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + bindParam.insert(std::make_pair(PARAM_KEY_AUTH_TYPE, "")); + ret = authManager_->ParseAuthType(bindParam, authType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + bindParam[PARAM_KEY_AUTH_TYPE] = "bindInfo"; + ret = authManager_->ParseAuthType(bindParam, authType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + bindParam[PARAM_KEY_AUTH_TYPE] = "b"; + ret = authManager_->ParseAuthType(bindParam, authType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + bindParam[PARAM_KEY_AUTH_TYPE] = "1"; + ret = authManager_->ParseAuthType(bindParam, authType); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(AuthManagerTest, GeneratePincode_001, testing::ext::TestSize.Level1) +{ + int32_t ret = authManager_->GeneratePincode(); + ASSERT_NE(ret, DM_OK); +} + +HWTEST_F(AuthManagerTest, RegisterUiStateCallback_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = "pkgName"; + int32_t ret = authManager_->RegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->authUiStateMgr = nullptr; + authManager_->UnRegisterUiStateCallback(pkgName); + ret = authManager_->RegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_001, testing::ext::TestSize.Level1) +{ + authManager_->context_->authUiStateMgr = nullptr; + std::string pkgName = "pkgName"; + int32_t ret = authManager_->UnRegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->authUiStateMgr = std::make_shared(authManager_->context_->listener); + ret = authManager_->UnRegisterUiStateCallback(pkgName); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(AuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = "pkgName"; + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + int32_t ret = authManager_->StopAuthenticateDevice(pkgName); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = authManager_->StopAuthenticateDevice(pkgName); + ASSERT_EQ(ret, DM_OK); + + authManager_->OnScreenLocked(); + int64_t requestId = 1; + uint8_t *sessionKey = nullptr; + uint32_t sessionKeyLen = 0; + authManager_->context_->authMessageProcessor = nullptr; + authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); + + authManager_->context_->authMessageProcessor = std::make_shared(); + authManager_->context_->authStateMachine = nullptr; + authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); + + authManager_->context_->authStateMachine = std::make_shared(authManager_->context_); + authManager_->context_->requestId = 0; + authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); + + authManager_->context_->requestId = 1; + uint8_t array[] = {1, 2, 3, 4, 5}; + sessionKey = array; + sessionKeyLen = static_cast(sizeof(array)); + authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); +} + +HWTEST_F(AuthManagerTest, ParseConnectAddr_001, testing::ext::TestSize.Level1) +{ + PeerTargetId targetId = { + .deviceId = "device****45", + .brMac = "172.0.0.1", + .bleMac = "125.0.0.1", + .wifiIp = "1.1.1.2", + .wifiPort = 1002 + }; + std::string deviceId = ""; + std::string addrType = "1"; + int32_t ret = authManager_->ParseConnectAddr(targetId, deviceId, addrType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + deviceId = "dww*******7"; + targetId.wifiIp = ""; + ret = authManager_->ParseConnectAddr(targetId, deviceId, addrType); + ASSERT_EQ(ret, DM_OK); + + targetId.brMac = ""; + ret = authManager_->ParseConnectAddr(targetId, deviceId, addrType); + ASSERT_EQ(ret, DM_OK); + + targetId.bleMac = ""; + ret = authManager_->ParseConnectAddr(targetId, deviceId, addrType); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(AuthManagerTest, IsAuthTypeSupported_001, testing::ext::TestSize.Level1) +{ + int32_t authType = 1; + bool ret = authManager_->IsAuthTypeSupported(authType); + ASSERT_FALSE(ret); + + authManager_->context_->authenticationMap.insert(std::make_pair(authType, nullptr)); + ret = authManager_->IsAuthTypeSupported(authType); + ASSERT_TRUE(ret); +} + +HWTEST_F(AuthManagerTest, IsAuthCodeReady_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = "importPkgName"; + authManager_->context_->importAuthCode = ""; + bool ret = authManager_->IsAuthCodeReady(pkgName); + ASSERT_FALSE(ret); + + authManager_->context_->importAuthCode = "importAuthCode"; + authManager_->context_->importPkgName = ""; + ret = authManager_->IsAuthCodeReady(pkgName); + ASSERT_FALSE(ret); + + authManager_->context_->importPkgName = "dewdef255"; + ret = authManager_->IsAuthCodeReady(pkgName); + ASSERT_FALSE(ret); + + authManager_->context_->importPkgName = pkgName; + ret = authManager_->IsAuthCodeReady(pkgName); + ASSERT_TRUE(ret); +} + +HWTEST_F(AuthManagerTest, CheckAuthParamVaild_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = "importPkgName"; + int32_t authType = -1; + std::string deviceId; + std::string extra; + int32_t ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_AUTH_FAILED); + + authType = DM_AUTH_TYPE_MAX; + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_AUTH_FAILED); + + authType = 1; + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + deviceId = "de********7"; + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + authType = 2; + authManager_->context_->authUiStateMgr = std::make_shared(authManager_->context_->listener); + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_UNSUPPORTED_AUTH_TYPE); + + authManager_->context_->authenticationMap.insert(std::make_pair(authType, nullptr)); + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + std::shared_ptr deviceInfo = std::make_shared(); + authManager_->context_->softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo); + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, DM_OK); + + authType = DmAuthType::AUTH_TYPE_IMPORT_AUTH_CODE; + authManager_->context_->importAuthCode = ""; + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + authManager_->context_->listener = nullptr; + ret = authManager_->CheckAuthParamVaild(pkgName, authType, deviceId, extra); + ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(AuthManagerTest, GetBundleName_001, testing::ext::TestSize.Level1) +{ + JsonObject jsonObject; + jsonObject[BUNDLE_NAME_KEY] = "bu*********lk"; + auto ret = authManager_->GetBundleName(jsonObject); + ASSERT_EQ(ret.empty(), false); + + jsonObject[PARAM_KEY_CONN_SESSIONTYPE] = "keyConnSession"; + authManager_->ParseHmlInfoInJsonObject(jsonObject); + + jsonObject[PARAM_KEY_CONN_SESSIONTYPE] = CONN_SESSION_TYPE_HML; + jsonObject[PARAM_KEY_HML_ENABLE_160M] = true; + jsonObject[PARAM_KEY_HML_ACTIONID] = 1; + authManager_->ParseHmlInfoInJsonObject(jsonObject); + + jsonObject[APP_OPERATION_KEY] = "appkey"; + jsonObject[CUSTOM_DESCRIPTION_KEY] = "customkey"; + jsonObject[APP_THUMBNAIL] = "thumbnail"; + jsonObject[PARAM_CLOSE_SESSION_DELAY_SECONDS] = "closesession"; + jsonObject[TAG_BIND_LEVEL] = 1; + jsonObject[TAG_PEER_BUNDLE_NAME] = "peerbindInfo"; + authManager_->ParseJsonObject(jsonObject); + + jsonObject[TAG_PEER_BUNDLE_NAME] = ""; + authManager_->ParseJsonObject(jsonObject); +} + +HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level1) +{ + int32_t bindLevel = 1; + EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(false)); + int32_t ret = authManager_->GetBindLevel(bindLevel); + EXPECT_EQ(ret, 3); + + EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(true)); + int32_t ret = authManager_->GetBindLevel(bindLevel); + EXPECT_EQ(ret, 1); + + bindLevel = 0; + EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(true)); + int32_t ret = authManager_->GetBindLevel(bindLevel); + EXPECT_EQ(ret, 1); + + bindLevel = 5; + EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(true)); + int32_t ret = authManager_->GetBindLevel(bindLevel); + EXPECT_EQ(ret, 1); + + bindLevel = 0; + EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(false)); + int32_t ret = authManager_->GetBindLevel(bindLevel); + EXPECT_EQ(ret, 3); + + JsonObject jsonObject; + jsonObject[APP_OPERATION_KEY] = "appkey"; + jsonObject[CUSTOM_DESCRIPTION_KEY] = "customkey"; + jsonObject[APP_THUMBNAIL] = "thumbnail"; + jsonObject[PARAM_CLOSE_SESSION_DELAY_SECONDS] = "closesession"; + jsonObject[TAG_BIND_LEVEL] = 1; + jsonObject[TAG_PEER_BUNDLE_NAME] = "peerbindInfo"; + std::string pkgName = "pkgName"; + int32_t authType = DmAuthType::AUTH_TYPE_PIN; + std::string deviceId = "deviceId"; + std::string extra = jsonObject.Dump(); + authManager_->GetAuthParam(pkgName, authType, deviceId, extra); +} + +HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = "pkgName"; + int32_t authType = 1; + std::string deviceId = ""; + std::string extra = "sdsas565656"; + int32_t ret = authManager_->AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + deviceId = "deviceId***3"; + authManager_->context_->listener = std::make_shared(); + authManager_->context_->authUiStateMgr = std::make_shared(authManager_->context_->listener); + authManager_->context_->authenticationMap.insert(std::make_pair(authType, nullptr)); + std::shared_ptr deviceInfo = std::make_shared(); + authManager_->context_->softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo); + ret = authManager_->AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, DM_OK); + + JsonObject jsonObject; + jsonObject[TAG_BIND_LEVEL] = 5; + extra = jsonObject.Dump(); + ret = authManager_->AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + authManager_->InitAuthState(pkgName, authType, deviceId, extra); +} + +HWTEST_F(AuthManagerTest, BindTarget_001, testing::ext::TestSize.Level1) +{ + std::string pkgName = ""; + PeerTargetId targetId = { + .deviceId = "de******312", + .brMac = "1.2.3.5", + .bleMac = "1.1.0.2", + .wifiIp = "172.1.1.0", + .wifiPort = 10 + }; + std::map bindParam; + int32_t ret = authManager_->BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + pkgName = "p*******kki"; + ret = authManager_->BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + + bindParam.insert(std::make_pair(PARAM_KEY_AUTH_TYPE, "1")); + ret = authManager_->BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + bindParam.insert(std::make_pair(PARAM_KEY_CONN_ADDR_TYPE, "2")); + ret = authManager_->BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + PeerTargetId targetIdInfo; + ret = authManager_->BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1) +{ + authManager_->context_->authStateMachine = std::make_shared(authManager_->context_); + int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH; + std::string params = "skjcksdj"; + int32_t ret = authSinkManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + action = USER_OPERATION_TYPE_AUTH_CONFIRM_TIMEOUT; + ret = authSinkManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + action = USER_OPERATION_TYPE_CANCEL_PINCODE_DISPLAY; + ret = authSinkManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + action = USER_OPERATION_TYPE_CANCEL_PINCODE_INPUT; + ret = authSinkManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + int64_t requestId = 1; + int32_t errorCode = 1; + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SINK_PIN_AUTH_START_STATE); + authManager_->context_->authType = DmAuthType::AUTH_TYPE_PIN_SHOW; + authSinkManager_->AuthDeviceError(requestId, errorCode); + + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SINK_PIN_AUTH_MSG_NEGOTIATE_STATE); + authSinkManager_->AuthDeviceError(requestId, errorCode); + + authManager_->context_->authType = DmAuthType::AUTH_TYPE_PIN_IMPORT; + authManager_->context_->fallBackToInputPin = false; + authSinkManager_->AuthDeviceError(requestId, errorCode); + + +} + +HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level1) +{ + authManager_->context_->authStateMachine = std::make_shared(authManager_->context_); + int32_t action = USER_OPERATION_TYPE_CANCEL_PINCODE_INPUT; + std::string params = "skjcksdj"; + int32_t ret = authSrcManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + action = USER_OPERATION_TYPE_AUTH_CONFIRM_TIMEOUT; + ret = authSrcManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + action = USER_OPERATION_TYPE_DONE_PINCODE_INPUT; + ret = authSrcManager_->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); + + int64_t requestId = 1; + int32_t errorCode = 1; + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SRC_PIN_AUTH_START_STATE); + authManager_->context_->authType = DmAuthType::AUTH_TYPE_PIN_SHOW; + authSrcManager_->AuthDeviceError(requestId, errorCode); + + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SRC_PIN_AUTH_MSG_NEGOTIATE_STATE); + authSrcManager_->AuthDeviceError(requestId, errorCode); + + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SRC_PIN_AUTH_DONE_STATE); + authSrcManager_->AuthDeviceError(requestId, errorCode); + + authManager_->context_->authType = DmAuthType::AUTH_TYPE_PIN_IMPORT; + authManager_->context_->fallBackToInputPin = false; + authSrcManager_->AuthDeviceError(requestId, errorCode); +} + +HWTEST_F(AuthManagerTest, AuthDeviceTransmit_001, testing::ext::TestSize.Level1) +{ + int64_t requestId = 1; + uint8_t *data = nullptr; + uint32_t dataLen = 0; + authManager_->context_->requestId = 0; + bool ret = authSrcManager_->AuthDeviceTransmit(requestId, data, dataLen); + EXPECT_FALSE(ret); + + uint8_t arr[5] = {0, 1, 2, 3, 4}; + dataLen = static_cast(sizeof(arr)); + authManager_->context_->requestId = 1; + ret = authSrcManager_->AuthDeviceTransmit(requestId, data, dataLen); + EXPECT_TRUE(ret); + + authManager_->context_->authStateMachine->SetCurState(DmAuthStateType::AUTH_SRC_PIN_AUTH_DONE_STATE); + authSrcManager_->AuthDeviceFinish(requestId); +} + +HWTEST_F(AuthManagerTest, AuthDeviceTransmit_002, testing::ext::TestSize.Level1) +{ + int64_t requestId = 1; + uint8_t *data = nullptr; + uint32_t dataLen = 0; + authManager_->context_->requestId = 0; + bool ret = authSinkManager_->AuthDeviceTransmit(requestId, data, dataLen); + EXPECT_FALSE(ret); + + uint8_t arr[5] = {0, 1, 2, 3, 4}; + data = arr; + dataLen = static_cast(sizeof(arr)); + authManager_->context_->requestId = 1; + ret = authSinkManager_->AuthDeviceTransmit(requestId, data, dataLen); + EXPECT_TRUE(ret); + + authSinkManager_->AuthDeviceFinish(requestId); + + uint8_t *sessionKey = arr; + uint32_t sessionKeyLen = static_cast(sizeof(arr)); + authSinkManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); + + authManager_->context_->authMessageProcessor = nullptr; + authSinkManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); +} + +HWTEST_F(AuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level1) +{ + int32_t code = 1; + int32_t ret = GetPinCode(code); + EXPECT_EQ(ret, DM_OK); + + authManager_->context_ = nullptr; + ret = GetPinCode(code); + EXPECT_EQ(ret, ERR_DM_FAILED); +} +} +} +} \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_auth_manager_new.h b/test/authenticationunittest/UTTest_auth_manager_new.h new file mode 100644 index 0000000000000000000000000000000000000000..8690f09d888d9977ebc701372961282bee70c33c --- /dev/null +++ b/test/authenticationunittest/UTTest_auth_manager_new.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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. + */ +#ifndef OHOS_UTTEST_AUTH_MANAGER_NEW_TEST_H +#define OHOS_UTTEST_AUTH_MANAGER_NEW_TEST_H + +#include +#include +#include "auth_manager.h" +#include "softbus_connector.h" +#include "hichain_connector.h" +#include "device_manager_service_listener.h" +#include "app_manager_mock.h" +#include + +namespace OHOS { +namespace DistributedHardware { +class AuthManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr hiChainAuthConnector = std::make_shared(); + + std::shared_ptr authManager_ = + std::make_shared(softbusConnector, listener, hiChainAuthConnector); + std::shared_ptr authSinkManager_ = std::make_shared(); + std::shared_ptr authSrcManager_ = std::make_shared(); + static inline std::shared_ptr appManagerMock_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_context.cpp b/test/authenticationunittest/UTTest_dm_auth_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f219ec09bf99e09660ef8470bc48ed89380f355a --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_context.cpp @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2025 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 "UTTest_dm_auth_context.h" +#include "dm_error_type.h" +#include "dm_constants.h" +#include "dm_ability_manager.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void DmAuthContextTest::SetUp() +{ +} + +void DmAuthContextTest::TearDown() +{ +} + +void DmAuthContextTest::SetUpTestCase() +{ +} + +void DmAuthContextTest::TearDownTestCase() +{ +} + +namespace { + +HWTEST_F(DmAuthContextTest, GetDeviceId_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + auto ret = dmAuthContext_->GetDeviceId(side); + ASSERT_EQ(ret.empty(), true); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = dmAuthContext_->GetDeviceId(side); + ASSERT_EQ(ret.empty(), true); + + side = static_cast(2); + ret = dmAuthContext_->GetDeviceId(side); + ASSERT_EQ(ret.empty(), true); +} + +HWTEST_F(DmAuthContextTest, GetUserId_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + int32_t ret = dmAuthContext_->GetUserId(side); + ASSERT_EQ(ret, DM_OK); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = dmAuthContext_->GetUserId(side); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthContextTest, GetCredentialId_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + std::string ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + side = static_cast(2); + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; + ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); +} + +HWTEST_F(DmAuthContextTest, GetPublicKey_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + auto ret = dmAuthContext_->GetPublicKey(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->GetPublicKey(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + side = DM_AUTH_REMOTE_SIDE; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = dmAuthContext_->GetPublicKey(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->GetPublicKey(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); + + side = static_cast(2); + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; + ret = dmAuthContext_->GetCredentialId(side, authorizedScope); + ASSERT_EQ(ret.empty(), true); +} + +HWTEST_F(DmAuthContextTest, SetCredentialId_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + std::string credentialId = "credential******44"; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + int32_t ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + side = static_cast(2); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, ERR_DM_FAILED); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + authorizedScope = DM_AUTH_SCOPE_DEVICE; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthContextTest, SetPublicKey_001, testing::ext::TestSize.Level1) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + std::string publicKey = "pub******6"; + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SOURCE; + int32_t ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + side = static_cast(2); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, ERR_DM_FAILED); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + authorizedScope = DM_AUTH_SCOPE_DEVICE; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, ERR_DM_FAILED); +} +} +} +} \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_context.h b/test/authenticationunittest/UTTest_dm_auth_context.h new file mode 100644 index 0000000000000000000000000000000000000000..908628a24a917cada9e7016d7bf8bbac07308dca --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_context.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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. + */ +#ifndef OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H +#define OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H + +#include +#include +#include "dm_auth_context.h" +#include + +namespace OHOS { +namespace DistributedHardware { +class DmAuthContextTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr dmAuthContext_ = std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f87a973f1e98644de3feb76cf1aece63902f9a05 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp @@ -0,0 +1,789 @@ +/* + * Copyright (c) 2025 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 "UTTest_dm_auth_message_processor.h" +#include "dm_error_type.h" +#include "dm_constants.h" +#include "dm_ability_manager.h" +#include "dm_auth_context.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { +constexpr int32_t DM_MSG_TYPE_REQ_CREDENTIAL_EXCHANGE = 140; +constexpr int32_t DM_MSG_TYPE_RESP_CREDENTIAL_EXCHANGE = 150; +constexpr int32_t DM_MSG_TYPE_REQ_CREDENTIAL_AUTH_START = 160; +constexpr int32_t DM_MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE = 161; +constexpr int32_t DM_MSG_TYPE_REQ_DATA_SYNC = 180; +constexpr int32_t DM_MSG_TYPE_RESP_DATA_SYNC = 190; + +void SetUp() +{ +} + +void TearDown() +{ +} + +void SetUpTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; + cryptoMgrMock_ = std::make_shared(); + DmCryptoMgr::dmCryptoMgr = cryptoMgrMock_; + DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; +} + +void TearDownTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr; + distributedDeviceProfileClientMock_ = nullptr; + DmCryptoMgr::dmCryptoMgr = nullptr; + cryptoMgrMock_ = nullptr; + DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; + deviceProfileConnectorMock_ = nullptr; +} + +namespace { + +HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKey_001, testing::ext::TestSize.Level1) +{ + uint8_t arraySessionkey[] = {1, 2, 3, 4, 5}; + uint8_t *sessionKey = arraySessionkey; + uint32_t keyLen = static_cast(sizeof(arraySessionkey) / sizeof(arraySessionkey[0])); + dmAuthMessageProcessor_->cryptoMgr_ = nullptr; + int32_t ret = dmAuthMessageProcessor_->SaveSessionKey(arraySessionkey, keyLen); + ASSERT_EQ(ret, ERR_DM_FAILED); + + dmAuthMessageProcessor_->cryptoMgr_ = std::make_shared(); + ret = dmAuthMessageProcessor_->SaveSessionKey(arraySessionkey, keyLen); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKeyToDP_001, testing::ext::TestSize.Level1) +{ + int32_t skId = 1; + dmAuthMessageProcessor_->cryptoMgr_ = nullptr; + int32_t ret = dmAuthMessageProcessor_->SaveSessionKeyToDP(skId); + ASSERT_EQ(ret, ERR_DM_FAILED); + + dmAuthMessageProcessor_->cryptoMgr_ = std::make_shared(); + ret = dmAuthMessageProcessor_->SaveSessionKeyToDP(skId); + ASSERT_NE(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, PutAccessControlList_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + DmAccess access; + std::string trustDeviceId = "tru**********90"; + context->accesser.accountId = "ohosAnonymousUid"; + context->authResult = UiAction::USER_OPERATION_TYPE_ALLOW_AUTH_ALWAYS; + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutAccessControlProfile(_)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = dmAuthMessageProcessor_->PutAccessControlList(context, access, trustDeviceId); + ASSERT_EQ(ret, ERR_DM_FAILED); + + context->accessee.accountId == "ohosAnonymousUid"; + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutAccessControlProfile(_)).WillOnce(Return(ERR_DM_FAILED)); + ret = dmAuthMessageProcessor_->PutAccessControlList(context, access, trustDeviceId); + ASSERT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutAccessControlProfile(_)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->PutAccessControlList(context, access, trustDeviceId); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessage_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = nullptr; + std::string message = ""; + int32_t ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + context = std::make_shared(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + nlohmann::json jsonObject; + jsonObject[TAG_MSG_TYPE] = "msg_type"; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = 1; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_REQ_CREDENTIAL_AUTH_START; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_REQ_CREDENTIAL_EXCHANGE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_RESP_CREDENTIAL_EXCHANGE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_REQ_DATA_SYNC; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_RESP_DATA_SYNC; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageNegotiateTransmit_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject["hello"] = 1; + ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = 1; + ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "sddssaappllkkow558"; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(ERR_DM_FAILED)); + ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + std::string plainText = ""; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "s**********58"; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageNegotiateTransmit(jsonObject, context); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageOnTransmit_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageOnTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = 1; + ret = dmAuthMessageProcessor_->ParseMessageOnTransmit(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "wsxc*******3"; + ret = dmAuthMessageProcessor_->ParseMessageOnTransmit(jsonObject, context); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqCredExchange_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = 1; + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "q*******3"; + std::string plainText = ""; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(ERR_DM_FAILED))); + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USER_PUBLICK_KEY] = 1; + plainText = jsonObject.dump(); + context->isOnline = false; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USER_PUBLICK_KEY] = "userpublickey"; + plainText = jsonObject.dump(); + context->isOnline = false; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + context->isOnline = true; + jsonObject[DM_TAG_APP_PUBLICK_KEY] = "dssdasad"; + jsonObject[DM_TAG_DEVICE_ID] = "dssdalpl2344532"; + jsonObject[DM_TAG_PEER_USER_SPACE_ID] = 1; + jsonObject[DM_TAG_TOKEN_ID] = 1001; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, DM_OK); + + jsonObject[DM_TAG_APP_PUBLICK_KEY] = 1; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageReqCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRspCredExchange_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = 1; + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "q*******3"; + std::string plainText = ""; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(ERR_DM_FAILED))); + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USER_PUBLICK_KEY] = 1; + plainText = jsonObject.dump(); + context->isOnline = false; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USER_PUBLICK_KEY] = "userpublickey"; + jsonObject[DM_TAG_USER_CREDENTIAL_ID] = "credential**9"; + plainText = jsonObject.dump(); + context->isOnline = false; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); + + context->isOnline = true; + jsonObject[DM_TAG_APP_PUBLICK_KEY] = "dssdasad"; + jsonObject[DM_TAG_APP_CREDENTIAL_ID] = "appcredential**9"; + jsonObject[DM_TAG_DEVICE_ID] = "dssdalpl2344532"; + jsonObject[DM_TAG_PEER_USER_SPACE_ID] = 1; + jsonObject[DM_TAG_TOKEN_ID] = 1001; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, DM_OK); + + jsonObject[DM_TAG_APP_PUBLICK_KEY] = 1; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.Level1) +{ + DmMessageType msgType = DmMessageType::MSG_TYPE_REQ_CREDENTIAL_EXCHANGE; + std::shared_ptr context = std::make_shared(); + auto ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); + + msgType = DmMessageType::MSG_TYPE_RESP_CREDENTIAL_EXCHANGE; + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); + + msgType = DmMessageType::MSG_TYPE_REQ_CREDENTIAL_AUTH_START; + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); + + std::string encryptMsg = ""; + msgType = DmMessageType::MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE; + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(encryptMsg), Return(ERR_DM_FAILED))); + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), true); + + encryptMsg = "msgijkkjoiodo090"; + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(encryptMsg), Return(DM_OK))); + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); + + msgType = DmMessageType::MSG_TYPE_REQ_DATA_SYNC; + std::vector profiles; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), true); + + DistributedDeviceProfile::AccessControlProfile accessProfile; + DistributedDeviceProfile::Accessee accessee; + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserDeviceId("remoteUdid"); + accesser.SetAccesserUserId(DM_MSG_TYPE_REQ_DATA_SYNC); + accessee.SetAccesseeDeviceId("localUdid"); + accessee.SetAccesseeUserId(DM_MSG_TYPE_RESP_DATA_SYNC); + context->accesser.deviceId = "remoteUdid" + context->accesser.userId = DM_MSG_TYPE_REQ_DATA_SYNC; + context->accessee.deviceId = "localUdid" + context->accessee.userId = DM_MSG_TYPE_RESP_DATA_SYNC; + accessProfile.SetAccesserId(1); + accessProfile.SetAccesseeId(2); + accessProfile.SetTrustDeviceId("trustDeviceId"); + accessProfile.SetBindType(1); + accessProfile.SetAuthenticationType(2); + accessProfile.SetDeviceIdType(1); + accessProfile.SetStatus(1); + accessProfile.SetBindLevel(1); + accessProfile.SetAccesser(accesser); + accessProfile.SetAccessee(accessee); + profiles.push_back(accessProfile); + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), true); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_002, testing::ext::TestSize.Level1) +{ + DmMessageType msgType = DmMessageType::MSG_TYPE_RESP_DATA_SYNC; + std::shared_ptr context = std::make_shared(); + auto ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); + + msgType = DmMessageType::MSG_TYPE_AUTH_FINISH; + auto ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), false); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateCredentialNegotiateMessage_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = dmAuthMessageProcessor_->CreateCredentialNegotiateMessage(context, jsonObject); + ASSERT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)).WillOnce(Return(DM_OK)); + int32_t ret = dmAuthMessageProcessor_->CreateCredentialNegotiateMessage(context, jsonObject); + ASSERT_EQ(ret, DM_OK); + + context->isOnline = false; + dmAuthMessageProcessor_->CreateMessageReqCredExchange(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageRspCredExchange(context, jsonObject); + dmAuthMessageProcessor_->CreateNegotiateMessage(context, jsonObject); + + jsonObject[DM_TAG_ON_TRANSMIT_DATA] = "transmit*****6"; + context->extraInfo = jsonObject.dump(); + context->isAppCredentialVerified = false; + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)).WillOnce(Return(ERR_DM_FAILED)); + dmAuthMessageProcessor_->CreateMessageReqCredAuthStart(context, jsonObject); + + context->isAppCredentialVerified = true; + EXPECT_CALL(*cryptoMgrMock_, EncryptMessage(_, _)).WillOnce(Return(DM_OK)); + dmAuthMessageProcessor_->CreateMessageReqCredAuthStart(context, jsonObject); + + context->extraInfo = ""; + dmAuthMessageProcessor_->CreateMessageReqCredAuthStart(context, jsonObject); +} + +HWTEST_F(DmAuthMessageProcessorTest, ChecksumAcl_001, testing::ext::TestSize.Level1) +{ + DistributedDeviceProfile::AccessControlProfile acl; + std::string ret = dmAuthMessageProcessor_->ChecksumAcl(acl); + EXPECT_EQ(ret.empty(), true); + + acl.SetAccesserId(1); + acl.SetAccesseeId(2); + acl.SetTrustDeviceId("trustDeviceId"); + acl.SetBindType(1); + acl.SetAuthenticationType(2); + acl.SetDeviceIdType(1); + acl.SetStatus(1); + acl.SetBindLevel(1); + ret = dmAuthMessageProcessor_->ChecksumAcl(acl); + EXPECT_EQ(ret.empty(), true); + + std::shared_ptr context = std::make_shared(); + nlohmann::json jsonObject; + DistributedDeviceProfile::Accessee accessee; + DistributedDeviceProfile::Accesser accesser; + std::vector profiles; + accesser.SetAccesserDeviceId("remoteUdid"); + accesser.SetAccesserUserId(DM_MSG_TYPE_REQ_DATA_SYNC); + accessee.SetAccesseeDeviceId("localUdid"); + accessee.SetAccesseeUserId(DM_MSG_TYPE_RESP_DATA_SYNC); + context->accesser.deviceId = "remoteUdid" + context->accesser.userId = DM_MSG_TYPE_REQ_DATA_SYNC; + context->accessee.deviceId = "localUdid" + context->accessee.userId = DM_MSG_TYPE_RESP_DATA_SYNC; + acl.SetAccesser(accesser); + acl.SetAccessee(accessee); + profiles.push_back(acl); + context->direction = DmAuthDirection::DM_AUTH_SINK; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + dmAuthMessageProcessor_->CreateMessageSyncResp(context, jsonObject); + + context->direction = DmAuthDirection::DM_AUTH_SOURCE; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + dmAuthMessageProcessor_->CreateMessageSyncResp(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageFinish(context, jsonObject); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseSyncMessage_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + DmAccess access; + nlohmann::json jsonObject; + int32_t ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USERSKID] = "12"; + ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USERSK_TIMESTAMP] = "10"; + ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DMVERSION] = "1.100"; + ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_ACCESS] = "accesser"; + ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + std::vector vec{"acl", "accesser", "accessee"}; + jsonObject[DM_TAG_PROXY] = "proxyValues"; + jsonObject[DM_TAG_ACL_CHECKSUM] = vec; + jsonObject[DM_TAG_SERVICEINFO] = "serviceInfo"; + jsonObject[DM_TAG_ACCESS] = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseSyncMessage(context, access, jsonObject); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, DecryptSyncMessage_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + DmAccess access; + std::string enSyncMsg = "enMessage"; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + nlohmann::json jsonObject; + jsonObject[DM_TAG_COMPRESS_ORI_LEN] = "compress"; + enSyncMsg = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_COMPRESS_ORI_LEN] = 10; + enSyncMsg = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_COMPRESS] = 1; + enSyncMsg = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_COMPRESS] = "compresstag"; + enSyncMsg = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(DM_OK)); + ret = dmAuthMessageProcessor_->DecryptSyncMessage(context, enSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageSyncReq_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageSyncReq(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_SYNC] = "syncInfo"; + ret = dmAuthMessageProcessor_->ParseMessageSyncReq(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageSyncResp_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageSyncResp(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_SYNC] = "syncInfo"; + ret = dmAuthMessageProcessor_->ParseMessageSyncResp(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageFinish_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + jsonObject[DM_TAG_REPLY] = 1; + jsonObject[DM_TAG_STATE] = 1; + jsonObject[DM_TAG_REASON] = 1; + int32_t ret = dmAuthMessageProcessor_->ParseMessageFinish(jsonObject, context); + EXPECT_EQ(ret, DM_OK); + + jsonObject[TAG_DEVICE_VERSION] = "version"; + jsonObject[TAG_DEVICE_NAME] = "deviceName"; + jsonObject[TAG_DEVICE_ID_HASH] = "d*********9"; + jsonObject[TAG_USER_ID_HASH] = "d*********3"; + jsonObject[TAG_ACCOUNT_ID_HASH] = "d********33"; + jsonObject[TAG_TOKEN_ID_HASH] = "t********3"; + jsonObject[TAG_BUNDLE_NAME] = "b********Info"; + jsonObject[TAG_PEER_BUNDLE_NAME] = "peerBundName"; + jsonObject[TAG_BIND_LEVEL] = 1; + dmAuthMessageProcessor_->ParseNegotiateMessage(jsonObject, context); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqUserConfirm_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageReqUserConfirm(jsonObject, context); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRespUserConfirm_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + jsonObject[TAG_AUTH_TYPE] = 1; + jsonObject[TAG_REQUEST_ID] = 1; + std::shared_ptr context = std::make_shared(); + context->authType = DmAuthType::AUTH_TYPE_PIN_SHOW; + int32_t ret = dmAuthMessageProcessor_->ParseMessageReqUserConfirm(jsonObject, context); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqPinAuthStart_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageReqPinAuthStart(jsonObject, context); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRespPinAuthStart_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageRespPinAuthStart(jsonObject, context); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqPinAuthNegotiate_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseMessageReqPinAuthNegotiate(jsonObject, context); + EXPECT_EQ(ret, DM_OK); + + context->authType = DmAuthType::AUTH_TYPE_PIN_SHOW; + dmAuthMessageProcessor_->CreateMessageRespUserConfirm(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageReqPinAuthStart(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageRespPinAuthStart(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageReqPinAuthNegotiate(context, jsonObject); + dmAuthMessageProcessor_->CreateMessageRespPinAuthNegotiate(context, jsonObject); + DmMessageType msgType = DmMessageType::MSG_TYPE_AUTH_TERMINATE; + dmAuthMessageProcessor_->CreateAndSendMsg(msgType, context); +} + +HWTEST_F(DmAuthMessageProcessorTest, CompressSyncMsg_001, testing::ext::TestSize.Level1) +{ + std::string inputStr = "input********iuweuiu"; + std::string ret = dmAuthMessageProcessor_->CompressSyncMsg(inputStr); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DmAuthMessageProcessorTest, CompressSyncMsg_001, testing::ext::TestSize.Level1) +{ + std::string inputStr = "input********iuweuiu"; + std::string ret = dmAuthMessageProcessor_->CompressSyncMsg(inputStr); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DmAuthMessageProcessorTest, DecompressSyncMsg_001, testing::ext::TestSize.Level1) +{ + std::string compressed = "compre*******kjdnwsdj"; + uint32_t oriLen = static_cast(compressed.length()); + std::string ret = dmAuthMessageProcessor_->DecompressSyncMsg(inputStr); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DmAuthMessageProcessorTest, EncryptSyncMessage_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + context->isOnline = false; + std::vector aclList{"accessInfo1"}; + DmAccess accessSide; + std::string encSyncMsg = "en*********msg"; + int32_t ret = dmAuthMessageProcessor_->EncryptSyncMessage(context, aclList, accessSide, encSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); + + context->isOnline = true; + ret = dmAuthMessageProcessor_->EncryptSyncMessage(context, aclList, accessSide, encSyncMsg); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthMessageProcessorTest, ACLToStr_001, testing::ext::TestSize.Level1) +{ + DistributedDeviceProfile::AccessControlProfile acl; + std::string aclStr = ""; + int32_t ret = dmAuthMessageProcessor_->ACLToStr(acl, aclStr); + EXPECT_EQ(ret, ERR_DM_FAILED); + + acl.SetAccesserId(1); + acl.SetAccesseeId(2); + acl.SetTrustDeviceId("trustDeviceId"); + acl.SetBindType(1); + acl.SetAuthenticationType(2); + acl.SetDeviceIdType(1); + acl.SetStatus(1); + acl.SetBindLevel(1); + ret = dmAuthMessageProcessor_->ACLToStr(acl, aclStr); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateSyncMessage_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + nlohmann::json jsonObject; + int32_t ret = dmAuthMessageProcessor_->CreateSyncMessage(context, jsonObject); + EXPECT_EQ(ret, ERR_DM_FAILED); + + DistributedDeviceProfile::AccessControlProfile acl; + acl.SetAccesserId(1); + acl.SetAccesseeId(2); + acl.SetTrustDeviceId("trustDeviceId"); + acl.SetBindType(1); + acl.SetAuthenticationType(2); + acl.SetDeviceIdType(1); + acl.SetStatus(1); + acl.SetBindLevel(1); + DistributedDeviceProfile::Accessee accessee; + DistributedDeviceProfile::Accesser accesser; + std::vector profiles; + accesser.SetAccesserDeviceId("remoteUdid"); + accesser.SetAccesserUserId(DM_MSG_TYPE_REQ_DATA_SYNC); + accessee.SetAccesseeDeviceId("localUdid"); + accessee.SetAccesseeUserId(DM_MSG_TYPE_RESP_DATA_SYNC); + context->accesser.deviceId = "remoteUdid" + context->accesser.userId = DM_MSG_TYPE_REQ_DATA_SYNC; + context->accessee.deviceId = "localUdid" + context->accessee.userId = DM_MSG_TYPE_RESP_DATA_SYNC; + acl.SetAccesser(accesser); + acl.SetAccessee(accessee); + profiles.push_back(acl); + context->direction = DmAuthDirection::DM_AUTH_SINK; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + ret = dmAuthMessageProcessor_->CreateSyncMessage(context, jsonObject); + EXPECT_NE(ret, DM_OK); + + context->direction = DmAuthDirection::DM_AUTH_SOURCE; + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + ret = dmAuthMessageProcessor_->CreateSyncMessage(context, jsonObject); + EXPECT_NE(ret, DM_OK); +} + +HWTEST_F(DmAuthMessageProcessorTest, GetTransmitFromContext_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared() + context->extraInfo = "extra"; + auto ret = dmAuthMessageProcessor_->GetTransmitFromContext(context); + EXPECT_TRUE(ret.empty()); + + nlohmann::json jsonObject; + jsonObject[DM_TAG_DATA] = "dataInfo"; + context->extraInfo = jsonObject.dump(); + ret = dmAuthMessageProcessor_->GetTransmitFromContext(context); + EXPECT_FALSE(ret.empty()); +} + +HWTEST_F(DmAuthMessageProcessorTest, ParseAuthStartMessgae_001, testing::ext::TestSize.Level1) +{ + nlohmann::json jsonObject; + std::shared_ptr context = std::make_shared(); + int32_t ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_DATA] = "data********mjdsj1"; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(Return(ERR_DM_FAILED)); + ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + std::string plainText = ""; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + plainText = jsonObject.dump(); + context->isOnline = false; + context->isAppCredentailVerified = false; + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, ERR_DM_FAILED); + + jsonObject[DM_TAG_USER_CREDENTIAL_ID] = "credential***13"; + context->isAppCredentailVerified = true; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, DM_OK); + + context->isOnline = true; + jsonObject[DM_TAG_APP_CREDENTIAL_ID] = "credential***14"; + plainText = jsonObject.dump(); + EXPECT_CALL(*cryptoMgrMock_, DecryptMessage(_, _)).WillOnce(DoAll(SetArgReferee<1>(plainText), Return(DM_OK))); + ret = dmAuthMessageProcessor_->ParseAuthStartMessgae(jsonObject, context); + EXPECT_EQ(ret, DM_OK); +} +} +} +} \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.h b/test/authenticationunittest/UTTest_dm_auth_message_processor.h new file mode 100644 index 0000000000000000000000000000000000000000..49e2b6681960e7f90f6f379dfb2e18aa0559f89a --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 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. + */ +#ifndef OHOS_UTTEST_DM_AUTH_MESSAGE_PROCESSOR_TEST_H +#define OHOS_UTTEST_DM_AUTH_MESSAGE_PROCESSOR_TEST_H + +#include +#include +#include "dm_auth_message_processor.h" +#include +#include "distributed_device_profile_client_mock.h" +#include "crypto_mgr_mock.h" +#include "deviceprofile_connector_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAuthMessageProcessorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr dmAuthMessageProcessor_ = std::make_shared(); + static inline std::shared_ptr + distributedDeviceProfileClientMock_ = + std::make_shared(); + _UTTEST + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_state.cpp b/test/authenticationunittest/UTTest_dm_auth_state.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a2932fb48774e2bc472a7dd8846bdc9d518d84e --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2025 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 +#include "UTTest_dm_auth_state.h" +#include "dm_error_type.h" +#include "dm_constants.h" +#include "dm_auth_context.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void DmAuthStateTest::SetUp() +{ +} + +void DmAuthStateTest::TearDown() +{ +} + +void DmAuthStateTest::SetUpTestCase() +{ + DmHiChainAuthConnector::dmHiChainAuthConnector = hiChainAuthConnectorMock_; +} + +void DmAuthStateTest::TearDownTestCase() +{ + DmHiChainAuthConnector::dmHiChainAuthConnector = nullptr; + hiChainAuthConnectorMock_ = nullptr; +} + +namespace { +HWTEST_F(DmAuthStateTest, GetTaskTimeout_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + context->authType = AUTH_TYPE_IMPORT_AUTH_CODE; + std::string taskName = ""; + int32_t taskTimeOut = 1; + int32_t ret = dmAuthState_->GetTaskTimeout(context, taskName.c_str(), taskTimeOut); + ASSERT_EQ(ret, 1); + + taskName = std::string(NEGOTIATE_TIMEOUT_TASK); + ret = dmAuthState_->GetTaskTimeout(context, taskName.c_str(), taskTimeOut); + ASSERT_EQ(ret, 10); + + dmAuthState_->HandleAuthenticateTimeout(context, taskName); + + int32_t accountId = 1; + std::string credId = "creInfo"; + int32_t sessionKeyId = 1; + int32_t aclId = 1; + context->hiChainAuthConnector = std::make_shared(); + EXPECT_CALL(*hiChainAuthConnectorMock_, DeleteCredential(_, _)).WillOnce(Return(ERR_DM_FAILED)); + dmAuthState_->SyncAclList(context, accountId, credId, sessionKeyId, aclId); + + EXPECT_CALL(*hiChainAuthConnectorMock_, DeleteCredential(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceProfileConnectorMock_, DeleteSessionKey(_)).WillOnce(Return(ERR_DM_FAILED)); + dmAuthState_->SyncAclList(context, accountId, credId, sessionKeyId, aclId); + + EXPECT_CALL(*hiChainAuthConnectorMock_, DeleteCredential(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceProfileConnectorMock_, DeleteSessionKey(_)).WillOnce(Return(DM_OK)); + dmAuthState_->SyncAclList(context, accountId, credId, sessionKeyId, aclId); +} + +HWTEST_F(DmAuthStateTest, IsScreenLocked_001, testing::ext::TestSize.Level1) +{ + bool ret = dmAuthState_->IsScreenLocked(); + ASSERT_FALSE(ret); +} +} +} +} \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_state.h b/test/authenticationunittest/UTTest_dm_auth_state.h new file mode 100644 index 0000000000000000000000000000000000000000..bb1c7a10e159367a51fc3c21c56e9f6c486d9018 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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. + */ +#ifndef OHOS_UTTEST_DM_AUTH_STATE_TEST_H +#define OHOS_UTTEST_DM_AUTH_STATE_TEST_H + +#include +#include +#include "dm_auth_state.h" +#include +#include "hichain_auth_connector_mock.h" +#include "deviceprofile_connector_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAuthStateTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr dmAuthState_ = std::make_shared(); + static inline std::shared_ptr hiChainAuthConnectorMock_ = + std::make_shared(); + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp b/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc45c34c2227157f98ea423a664c748fc354b35e --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 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 +#include "UTTest_dm_auth_state_machine.h" +#include "dm_error_type.h" +#include "dm_constants.h" +#include "dm_auth_context.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +std::shared_ptr dmAuthStateMock_ = std::make_shared(); + +void DmAuthStateMachineTest::SetUp() +{ +} + +void DmAuthStateMachineTest::TearDown() +{ +} + +void DmAuthStateMachineTest::SetUpTestCase() +{ +} + +void DmAuthStateMachineTest::TearDownTestCase() +{ +} + +namespace { +HWTEST_F(DmAuthStateMachineTest, TransitionTo_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr dmAuthState_ = std::make_shared(); + EXPECT_CALL(*dmAuthStateMock_, GetStateType()).WillOnce(Return(DmAuthStateType::AUTH_SRC_DATA_SYNC_STATE)); + int32_t ret = dmAuthStateMachine_->TransitionTo(dmAuthState_); + ASSERT_EQ(ret, ERR_DM_NEXT_STATE_INVALID); + + EXPECT_CALL(*dmAuthStateMock_, GetStateType()).WillOnce(Return(DmAuthStateType::AUTH_SRC_FINISH_STATE)); + ret = dmAuthStateMachine_->TransitionTo(dmAuthState_); + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(DmAuthStateMachineTest, WaitExpectEvent_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr context = std::make_shared(); + DmEventType eventType = DmEventType::ON_ERROR; + dmAuthStateMachine_->NotifyEventFinish(eventType); + auto ret = dmAuthStateMachine_->WaitExpectEvent(eventType); + ASSERT_EQ(ret, DmEventType::ON_ERROR); + + eventType = DmEventType::ON_FINISH; + ret = dmAuthStateMachine_->WaitExpectEvent(eventType); + ASSERT_EQ(ret, DmEventType::ON_TIMEOUT); + + dmAuthStateMachine_->Run(context); +} + +HWTEST_F(DmAuthStateMachineTest, GetCurState_001, testing::ext::TestSize.Level1) +{ + DmAuthStateType state = DmAuthStateType::AUTH_SRC_START_STATE; + dmAuthStateMachine_->SetCurState(state); + + auto ret = dmAuthStateMachine_->GetCurState(); + EXPECT_EQ(ret, DmAuthStateType::AUTH_SRC_START_STATE); +} + +HWTEST_F(DmAuthStateMachineTest, CheckStateTransitValid_001, testing::ext::TestSize.Level1) +{ + DmAuthStateType nextState = DmAuthStateType::AUTH_SRC_FINISH_STATE; + bool ret = dmAuthStateMachine_->CheckStateTransitValid(nextState); + EXPECT_TRUE(ret); + + nextState = DmAuthStateType::AUTH_SINK_FINISH_STATE; + ret = dmAuthStateMachine_->CheckStateTransitValid(nextState); + EXPECT_TRUE(ret); + + nextState = DmAuthStateType::AUTH_SRC_PIN_AUTH_START_STATE; + ret = dmAuthStateMachine_->CheckStateTransitValid(nextState); + EXPECT_FALSE(ret); + + dmAuthStateMachine_->SetCurState(DmAuthStateType::AUTH_SRC_PIN_AUTH_DONE_STATE); + nextState = DmAuthStateType::AUTH_SINK_START_STATE; + ret = dmAuthStateMachine_->CheckStateTransitValid(nextState); + EXPECT_TRUE(ret); + + dmAuthStateMachine_->Stop(); +} +} +} +} \ No newline at end of file diff --git a/test/authenticationunittest/UTTest_dm_auth_state_machine.h b/test/authenticationunittest/UTTest_dm_auth_state_machine.h new file mode 100644 index 0000000000000000000000000000000000000000..e73ac7537ae1c8895457ad200b453a3e4c456a46 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state_machine.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025 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. + */ +#ifndef OHOS_UTTEST_DM_AUTH_STATE_MACHINE_TEST_H +#define OHOS_UTTEST_DM_AUTH_STATE_MACHINE_TEST_H + +#include +#include +#include "dm_auth_state_machine.h" +#include "dm_auth_context.h" +#include +#include "deviceprofile_connector_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAuthStateMachineTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + std::shared_ptr dmAuthContext_ = std::make_shared(); + std::shared_ptr dmAuthStateMachine_ = std::make_shared(dmAuthContext_); +}; + + +class DmAuthStateMock : public DmAuthState { +public: + virtual ~DmAuthStateMock() {}; + MOCK_METHOD(DmAuthStateType, GetStateType, ()); + MOCK_METHOD(int32_t, Action, (std::shared_ptr)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 3770a184f4442ac2ec72dc3c82138ef840aba9e0..ecdac790334c8b614f2d125a0e2d305efc917230 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -19,6 +19,7 @@ group("unittest") { testonly = true deps = [ + ":UTTest_auth_manager_new", ":UTTest_advertise_manager", ":UTTest_app_manager", ":UTTest_auth_message_processor", @@ -39,9 +40,13 @@ group("unittest") { ":UTTest_dm_account_common_event", ":UTTest_dm_adapter_manager", ":UTTest_dm_anonymous", + ":UTTest_dm_auth_context", ":UTTest_dm_auth_manager_first", ":UTTest_dm_auth_manager_second", ":UTTest_dm_auth_manager_third", + ":UTTest_dm_auth_message_processor", + ":UTTest_dm_auth_state", + ":UTTest_dm_auth_state_machine", ":UTTest_dm_comm_tool", ":UTTest_dm_common_event_manager", ":UTTest_dm_credential_manager", @@ -1483,6 +1488,200 @@ ohos_unittest("UTTest_dm_auth_manager_third") { ## UTTest_dm_auth_manager_third }}} +## UnitTest UTTest_auth_manager_new {{{ +ohos_unittest("UTTest_auth_manager_new") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] + + sources = [ + "${devicemanager_path}/test/authenticationunittest/UTTest_auth_manager_new.cpp", + "mock/app_manager_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "cJSON:cjson", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] +} + +## UTTest_auth_manager_new }}} + +## UnitTest UTTest_dm_auth_context {{{ +ohos_unittest("UTTest_dm_auth_context") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] + + sources = [ "${devicemanager_path}/test/authenticationunittest/UTTest_dm_auth_context.cpp", ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "cJSON:cjson", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] +} + +## UTTest_dm_auth_context }}} + +## UnitTest UTTest_dm_auth_message_processor {{{ +ohos_unittest("UTTest_dm_auth_message_processor") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] + + sources = [ + "${devicemanager_path}/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp", + "mock/crypto_mgr_mock.cpp", + "mock/deviceprofile_connector_mock.cpp", + "mock/distributed_device_profile_client_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "cJSON:cjson", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] +} + +## UTTest_dm_auth_message_processor }}} + +## UnitTest UTTest_dm_auth_state {{{ +ohos_unittest("UTTest_dm_auth_state") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] + + sources = [ + "${devicemanager_path}/test/authenticationunittest/UTTest_dm_auth_state.cpp", + "mock/deviceprofile_connector_mock.cpp", + "mock/hichain_auth_connector_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "cJSON:cjson", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] +} + +## UTTest_dm_auth_state }}} + +## UnitTest UTTest_dm_auth_state_machine {{{ +ohos_unittest("UTTest_dm_auth_state_machine") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] + + sources = [ + "${devicemanager_path}/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "cJSON:cjson", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] +} + +## UTTest_dm_auth_state_machine }}} + ############################### ## UnitTest UTTest_dm_radar_helper_test {{{ ohos_unittest("UTTest_dm_radar_helper_test") { diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index fb16ca57d9746a773831ec920276cb6f2d591c12..12d7ab0c8103fbe958f869671e22294d5fa10543 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -130,6 +130,11 @@ int32_t DeviceProfileConnector::PutSessionKey(const std::vector & return DmDeviceProfileConnector::dmDeviceProfileConnector->PutSessionKey(sessionKeyArray, sessionKeyId); } +std::vector DeviceProfileConnector::GetAccessControlProfile() +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAccessControlProfile(); +} + int32_t DeviceProfileConnector::PutLocalServiceInfo( const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo) { @@ -159,5 +164,10 @@ std::map DeviceProfileConnector::GetDeviceIdAndBindLevel(s { return DmDeviceProfileConnector::dmDeviceProfileConnector->GetDeviceIdAndBindLevel(userIds, localUdid); } + +int32_t DeviceProfileConnector::DeleteSessionKey(int32_t sessionKeyId) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->DeleteSessionKey(sessionKeyId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/deviceprofile_connector_mock.h b/test/unittest/mock/deviceprofile_connector_mock.h index 2dee03bdf826b984824ffb46e29e626b2d99f3c3..387631b285e003ab07ec14dea98c08525b719e9f 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -57,6 +57,7 @@ public: virtual int32_t GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string& bundleName, int32_t pinExchangeType, DistributedDeviceProfile::LocalServiceInfo &serviceInfo) = 0; virtual int32_t PutSessionKey(const std::vector &sessionKeyArray, int32_t &sessionKeyId) = 0; + virtual std::vector GetAccessControlProfile() = 0; virtual int32_t PutLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo) = 0; virtual int32_t DeleteLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType) = 0; virtual int32_t UpdateLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo) = 0; @@ -64,6 +65,7 @@ public: const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) = 0; virtual std::map GetDeviceIdAndBindLevel(std::vector userIds, const std::string &localUdid) = 0; + virtual int32_t DeleteSessionKey(int32_t sessionKeyId) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -95,6 +97,7 @@ public: MOCK_METHOD(int32_t, GetLocalServiceInfoByBundleNameAndPinExchangeType, (const std::string&, int32_t, DistributedDeviceProfile::LocalServiceInfo &serviceInfo)); MOCK_METHOD(int32_t, PutSessionKey, (const std::vector &, int32_t&)); + MOCK_METHOD((std::vector), GetAccessControlProfile, ()); MOCK_METHOD(int32_t, PutLocalServiceInfo, (const DistributedDeviceProfile::LocalServiceInfo &)); MOCK_METHOD(int32_t, DeleteLocalServiceInfo, (const std::string &, int32_t)); MOCK_METHOD(int32_t, UpdateLocalServiceInfo, (const DistributedDeviceProfile::LocalServiceInfo &)); @@ -102,6 +105,7 @@ public: (const std::string &, (const std::vector &), (const std::vector &))); MOCK_METHOD((std::map), GetDeviceIdAndBindLevel, ((std::vector), const std::string &)); + MOCK_METHOD(int32_t, DeleteSessionKey, (int32_t)); }; } } diff --git a/test/unittest/mock/hichain_auth_connector_mock.cpp b/test/unittest/mock/hichain_auth_connector_mock.cpp index e6c8451c821d2dfa79ce1b1212eb2d4b5c2b7740..0d4e1be7ec5777f9067e58a09fd24eae23b7a638 100644 --- a/test/unittest/mock/hichain_auth_connector_mock.cpp +++ b/test/unittest/mock/hichain_auth_connector_mock.cpp @@ -33,5 +33,10 @@ int32_t HiChainAuthConnector::ImportCredential(int32_t osAccountId, std::string { return DmHiChainAuthConnector::dmHiChainAuthConnector->ImportCredential(osAccountId, deviceId, publicKey); } + +int32_t HiChainAuthConnector::DeleteCredential(const std::string &deviceId, int32_t userId) +{ + return DmHiChainAuthConnector::dmHiChainAuthConnector->DeleteCredential(deviceId, userId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/hichain_auth_connector_mock.h b/test/unittest/mock/hichain_auth_connector_mock.h index 174867f64715fdb4b00c866237243b4026413797..6a5c260f932ec5094ceb342866714d83e2988ddc 100644 --- a/test/unittest/mock/hichain_auth_connector_mock.h +++ b/test/unittest/mock/hichain_auth_connector_mock.h @@ -30,6 +30,7 @@ public: virtual int32_t AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId) = 0; virtual int32_t ImportCredential(int32_t osAccountId, std::string deviceId, std::string publicKey) = 0; + virtual int32_t DeleteCredential(const std::string &deviceId, int32_t userId) = 0; public: static inline std::shared_ptr dmHiChainAuthConnector = nullptr; }; @@ -39,6 +40,7 @@ public: MOCK_METHOD(bool, QueryCredential, (std::string &, int32_t)); MOCK_METHOD(int32_t, AuthDevice, (int32_t, int32_t, std::string, int64_t)); MOCK_METHOD(int32_t, ImportCredential, (int32_t, std::string, std::string)); + MOCK_METHOD(int32_t, DeleteCredential, (const std::string &, int32_t)); }; } }