From d2b41c8b2f83a419bbc041b31d306840db7db3a0 Mon Sep 17 00:00:00 2001 From: liuhu Date: Wed, 16 Apr 2025 18:47:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6ut=E5=88=86=E6=94=AF=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E7=8E=87=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuhu Change-Id: Ic357dbb3ca63d2426c8cc4fb3e6622c1697bf0dc --- test/unittest/services/BUILD.gn | 2 + .../services/inc/remote_auth_context_test.h | 37 ++++ .../inc/remote_auth_invoker_context_test.h | 37 ++++ .../services/src/remote_auth_context_test.cpp | 194 ++++++++++++++++++ .../src/remote_auth_invoker_context_test.cpp | 145 +++++++++++++ 5 files changed, 415 insertions(+) create mode 100644 test/unittest/services/inc/remote_auth_context_test.h create mode 100644 test/unittest/services/inc/remote_auth_invoker_context_test.h create mode 100644 test/unittest/services/src/remote_auth_context_test.cpp create mode 100644 test/unittest/services/src/remote_auth_invoker_context_test.cpp diff --git a/test/unittest/services/BUILD.gn b/test/unittest/services/BUILD.gn index 79d0443c8..a740e093f 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -145,6 +145,8 @@ ohos_unittest("iam_services_test") { "src/ipc_common_test.cpp", "src/keygurad_status_listener_test.cpp", "src/relative_timer_test.cpp", + "src/remote_auth_context_test.cpp", + "src/remote_auth_invoker_context_test.cpp", "src/resource_node_pool_test.cpp", "src/resource_node_test.cpp", "src/resource_node_utils_test.cpp", diff --git a/test/unittest/services/inc/remote_auth_context_test.h b/test/unittest/services/inc/remote_auth_context_test.h new file mode 100644 index 000000000..846530915 --- /dev/null +++ b/test/unittest/services/inc/remote_auth_context_test.h @@ -0,0 +1,37 @@ +/* + * 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 IAM_REMOTE_AUTH_CONTEXT_TEST_H +#define IAM_REMOTE_AUTH_CONTEXT_TEST_H + +#include +#include + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class RemoteAuthContextTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_REMOTE_AUTH_CONTEXT_TEST_H \ No newline at end of file diff --git a/test/unittest/services/inc/remote_auth_invoker_context_test.h b/test/unittest/services/inc/remote_auth_invoker_context_test.h new file mode 100644 index 000000000..6fa65d1b3 --- /dev/null +++ b/test/unittest/services/inc/remote_auth_invoker_context_test.h @@ -0,0 +1,37 @@ +/* + * 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 IAM_REMOTE_AUTH_INVOKER_CONTEXT_TEST_H +#define IAM_REMOTE_AUTH_INVOKER_CONTEXT_TEST_H + +#include +#include + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +class RemoteAuthInvokerContextTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; +}; +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // IAM_REMOTE_AUTH_INVOKER_CONTEXT_TEST_H \ No newline at end of file diff --git a/test/unittest/services/src/remote_auth_context_test.cpp b/test/unittest/services/src/remote_auth_context_test.cpp new file mode 100644 index 000000000..7e42d31af --- /dev/null +++ b/test/unittest/services/src/remote_auth_context_test.cpp @@ -0,0 +1,194 @@ +/* + * 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 "remote_auth_context_test.h" + +#include "attributes.h" +#include "context_pool.h" +#include "context_callback_impl.h" +#include "simple_auth_context.h" +#include "iam_common_defines.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "remote_auth_context.h" +#include "remote_auth_invoker_context.h" +#include "remote_iam_callback.h" +#include "context_appstate_observer.h" +#include "auth_widget_helper.h" +#include "remote_auth_service.h" +#include "mock_context.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +using namespace testing; +using namespace testing::ext; + +void RemoteAuthContextTest::SetUpTestCase() +{ +} + +void RemoteAuthContextTest::TearDownTestCase() +{ +} + +void RemoteAuthContextTest::SetUp() +{ +} + +void RemoteAuthContextTest::TearDown() +{ +} + +HWTEST_F(RemoteAuthContextTest, RemoteAuthContextTest, TestSize.Level0) +{ + IAM_LOGI("RemoteAuthContextTest start"); + uint64_t contextId = 100; + std::shared_ptr auth; + RemoteAuthContextParam param; + param.authType = ALL; + std::string connectionName = "test"; + std::string collectorNetworkId = "1"; + param.connectionName = connectionName; + param.collectorNetworkId = collectorNetworkId; + param.executorInfoMsg = {}; + std::shared_ptr contextCallback = Common::MakeShared(); + auto remoteAuthContext1 = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NE(remoteAuthContext1, nullptr); + std::vector executorInfoMsg = {7, 8, 9}; + param.executorInfoMsg = executorInfoMsg; + auto remoteAuthContext2 = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NE(remoteAuthContext2, nullptr); + bool needSetupConnection = true; + std::optional cancelTimerId = 10; + remoteAuthContext2->cancelTimerId_ = cancelTimerId; + remoteAuthContext2->needSetupConnection_ = needSetupConnection; + IAM_LOGI("RemoteAuthContextTest end"); +} + +HWTEST_F(RemoteAuthContextTest, OnStartTest, TestSize.Level0) +{ + IAM_LOGI("OnStartTest start"); + uint64_t contextId = 100; + std::shared_ptr auth; + RemoteAuthContextParam param; + param.authType = ALL; + std::string connectionName = "test2"; + std::string collectorNetworkId = "2"; + param.connectionName = connectionName; + param.collectorNetworkId = collectorNetworkId; + param.executorInfoMsg = {}; + std::shared_ptr contextCallback = Common::MakeShared(); + std::vector executorInfoMsg = {7, 8, 9}; + param.executorInfoMsg = executorInfoMsg; + auto remoteAuthContext = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NE(remoteAuthContext, nullptr); + bool result = remoteAuthContext->OnStart(); + EXPECT_EQ(result, false); + bool needSetupConnection = true; + std::optional cancelTimerId = 10; + remoteAuthContext->cancelTimerId_ = cancelTimerId; + remoteAuthContext->needSetupConnection_ = needSetupConnection; + result = remoteAuthContext->OnStart(); + EXPECT_EQ(result, true); + IAM_LOGI("OnStartTest end"); +} + +HWTEST_F(RemoteAuthContextTest, StartAuthDelayedTest, TestSize.Level1) +{ + IAM_LOGI("StartAuthDelayedTest start"); + uint64_t contextId = 100; + std::shared_ptr auth; + RemoteAuthContextParam param; + param.authType = ALL; + std::string connectionName = "test3"; + std::string collectorNetworkId = "3"; + param.connectionName = connectionName; + param.collectorNetworkId = collectorNetworkId; + param.executorInfoMsg = {}; + std::shared_ptr contextCallback = Common::MakeShared(); + std::vector executorInfoMsg = {6, 8, 9}; + param.executorInfoMsg = executorInfoMsg; + auto remoteAuthContext = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NE(remoteAuthContext, nullptr); + bool needSetupConnection = true; + remoteAuthContext->needSetupConnection_ = needSetupConnection; + remoteAuthContext->OnStart(); + EXPECT_NO_THROW(remoteAuthContext->StartAuthDelayed()); + IAM_LOGI("StartAuthDelayedTest end"); +} + +HWTEST_F(RemoteAuthContextTest, SendQueryExecutorInfoMsgTest, TestSize.Level2) +{ + IAM_LOGI("SendQueryExecutorInfoMsgTest start"); + uint64_t contextId = 100; + std::shared_ptr auth; + RemoteAuthContextParam param; + param.authType = ALL; + std::string connectionName = "test4"; + std::string collectorNetworkId = "4"; + param.connectionName = connectionName; + param.collectorNetworkId = collectorNetworkId; + param.executorInfoMsg = {}; + std::shared_ptr contextCallback = Common::MakeShared(); + std::vector executorInfoMsg = {6, 8, 7}; + param.executorInfoMsg = executorInfoMsg; + auto remoteAuthContext = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NE(remoteAuthContext, nullptr); + bool needSetupConnection = true; + remoteAuthContext->needSetupConnection_ = needSetupConnection; + bool result = remoteAuthContext->SendQueryExecutorInfoMsg(); + EXPECT_EQ(result, false); + IAM_LOGI("SendQueryExecutorInfoMsgTest end"); +} + +HWTEST_F(RemoteAuthContextTest, OnConnectStatusTest, TestSize.Level0) +{ + IAM_LOGI("OnConnectStatusTest start"); + uint64_t contextId = 100; + std::shared_ptr auth; + RemoteAuthContextParam param; + param.authType = ALL; + std::string connectionName = "test5"; + std::string collectorNetworkId = "5"; + param.connectionName = connectionName; + param.collectorNetworkId = collectorNetworkId; + param.executorInfoMsg = {}; + std::shared_ptr contextCallback = Common::MakeShared(); + std::vector executorInfoMsg = {6, 5, 7}; + param.executorInfoMsg = executorInfoMsg; + auto remoteAuthContext = Common::MakeShared( + contextId, auth, param, contextCallback + ); + EXPECT_NO_THROW(remoteAuthContext->OnConnectStatus(connectionName, ConnectStatus::DISCONNECTED)); + EXPECT_NO_THROW(remoteAuthContext->OnConnectStatus(connectionName, ConnectStatus::CONNECTED)); + IAM_LOGI("OnConnectStatusTest end"); +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS + \ No newline at end of file diff --git a/test/unittest/services/src/remote_auth_invoker_context_test.cpp b/test/unittest/services/src/remote_auth_invoker_context_test.cpp new file mode 100644 index 000000000..ea0d03388 --- /dev/null +++ b/test/unittest/services/src/remote_auth_invoker_context_test.cpp @@ -0,0 +1,145 @@ + +/* + * 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 "remote_auth_invoker_context_test.h" + +#include "attributes.h" +#include "context_pool.h" +#include "context_callback_impl.h" +#include "simple_auth_context.h" +#include "iam_common_defines.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "remote_auth_context.h" +#include "remote_auth_invoker_context.h" +#include "remote_iam_callback.h" +#include "context_appstate_observer.h" +#include "auth_widget_helper.h" +#include "remote_auth_service.h" +#include "mock_context.h" +#include "mock_iuser_auth_interface.h" + +#define LOG_TAG "USER_AUTH_SA" + +namespace OHOS { +namespace UserIam { +namespace UserAuth { +using namespace testing; +using namespace testing::ext; + +void RemoteAuthInvokerContextTest::SetUpTestCase() +{ +} + +void RemoteAuthInvokerContextTest::TearDownTestCase() +{ +} + +void RemoteAuthInvokerContextTest::SetUp() +{ +} + +void RemoteAuthInvokerContextTest::TearDown() +{ +} + +HWTEST_F(RemoteAuthInvokerContextTest, OnMessageTest, TestSize.Level1) +{ + IAM_LOGI("OnMessageTest start"); + uint64_t contextId = 100; + AuthParamInner authParam = {}; + RemoteAuthInvokerContextParam param = {}; + std::string connectionName = "test5"; + std::string collectorNetworkId = "5"; + std::shared_ptr contextCallback = Common::MakeShared(); + auto remoteAuthInvokerContext = Common::MakeShared( + contextId, authParam, param, contextCallback + ); + std::string srcEndPoint = "start"; + auto request = Common::MakeShared(); + auto reply = Common::MakeShared(); + request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_TIP); + EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply)); + request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_RESULT); + EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply)); + request->SetInt32Value(Attributes::ATTR_MSG_TYPE, START_REMOTE_AUTH); + std::optional cancelTimerId = 10; + remoteAuthInvokerContext->cancelTimerId_ = cancelTimerId; + EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply)); + IAM_LOGI("OnMessageTest end"); +} + +HWTEST_F(RemoteAuthInvokerContextTest, OnConnectStatusTest, TestSize.Level1) +{ + IAM_LOGI("OnConnectStatusTest start"); + uint64_t contextId = 100; + AuthParamInner authParam = {}; + RemoteAuthInvokerContextParam param = {}; + std::string connectionName = "test6"; + std::string collectorNetworkId = "6"; + std::shared_ptr contextCallback = Common::MakeShared(); + auto remoteAuthInvokerContext = Common::MakeShared( + contextId, authParam, param, contextCallback + ); + EXPECT_NO_THROW(remoteAuthInvokerContext->OnConnectStatus(connectionName, ConnectStatus::DISCONNECTED)); + EXPECT_NO_THROW(remoteAuthInvokerContext->OnConnectStatus(connectionName, ConnectStatus::CONNECTED)); + IAM_LOGI("OnConnectStatusTest end"); +} + +HWTEST_F(RemoteAuthInvokerContextTest, ProcAuthResultMsgInnerTest, TestSize.Level1) +{ + IAM_LOGI("ProcAuthResultMsgInner start"); + uint64_t contextId = 100; + AuthParamInner authParam = {}; + RemoteAuthInvokerContextParam param = {}; + std::string connectionName = "test7"; + std::string collectorNetworkId = "7"; + std::shared_ptr contextCallback = Common::MakeShared(); + auto remoteAuthInvokerContext = Common::MakeShared( + contextId, authParam, param, contextCallback + ); + auto message = Common::MakeShared(); + message->SetInt32Value(Attributes::ATTR_RESULT, ResultCode::GENERAL_ERROR); + message->SetInt32Value(Attributes::ATTR_SIGNED_AUTH_RESULT, true); + int32_t resultCode; + Attributes attr; + auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); + EXPECT_NE(mockHdi, nullptr); + EXPECT_CALL(*mockHdi, GetAuthResultFromMessage(_, _, _)) + .WillOnce([](const std::string &remoteUdid, const std::vector &message, + HdiAuthResultInfo &authResultInfo) { + authResultInfo.result = ResultCode::FAIL; + return 0; + }) + .WillOnce([](const std::string &remoteUdid, const std::vector &message, + HdiAuthResultInfo &authResultInfo) { + authResultInfo.result = ResultCode::LOCKED; + return 0; + }) + .WillRepeatedly([](const std::string &remoteUdid, const std::vector &message, + HdiAuthResultInfo &authResultInfo) { + authResultInfo.result = ResultCode::SUCCESS; + return 0; + }); + EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr)); + EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr)); + EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr)); + IAM_LOGI("ProcAuthResultMsgInner end"); +} + } // namespace UserAuth + } // namespace UserIam + } // namespace OHOS + \ No newline at end of file -- Gitee