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,
__anon0b382b270102(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,
__anon0b382b270202(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 EXPECT_EQ(ResourceNodePool::Instance().Delete(executorIndex), true);
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, CoAuthServiceTest003, TestSize.Level0)
111 {
112 int testFd1 = -1;
113 int testFd2 = 1;
114 std::vector<std::u16string> testArgs;
115
116 auto service = Common::MakeShared<CoAuthService>();
117 EXPECT_NE(service, nullptr);
118
119 auto node = MockResourceNode::CreateWithExecuteIndex(20);
120 EXPECT_NE(node, nullptr);
121 EXPECT_TRUE(ResourceNodePool::Instance().Insert(node));
122
123 EXPECT_EQ(service->Dump(testFd1, testArgs), INVALID_PARAMETERS);
124 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
125 testArgs.push_back(u"-h");
126 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
127 testArgs.clear();
128 testArgs.push_back(u"-l");
129 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
130 testArgs.clear();
131 testArgs.push_back(u"-k");
132 EXPECT_EQ(service->Dump(testFd2, testArgs), GENERAL_ERROR);
133
134 EXPECT_TRUE(ResourceNodePool::Instance().Delete(20));
135 }
136 } // namespace UserAuth
137 } // namespace UserIam
138 } // namespace OHOS