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