1 /*
2 * Copyright (C) 2022 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 "co_auth_service.h"
19 #include "iam_ptr.h"
20 #include "mock_executor_callback.h"
21 #include "mock_ipc_common.h"
22 #include "mock_iuser_auth_interface.h"
23 #include "mock_resource_node.h"
24 #include "resource_node_pool.h"
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 using namespace testing;
30 using namespace testing::ext;
31
32 using HdiExecutorRegisterInfo = OHOS::HDI::UserAuth::V1_0::ExecutorRegisterInfo;
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<ExecutorCallbackInterface> testCallback = new MockExecutorCallback();
55 EXPECT_NE(testCallback, nullptr);
56 sptr<MockExecutorCallback> tempCallback = static_cast<MockExecutorCallback *>(testCallback.GetRefPtr());
57 EXPECT_NE(tempCallback, nullptr);
58
59 CoAuthInterface::ExecutorRegisterInfo info = {};
60 info.authType = FINGERPRINT;
61 info.executorRole = SCHEDULER;
62 info.executorSensorHint = 0;
63 info.executorMatcher = 0;
64 info.esl = ESL1;
65 info.publicKey = {'a', 'b', 'c', 'd'};
66
67 auto service = Common::MakeShared<CoAuthService>(1, true);
68 EXPECT_NE(service, nullptr);
69 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
70 EXPECT_NE(mockHdi, nullptr);
71 EXPECT_CALL(*tempCallback, OnMessengerReady(_, _, _)).Times(1);
72 EXPECT_CALL(*mockHdi, AddExecutor(_, _, _, _))
73 .Times(2)
74 .WillOnce(Return(HDF_FAILURE))
75 .WillOnce(
76 [](const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey,
__anon03f378b00102(const HdiExecutorRegisterInfo &info, uint64_t &index, std::vector<uint8_t> &publicKey, std::vector<uint64_t> &templateIds) 77 std::vector<uint64_t> &templateIds) {
78 index = 12345;
79 return HDF_SUCCESS;
80 }
81 );
82 EXPECT_CALL(*mockHdi, DeleteExecutor(_)).Times(1);
83 IpcCommon::AddPermission(ACCESS_AUTH_RESPOOL);
84 uint64_t executorIndex = service->ExecutorRegister(info, testCallback);
85 EXPECT_EQ(executorIndex, 0);
86 executorIndex = service->ExecutorRegister(info, testCallback);
87 EXPECT_NE(executorIndex, 0);
88 EXPECT_EQ(ResourceNodePool::Instance().Delete(executorIndex), true);
89 IpcCommon::DeleteAllPermission();
90 }
91
92 HWTEST_F(CoAuthServiceTest, CoAuthServiceTest003, TestSize.Level0)
93 {
94 auto service = Common::MakeShared<CoAuthService>(1, true);
95 EXPECT_NE(service, nullptr);
96
97 CoAuthInterface::ExecutorRegisterInfo info = {};
98 sptr<ExecutorCallbackInterface> testCallback = nullptr;
99 uint64_t executorIndex = service->ExecutorRegister(info, testCallback);
100 EXPECT_EQ(executorIndex, 0);
101 }
102
103 HWTEST_F(CoAuthServiceTest, CoAuthServiceTest004, TestSize.Level0)
104 {
105 int testFd1 = -1;
106 int testFd2 = 1;
107 std::vector<std::u16string> testArgs;
108
109 auto service = Common::MakeShared<CoAuthService>(1, true);
110 EXPECT_NE(service, nullptr);
111
112 auto node = MockResourceNode::CreateWithExecuteIndex(20);
113 EXPECT_NE(node, nullptr);
114 EXPECT_TRUE(ResourceNodePool::Instance().Insert(node));
115
116 EXPECT_EQ(service->Dump(testFd1, testArgs), INVALID_PARAMETERS);
117 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
118 testArgs.push_back(u"-h");
119 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
120 testArgs.clear();
121 testArgs.push_back(u"-l");
122 EXPECT_EQ(service->Dump(testFd2, testArgs), SUCCESS);
123 testArgs.clear();
124 testArgs.push_back(u"-k");
125 EXPECT_EQ(service->Dump(testFd2, testArgs), GENERAL_ERROR);
126
127 EXPECT_TRUE(ResourceNodePool::Instance().Delete(20));
128 }
129 } // namespace UserAuth
130 } // namespace UserIam
131 } // namespace OHOS