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 "fingerprint_auth_hdi.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "mock_isa_command_processor.h"
24
25 #define LOG_TAG "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))
__anon3b3ef3d10102(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor) 90 .WillOnce([](std::shared_ptr<FingerprintAllInOneExecutorHdi> 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 = { .id = getSaCommandId(), .param = {} };
98 commands.push_back(saCommand);
99 IAM_LOGI("ProcessSaCommands %{public}d", saCommand.id);
100 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
101 EXPECT_TRUE(ret == UserAuth::GENERAL_ERROR);
102 SaCommandManager::GetInstance().OnHdiDisconnect(nullptr);
103 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
104 }
105
106 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_003, TestSize.Level0)
107 {
108 IAM_LOGI("begin SaCommandManagerUnitTest_003");
109 auto saCommandProcessor = Common::MakeShared<MockISaCommandProcessor>();
110 ASSERT_TRUE(saCommandProcessor != nullptr);
111 EXPECT_CALL(*saCommandProcessor, ProcessSaCommand(_, _))
112 .Times(Exactly(1))
__anon3b3ef3d10202(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommand &command) 113 .WillOnce([](std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommand &command) {
114 IAM_LOGI("SaCommandManagerUnitTest_003 ProcessSaCommand called");
115 return UserAuth::SUCCESS;
116 });
117 std::vector<SaCommandId> commandIds = { getSaCommandId() };
118 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, saCommandProcessor);
119 std::vector<SaCommand> commands;
120 SaCommand saCommand = { .id = commandIds[0], .param = {} };
121 commands.push_back(saCommand);
122 IAM_LOGI("ProcessSaCommands %{public}d", commandIds[0]);
123 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
124 EXPECT_TRUE(ret == UserAuth::SUCCESS);
125 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
126 }
127
128 HWTEST_F(SaCommandManagerUnitTest, SaCommandManagerUnitTest_004, TestSize.Level0)
129 {
130 IAM_LOGI("begin SaCommandManagerUnitTest_004");
131 auto saCommandProcessor = Common::MakeShared<MockISaCommandProcessor>();
132 ASSERT_TRUE(saCommandProcessor != nullptr);
133 EXPECT_CALL(*saCommandProcessor, ProcessSaCommand(_, _))
134 .Times(Exactly(1))
__anon3b3ef3d10302(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommand &command) 135 .WillOnce([](std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommand &command) {
136 IAM_LOGI("SaCommandManagerUnitTest_004 ProcessSaCommand called");
137 return UserAuth::GENERAL_ERROR;
138 });
139 std::vector<SaCommandId> commandIds = { getSaCommandId() };
140 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, saCommandProcessor);
141 std::vector<SaCommand> commands;
142 SaCommand saCommand = { .id = commandIds[0], .param = {} };
143 commands.push_back(saCommand);
144 IAM_LOGI("ProcessSaCommands %{public}d", commandIds[0]);
145 auto ret = SaCommandManager::GetInstance().ProcessSaCommands(nullptr, commands);
146 EXPECT_TRUE(ret == UserAuth::GENERAL_ERROR);
147 SaCommandManager::GetInstance().UnregisterSaCommandProcessor(commandIds, saCommandProcessor);
148 }
149 } // namespace FingerprintAuth
150 } // namespace UserIam
151 } // namespace OHOS