1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "remote_auth_context_test.h"
17
18 #include "attributes.h"
19 #include "context_pool.h"
20 #include "context_callback_impl.h"
21 #include "simple_auth_context.h"
22 #include "iam_common_defines.h"
23 #include "iam_logger.h"
24 #include "iam_ptr.h"
25 #include "remote_auth_context.h"
26 #include "remote_auth_invoker_context.h"
27 #include "remote_iam_callback.h"
28 #include "context_appstate_observer.h"
29 #include "auth_widget_helper.h"
30 #include "remote_auth_service.h"
31 #include "mock_context.h"
32
33 #define LOG_TAG "USER_AUTH_SA"
34
35 namespace OHOS {
36 namespace UserIam {
37 namespace UserAuth {
38 using namespace testing;
39 using namespace testing::ext;
40
SetUpTestCase()41 void RemoteAuthContextTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void RemoteAuthContextTest::TearDownTestCase()
46 {
47 }
48
SetUp()49 void RemoteAuthContextTest::SetUp()
50 {
51 }
52
TearDown()53 void RemoteAuthContextTest::TearDown()
54 {
55 }
56
57 HWTEST_F(RemoteAuthContextTest, RemoteAuthContextTest, TestSize.Level0)
58 {
59 IAM_LOGI("RemoteAuthContextTest start");
60 uint64_t contextId = 100;
61 std::shared_ptr<Authentication> auth;
62 RemoteAuthContextParam param;
63 param.authType = ALL;
64 std::string connectionName = "test";
65 std::string collectorNetworkId = "1";
66 param.connectionName = connectionName;
67 param.collectorNetworkId = collectorNetworkId;
68 param.executorInfoMsg = {};
69 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
70 auto remoteAuthContext1 = Common::MakeShared<RemoteAuthContext>(
71 contextId, auth, param, contextCallback
72 );
73 EXPECT_NE(remoteAuthContext1, nullptr);
74 std::vector<uint8_t> executorInfoMsg = {7, 8, 9};
75 param.executorInfoMsg = executorInfoMsg;
76 auto remoteAuthContext2 = Common::MakeShared<RemoteAuthContext>(
77 contextId, auth, param, contextCallback
78 );
79 EXPECT_NE(remoteAuthContext2, nullptr);
80 bool needSetupConnection = true;
81 std::optional<uint32_t> cancelTimerId = 10;
82 remoteAuthContext2->cancelTimerId_ = cancelTimerId;
83 remoteAuthContext2->needSetupConnection_ = needSetupConnection;
84 IAM_LOGI("RemoteAuthContextTest end");
85 }
86
87 HWTEST_F(RemoteAuthContextTest, OnStartTest, TestSize.Level0)
88 {
89 IAM_LOGI("OnStartTest start");
90 uint64_t contextId = 100;
91 std::shared_ptr<Authentication> auth;
92 RemoteAuthContextParam param;
93 param.authType = ALL;
94 std::string connectionName = "test2";
95 std::string collectorNetworkId = "2";
96 param.connectionName = connectionName;
97 param.collectorNetworkId = collectorNetworkId;
98 param.executorInfoMsg = {};
99 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
100 std::vector<uint8_t> executorInfoMsg = {7, 8, 9};
101 param.executorInfoMsg = executorInfoMsg;
102 auto remoteAuthContext = Common::MakeShared<RemoteAuthContext>(
103 contextId, auth, param, contextCallback
104 );
105 EXPECT_NE(remoteAuthContext, nullptr);
106 bool result = remoteAuthContext->OnStart();
107 EXPECT_EQ(result, false);
108 bool needSetupConnection = true;
109 std::optional<uint32_t> cancelTimerId = 10;
110 remoteAuthContext->cancelTimerId_ = cancelTimerId;
111 remoteAuthContext->needSetupConnection_ = needSetupConnection;
112 result = remoteAuthContext->OnStart();
113 EXPECT_EQ(result, true);
114 IAM_LOGI("OnStartTest end");
115 }
116
117 HWTEST_F(RemoteAuthContextTest, StartAuthDelayedTest, TestSize.Level1)
118 {
119 IAM_LOGI("StartAuthDelayedTest start");
120 uint64_t contextId = 100;
121 std::shared_ptr<Authentication> auth;
122 RemoteAuthContextParam param;
123 param.authType = ALL;
124 std::string connectionName = "test3";
125 std::string collectorNetworkId = "3";
126 param.connectionName = connectionName;
127 param.collectorNetworkId = collectorNetworkId;
128 param.executorInfoMsg = {};
129 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
130 std::vector<uint8_t> executorInfoMsg = {6, 8, 9};
131 param.executorInfoMsg = executorInfoMsg;
132 auto remoteAuthContext = Common::MakeShared<RemoteAuthContext>(
133 contextId, auth, param, contextCallback
134 );
135 EXPECT_NE(remoteAuthContext, nullptr);
136 bool needSetupConnection = true;
137 remoteAuthContext->needSetupConnection_ = needSetupConnection;
138 remoteAuthContext->OnStart();
139 EXPECT_NO_THROW(remoteAuthContext->StartAuthDelayed());
140 IAM_LOGI("StartAuthDelayedTest end");
141 }
142
143 HWTEST_F(RemoteAuthContextTest, SendQueryExecutorInfoMsgTest, TestSize.Level2)
144 {
145 IAM_LOGI("SendQueryExecutorInfoMsgTest start");
146 uint64_t contextId = 100;
147 std::shared_ptr<Authentication> auth;
148 RemoteAuthContextParam param;
149 param.authType = ALL;
150 std::string connectionName = "test4";
151 std::string collectorNetworkId = "4";
152 param.connectionName = connectionName;
153 param.collectorNetworkId = collectorNetworkId;
154 param.executorInfoMsg = {};
155 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
156 std::vector<uint8_t> executorInfoMsg = {6, 8, 7};
157 param.executorInfoMsg = executorInfoMsg;
158 auto remoteAuthContext = Common::MakeShared<RemoteAuthContext>(
159 contextId, auth, param, contextCallback
160 );
161 EXPECT_NE(remoteAuthContext, nullptr);
162 bool needSetupConnection = true;
163 remoteAuthContext->needSetupConnection_ = needSetupConnection;
164 bool result = remoteAuthContext->SendQueryExecutorInfoMsg();
165 EXPECT_EQ(result, false);
166 IAM_LOGI("SendQueryExecutorInfoMsgTest end");
167 }
168
169 HWTEST_F(RemoteAuthContextTest, OnConnectStatusTest, TestSize.Level0)
170 {
171 IAM_LOGI("OnConnectStatusTest start");
172 uint64_t contextId = 100;
173 std::shared_ptr<Authentication> auth;
174 RemoteAuthContextParam param;
175 param.authType = ALL;
176 std::string connectionName = "test5";
177 std::string collectorNetworkId = "5";
178 param.connectionName = connectionName;
179 param.collectorNetworkId = collectorNetworkId;
180 param.executorInfoMsg = {};
181 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
182 std::vector<uint8_t> executorInfoMsg = {6, 5, 7};
183 param.executorInfoMsg = executorInfoMsg;
184 auto remoteAuthContext = Common::MakeShared<RemoteAuthContext>(
185 contextId, auth, param, contextCallback
186 );
187 EXPECT_NO_THROW(remoteAuthContext->OnConnectStatus(connectionName, ConnectStatus::DISCONNECTED));
188 EXPECT_NO_THROW(remoteAuthContext->OnConnectStatus(connectionName, ConnectStatus::CONNECTED));
189 IAM_LOGI("OnConnectStatusTest end");
190 }
191 } // namespace UserAuth
192 } // namespace UserIam
193 } // namespace OHOS
194