1 /*
2 * Copyright (c) 2022-2024 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 "inputer_data_impl.h"
17 #include "inputer_data_impl_test.h"
18
19 #include <openssl/sha.h>
20
21 #include "iam_ptr.h"
22 #include "mock_inputer_set_data.h"
23 #include "scrypt.h"
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace PinAuth {
28 using namespace testing;
29 using namespace testing::ext;
30
SetUpTestCase()31 void InputerDataImplTest::SetUpTestCase()
32 {
33 }
34
TearDownTestCase()35 void InputerDataImplTest::TearDownTestCase()
36 {
37 }
38
SetUp()39 void InputerDataImplTest::SetUp()
40 {
41 }
42
TearDown()43 void InputerDataImplTest::TearDown()
44 {
45 }
46
47 namespace {
GetMockInputerSetData(int32_t testAuthSubType, std::vector<uint8_t> testSetData, int32_t testErrorCode)48 sptr<MockInputerSetData> GetMockInputerSetData(int32_t testAuthSubType,
49 std::vector<uint8_t> testSetData, int32_t testErrorCode)
50 {
51 sptr<MockInputerSetData> mockInputerSetData(new (std::nothrow) MockInputerSetData());
52 if (mockInputerSetData == nullptr) {
53 return nullptr;
54 }
55 return mockInputerSetData;
56 }
57 }
58
HWTEST_F(InputerDataImplTest, CheckPinComplexity001, TestSize.Level0)59 HWTEST_F(InputerDataImplTest, CheckPinComplexity001, TestSize.Level0)
60 {
61 InputerGetDataParam param = {};
62 param.algoVersion = PIN_ALGO_VERSION_V2;
63 param.algoParameter = {1, 2, 3, 4, 5};
64 param.mode = GET_DATA_MODE_NONE;
65 constexpr int32_t testAuthSubType = 10000;
66 #define CUSTOMIZATION_ENTERPRISE_DEVICE_MANAGEMENT_ENABLE
67 InputerDataImpl inputerDataImpl(param);
68 std::vector<uint8_t> testSalt = {1, 2, 3, 4, 5, 6};
69 int32_t result = inputerDataImpl.CheckPinComplexity(testAuthSubType, testSalt);
70 EXPECT_EQ(result, 0);
71 }
72
HWTEST_F(InputerDataImplTest, CheckPinComplexity002, TestSize.Level0)73 HWTEST_F(InputerDataImplTest, CheckPinComplexity002, TestSize.Level0)
74 {
75 constexpr int32_t testAuthSubType = 10000;
76 InputerGetDataParam param = {};
77 param.algoVersion = PIN_ALGO_VERSION_V2;
78 param.algoParameter = {1, 2, 3, 4, 5};
79 param.mode = GET_DATA_MODE_NONE;
80 #define CUSTOMIZATION_ENTERPRISE_DEVICE_MANAGEMENT_ENABLE
81 InputerDataImpl inputerDataImpl(param);
82 std::vector<uint8_t> testSalt = {1, 2, 3, 4, 5, 6};
83 int32_t result = inputerDataImpl.CheckPinComplexity(testAuthSubType, testSalt);
84 EXPECT_EQ(result, 0);
85 }
86
HWTEST_F(InputerDataImplTest, OnSetDataInner001, TestSize.Level0)87 HWTEST_F(InputerDataImplTest, OnSetDataInner001, TestSize.Level0)
88 {
89 constexpr int32_t testAuthSubType = 10000;
90 InputerGetDataParam param = {};
91 param.algoVersion = PIN_ALGO_VERSION_V2;
92 param.algoParameter = {1, 2, 3, 4, 5};
93 param.mode = GET_DATA_MODE_NONE;
94 std::vector<uint8_t> testSetData;
95 constexpr int32_t testErrorCode = 14;
96 InputerDataImpl inputerDataImpl(param);
97 EXPECT_NO_THROW(inputerDataImpl.OnSetDataInner(testAuthSubType, testSetData, testErrorCode));
98 }
99
HWTEST_F(InputerDataImplTest, GetRecoveryKeyDataTest001, TestSize.Level0)100 HWTEST_F(InputerDataImplTest, GetRecoveryKeyDataTest001, TestSize.Level0)
101 {
102 constexpr int32_t testAuthSubType = 10000;
103 std::vector<uint8_t> testData = {6, 7};
104 std::vector<uint8_t> testSetData;
105 InputerGetDataParam param = {};
106 param.algoVersion = RECOVERY_KEY_ALGO_VERSION_V0;
107 param.algoParameter = {1, 2, 3, 4, 5};
108 param.mode = GET_DATA_MODE_NONE;
109 int32_t testErrorCode = 1;
110 param.inputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
111 ASSERT_NE(param.inputerSetData, nullptr);
112
113 InputerDataImpl inputerDataImpl(param);
114 EXPECT_NO_THROW(inputerDataImpl.GetRecoveryKeyData(testData, testSetData, testErrorCode));
115
116 param.algoVersion = PIN_ALGO_VERSION_V2;
117 InputerDataImpl inputerDataImpl1(param);
118 EXPECT_NO_THROW(inputerDataImpl1.GetRecoveryKeyData(testData, testSetData, testErrorCode));
119 }
120
121
HWTEST_F(InputerDataImplTest, GetPinDataTest001, TestSize.Level0)122 HWTEST_F(InputerDataImplTest, GetPinDataTest001, TestSize.Level0)
123 {
124 InputerGetDataParam param = {};
125 param.mode = GET_DATA_MODE_NONE;
126 param.algoVersion = PIN_ALGO_VERSION_V2;
127 param.authSubType = 10000;
128 param.algoParameter = {1, 2, 3, 4, 5};
129
130 std::vector<uint8_t> testData = {6, 7};
131 std::vector<uint8_t> testSetData;
132 int32_t testErrorCode = 14;
133
134 auto mockInputerSetData = GetMockInputerSetData(param.authSubType, testSetData, testErrorCode);
135 ASSERT_NE(mockInputerSetData, nullptr);
136
137 InputerDataImpl inputerDataImpl(param);
138 EXPECT_NO_THROW(inputerDataImpl.GetPinData(param.authSubType, testData, testSetData, testErrorCode));
139 }
140
HWTEST_F(InputerDataImplTest, GetPinDataTest002, TestSize.Level0)141 HWTEST_F(InputerDataImplTest, GetPinDataTest002, TestSize.Level0)
142 {
143 constexpr int32_t testAuthSubType = 10000;
144 InputerGetDataParam param = {};
145 param.algoVersion = PIN_ALGO_VERSION_V2;
146 param.algoParameter = {1, 2, 3, 4, 5};
147 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
148 std::vector<uint8_t> testData = {1, 2, 3, 4, 6};
149 std::vector<uint8_t> testSetData;
150 int32_t testErrorCode = 14;
151
152 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
153 ASSERT_NE(mockInputerSetData, nullptr);
154
155 InputerDataImpl inputerDataImpl(param);
156 EXPECT_NO_THROW(inputerDataImpl.GetPinData(testAuthSubType, testData, testSetData, testErrorCode));
157 }
158
HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest001, TestSize.Level0)159 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest001, TestSize.Level0)
160 {
161 constexpr int32_t testAuthSubType = 10000;
162 InputerGetDataParam param = {};
163 param.algoVersion = PIN_ALGO_VERSION_V0;
164 param.algoParameter = {1, 2, 3, 4, 5};
165 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
166 std::vector<uint8_t> testData;
167 std::vector<uint8_t> testSetData;
168 int32_t testErrorCode = 14;
169
170 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
171 ASSERT_NE(mockInputerSetData, nullptr);
172
173 InputerDataImpl inputerDataImpl(param);
174 inputerDataImpl.OnSetData(testAuthSubType, testData);
175 }
176
HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest002, TestSize.Level0)177 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest002, TestSize.Level0)
178 {
179 constexpr int32_t testAuthSubType = 10000;
180 InputerGetDataParam param = {};
181 param.algoVersion = PIN_ALGO_VERSION_V0;
182 param.algoParameter = {2, 3, 4, 5, 6, 7};
183 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
184 std::vector<uint8_t> testData = {1, 2, 3, 4, 5, 6};
185 constexpr int32_t testErrorCode = 0;
186
187 std::vector<uint8_t> testSalt = {2, 3, 4, 5, 6, 7};
188 Scrypt scrypt(testSalt);
189 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
190
191 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
192 ASSERT_NE(mockInputerSetData, nullptr);
193
194 InputerDataImpl inputerDataImpl(param);
195 inputerDataImpl.OnSetData(testAuthSubType, testData);
196 }
197
HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest003, TestSize.Level0)198 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest003, TestSize.Level0)
199 {
200 constexpr int32_t testAuthSubType = 10000;
201 InputerGetDataParam param = {};
202 param.algoVersion = PIN_ALGO_VERSION_V1;
203 param.algoParameter = {3, 4, 5, 6, 7, 8};
204 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
205 std::vector<uint8_t> testData = {2, 3, 4, 5, 6, 7};
206 constexpr int32_t testErrorCode = 0;
207
208 std::vector<uint8_t> testSalt = {2, 3, 4, 5, 6, 7};
209 Scrypt scrypt(testSalt);
210 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
211
212 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
213 ASSERT_NE(mockInputerSetData, nullptr);
214
215 InputerDataImpl inputerDataImpl(param);
216 inputerDataImpl.OnSetData(testAuthSubType, testData);
217 }
218
HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest004, TestSize.Level0)219 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest004, TestSize.Level0)
220 {
221 constexpr int32_t testAuthSubType = 10000;
222 InputerGetDataParam param = {};
223 param.algoVersion = PIN_ALGO_VERSION_V2;
224 param.algoParameter = {4, 5, 6, 7, 8, 9};
225 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
226 std::vector<uint8_t> testData = {3, 4, 5, 6, 7, 8};
227 constexpr int32_t testErrorCode = 0;
228
229 std::vector<uint8_t> testSalt = {3, 4, 5, 6, 7, 8};
230 Scrypt scrypt(testSalt);
231 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
232
233 uint8_t sha256Result[SHA256_DIGEST_LENGTH] = {};
234 EXPECT_EQ(SHA256(testData.data(), testData.size(), sha256Result), sha256Result);
235 testSetData.insert(testSetData.end(), sha256Result, sha256Result + SHA256_DIGEST_LENGTH);
236
237 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
238 ASSERT_NE(mockInputerSetData, nullptr);
239
240 InputerDataImpl inputerDataImpl(param);
241 inputerDataImpl.OnSetData(testAuthSubType, testData);
242 }
243
HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest001, TestSize.Level0)244 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest001, TestSize.Level0)
245 {
246 constexpr int32_t testAuthSubType = 10000;
247 InputerGetDataParam param = {};
248 param.algoVersion = PIN_ALGO_VERSION_V0;
249 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
250 std::vector<uint8_t> testData;
251 std::vector<uint8_t> testSetData;
252 constexpr int32_t testErrorCode = 14;
253
254 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
255 ASSERT_NE(mockInputerSetData, nullptr);
256
257 InputerDataImpl inputerDataImpl(param);
258 inputerDataImpl.OnSetData(testAuthSubType, testData);
259 }
260
HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest002, TestSize.Level0)261 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest002, TestSize.Level0)
262 {
263 constexpr int32_t testAuthSubType = 10000;
264 InputerGetDataParam param = {};
265 param.algoVersion = PIN_ALGO_VERSION_V0;
266 param.algoParameter = {5, 6, 7, 8, 9, 10};
267 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
268 std::vector<uint8_t> testData = {5, 6, 7, 8, 9, 10};
269 constexpr int32_t testErrorCode = 0;
270
271 std::vector<uint8_t> testSalt = {5, 6, 7, 8, 9, 10};
272 Scrypt scrypt(testSalt);
273 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
274
275 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
276 ASSERT_NE(mockInputerSetData, nullptr);
277
278 InputerDataImpl inputerDataImpl(param);
279 inputerDataImpl.OnSetData(testAuthSubType, testData);
280 }
281
HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest003, TestSize.Level0)282 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest003, TestSize.Level0)
283 {
284 constexpr int32_t testAuthSubType = 10000;
285 InputerGetDataParam param = {};
286 param.algoVersion = PIN_ALGO_VERSION_V1;
287 param.algoParameter = {6, 7, 8, 9, 10, 11};
288 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
289 std::vector<uint8_t> testData = {6, 7, 8, 9, 10, 11};
290 constexpr int32_t testErrorCode = 0;
291
292 std::vector<uint8_t> testSalt = {6, 7, 8, 9, 10, 11};
293 Scrypt scrypt(testSalt);
294 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
295
296 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
297 ASSERT_NE(mockInputerSetData, nullptr);
298 InputerDataImpl inputerDataImpl(param);
299 inputerDataImpl.OnSetData(testAuthSubType, testData);
300 }
301 } // namespace PinAuth
302 } // namespace UserIam
303 } // namespace OHOS
304