• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     IpcExecutorRegisterInfo info = {};
58     info.authType = static_cast<int32_t>(FINGERPRINT);
59     info.executorRole = static_cast<int32_t>(SCHEDULER);
60     info.executorSensorHint = 0;
61     info.executorMatcher = 0;
62     info.esl = static_cast<int32_t>(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](const sptr<IExecutorMessenger> &messenger,
__anon9e2fa1110102(const sptr<IExecutorMessenger> &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             return SUCCESS;
77         }
78     );
79     EXPECT_CALL(*mockHdi, AddExecutor(_, _, _, _))
80         .Times(2)
81         .WillOnce(Return(HDF_FAILURE))
82         .WillOnce(
83             [](const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey,
__anon9e2fa1110202(const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey, std::vector<uint64_t> &templateIds) 84                 std::vector<uint64_t> &templateIds) {
85                 index = 12345;
86                 return HDF_SUCCESS;
87             }
88         );
89     IpcCommon::AddPermission(ACCESS_AUTH_RESPOOL);
90     sptr<IExecutorCallback> callbackInterface = testCallback;
91     uint64_t executorIndex = 0;
92     EXPECT_EQ(service->ExecutorRegister(info, callbackInterface, executorIndex), GENERAL_ERROR);
93     EXPECT_EQ(executorIndex, 0);
94     EXPECT_EQ(service->ExecutorRegister(info, callbackInterface, executorIndex), SUCCESS);
95     EXPECT_NE(executorIndex, 0);
96     promise.get_future().get();
97     EXPECT_EQ(service->ExecutorUnregister(executorIndex), SUCCESS);
98     IpcCommon::DeleteAllPermission();
99 }
100 
101 HWTEST_F(CoAuthServiceTest, CoAuthServiceTest002, TestSize.Level0)
102 {
103     auto service = Common::MakeShared<CoAuthService>();
104     EXPECT_NE(service, nullptr);
105 
106     IpcExecutorRegisterInfo info = {};
107     sptr<IExecutorCallback> testCallback(nullptr);
108     uint64_t executorIndex = 0;
109     EXPECT_EQ(service->ExecutorRegister(info, testCallback, executorIndex), INVALID_PARAMETERS);
110     EXPECT_EQ(executorIndex, 0);
111 }
112 
113 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestExecutorRegister001, TestSize.Level0)
114 {
115     sptr<MockExecutorCallback> testCallback(new (std::nothrow) MockExecutorCallback());
116     EXPECT_NE(testCallback, nullptr);
117 
118     IpcExecutorRegisterInfo info = {};
119     info.authType = static_cast<int32_t>(FINGERPRINT);
120     info.executorRole = static_cast<int32_t>(SCHEDULER);
121     info.executorSensorHint = 0;
122     info.executorMatcher = 0;
123     info.esl = static_cast<int32_t>(ESL1);
124     info.publicKey = {'a', 'b', 'c', 'd'};
125 
126     auto service = Common::MakeShared<CoAuthService>();
127     EXPECT_NE(service, nullptr);
128     service->SetIsReady(false);
129     service->SetAccessTokenReady(false);
130     sptr<IExecutorCallback> callbackInterface = testCallback;
131     uint64_t executorIndex = 0;
132     EXPECT_EQ(service->ExecutorRegister(info, callbackInterface, executorIndex), GENERAL_ERROR);
133     EXPECT_EQ(executorIndex, INVALID_EXECUTOR_INDEX);
134     EXPECT_EQ(service->ExecutorUnregister(executorIndex), SUCCESS);
135     IpcCommon::DeleteAllPermission();
136 }
137 
138 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestExecutorRegister002, TestSize.Level0)
139 {
140     sptr<MockExecutorCallback> testCallback(new (std::nothrow) MockExecutorCallback());
141     EXPECT_NE(testCallback, nullptr);
142 
143     IpcExecutorRegisterInfo info = {};
144     info.authType = static_cast<int32_t>(FINGERPRINT);
145     info.executorRole = static_cast<int32_t>(SCHEDULER);
146     info.executorSensorHint = 0;
147     info.executorMatcher = 0;
148     info.esl = static_cast<int32_t>(ESL1);
149     info.publicKey = {'a', 'b', 'c', 'd'};
150 
151     auto service = Common::MakeShared<CoAuthService>();
152     EXPECT_NE(service, nullptr);
153     service->SetIsReady(true);
154     service->SetAccessTokenReady(true);
155     sptr<IExecutorCallback> callbackInterface = testCallback;
156     uint64_t executorIndex = 0;
157     EXPECT_EQ(service->ExecutorRegister(info, callbackInterface, executorIndex), CHECK_PERMISSION_FAILED);
158     EXPECT_EQ(executorIndex, INVALID_EXECUTOR_INDEX);
159     EXPECT_EQ(service->ExecutorUnregister(executorIndex), CHECK_PERMISSION_FAILED);
160     IpcCommon::DeleteAllPermission();
161 }
162 
163 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestDump, TestSize.Level0)
164 {
165     int testFd1 = -1;
166     int testFd2 = 1;
167     std::vector<std::u16string> testArgs;
168 
169     auto service = Common::MakeShared<CoAuthService>();
170     EXPECT_NE(service, nullptr);
171 
172     auto node = MockResourceNode::CreateWithExecuteIndex(20);
173     EXPECT_NE(node, nullptr);
174     EXPECT_TRUE(ResourceNodePool::Instance().Insert(node));
175 
176     EXPECT_EQ(service->Dump(testFd1, testArgs), INVALID_PARAMETERS);
177     EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
178     testArgs.push_back(u"-h");
179     EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
180     testArgs.clear();
181     testArgs.push_back(u"-l");
182     EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
183     testArgs.clear();
184     testArgs.push_back(u"-k");
185     EXPECT_EQ(service->Dump(testFd2, testArgs), GENERAL_ERROR);
186 
187     EXPECT_TRUE(ResourceNodePool::Instance().Delete(20));
188 }
189 
190 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestRegisterAccessTokenListener, TestSize.Level0)
191 {
192     auto service = Common::MakeShared<CoAuthService>();
193     EXPECT_NE(service, nullptr);
194     service->OnDriverStart();
195     service->SetIsReady(true);
196     service->SetAccessTokenReady(true);
197     EXPECT_EQ(service->RegisterAccessTokenListener(), SUCCESS);
198     EXPECT_EQ(service->RegisterAccessTokenListener(), SUCCESS);
199     EXPECT_EQ(service->UnRegisterAccessTokenListener(), SUCCESS);
200     EXPECT_EQ(service->UnRegisterAccessTokenListener(), SUCCESS);
201 }
202 
203 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestNotifyFwkReady, TestSize.Level0)
204 {
205     auto service = Common::MakeShared<CoAuthService>();
206     EXPECT_NE(service, nullptr);
207     service->SetIsReady(false);
208     EXPECT_NO_THROW(service->NotifyFwkReady());
209     service->SetIsReady(true);
210     EXPECT_NO_THROW(service->NotifyFwkReady());
211 }
212 
213 HWTEST_F(CoAuthServiceTest, CoAuthServiceTestOnDriverStop, TestSize.Level0)
214 {
215     auto service = Common::MakeShared<CoAuthService>();
216     EXPECT_NE(service, nullptr);
217     service->SetIsReady(true);
218     EXPECT_NO_THROW(service->OnDriverStart());
219     EXPECT_NO_THROW(service->OnDriverStop());
220     EXPECT_NO_THROW(service->OnDriverStop());
221 }
222 } // namespace UserAuth
223 } // namespace UserIam
224 } // namespace OHOS