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
59 HWTEST_F(InputerDataImplTest, CheckPinComplexity001, TestSize.Level1)
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
73 HWTEST_F(InputerDataImplTest, CheckPinComplexity002, TestSize.Level1)
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
87 HWTEST_F(InputerDataImplTest, OnSetDataInner001, TestSize.Level1)
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 uint32_t pinLength = 6;
96 constexpr int32_t testErrorCode = 14;
97 InputerDataImpl inputerDataImpl(param);
98 EXPECT_NO_THROW(inputerDataImpl.OnSetDataInner(testAuthSubType, testSetData, pinLength, testErrorCode));
99 }
100
101 HWTEST_F(InputerDataImplTest, GetRecoveryKeyDataTest001, TestSize.Level1)
102 {
103 constexpr int32_t testAuthSubType = 10000;
104 std::vector<uint8_t> testData = {6, 7};
105 std::vector<uint8_t> testSetData;
106 InputerGetDataParam param = {};
107 param.algoVersion = RECOVERY_KEY_ALGO_VERSION_V0;
108 param.algoParameter = {1, 2, 3, 4, 5};
109 param.mode = GET_DATA_MODE_NONE;
110 int32_t testErrorCode = 1;
111 param.inputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
112 ASSERT_NE(param.inputerSetData, nullptr);
113
114 InputerDataImpl inputerDataImpl(param);
115 EXPECT_NO_THROW(inputerDataImpl.GetRecoveryKeyData(testData, testSetData, testErrorCode));
116
117 param.algoVersion = PIN_ALGO_VERSION_V2;
118 InputerDataImpl inputerDataImpl1(param);
119 EXPECT_NO_THROW(inputerDataImpl1.GetRecoveryKeyData(testData, testSetData, testErrorCode));
120 }
121
122
123 HWTEST_F(InputerDataImplTest, GetPinDataTest001, TestSize.Level1)
124 {
125 InputerGetDataParam param = {};
126 param.mode = GET_DATA_MODE_NONE;
127 param.algoVersion = PIN_ALGO_VERSION_V2;
128 param.authSubType = 10000;
129 param.algoParameter = {1, 2, 3, 4, 5};
130
131 std::vector<uint8_t> testData = {6, 7};
132 std::vector<uint8_t> testSetData;
133 int32_t testErrorCode = 14;
134
135 auto mockInputerSetData = GetMockInputerSetData(param.authSubType, testSetData, testErrorCode);
136 ASSERT_NE(mockInputerSetData, nullptr);
137
138 InputerDataImpl inputerDataImpl(param);
139 EXPECT_NO_THROW(inputerDataImpl.GetPinData(param.authSubType, testData, testSetData, testErrorCode));
140 }
141
142 HWTEST_F(InputerDataImplTest, GetPinDataTest002, TestSize.Level1)
143 {
144 constexpr int32_t testAuthSubType = 10000;
145 InputerGetDataParam param = {};
146 param.algoVersion = PIN_ALGO_VERSION_V2;
147 param.algoParameter = {1, 2, 3, 4, 5};
148 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
149 std::vector<uint8_t> testData = {1, 2, 3, 4, 6};
150 std::vector<uint8_t> testSetData;
151 int32_t testErrorCode = 14;
152
153 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
154 ASSERT_NE(mockInputerSetData, nullptr);
155
156 InputerDataImpl inputerDataImpl(param);
157 EXPECT_NO_THROW(inputerDataImpl.GetPinData(testAuthSubType, testData, testSetData, testErrorCode));
158 }
159
160 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest001, TestSize.Level1)
161 {
162 constexpr int32_t testAuthSubType = 10000;
163 InputerGetDataParam param = {};
164 param.algoVersion = PIN_ALGO_VERSION_V0;
165 param.algoParameter = {1, 2, 3, 4, 5};
166 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
167 std::vector<uint8_t> testData;
168 std::vector<uint8_t> testSetData;
169 int32_t testErrorCode = 14;
170
171 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
172 ASSERT_NE(mockInputerSetData, nullptr);
173
174 InputerDataImpl inputerDataImpl(param);
175 inputerDataImpl.OnSetData(testAuthSubType, testData);
176 }
177
178 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest002, TestSize.Level1)
179 {
180 constexpr int32_t testAuthSubType = 10000;
181 InputerGetDataParam param = {};
182 param.algoVersion = PIN_ALGO_VERSION_V0;
183 param.algoParameter = {2, 3, 4, 5, 6, 7};
184 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
185 std::vector<uint8_t> testData = {1, 2, 3, 4, 5, 6};
186 constexpr int32_t testErrorCode = 0;
187
188 std::vector<uint8_t> testSalt = {2, 3, 4, 5, 6, 7};
189 Scrypt scrypt(testSalt);
190 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
191
192 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
193 ASSERT_NE(mockInputerSetData, nullptr);
194
195 InputerDataImpl inputerDataImpl(param);
196 inputerDataImpl.OnSetData(testAuthSubType, testData);
197 }
198
199 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest003, TestSize.Level1)
200 {
201 constexpr int32_t testAuthSubType = 10000;
202 InputerGetDataParam param = {};
203 param.algoVersion = PIN_ALGO_VERSION_V1;
204 param.algoParameter = {3, 4, 5, 6, 7, 8};
205 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
206 std::vector<uint8_t> testData = {2, 3, 4, 5, 6, 7};
207 constexpr int32_t testErrorCode = 0;
208
209 std::vector<uint8_t> testSalt = {2, 3, 4, 5, 6, 7};
210 Scrypt scrypt(testSalt);
211 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
212
213 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
214 ASSERT_NE(mockInputerSetData, nullptr);
215
216 InputerDataImpl inputerDataImpl(param);
217 inputerDataImpl.OnSetData(testAuthSubType, testData);
218 }
219
220 HWTEST_F(InputerDataImplTest, InputerDataImplEnrollTest004, TestSize.Level0)
221 {
222 constexpr int32_t testAuthSubType = 10000;
223 InputerGetDataParam param = {};
224 param.algoVersion = PIN_ALGO_VERSION_V2;
225 param.algoParameter = {4, 5, 6, 7, 8, 9};
226 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL;
227 std::vector<uint8_t> testData = {3, 4, 5, 6, 7, 8};
228 constexpr int32_t testErrorCode = 0;
229
230 std::vector<uint8_t> testSalt = {3, 4, 5, 6, 7, 8};
231 Scrypt scrypt(testSalt);
232 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
233
234 uint8_t sha256Result[SHA256_DIGEST_LENGTH] = {};
235 EXPECT_EQ(SHA256(testData.data(), testData.size(), sha256Result), sha256Result);
236 testSetData.insert(testSetData.end(), sha256Result, sha256Result + SHA256_DIGEST_LENGTH);
237
238 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
239 ASSERT_NE(mockInputerSetData, nullptr);
240
241 InputerDataImpl inputerDataImpl(param);
242 inputerDataImpl.OnSetData(testAuthSubType, testData);
243 }
244
245 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest001, TestSize.Level0)
246 {
247 constexpr int32_t testAuthSubType = 10000;
248 InputerGetDataParam param = {};
249 param.algoVersion = PIN_ALGO_VERSION_V0;
250 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
251 std::vector<uint8_t> testData;
252 std::vector<uint8_t> testSetData;
253 constexpr int32_t testErrorCode = 14;
254
255 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
256 ASSERT_NE(mockInputerSetData, nullptr);
257
258 InputerDataImpl inputerDataImpl(param);
259 inputerDataImpl.OnSetData(testAuthSubType, testData);
260 }
261
262 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest002, TestSize.Level0)
263 {
264 constexpr int32_t testAuthSubType = 10000;
265 InputerGetDataParam param = {};
266 param.algoVersion = PIN_ALGO_VERSION_V0;
267 param.algoParameter = {5, 6, 7, 8, 9, 10};
268 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
269 std::vector<uint8_t> testData = {5, 6, 7, 8, 9, 10};
270 constexpr int32_t testErrorCode = 0;
271
272 std::vector<uint8_t> testSalt = {5, 6, 7, 8, 9, 10};
273 Scrypt scrypt(testSalt);
274 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
275
276 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
277 ASSERT_NE(mockInputerSetData, nullptr);
278
279 InputerDataImpl inputerDataImpl(param);
280 inputerDataImpl.OnSetData(testAuthSubType, testData);
281 }
282
283 HWTEST_F(InputerDataImplTest, InputerDataImplAuthTest003, TestSize.Level0)
284 {
285 constexpr int32_t testAuthSubType = 10000;
286 InputerGetDataParam param = {};
287 param.algoVersion = PIN_ALGO_VERSION_V1;
288 param.algoParameter = {6, 7, 8, 9, 10, 11};
289 param.mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH;
290 std::vector<uint8_t> testData = {6, 7, 8, 9, 10, 11};
291 constexpr int32_t testErrorCode = 0;
292
293 std::vector<uint8_t> testSalt = {6, 7, 8, 9, 10, 11};
294 Scrypt scrypt(testSalt);
295 std::vector<uint8_t> testSetData = scrypt.GetScrypt(testData, param.algoVersion);
296
297 auto mockInputerSetData = GetMockInputerSetData(testAuthSubType, testSetData, testErrorCode);
298 ASSERT_NE(mockInputerSetData, nullptr);
299 InputerDataImpl inputerDataImpl(param);
300 inputerDataImpl.OnSetData(testAuthSubType, testData);
301 }
302 } // namespace PinAuth
303 } // namespace UserIam
304 } // namespace OHOS
305