1 /*
2 * Copyright (c) 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 "gtest/gtest.h"
17
18 #include "sa_command_manager.h"
19
20 #include "iam_logger.h"
21 #include "iam_ptr.h"
22 #include "fingerprint_auth_hdi.h"
23 #include "mock_isa_command_processor.h"
24
25 #define LOG_LABEL UserIam::Common::LABEL_FINGERPRINT_AUTH_SA
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace UserIam {
32 namespace FingerprintAuth {
33 class SaCommandManagerUnitTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39
40 SaCommandId getSaCommandId();
41
42 private:
43 static int32_t saCommandId_;
44 };
45
46 int32_t SaCommandManagerUnitTest::saCommandId_ = 600000;
47
SetUpTestCase()48 void SaCommandManagerUnitTest::SetUpTestCase()
49 {
50 }
51
TearDownTestCase()52 void SaCommandManagerUnitTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void SaCommandManagerUnitTest::SetUp()
57 {
58 }
59
TearDown()60 void SaCommandManagerUnitTest::TearDown()
61 {
62 }
63
getSaCommandId()64 SaCommandId SaCommandManagerUnitTest::getSaCommandId()
65 {
66 int32_t saCommandId = saCommandId_;
67 saCommandId_++;
68 IAM_LOGI("get command %{public}d", saCommandId);
69 return static_cast<SaCommandId>(saCommandId);
70 }
71
72 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_001, TestSize.Level0)
73 {
74 IAM_LOGI("begin SaCommandManagerUnitTest_001");
75 std::vector<SaCommandId> commandIds;
76 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, nullptr);
77 const std::vector<SaCommand> commands;
78 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
79 EXPECT_TRUE(ret == UserAuth::SUCCESS);
80 SaCommandManager::GetInstance().OnHdiDisconnect(nullptr);
81 }
82
83 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_002, TestSize.Level0)
84 {
85 IAM_LOGI("begin SaCommandManagerUnitTest_002");
86 auto saCommandProcessor = Common::MakeShared<MockISaCommandProcessor>();
87 ASSERT_TRUE(saCommandProcessor != nullptr);
88 EXPECT_CALL(*saCommandProcessor, OnHdiDisconnect(_))
89 .Times(Exactly(1))
__anon011c21f90102(std::shared_ptr<FingerprintAuthExecutorHdi> executor) 90 .WillOnce([](std::shared_ptr<FingerprintAuthExecutorHdi> executor) {
91 IAM_LOGI("SaCommandManagerUnitTest_002 OnHdiDisconnect called");
92 return;
93 });
94 std::vector<SaCommandId> commandIds = { getSaCommandId() };
95 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, saCommandProcessor);
96 std::vector<SaCommand> commands;
97 SaCommand saCommand = {
98 .id = getSaCommandId(),
99 .param = {}
100 };
101 commands.push_back(saCommand);
102 IAM_LOGI("ProcessSaCommands %{public}d", saCommand.id);
103 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
104 EXPECT_TRUE(ret == UserAuth::GENERAL_ERROR);
105 SaCommandManager::GetInstance().OnHdiDisconnect(nullptr);
106 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
107 }
108
109 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_003, TestSize.Level0)
110 {
111 IAM_LOGI("begin SaCommandManagerUnitTest_003");
112 auto saCommandProcessor = Common::MakeShared<MockISaCommandProcessor>();
113 ASSERT_TRUE(saCommandProcessor != nullptr);
114 EXPECT_CALL(*saCommandProcessor, ProcessSaCommand(_, _))
115 .Times(Exactly(1))
__anon011c21f90202(std::shared_ptr<FingerprintAuthExecutorHdi> executor, const SaCommand &command) 116 .WillOnce([](std::shared_ptr<FingerprintAuthExecutorHdi> executor, const SaCommand &command) {
117 IAM_LOGI("SaCommandManagerUnitTest_003 ProcessSaCommand called");
118 return UserAuth::SUCCESS;
119 });
120 std::vector<SaCommandId> commandIds = { getSaCommandId() };
121 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, saCommandProcessor);
122 std::vector<SaCommand> commands;
123 SaCommand saCommand = {
124 .id = commandIds[0],
125 .param = {}
126 };
127 commands.push_back(saCommand);
128 IAM_LOGI("ProcessSaCommands %{public}d", commandIds[0]);
129 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
130 EXPECT_TRUE(ret == UserAuth::SUCCESS);
131 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
132 }
133
134 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_004, TestSize.Level0)
135 {
136 IAM_LOGI("begin SaCommandManagerUnitTest_004");
137 auto saCommandProcessor = Common::MakeShared<MockISaCommandProcessor>();
138 ASSERT_TRUE(saCommandProcessor != nullptr);
139 EXPECT_CALL(*saCommandProcessor, ProcessSaCommand(_, _))
140 .Times(Exactly(1))
__anon011c21f90302(std::shared_ptr<FingerprintAuthExecutorHdi> executor, const SaCommand &command) 141 .WillOnce([](std::shared_ptr<FingerprintAuthExecutorHdi> executor, const SaCommand &command) {
142 IAM_LOGI("SaCommandManagerUnitTest_004 ProcessSaCommand called");
143 return UserAuth::GENERAL_ERROR;
144 });
145 std::vector<SaCommandId> commandIds = { getSaCommandId() };
146 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, saCommandProcessor);
147 std::vector<SaCommand> commands;
148 SaCommand saCommand = {
149 .id = commandIds[0],
150 .param = {}
151 };
152 commands.push_back(saCommand);
153 IAM_LOGI("ProcessSaCommands %{public}d", commandIds[0]);
154 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
155 EXPECT_TRUE(ret == UserAuth::GENERAL_ERROR);
156 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
157 }
158 } // namespace FingerprintAuth
159 } // namespace UserIam
160 } // namespace OHOS