1 /*
2 * Copyright (c) 2021-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 "gtest/gtest.h"
17
18 #include "enroll_command.h"
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_mem.h"
22 #include "iam_ptr.h"
23
24 #include "executor.h"
25 #include "mock_iexecutor_messenger.h"
26
27 #define LOG_TAG "USER_AUTH_EXECUTOR"
28
29 using namespace std;
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace OHOS::UserIam;
33 using namespace OHOS::UserIam::Common;
34 using namespace OHOS::UserIam::UserAuth;
35
36 namespace OHOS {
37 namespace UserIam {
38 namespace UserAuth {
39 class EnrollCommandUnitTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase()47 void EnrollCommandUnitTest::SetUpTestCase()
48 {
49 }
50
TearDownTestCase()51 void EnrollCommandUnitTest::TearDownTestCase()
52 {
53 }
54
SetUp()55 void EnrollCommandUnitTest::SetUp()
56 {
57 }
58
TearDown()59 void EnrollCommandUnitTest::TearDown()
60 {
61 }
62
63 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnResultTest_001, TestSize.Level0)
64 {
65 static const uint64_t testScheduleId = 123;
66 static const ResultCode testResultCode = static_cast<ResultCode>(456);
67 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
68
69 auto messenger = MakeShared<MockIExecutorMessenger>();
70 ASSERT_NE(messenger, nullptr);
71 EXPECT_CALL(*messenger, Finish(_, _, _))
72 .Times(Exactly(1))
__anon10925dc40102(uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) 73 .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) {
74 EXPECT_EQ(scheduleId, testScheduleId);
75 EXPECT_EQ(resultCode, testResultCode);
76 uint32_t attrResultCode;
77 EXPECT_EQ(finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode), true);
78 EXPECT_EQ(attrResultCode, static_cast<uint32_t>(testResultCode));
79 std::vector<uint8_t> extraInfo;
80 EXPECT_EQ(finalResult.GetUint8ArrayValue(Attributes::ATTR_RESULT, extraInfo), true);
81 EXPECT_EQ(extraInfo, testExtraInfo);
82 return USERAUTH_SUCCESS;
83 });
84 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
85 ASSERT_NE(executor, nullptr);
86 Attributes attr;
87 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
88 ASSERT_NE(command, nullptr);
89 command->OnResult(testResultCode, testExtraInfo);
90 }
91
92 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnResultTest_002, TestSize.Level0)
93 {
94 static const uint64_t testScheduleId = 123;
95 static const ResultCode testResultCode = static_cast<ResultCode>(456);
96 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
97
98 auto messenger = MakeShared<MockIExecutorMessenger>();
99 ASSERT_NE(messenger, nullptr);
100 EXPECT_CALL(*messenger, Finish(_, _, _))
101 .Times(Exactly(1))
__anon10925dc40202(uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) 102 .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) {
103 // return error
104 return USERAUTH_ERROR;
105 });
106 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
107 ASSERT_NE(executor, nullptr);
108 Attributes attr;
109 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
110 ASSERT_NE(command, nullptr);
111 command->OnResult(testResultCode, testExtraInfo);
112 }
113
114 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnResultTest_003, TestSize.Level0)
115 {
116 static const uint64_t testScheduleId = 123;
117 static const ResultCode testResultCode = static_cast<ResultCode>(456);
118 static const std::vector<uint8_t> testExtraInfo = {};
119
120 auto messenger = MakeShared<MockIExecutorMessenger>();
121 ASSERT_NE(messenger, nullptr);
122 EXPECT_CALL(*messenger, Finish(_, _, _))
123 .Times(Exactly(1))
__anon10925dc40302(uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) 124 .WillOnce([](uint64_t scheduleId, int32_t resultCode, const Attributes &finalResult) {
125 EXPECT_EQ(scheduleId, testScheduleId);
126 EXPECT_EQ(resultCode, testResultCode);
127 uint32_t attrResultCode;
128 EXPECT_EQ(finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode), true);
129 EXPECT_EQ(attrResultCode, static_cast<uint32_t>(testResultCode));
130 std::vector<uint8_t> extraInfo;
131 EXPECT_EQ(finalResult.GetUint8ArrayValue(Attributes::ATTR_RESULT, extraInfo), true);
132 EXPECT_EQ(extraInfo, testExtraInfo);
133 return USERAUTH_SUCCESS;
134 });
135 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
136 ASSERT_NE(executor, nullptr);
137 Attributes attr;
138 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
139 ASSERT_NE(command, nullptr);
140 command->OnResult(testResultCode);
141 }
142
143 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnResultTest_004, TestSize.Level0)
144 {
145 static const uint64_t testScheduleId = 123;
146 static const ResultCode testResultCode = static_cast<ResultCode>(456);
147 static const std::vector<uint8_t> testExtraInfo = {};
148
149 auto messenger = MakeShared<MockIExecutorMessenger>();
150 ASSERT_NE(messenger, nullptr);
151 EXPECT_CALL(*messenger, Finish(_, _, _)).Times(Exactly(1));
152 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
153 ASSERT_NE(executor, nullptr);
154 Attributes attr;
155 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
156 ASSERT_NE(command, nullptr);
157 command->OnResult(testResultCode);
158 command->OnResult(testResultCode);
159 command->OnResult(testResultCode);
160 }
161
162 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnAcquireInfoTest_001, TestSize.Level0)
163 {
164 static const uint64_t testScheduleId = 123;
165 static const uint64_t testAcquire = 456;
166 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
167
168 auto messenger = MakeShared<MockIExecutorMessenger>();
169 ASSERT_NE(messenger, nullptr);
170 EXPECT_CALL(*messenger, SendData(_, _, _))
171 .Times(Exactly(1))
__anon10925dc40402(uint64_t scheduleId, ExecutorRole dstRole, const std::shared_ptr<AuthMessage> &msg) 172 .WillOnce([](uint64_t scheduleId, ExecutorRole dstRole, const std::shared_ptr<AuthMessage> &msg) {
173 EXPECT_EQ(scheduleId, testScheduleId);
174 EXPECT_EQ(dstRole, SCHEDULER);
175 EXPECT_NE(msg, nullptr);
176 std::vector<uint8_t> extraInfo;
177 return USERAUTH_SUCCESS;
178 });
179 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
180 ASSERT_NE(executor, nullptr);
181 Attributes attr;
182 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
183 ASSERT_NE(command, nullptr);
184 command->OnAcquireInfo(testAcquire, testExtraInfo);
185 }
186
187 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnAcquireInfoTest_002, TestSize.Level0)
188 {
189 static const uint64_t testScheduleId = 123;
190 static const uint64_t testAcquire = 456;
191 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
192
193 auto messenger = MakeShared<MockIExecutorMessenger>();
194 ASSERT_NE(messenger, nullptr);
195 EXPECT_CALL(*messenger, SendData(_, _, _))
196 .Times(Exactly(1))
197 .WillOnce([](uint64_t scheduleId, ExecutorRole dstRole,
__anon10925dc40502(uint64_t scheduleId, ExecutorRole dstRole, const std::shared_ptr<AuthMessage> &msg) 198 const std::shared_ptr<AuthMessage> &msg) { return USERAUTH_ERROR; });
199 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
200 ASSERT_NE(executor, nullptr);
201 Attributes attr;
202 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
203 ASSERT_NE(command, nullptr);
204 command->OnAcquireInfo(testAcquire, testExtraInfo);
205 }
206
207 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_OnAcquireInfoTest_003, TestSize.Level0)
208 {
209 static const uint64_t testScheduleId = 123;
210 static const uint64_t testAcquire = 456;
211 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
212
213 auto messenger = MakeShared<MockIExecutorMessenger>();
214 ASSERT_NE(messenger, nullptr);
215 EXPECT_CALL(*messenger, SendData(_, _, _))
216 .Times(Exactly(3))
__anon10925dc40602(uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) 217 .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) {
218 return USERAUTH_SUCCESS;
219 })
__anon10925dc40702(uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) 220 .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) {
221 return USERAUTH_ERROR;
222 })
__anon10925dc40802(uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) 223 .WillOnce([](uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) {
224 return USERAUTH_SUCCESS;
225 });
226 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
227 ASSERT_NE(executor, nullptr);
228 Attributes attr;
229 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
230 ASSERT_NE(command, nullptr);
231 command->OnAcquireInfo(testAcquire, testExtraInfo);
232 command->OnAcquireInfo(testAcquire, testExtraInfo);
233 command->OnAcquireInfo(testAcquire, testExtraInfo);
234 }
235
236 HWTEST_F(EnrollCommandUnitTest, EnrollCommand_MixTest_003, TestSize.Level0)
237 {
238 static const uint64_t testScheduleId = 123;
239 static const uint64_t testAcquire = 456;
240 static const ResultCode testResultCode = static_cast<ResultCode>(456);
241 static const std::vector<uint8_t> testExtraInfo = {7, 8, 9};
242
243 auto messenger = MakeShared<MockIExecutorMessenger>();
244 ASSERT_NE(messenger, nullptr);
245 EXPECT_CALL(*messenger, Finish(_, _, _)).Times(Exactly(1));
246 EXPECT_CALL(*messenger, SendData(_, _, _))
247 .Times(Exactly(3))
248 .WillRepeatedly([](uint64_t scheduleId, int32_t dstType,
__anon10925dc40902(uint64_t scheduleId, int32_t dstType, std::shared_ptr<AuthMessage> msg) 249 std::shared_ptr<AuthMessage> msg) { return USERAUTH_SUCCESS; });
250 auto executor = Common::MakeShared<Executor>(nullptr, nullptr, 3);
251 ASSERT_NE(executor, nullptr);
252 Attributes attr;
253 auto command = Common::MakeShared<EnrollCommand>(executor, testScheduleId, attr, messenger);
254 ASSERT_NE(command, nullptr);
255 command->OnAcquireInfo(testAcquire, testExtraInfo);
256 command->OnAcquireInfo(testAcquire, testExtraInfo);
257 command->OnAcquireInfo(testAcquire, testExtraInfo);
258 command->OnResult(testResultCode);
259 command->OnAcquireInfo(testAcquire, testExtraInfo);
260 command->OnResult(testResultCode);
261 command->OnAcquireInfo(testAcquire, testExtraInfo);
262 command->OnAcquireInfo(testAcquire, testExtraInfo);
263 }
264 } // namespace UserAuth
265 } // namespace UserIam
266 } // namespace OHOS
267