1 /*
2 * Copyright (C) 2022-2023 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 "co_auth_service_test.h"
17
18 #include <future>
19
20 #include "co_auth_service.h"
21 #include "iam_ptr.h"
22 #include "mock_executor_callback.h"
23 #include "mock_ipc_common.h"
24 #include "mock_iuser_auth_interface.h"
25 #include "mock_resource_node.h"
26 #include "resource_node_pool.h"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 using namespace testing;
32 using namespace testing::ext;
33
SetUpTestCase()34 void CoAuthServiceTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void CoAuthServiceTest::TearDownTestCase()
39 {
40 }
41
SetUp()42 void CoAuthServiceTest::SetUp()
43 {
44 MockIUserAuthInterface::Holder::GetInstance().Reset();
45 }
46
TearDown()47 void CoAuthServiceTest::TearDown()
48 {
49 MockIUserAuthInterface::Holder::GetInstance().Reset();
50 }
51
52 HWTEST_F(CoAuthServiceTest, CoAuthServiceTest001, TestSize.Level0)
53 {
54 sptr<MockExecutorCallback> testCallback(new (std::nothrow) MockExecutorCallback());
55 EXPECT_NE(testCallback, nullptr);
56
57 CoAuthInterface::ExecutorRegisterInfo info = {};
58 info.authType = FINGERPRINT;
59 info.executorRole = SCHEDULER;
60 info.executorSensorHint = 0;
61 info.executorMatcher = 0;
62 info.esl = ESL1;
63 info.publicKey = {'a', 'b', 'c', 'd'};
64
65 auto service = Common::MakeShared<CoAuthService>();
66 EXPECT_NE(service, nullptr);
67 service->SetIsReady(true);
68 service->SetAccessTokenReady(true);
69 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
70 EXPECT_NE(mockHdi, nullptr);
71 std::promise<void> promise;
72 EXPECT_CALL(*testCallback, OnMessengerReady(_, _, _)).Times(1).WillOnce(
73 [&promise](sptr<ExecutorMessengerInterface> &messenger,
__anonbd918b2f0102(sptr<ExecutorMessengerInterface> &messenger, const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList) 74 const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList) {
75 promise.set_value();
76 }
77 );
78 EXPECT_CALL(*mockHdi, AddExecutor(_, _, _, _))
79 .Times(2)
80 .WillOnce(Return(HDF_FAILURE))
81 .WillOnce(
82 [](const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey,
__anonbd918b2f0202(const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey, std::vector<uint64_t> &templateIds) 83 std::vector<uint64_t> &templateIds) {
84 index = 12345;
85 return HDF_SUCCESS;
86 }
87 );
88 IpcCommon::AddPermission(ACCESS_AUTH_RESPOOL);
89 sptr<ExecutorCallbackInterface> callbackInterface = testCallback;
90 uint64_t executorIndex = service->ExecutorRegister(info, callbackInterface);
91 EXPECT_EQ(executorIndex, 0);
92 executorIndex = service->ExecutorRegister(info, callbackInterface);
93 EXPECT_NE(executorIndex, 0);
94 promise.get_future().get();
95 service->ExecutorUnregister(executorIndex);
96 IpcCommon::DeleteAllPermission();
97 }
98
99 HWTEST_F(CoAuthServiceTest, CoAuthServiceTest002, TestSize.Level0)
100 {
101 auto service = Common::MakeShared<CoAuthService>();
102 EXPECT_NE(service, nullptr);
103
104 CoAuthInterface::ExecutorRegisterInfo info = {};
105 sptr<ExecutorCallbackInterface> testCallback(nullptr);
106 uint64_t executorIndex = service->ExecutorRegister(info, testCallback);
107 EXPECT_EQ(executorIndex, 0);
108 }
109
110 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestExecutorRegister001, TestSize.Level0)
111 {
112 sptr<MockExecutorCallback> testCallback(new (std::nothrow) MockExecutorCallback());
113 EXPECT_NE(testCallback, nullptr);
114
115 CoAuthInterface::ExecutorRegisterInfo info = {};
116 info.authType = FINGERPRINT;
117 info.executorRole = SCHEDULER;
118 info.executorSensorHint = 0;
119 info.executorMatcher = 0;
120 info.esl = ESL1;
121 info.publicKey = {'a', 'b', 'c', 'd'};
122
123 auto service = Common::MakeShared<CoAuthService>();
124 EXPECT_NE(service, nullptr);
125 service->SetIsReady(false);
126 service->SetAccessTokenReady(false);
127 sptr<ExecutorCallbackInterface> callbackInterface = testCallback;
128 uint64_t executorIndex = service->ExecutorRegister(info, callbackInterface);
129 EXPECT_EQ(executorIndex, INVALID_EXECUTOR_INDEX);
130 service->ExecutorUnregister(executorIndex);
131 IpcCommon::DeleteAllPermission();
132 }
133
134 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestExecutorRegister002, TestSize.Level0)
135 {
136 sptr<MockExecutorCallback> testCallback(new (std::nothrow) MockExecutorCallback());
137 EXPECT_NE(testCallback, nullptr);
138
139 CoAuthInterface::ExecutorRegisterInfo info = {};
140 info.authType = FINGERPRINT;
141 info.executorRole = SCHEDULER;
142 info.executorSensorHint = 0;
143 info.executorMatcher = 0;
144 info.esl = ESL1;
145 info.publicKey = {'a', 'b', 'c', 'd'};
146
147 auto service = Common::MakeShared<CoAuthService>();
148 EXPECT_NE(service, nullptr);
149 service->SetIsReady(true);
150 service->SetAccessTokenReady(true);
151 sptr<ExecutorCallbackInterface> callbackInterface = testCallback;
152 uint64_t executorIndex = service->ExecutorRegister(info, callbackInterface);
153 EXPECT_EQ(executorIndex, INVALID_EXECUTOR_INDEX);
154 service->ExecutorUnregister(executorIndex);
155 IpcCommon::DeleteAllPermission();
156 }
157
158 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestDump, TestSize.Level0)
159 {
160 int testFd1 = -1;
161 int testFd2 = 1;
162 std::vector<std::u16string> testArgs;
163
164 auto service = Common::MakeShared<CoAuthService>();
165 EXPECT_NE(service, nullptr);
166
167 auto node = MockResourceNode::CreateWithExecuteIndex(20);
168 EXPECT_NE(node, nullptr);
169 EXPECT_TRUE(ResourceNodePool::Instance().Insert(node));
170
171 EXPECT_EQ(service->Dump(testFd1, testArgs), INVALID_PARAMETERS);
172 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
173 testArgs.push_back(u"-h");
174 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
175 testArgs.clear();
176 testArgs.push_back(u"-l");
177 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
178 testArgs.clear();
179 testArgs.push_back(u"-k");
180 EXPECT_EQ(service->Dump(testFd2, testArgs), GENERAL_ERROR);
181
182 EXPECT_TRUE(ResourceNodePool::Instance().Delete(20));
183 }
184
185 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestRegisterAccessTokenListener, TestSize.Level0)
186 {
187 auto service = Common::MakeShared<CoAuthService>();
188 EXPECT_NE(service, nullptr);
189 service->OnDriverStart();
190 service->SetIsReady(true);
191 service->SetAccessTokenReady(true);
192 EXPECT_EQ(service->RegisterAccessTokenListener(), SUCCESS);
193 EXPECT_EQ(service->RegisterAccessTokenListener(), SUCCESS);
194 EXPECT_EQ(service->UnRegisterAccessTokenListener(), SUCCESS);
195 EXPECT_EQ(service->UnRegisterAccessTokenListener(), SUCCESS);
196 }
197
198 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestNotifyFwkReady, TestSize.Level0)
199 {
200 auto service = Common::MakeShared<CoAuthService>();
201 EXPECT_NE(service, nullptr);
202 service->SetIsReady(false);
203 EXPECT_NO_THROW(service->NotifyFwkReady());
204 service->SetIsReady(true);
205 EXPECT_NO_THROW(service->NotifyFwkReady());
206 }
207
208 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestOnDriverStop, TestSize.Level0)
209 {
210 auto service = Common::MakeShared<CoAuthService>();
211 EXPECT_NE(service, nullptr);
212 service->SetIsReady(true);
213 EXPECT_NO_THROW(service->OnDriverStart());
214 EXPECT_NO_THROW(service->OnDriverStop());
215 EXPECT_NO_THROW(service->OnDriverStop());
216 }
217 } // namespace UserAuth
218 } // namespace UserIam
219 } // namespace OHOS