1
2 /*
3 * Copyright (C) 2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "remote_auth_invoker_context_test.h"
18
19 #include "attributes.h"
20 #include "context_pool.h"
21 #include "context_callback_impl.h"
22 #include "simple_auth_context.h"
23 #include "iam_common_defines.h"
24 #include "iam_logger.h"
25 #include "iam_ptr.h"
26 #include "remote_auth_context.h"
27 #include "remote_auth_invoker_context.h"
28 #include "remote_iam_callback.h"
29 #include "context_appstate_observer.h"
30 #include "auth_widget_helper.h"
31 #include "remote_auth_service.h"
32 #include "mock_context.h"
33 #include "mock_iuser_auth_interface.h"
34
35 #define LOG_TAG "USER_AUTH_SA"
36
37 namespace OHOS {
38 namespace UserIam {
39 namespace UserAuth {
40 using namespace testing;
41 using namespace testing::ext;
42
SetUpTestCase()43 void RemoteAuthInvokerContextTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void RemoteAuthInvokerContextTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void RemoteAuthInvokerContextTest::SetUp()
52 {
53 }
54
TearDown()55 void RemoteAuthInvokerContextTest::TearDown()
56 {
57 }
58
59 HWTEST_F(RemoteAuthInvokerContextTest, OnMessageTest, TestSize.Level1)
60 {
61 IAM_LOGI("OnMessageTest start");
62 uint64_t contextId = 100;
63 AuthParamInner authParam = {};
64 RemoteAuthInvokerContextParam param = {};
65 std::string connectionName = "test5";
66 std::string collectorNetworkId = "5";
67 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
68 auto remoteAuthInvokerContext = Common::MakeShared<RemoteAuthInvokerContext>(
69 contextId, authParam, param, contextCallback
70 );
71 std::string srcEndPoint = "start";
72 auto request = Common::MakeShared<Attributes>();
73 auto reply = Common::MakeShared<Attributes>();
74 request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_TIP);
75 EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply));
76 request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_RESULT);
77 EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply));
78 request->SetInt32Value(Attributes::ATTR_MSG_TYPE, START_REMOTE_AUTH);
79 std::optional<uint32_t> cancelTimerId = 10;
80 remoteAuthInvokerContext->cancelTimerId_ = cancelTimerId;
81 EXPECT_NO_THROW(remoteAuthInvokerContext->OnMessage(connectionName, srcEndPoint, request, reply));
82 IAM_LOGI("OnMessageTest end");
83 }
84
85 HWTEST_F(RemoteAuthInvokerContextTest, OnConnectStatusTest, TestSize.Level1)
86 {
87 IAM_LOGI("OnConnectStatusTest start");
88 uint64_t contextId = 100;
89 AuthParamInner authParam = {};
90 RemoteAuthInvokerContextParam param = {};
91 std::string connectionName = "test6";
92 std::string collectorNetworkId = "6";
93 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
94 auto remoteAuthInvokerContext = Common::MakeShared<RemoteAuthInvokerContext>(
95 contextId, authParam, param, contextCallback
96 );
97 EXPECT_NO_THROW(remoteAuthInvokerContext->OnConnectStatus(connectionName, ConnectStatus::DISCONNECTED));
98 EXPECT_NO_THROW(remoteAuthInvokerContext->OnConnectStatus(connectionName, ConnectStatus::CONNECTED));
99 IAM_LOGI("OnConnectStatusTest end");
100 }
101
102 HWTEST_F(RemoteAuthInvokerContextTest, ProcAuthResultMsgInnerTest, TestSize.Level1)
103 {
104 IAM_LOGI("ProcAuthResultMsgInner start");
105 uint64_t contextId = 100;
106 AuthParamInner authParam = {};
107 RemoteAuthInvokerContextParam param = {};
108 std::string connectionName = "test7";
109 std::string collectorNetworkId = "7";
110 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
111 auto remoteAuthInvokerContext = Common::MakeShared<RemoteAuthInvokerContext>(
112 contextId, authParam, param, contextCallback
113 );
114 auto message = Common::MakeShared<Attributes>();
115 message->SetInt32Value(Attributes::ATTR_RESULT, ResultCode::GENERAL_ERROR);
116 message->SetInt32Value(Attributes::ATTR_SIGNED_AUTH_RESULT, true);
117 int32_t resultCode;
118 Attributes attr;
119 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
120 EXPECT_NE(mockHdi, nullptr);
121 EXPECT_CALL(*mockHdi, GetAuthResultFromMessage(_, _, _))
122 .WillOnce([](const std::string &remoteUdid, const std::vector<uint8_t> &message,
__anon830b301c0102(const std::string &remoteUdid, const std::vector<uint8_t> &message, HdiAuthResultInfo &authResultInfo) 123 HdiAuthResultInfo &authResultInfo) {
124 authResultInfo.result = ResultCode::FAIL;
125 return 0;
126 })
127 .WillOnce([](const std::string &remoteUdid, const std::vector<uint8_t> &message,
__anon830b301c0202(const std::string &remoteUdid, const std::vector<uint8_t> &message, HdiAuthResultInfo &authResultInfo) 128 HdiAuthResultInfo &authResultInfo) {
129 authResultInfo.result = ResultCode::LOCKED;
130 return 0;
131 })
132 .WillRepeatedly([](const std::string &remoteUdid, const std::vector<uint8_t> &message,
__anon830b301c0302(const std::string &remoteUdid, const std::vector<uint8_t> &message, HdiAuthResultInfo &authResultInfo) 133 HdiAuthResultInfo &authResultInfo) {
134 authResultInfo.result = ResultCode::SUCCESS;
135 return 0;
136 });
137 EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr));
138 EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr));
139 EXPECT_NO_THROW(remoteAuthInvokerContext->ProcAuthResultMsgInner(*message, resultCode, attr));
140 IAM_LOGI("ProcAuthResultMsgInner end");
141 }
142 } // namespace UserAuth
143 } // namespace UserIam
144 } // namespace OHOS
145