From e681a823d03a28636d0e7a96dfd238a957a5ea5e Mon Sep 17 00:00:00 2001 From: l60055366 Date: Sat, 8 Mar 2025 17:49:21 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_auth_manager_new.cpp | 321 ++++++++++++++++++ .../UTTest_auth_manager_new.h | 43 +++ test/unittest/BUILD.gn | 45 +++ 3 files changed, 409 insertions(+) create mode 100644 test/authenticationunittest/UTTest_auth_manager_new.cpp create mode 100644 test/authenticationunittest/UTTest_auth_manager_new.h diff --git a/test/authenticationunittest/UTTest_auth_manager_new.cpp b/test/authenticationunittest/UTTest_auth_manager_new.cpp new file mode 100644 index 000000000..c750f8807 --- /dev/null +++ b/test/authenticationunittest/UTTest_auth_manager_new.cpp @@ -0,0 +1,321 @@ +/* + * 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_auth_context.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void 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(); +} + +void TearDown() +{ + +} + +void SetUpTestCase() +{ + appManagerMock_ = std::make_shared(); + DmAppManager::dmAppManager = appManagerMock_; +} + +void TearDownTestCase() +{ + DmAppManager::dmAppManager = nullptr; + appManagerMock_ = nullptr; +} + +namespace { + +const int32_t DM_AUTH_TYPE_MAX = 10; +HWTEST_F(AuthManagerTest, ParseAuthType_001, testing::ext::TestSize.Level0) +{ + 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.Level0) +{ + int32_t ret = authManager_->GeneratePincode(); + ASSERT_NE(ret, DM_OK); +} + +HWTEST_F(AuthManagerTest, RegisterUiStateCallback_001, testing::ext::TestSize.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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_->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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + nlohmann::json jsonObject; + jsonObject[BUNDLE_NAME_KEY] = "bu*********lk"; + auto ret = authManager_->GetBundleName(jsonObject); + ASSERT_EQ(ret.empty(), false); + + jsonObject.clear(); + ret = authManager_->GetBundleName(jsonObject); + ASSERT_EQ(ret.empty(), true); + + 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.Level0) +{ + 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); + + nlohmann::json 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); +} +} +} +} \ 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 000000000..b69a39dd8 --- /dev/null +++ b/test/authenticationunittest/UTTest_auth_manager_new.h @@ -0,0 +1,43 @@ +/* + * 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_AUTH_MANAGER_NEW_TEST_H +#define OHOS_AUTH_MANAGER_NEW_TEST_H + +#include "auth_manager.h" +#include "softbus_connector.h" +#include "hichain_connector.h" +#include "device_manager_service_listener.h" +#include "app_manager_mock.h" + +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); + static inline std::shared_ptr appManagerMock_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index eadcdfe56..c5616a31d 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -36,6 +36,7 @@ group("unittest") { ":UTTest_dm_adapter_manager", ":UTTest_dm_anonymous", ":UTTest_dm_auth_manager_first", + ":UTTest_auth_manager_new", ":UTTest_dm_auth_manager_second", ":UTTest_dm_auth_manager_third", ":UTTest_dm_comm_tool", @@ -1452,6 +1453,50 @@ 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", + "mock/crypto_mgr_mock.cpp", + "mock/deviceprofile_connector_mock.cpp", + "mock/dm_crypto_mock.cpp", + "mock/hichain_auth_connector_mock.cpp", + "mock/multiple_user_connector_mock.cpp", + "mock/softbus_session_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", + "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_radar_helper_test {{{ ohos_unittest("UTTest_dm_radar_helper_test") { -- Gitee From 26cc95d62095767ecd9e594054aef12d14e0e592 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Wed, 12 Mar 2025 16:44:19 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_auth_manager_new.cpp | 185 +++++++++- .../UTTest_auth_manager_new.h | 3 + .../UTTest_dm_auth_context.cpp | 222 ++++++++++++ .../UTTest_dm_auth_context.h | 34 ++ .../UTTest_dm_auth_message_processor.cpp | 321 ++++++++++++++++++ .../UTTest_dm_auth_message_processor.h | 40 +++ test/unittest/BUILD.gn | 80 ++++- 7 files changed, 877 insertions(+), 8 deletions(-) create mode 100644 test/authenticationunittest/UTTest_dm_auth_context.cpp create mode 100644 test/authenticationunittest/UTTest_dm_auth_context.h create mode 100644 test/authenticationunittest/UTTest_dm_auth_message_processor.cpp create mode 100644 test/authenticationunittest/UTTest_dm_auth_message_processor.h diff --git a/test/authenticationunittest/UTTest_auth_manager_new.cpp b/test/authenticationunittest/UTTest_auth_manager_new.cpp index c750f8807..af1510e9a 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.cpp +++ b/test/authenticationunittest/UTTest_auth_manager_new.cpp @@ -18,7 +18,7 @@ #include "UTTest_auth_manager_new.h" #include "dm_error_type.h" #include "dm_constants.h" -#include "dm_auth_context.h" +#include "dm_ability_manager.h" using namespace testing; using namespace testing::ext; @@ -316,6 +316,189 @@ HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level0) std::string extra = jsonObject.dump(); authManager_->GetAuthParam(pkgName, authType, deviceId, extra); } + +HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level0) +{ + 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); + + nlohmann::json 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.Level0) +{ + 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.Level0) +{ + authManager_->context_->authStateMachine = std::make_shared(); + 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.Level0) +{ + authManager_->context_->authStateMachine = std::make_shared(); + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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 index b69a39dd8..e2d64f75f 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.h +++ b/test/authenticationunittest/UTTest_auth_manager_new.h @@ -20,6 +20,7 @@ #include "hichain_connector.h" #include "device_manager_service_listener.h" #include "app_manager_mock.h" +#include namespace OHOS { namespace DistributedHardware { @@ -36,6 +37,8 @@ public: 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 diff --git a/test/authenticationunittest/UTTest_dm_auth_context.cpp b/test/authenticationunittest/UTTest_dm_auth_context.cpp new file mode 100644 index 000000000..0aae732e3 --- /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 SetUp() +{ +} + +void TearDown() +{ +} + +void SetUpTestCase() +{ +} + +void TearDownTestCase() +{ +} + +namespace { + +HWTEST_F(DmAuthContextTest, GetDeviceId_001, testing::ext::TestSize.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + std::string credentialId = "credential******44"; + authManager_->context_->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 = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, DM_OK); + + side = static_cast(2); + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, ERR_DM_FAILED); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + authorizedScope = DM_AUTH_SCOPE_DEVICE; + ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ASSERT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DmAuthContextTest, SetPublicKey_001, testing::ext::TestSize.Level0) +{ + DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; + DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + std::string publicKey = "pub******6"; + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + int32_t ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, DM_OK); + + side = static_cast(2); + ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ASSERT_EQ(ret, ERR_DM_FAILED); + + side = DmAuthSide::DM_AUTH_REMOTE_SIDE; + authorizedScope = DM_AUTH_SCOPE_DEVICE; + ret = authManager_->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 000000000..515cd20a6 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_context.h @@ -0,0 +1,34 @@ +/* + * 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_DM_AUTH_MESSAGE_PROCESSOR_TEST_H +#define OHOS_DM_AUTH_MESSAGE_PROCESSOR_TEST_H + +#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 000000000..078da1081 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp @@ -0,0 +1,321 @@ +/* + * 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 MSG_TYPE_REQ_CREDENTIAL_EXCHANGE = 140; +constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_EXCHANGE = 150; +constexpr int32_t MSG_TYPE_REQ_CREDENTIAL_AUTH_START = 160; +constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_AUTH_START = 170; +constexpr int32_t MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE = 161; +constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_AUTH_NEGOTIATE = 171; +constexpr int32_t MSG_TYPE_REQ_DATA_SYNC = 180; +constexpr int32_t MSG_TYPE_RESP_DATA_SYNC = 190; + +void SetUp() +{ +} + +void TearDown() +{ +} + +void SetUpTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; + cryptoMgrMock_ = std::make_shared(); + DmCryptoMgr::dmCryptoMgr = cryptoMgrMock_; +} + +void TearDownTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr; + distributedDeviceProfileClientMock_ = nullptr; + DmCryptoMgr::dmCryptoMgr = nullptr; + cryptoMgrMock_ = nullptr; +} + +namespace { + +HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKey_001, testing::ext::TestSize.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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] = MSG_TYPE_REQ_CREDENTIAL_AUTH_START; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = MSG_TYPE_REQ_CREDENTIAL_EXCHANGE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = MSG_TYPE_RESP_CREDENTIAL_EXCHANGE; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = MSG_TYPE_REQ_DATA_SYNC; + message = jsonObject.dump(); + ret = dmAuthMessageProcessor_->ParseMessage(context, message); + ASSERT_EQ(ret, ERR_DM_FAILED); + + jsonObject[TAG_MSG_TYPE] = 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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.Level0) +{ + 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); +} +} +} +} \ 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 000000000..739330f56 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.h @@ -0,0 +1,40 @@ +/* + * 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_DM_AUTH_CONTEXT_TEST_H +#define OHOS_DM_AUTH_CONTEXT_TEST_H + +#include "dm_auth_message_processor.h" +#include +#include "distributed_device_profile_client_mock.h" +#include "crypto_mgr_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(); + static inline std::shared_ptr cryptoMgrMock_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c5616a31d..d1ddc2153 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_auth_message_processor", ":UTTest_auth_request_state", ":UTTest_auth_response_state", @@ -35,10 +36,11 @@ group("unittest") { ":UTTest_dm_account_common_event", ":UTTest_dm_adapter_manager", ":UTTest_dm_anonymous", + ":UTTest_dm_auth_context", ":UTTest_dm_auth_manager_first", - ":UTTest_auth_manager_new", ":UTTest_dm_auth_manager_second", ":UTTest_dm_auth_manager_third", + ":UTTest_dm_auth_message_processor", ":UTTest_dm_comm_tool", ":UTTest_dm_common_event_manager", ":UTTest_dm_credential_manager", @@ -1462,12 +1464,6 @@ ohos_unittest("UTTest_auth_manager_new") { sources = [ "${devicemanager_path}/test/authenticationunittest/UTTest_auth_manager_new.cpp", "mock/app_manager_mock.cpp", - "mock/crypto_mgr_mock.cpp", - "mock/deviceprofile_connector_mock.cpp", - "mock/dm_crypto_mock.cpp", - "mock/hichain_auth_connector_mock.cpp", - "mock/multiple_user_connector_mock.cpp", - "mock/softbus_session_mock.cpp", ] deps = [ ":device_manager_test_common" ] @@ -1497,6 +1493,76 @@ ohos_unittest("UTTest_auth_manager_new") { ## 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", + "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", ] + + 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", + "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_radar_helper_test {{{ ohos_unittest("UTTest_dm_radar_helper_test") { -- Gitee From d3eed709c896fa83cdaff612861b4bd1dc895b8e Mon Sep 17 00:00:00 2001 From: l60055366 Date: Wed, 12 Mar 2025 18:19:17 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_dm_auth_message_processor.cpp | 114 +++++++++++++++--- .../mock/deviceprofile_connector_mock.cpp | 5 + .../mock/deviceprofile_connector_mock.h | 2 + 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp index 078da1081..3ebf43a04 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp @@ -26,14 +26,12 @@ using namespace testing; using namespace testing::ext; namespace OHOS { namespace DistributedHardware { -constexpr int32_t MSG_TYPE_REQ_CREDENTIAL_EXCHANGE = 140; -constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_EXCHANGE = 150; -constexpr int32_t MSG_TYPE_REQ_CREDENTIAL_AUTH_START = 160; -constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_AUTH_START = 170; -constexpr int32_t MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE = 161; -constexpr int32_t MSG_TYPE_RESP_CREDENTIAL_AUTH_NEGOTIATE = 171; -constexpr int32_t MSG_TYPE_REQ_DATA_SYNC = 180; -constexpr int32_t MSG_TYPE_RESP_DATA_SYNC = 190; +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() { @@ -130,32 +128,32 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessage_001, testing::ext::TestSize.Le ret = dmAuthMessageProcessor_->ParseMessage(context, message); ASSERT_EQ(ret, ERR_DM_FAILED); - jsonObject[TAG_MSG_TYPE] = MSG_TYPE_REQ_CREDENTIAL_AUTH_START; + 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] = MSG_TYPE_REQ_CREDENTIAL_AUTH_NEGOTIATE; + 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] = MSG_TYPE_REQ_CREDENTIAL_EXCHANGE; + 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] = MSG_TYPE_RESP_CREDENTIAL_EXCHANGE; + 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] = MSG_TYPE_REQ_DATA_SYNC; + 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] = MSG_TYPE_RESP_DATA_SYNC; + jsonObject[TAG_MSG_TYPE] = DM_MSG_TYPE_RESP_DATA_SYNC; message = jsonObject.dump(); ret = dmAuthMessageProcessor_->ParseMessage(context, message); ASSERT_EQ(ret, ERR_DM_FAILED); @@ -316,6 +314,94 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRspCredExchange_001, testing::e ret = dmAuthMessageProcessor_->ParseMessageRspCredExchange(jsonObject, context); ASSERT_EQ(ret, ERR_DM_FAILED); } + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.Level0) +{ + 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(*cryptoMgrMock_, 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(*cryptoMgrMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); + ASSERT_EQ(ret.empty(), true); +} + +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_002, testing::ext::TestSize.Level0) +{ + 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.Level0) +{ + 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); } } } \ No newline at end of file diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index a0b920118..c0b01300a 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -129,5 +129,10 @@ int32_t DeviceProfileConnector::PutSessionKey(const std::vector & { return DmDeviceProfileConnector::dmDeviceProfileConnector->PutSessionKey(sessionKeyArray, sessionKeyId); } + +std::vector DeviceProfileConnector::GetAccessControlProfile() +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAccessControlProfile(); +} } // 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 209e54309..d65dba7b0 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; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -88,6 +89,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, ()); }; } } -- Gitee From 311ab8a5219944d3fdf31655dbdc1adbbef66b17 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Thu, 13 Mar 2025 19:55:49 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_dm_auth_message_processor.cpp | 206 ++++++++++++++++-- .../UTTest_dm_auth_message_processor.h | 3 + test/unittest/BUILD.gn | 7 +- 3 files changed, 202 insertions(+), 14 deletions(-) diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp index 3ebf43a04..14e44accb 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp @@ -47,6 +47,7 @@ void SetUpTestCase() distributedDeviceProfileClientMock_; cryptoMgrMock_ = std::make_shared(); DmCryptoMgr::dmCryptoMgr = cryptoMgrMock_; + DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; } void TearDownTestCase() @@ -55,11 +56,13 @@ void TearDownTestCase() distributedDeviceProfileClientMock_ = nullptr; DmCryptoMgr::dmCryptoMgr = nullptr; cryptoMgrMock_ = nullptr; + DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; + deviceProfileConnectorMock_ = nullptr; } namespace { -HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKey_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKey_001, testing::ext::TestSize.Level1) { uint8_t arraySessionkey[] = {1, 2, 3, 4, 5}; uint8_t *sessionKey = arraySessionkey; @@ -73,7 +76,7 @@ HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKey_001, testing::ext::TestSize. ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKeyToDP_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKeyToDP_001, testing::ext::TestSize.Level1) { int32_t skId = 1; dmAuthMessageProcessor_->cryptoMgr_ = nullptr; @@ -85,7 +88,7 @@ HWTEST_F(DmAuthMessageProcessorTest, SaveSessionKeyToDP_001, testing::ext::TestS ASSERT_NE(ret, DM_OK); } -HWTEST_F(DmAuthMessageProcessorTest, PutAccessControlList_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, PutAccessControlList_001, testing::ext::TestSize.Level1) { std::shared_ptr context = std::make_shared(); DmAccess access; @@ -106,7 +109,7 @@ HWTEST_F(DmAuthMessageProcessorTest, PutAccessControlList_001, testing::ext::Tes ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthMessageProcessorTest, ParseMessage_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, ParseMessage_001, testing::ext::TestSize.Level1) { std::shared_ptr context = nullptr; std::string message = ""; @@ -159,7 +162,7 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessage_001, testing::ext::TestSize.Le ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(DmAuthMessageProcessorTest, ParseMessageNegotiateTransmit_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageNegotiateTransmit_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; std::shared_ptr context = std::make_shared(); @@ -191,7 +194,7 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageNegotiateTransmit_001, testing: ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthMessageProcessorTest, ParseMessageOnTransmit_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageOnTransmit_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; std::shared_ptr context = std::make_shared(); @@ -207,7 +210,7 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageOnTransmit_001, testing::ext::T ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqCredExchange_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqCredExchange_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; std::shared_ptr context = std::make_shared(); @@ -260,7 +263,7 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageReqCredExchange_001, testing::e ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRspCredExchange_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRspCredExchange_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; std::shared_ptr context = std::make_shared(); @@ -315,7 +318,7 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageRspCredExchange_001, testing::e ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.Level1) { DmMessageType msgType = DmMessageType::MSG_TYPE_REQ_CREDENTIAL_EXCHANGE; std::shared_ptr context = std::make_shared(); @@ -345,7 +348,7 @@ HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.L msgType = DmMessageType::MSG_TYPE_REQ_DATA_SYNC; std::vector profiles; - EXPECT_CALL(*cryptoMgrMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + EXPECT_CALL(*deviceProfileConnectorMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); ret = dmAuthMessageProcessor_->CreateMessage(msgType, context); ASSERT_EQ(ret.empty(), true); @@ -371,12 +374,12 @@ HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_001, testing::ext::TestSize.L accessProfile.SetAccesser(accesser); accessProfile.SetAccessee(accessee); profiles.push_back(accessProfile); - EXPECT_CALL(*cryptoMgrMock_, GetAccessControlProfile()).WillOnce(Return(profiles)); + 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.Level0) +HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_002, testing::ext::TestSize.Level1) { DmMessageType msgType = DmMessageType::MSG_TYPE_RESP_DATA_SYNC; std::shared_ptr context = std::make_shared(); @@ -388,7 +391,7 @@ HWTEST_F(DmAuthMessageProcessorTest, CreateMessage_002, testing::ext::TestSize.L ASSERT_EQ(ret.empty(), false); } -HWTEST_F(DmAuthMessageProcessorTest, CreateCredentialNegotiateMessage_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthMessageProcessorTest, CreateCredentialNegotiateMessage_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; std::shared_ptr context = std::make_shared(); @@ -402,6 +405,183 @@ HWTEST_F(DmAuthMessageProcessorTest, CreateCredentialNegotiateMessage_001, testi 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); +} } } } \ 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 index 739330f56..0c4260696 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.h +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.h @@ -19,6 +19,7 @@ #include #include "distributed_device_profile_client_mock.h" #include "crypto_mgr_mock.h" +#include "deviceprofile_connector_mock.h" namespace OHOS { namespace DistributedHardware { @@ -34,6 +35,8 @@ public: distributedDeviceProfileClientMock_ = std::make_shared(); static inline std::shared_ptr cryptoMgrMock_ = nullptr; + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 3325a4a8a..48c0ff903 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1543,7 +1543,12 @@ ohos_unittest("UTTest_dm_auth_message_processor") { include_dirs = [ "${devicemanager_path}/test/authenticationunittest" ] - sources = [ "${devicemanager_path}/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp", ] + 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" ] -- Gitee From f33a39c86462480b71c204748b8f5d8ab01cfcf4 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Sat, 15 Mar 2025 18:38:23 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_auth_manager_new.cpp | 36 ++-- .../UTTest_dm_auth_context.cpp | 12 +- .../UTTest_dm_auth_message_processor.cpp | 202 ++++++++++++++++++ 3 files changed, 226 insertions(+), 24 deletions(-) diff --git a/test/authenticationunittest/UTTest_auth_manager_new.cpp b/test/authenticationunittest/UTTest_auth_manager_new.cpp index af1510e9a..02afeab13 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.cpp +++ b/test/authenticationunittest/UTTest_auth_manager_new.cpp @@ -54,7 +54,7 @@ void TearDownTestCase() namespace { const int32_t DM_AUTH_TYPE_MAX = 10; -HWTEST_F(AuthManagerTest, ParseAuthType_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, ParseAuthType_001, testing::ext::TestSize.Level1) { std::map bindParam; int32_t authType = 1; @@ -78,13 +78,13 @@ HWTEST_F(AuthManagerTest, ParseAuthType_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret, DM_OK); } -HWTEST_F(AuthManagerTest, GeneratePincode_001, testing::ext::TestSize.Level0) +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.Level0) +HWTEST_F(AuthManagerTest, RegisterUiStateCallback_001, testing::ext::TestSize.Level1) { std::string pkgName = "pkgName"; int32_t ret = authManager_->RegisterUiStateCallback(pkgName); @@ -96,7 +96,7 @@ HWTEST_F(AuthManagerTest, RegisterUiStateCallback_001, testing::ext::TestSize.Le ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_001, testing::ext::TestSize.Level1) { authManager_->context_->authUiStateMgr = nullptr; std::string pkgName = "pkgName"; @@ -108,7 +108,7 @@ HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_001, testing::ext::TestSize. ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(AuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.Level1) { std::string pkgName = "pkgName"; authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; @@ -141,7 +141,7 @@ HWTEST_F(AuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.Lev authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); } -HWTEST_F(AuthManagerTest, ParseConnectAddr_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, ParseConnectAddr_001, testing::ext::TestSize.Level1) { PeerTargetId targetId = { .deviceId = "device****45", @@ -169,7 +169,7 @@ HWTEST_F(AuthManagerTest, ParseConnectAddr_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } -HWTEST_F(AuthManagerTest, IsAuthTypeSupported_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, IsAuthTypeSupported_001, testing::ext::TestSize.Level1) { int32_t authType = 1; bool ret = authManager_->IsAuthTypeSupported(authType); @@ -180,7 +180,7 @@ HWTEST_F(AuthManagerTest, IsAuthTypeSupported_001, testing::ext::TestSize.Level0 ASSERT_TRUE(ret); } -HWTEST_F(AuthManagerTest, IsAuthCodeReady_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, IsAuthCodeReady_001, testing::ext::TestSize.Level1) { std::string pkgName = "importPkgName"; authManager_->context_->importAuthCode = ""; @@ -201,7 +201,7 @@ HWTEST_F(AuthManagerTest, IsAuthCodeReady_001, testing::ext::TestSize.Level0) ASSERT_TRUE(ret); } -HWTEST_F(AuthManagerTest, CheckAuthParamVaild_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, CheckAuthParamVaild_001, testing::ext::TestSize.Level1) { std::string pkgName = "importPkgName"; int32_t authType = -1; @@ -246,7 +246,7 @@ HWTEST_F(AuthManagerTest, CheckAuthParamVaild_001, testing::ext::TestSize.Level0 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } -HWTEST_F(AuthManagerTest, GetBundleName_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, GetBundleName_001, testing::ext::TestSize.Level1) { nlohmann::json jsonObject; jsonObject[BUNDLE_NAME_KEY] = "bu*********lk"; @@ -277,7 +277,7 @@ HWTEST_F(AuthManagerTest, GetBundleName_001, testing::ext::TestSize.Level0) authManager_->ParseJsonObject(jsonObject); } -HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level1) { int32_t bindLevel = 1; EXPECT_CALL(*appManagerMock_, IsSystemSA()).WillOnce(Return(false)); @@ -317,7 +317,7 @@ HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level0) authManager_->GetAuthParam(pkgName, authType, deviceId, extra); } -HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level1) { std::string pkgName = "pkgName"; int32_t authType = 1; @@ -343,7 +343,7 @@ HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level0) authManager_->InitAuthState(pkgName, authType, deviceId, extra); } -HWTEST_F(AuthManagerTest, BindTarget_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, BindTarget_001, testing::ext::TestSize.Level1) { std::string pkgName = ""; PeerTargetId targetId = { @@ -375,7 +375,7 @@ HWTEST_F(AuthManagerTest, BindTarget_001, testing::ext::TestSize.Level0) EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } -HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1) { authManager_->context_->authStateMachine = std::make_shared(); int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH; @@ -411,7 +411,7 @@ HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level0) } -HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level1) { authManager_->context_->authStateMachine = std::make_shared(); int32_t action = USER_OPERATION_TYPE_CANCEL_PINCODE_INPUT; @@ -444,7 +444,7 @@ HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level0) authSrcManager_->AuthDeviceError(requestId, errorCode); } -HWTEST_F(AuthManagerTest, AuthDeviceTransmit_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, AuthDeviceTransmit_001, testing::ext::TestSize.Level1) { int64_t requestId = 1; uint8_t *data = nullptr; @@ -463,7 +463,7 @@ HWTEST_F(AuthManagerTest, AuthDeviceTransmit_001, testing::ext::TestSize.Level0) authSrcManager_->AuthDeviceFinish(requestId); } -HWTEST_F(AuthManagerTest, AuthDeviceTransmit_002, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, AuthDeviceTransmit_002, testing::ext::TestSize.Level1) { int64_t requestId = 1; uint8_t *data = nullptr; @@ -489,7 +489,7 @@ HWTEST_F(AuthManagerTest, AuthDeviceTransmit_002, testing::ext::TestSize.Level0) authSinkManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); } -HWTEST_F(AuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level0) +HWTEST_F(AuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level1) { int32_t code = 1; int32_t ret = GetPinCode(code); diff --git a/test/authenticationunittest/UTTest_dm_auth_context.cpp b/test/authenticationunittest/UTTest_dm_auth_context.cpp index 0aae732e3..474032ebb 100644 --- a/test/authenticationunittest/UTTest_dm_auth_context.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_context.cpp @@ -44,7 +44,7 @@ void TearDownTestCase() namespace { -HWTEST_F(DmAuthContextTest, GetDeviceId_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, GetDeviceId_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; auto ret = dmAuthContext_->GetDeviceId(side); @@ -59,7 +59,7 @@ HWTEST_F(DmAuthContextTest, GetDeviceId_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret.empty(), true); } -HWTEST_F(DmAuthContextTest, GetUserId_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, GetUserId_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; int32_t ret = dmAuthContext_->GetUserId(side); @@ -70,7 +70,7 @@ HWTEST_F(DmAuthContextTest, GetUserId_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthContextTest, GetCredentialId_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, GetCredentialId_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; @@ -97,7 +97,7 @@ HWTEST_F(DmAuthContextTest, GetCredentialId_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret.empty(), true); } -HWTEST_F(DmAuthContextTest, GetPublicKey_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, GetPublicKey_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_DEVICE; @@ -124,7 +124,7 @@ HWTEST_F(DmAuthContextTest, GetPublicKey_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret.empty(), true); } -HWTEST_F(DmAuthContextTest, SetCredentialId_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, SetCredentialId_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; @@ -171,7 +171,7 @@ HWTEST_F(DmAuthContextTest, SetCredentialId_001, testing::ext::TestSize.Level0) ASSERT_EQ(ret, ERR_DM_FAILED); } -HWTEST_F(DmAuthContextTest, SetPublicKey_001, testing::ext::TestSize.Level0) +HWTEST_F(DmAuthContextTest, SetPublicKey_001, testing::ext::TestSize.Level1) { DmAuthSide side = DmAuthSide::DM_AUTH_LOCAL_SIDE; DmAuthScope authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp index 14e44accb..f87a973f1 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.cpp @@ -582,6 +582,208 @@ HWTEST_F(DmAuthMessageProcessorTest, ParseMessageFinish_001, testing::ext::TestS 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 -- Gitee From bc11377f4132c82be34917a8ae48124f4c0ad3f4 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Tue, 18 Mar 2025 16:06:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9dm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E4=BE=8B=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_auth_manager_new.h | 4 +- .../UTTest_dm_auth_context.h | 4 +- .../UTTest_dm_auth_message_processor.h | 6 +- .../UTTest_dm_auth_state.cpp | 89 +++++++++++++++ .../UTTest_dm_auth_state.h | 40 +++++++ .../UTTest_dm_auth_state_machine.cpp | 108 ++++++++++++++++++ .../UTTest_dm_auth_state_machine.h | 45 ++++++++ test/unittest/BUILD.gn | 78 +++++++++++++ .../mock/deviceprofile_connector_mock.cpp | 5 + .../mock/deviceprofile_connector_mock.h | 2 + .../mock/hichain_auth_connector_mock.cpp | 5 + .../mock/hichain_auth_connector_mock.h | 2 + 12 files changed, 381 insertions(+), 7 deletions(-) create mode 100644 test/authenticationunittest/UTTest_dm_auth_state.cpp create mode 100644 test/authenticationunittest/UTTest_dm_auth_state.h create mode 100644 test/authenticationunittest/UTTest_dm_auth_state_machine.cpp create mode 100644 test/authenticationunittest/UTTest_dm_auth_state_machine.h diff --git a/test/authenticationunittest/UTTest_auth_manager_new.h b/test/authenticationunittest/UTTest_auth_manager_new.h index e2d64f75f..1273a9ee7 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.h +++ b/test/authenticationunittest/UTTest_auth_manager_new.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef OHOS_AUTH_MANAGER_NEW_TEST_H -#define OHOS_AUTH_MANAGER_NEW_TEST_H +#ifndef OHOS_UTTEST_AUTH_MANAGER_NEW_TEST_H +#define OHOS_UTTEST_AUTH_MANAGER_NEW_TEST_H #include "auth_manager.h" #include "softbus_connector.h" diff --git a/test/authenticationunittest/UTTest_dm_auth_context.h b/test/authenticationunittest/UTTest_dm_auth_context.h index 515cd20a6..42e5f5165 100644 --- a/test/authenticationunittest/UTTest_dm_auth_context.h +++ b/test/authenticationunittest/UTTest_dm_auth_context.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef OHOS_DM_AUTH_MESSAGE_PROCESSOR_TEST_H -#define OHOS_DM_AUTH_MESSAGE_PROCESSOR_TEST_H +#ifndef OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H +#define OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H #include "dm_auth_context.h" #include diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.h b/test/authenticationunittest/UTTest_dm_auth_message_processor.h index 0c4260696..4a16ed16a 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.h +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef OHOS_DM_AUTH_CONTEXT_TEST_H -#define OHOS_DM_AUTH_CONTEXT_TEST_H +#ifndef OHOS_UTTEST_DM_AUTH_MESSAGE_PROCESSOR_TEST_H +#define OHOS_UTTEST_DM_AUTH_MESSAGE_PROCESSOR_TEST_H #include "dm_auth_message_processor.h" #include @@ -34,7 +34,7 @@ public: static inline std::shared_ptr distributedDeviceProfileClientMock_ = std::make_shared(); - static inline std::shared_ptr cryptoMgrMock_ = nullptr; + _UTTEST static inline std::shared_ptr deviceProfileConnectorMock_ = std::make_shared(); }; diff --git a/test/authenticationunittest/UTTest_dm_auth_state.cpp b/test/authenticationunittest/UTTest_dm_auth_state.cpp new file mode 100644 index 000000000..e0427254a --- /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 SetUp() +{ +} + +void TearDown() +{ +} + +void SetUpTestCase() +{ + DmHiChainAuthConnector::dmHiChainAuthConnector = hiChainAuthConnectorMock_; +} + +void 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 000000000..acfdee3c0 --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state.h @@ -0,0 +1,40 @@ +/* + * 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 "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 000000000..eee844e2d --- /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 SetUp() +{ +} + +void TearDown() +{ +} + +void SetUpTestCase() +{ +} + +void 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 000000000..5ed144b2e --- /dev/null +++ b/test/authenticationunittest/UTTest_dm_auth_state_machine.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_STATE_MACHINE_TEST_H +#define OHOS_UTTEST_DM_AUTH_STATE_MACHINE_TEST_H + +#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 cf06b8bb0..cd9a576d4 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -45,6 +45,8 @@ group("unittest") { ":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", @@ -1599,6 +1601,82 @@ ohos_unittest("UTTest_dm_auth_message_processor") { ## 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", + "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", + "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 b990de617..12d7ab0c8 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -164,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 bd1c7d47b..387631b28 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -65,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; }; @@ -104,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 e6c8451c8..0d4e1be7e 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 174867f64..6a5c260f9 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)); }; } } -- Gitee From 31b25c691287c16c00107d2d3c7b3e7ff28133a7 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Fri, 21 Mar 2025 16:41:38 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_auth_manager_new.cpp | 30 ++++++++----------- .../UTTest_auth_manager_new.h | 4 ++- .../UTTest_dm_auth_context.cpp | 8 ++--- .../UTTest_dm_auth_context.h | 2 ++ .../UTTest_dm_auth_message_processor.h | 2 ++ .../UTTest_dm_auth_state.cpp | 8 ++--- .../UTTest_dm_auth_state.h | 4 ++- .../UTTest_dm_auth_state_machine.cpp | 8 ++--- .../UTTest_dm_auth_state_machine.h | 2 ++ test/unittest/BUILD.gn | 5 ++++ 10 files changed, 42 insertions(+), 31 deletions(-) diff --git a/test/authenticationunittest/UTTest_auth_manager_new.cpp b/test/authenticationunittest/UTTest_auth_manager_new.cpp index 02afeab13..7afac9067 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.cpp +++ b/test/authenticationunittest/UTTest_auth_manager_new.cpp @@ -25,27 +25,27 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { -void SetUp() +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_->authStateMachine = std::make_shared(authManager_->context_); } -void TearDown() +void AuthManagerTest::TearDown() { } -void SetUpTestCase() +void AuthManagerTest::SetUpTestCase() { appManagerMock_ = std::make_shared(); DmAppManager::dmAppManager = appManagerMock_; } -void TearDownTestCase() +void AuthManagerTest::TearDownTestCase() { DmAppManager::dmAppManager = nullptr; appManagerMock_ = nullptr; @@ -130,7 +130,7 @@ HWTEST_F(AuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.Lev authManager_->context_->authStateMachine = nullptr; authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); - authManager_->context_->authStateMachine = std::make_shared(); + authManager_->context_->authStateMachine = std::make_shared(authManager_->context_); authManager_->context_->requestId = 0; authManager_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); @@ -248,15 +248,11 @@ HWTEST_F(AuthManagerTest, CheckAuthParamVaild_001, testing::ext::TestSize.Level1 HWTEST_F(AuthManagerTest, GetBundleName_001, testing::ext::TestSize.Level1) { - nlohmann::json jsonObject; + JsonObject jsonObject; jsonObject[BUNDLE_NAME_KEY] = "bu*********lk"; auto ret = authManager_->GetBundleName(jsonObject); ASSERT_EQ(ret.empty(), false); - jsonObject.clear(); - ret = authManager_->GetBundleName(jsonObject); - ASSERT_EQ(ret.empty(), true); - jsonObject[PARAM_KEY_CONN_SESSIONTYPE] = "keyConnSession"; authManager_->ParseHmlInfoInJsonObject(jsonObject); @@ -303,7 +299,7 @@ HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level1) int32_t ret = authManager_->GetBindLevel(bindLevel); EXPECT_EQ(ret, 3); - nlohmann::json jsonObject; + JsonObject jsonObject; jsonObject[APP_OPERATION_KEY] = "appkey"; jsonObject[CUSTOM_DESCRIPTION_KEY] = "customkey"; jsonObject[APP_THUMBNAIL] = "thumbnail"; @@ -313,7 +309,7 @@ HWTEST_F(AuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level1) std::string pkgName = "pkgName"; int32_t authType = DmAuthType::AUTH_TYPE_PIN; std::string deviceId = "deviceId"; - std::string extra = jsonObject.dump(); + std::string extra = jsonObject.Dump(); authManager_->GetAuthParam(pkgName, authType, deviceId, extra); } @@ -335,9 +331,9 @@ HWTEST_F(AuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level1) ret = authManager_->AuthenticateDevice(pkgName, authType, deviceId, extra); EXPECT_EQ(ret, DM_OK); - nlohmann::json jsonObject; + JsonObject jsonObject; jsonObject[TAG_BIND_LEVEL] = 5; - extra = jsonObject.dump(); + extra = jsonObject.Dump(); ret = authManager_->AuthenticateDevice(pkgName, authType, deviceId, extra); EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); authManager_->InitAuthState(pkgName, authType, deviceId, extra); @@ -377,7 +373,7 @@ HWTEST_F(AuthManagerTest, BindTarget_001, testing::ext::TestSize.Level1) HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1) { - authManager_->context_->authStateMachine = std::make_shared(); + 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); @@ -413,7 +409,7 @@ HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1) HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level1) { - authManager_->context_->authStateMachine = std::make_shared(); + 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); diff --git a/test/authenticationunittest/UTTest_auth_manager_new.h b/test/authenticationunittest/UTTest_auth_manager_new.h index 1273a9ee7..8690f09d8 100644 --- a/test/authenticationunittest/UTTest_auth_manager_new.h +++ b/test/authenticationunittest/UTTest_auth_manager_new.h @@ -15,6 +15,8 @@ #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" @@ -36,7 +38,7 @@ public: std::shared_ptr hiChainAuthConnector = std::make_shared(); std::shared_ptr authManager_ = - std::make_shared(softbusConnector, listener, hiChainAuthConnector); + 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; diff --git a/test/authenticationunittest/UTTest_dm_auth_context.cpp b/test/authenticationunittest/UTTest_dm_auth_context.cpp index 474032ebb..495d1ab94 100644 --- a/test/authenticationunittest/UTTest_dm_auth_context.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_context.cpp @@ -26,19 +26,19 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { -void SetUp() +void DmAuthContextTest::SetUp() { } -void TearDown() +void DmAuthContextTest::TearDown() { } -void SetUpTestCase() +void DmAuthContextTest::SetUpTestCase() { } -void TearDownTestCase() +void DmAuthContextTest::TearDownTestCase() { } diff --git a/test/authenticationunittest/UTTest_dm_auth_context.h b/test/authenticationunittest/UTTest_dm_auth_context.h index 42e5f5165..908628a24 100644 --- a/test/authenticationunittest/UTTest_dm_auth_context.h +++ b/test/authenticationunittest/UTTest_dm_auth_context.h @@ -15,6 +15,8 @@ #ifndef OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H #define OHOS_UTTEST_DM_AUTH_CONTEXT_TEST_H +#include +#include #include "dm_auth_context.h" #include diff --git a/test/authenticationunittest/UTTest_dm_auth_message_processor.h b/test/authenticationunittest/UTTest_dm_auth_message_processor.h index 4a16ed16a..49e2b6681 100644 --- a/test/authenticationunittest/UTTest_dm_auth_message_processor.h +++ b/test/authenticationunittest/UTTest_dm_auth_message_processor.h @@ -15,6 +15,8 @@ #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" diff --git a/test/authenticationunittest/UTTest_dm_auth_state.cpp b/test/authenticationunittest/UTTest_dm_auth_state.cpp index e0427254a..3a2932fb4 100644 --- a/test/authenticationunittest/UTTest_dm_auth_state.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_state.cpp @@ -27,20 +27,20 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { -void SetUp() +void DmAuthStateTest::SetUp() { } -void TearDown() +void DmAuthStateTest::TearDown() { } -void SetUpTestCase() +void DmAuthStateTest::SetUpTestCase() { DmHiChainAuthConnector::dmHiChainAuthConnector = hiChainAuthConnectorMock_; } -void TearDownTestCase() +void DmAuthStateTest::TearDownTestCase() { DmHiChainAuthConnector::dmHiChainAuthConnector = nullptr; hiChainAuthConnectorMock_ = nullptr; diff --git a/test/authenticationunittest/UTTest_dm_auth_state.h b/test/authenticationunittest/UTTest_dm_auth_state.h index acfdee3c0..bb1c7a10e 100644 --- a/test/authenticationunittest/UTTest_dm_auth_state.h +++ b/test/authenticationunittest/UTTest_dm_auth_state.h @@ -15,6 +15,8 @@ #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" @@ -29,7 +31,7 @@ public: void SetUp(); void TearDown(); - std::shared_ptr dmAuthState_ = std::make_shared(); + std::shared_ptr dmAuthState_ = std::make_shared(); static inline std::shared_ptr hiChainAuthConnectorMock_ = std::make_shared(); static inline std::shared_ptr deviceProfileConnectorMock_ = diff --git a/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp b/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp index eee844e2d..dc45c34c2 100644 --- a/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_state_machine.cpp @@ -29,19 +29,19 @@ namespace DistributedHardware { std::shared_ptr dmAuthStateMock_ = std::make_shared(); -void SetUp() +void DmAuthStateMachineTest::SetUp() { } -void TearDown() +void DmAuthStateMachineTest::TearDown() { } -void SetUpTestCase() +void DmAuthStateMachineTest::SetUpTestCase() { } -void TearDownTestCase() +void DmAuthStateMachineTest::TearDownTestCase() { } diff --git a/test/authenticationunittest/UTTest_dm_auth_state_machine.h b/test/authenticationunittest/UTTest_dm_auth_state_machine.h index 5ed144b2e..e73ac7537 100644 --- a/test/authenticationunittest/UTTest_dm_auth_state_machine.h +++ b/test/authenticationunittest/UTTest_dm_auth_state_machine.h @@ -15,6 +15,8 @@ #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 diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index cd9a576d4..ecdac7903 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1505,6 +1505,7 @@ ohos_unittest("UTTest_auth_manager_new") { "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", @@ -1540,6 +1541,7 @@ ohos_unittest("UTTest_dm_auth_context") { "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", @@ -1580,6 +1582,7 @@ ohos_unittest("UTTest_dm_auth_message_processor") { "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", @@ -1619,6 +1622,7 @@ ohos_unittest("UTTest_dm_auth_state") { "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", @@ -1656,6 +1660,7 @@ ohos_unittest("UTTest_dm_auth_state_machine") { "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", -- Gitee From 0c91fe4a20331e8177f038959e3bb1c85681324b Mon Sep 17 00:00:00 2001 From: l60055366 Date: Fri, 21 Mar 2025 16:48:15 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Edm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../UTTest_dm_auth_context.cpp | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test/authenticationunittest/UTTest_dm_auth_context.cpp b/test/authenticationunittest/UTTest_dm_auth_context.cpp index 495d1ab94..f219ec09b 100644 --- a/test/authenticationunittest/UTTest_dm_auth_context.cpp +++ b/test/authenticationunittest/UTTest_dm_auth_context.cpp @@ -129,45 +129,45 @@ 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"; - authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; + 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 = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); - authManager_->context_->direction = DmAuthDirection::DM_AUTH_SINK; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); side = DmAuthSide::DM_AUTH_REMOTE_SIDE; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, DM_OK); side = static_cast(2); - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, ERR_DM_FAILED); side = DmAuthSide::DM_AUTH_REMOTE_SIDE; authorizedScope = DM_AUTH_SCOPE_DEVICE; - ret = authManager_->SetCredentialId(side, authorizedScope, credentialId); + ret = dmAuthContext_->SetCredentialId(side, authorizedScope, credentialId); ASSERT_EQ(ret, ERR_DM_FAILED); } @@ -176,45 +176,45 @@ 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"; - authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; - int32_t ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + 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 = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); - authManager_->context_->direction = DmAuthDirection::DM_AUTH_SINK; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + dmAuthContext_->direction = DmAuthDirection::DM_AUTH_SINK; + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); side = DmAuthSide::DM_AUTH_REMOTE_SIDE; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_APP; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); authManager_->context_->direction = DmAuthDirection::DM_AUTH_SOURCE; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); authorizedScope = DmAuthScope::DM_AUTH_SCOPE_USER; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, DM_OK); side = static_cast(2); - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, ERR_DM_FAILED); side = DmAuthSide::DM_AUTH_REMOTE_SIDE; authorizedScope = DM_AUTH_SCOPE_DEVICE; - ret = authManager_->SetPublicKey(side, authorizedScope, publicKey); + ret = dmAuthContext_->SetPublicKey(side, authorizedScope, publicKey); ASSERT_EQ(ret, ERR_DM_FAILED); } } -- Gitee