1 /*
2 * Copyright (C) 2022-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 "auth_widget_helper.h"
17
18 #include <future>
19
20 #include "iam_common_defines.h"
21 #include "iam_logger.h"
22 #include "mock_iuser_auth_interface.h"
23 #include "mock_schedule_node.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30
31 class AuthWidgetHelperTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34
35 static void TearDownTestCase();
36
37 void SetUp() override;
38
39 void TearDown() override;
40 };
41
SetUpTestCase()42 void AuthWidgetHelperTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void AuthWidgetHelperTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void AuthWidgetHelperTest::SetUp()
51 {
52 }
53
TearDown()54 void AuthWidgetHelperTest::TearDown()
55 {
56 }
57
58 HWTEST_F(AuthWidgetHelperTest, AuthWidgetHelperTestInitWidgetContextParam001, TestSize.Level0)
59 {
60 AuthParamInner authParam;
61 authParam.authTypes.push_back(FACE);
62 authParam.authTypes.push_back(ALL);
63 authParam.authTypes.push_back(PIN);
64 authParam.authTypes.push_back(FINGERPRINT);
65 authParam.authTrustLevel = ATL2;
66 WidgetParam widgetParam;
67 widgetParam.title = "使用密码验证";
68 widgetParam.navigationButtonText = "确定";
69 ContextFactory::AuthWidgetContextPara para;
70 para.userId = 1;
71 std::vector<AuthType> validType;
72 EXPECT_TRUE(AuthWidgetHelper::InitWidgetContextParam(authParam, validType, widgetParam, para));
73 }
74
75 HWTEST_F(AuthWidgetHelperTest, AuthWidgetHelperTestInitWidgetContextParam002, TestSize.Level0)
76 {
77 AuthParamInner authParam;
78 authParam.authTypes.push_back(FACE);
79 authParam.authTypes.push_back(ALL);
80 authParam.authTypes.push_back(PIN);
81 authParam.authTypes.push_back(FINGERPRINT);
82 authParam.authTrustLevel = ATL2;
83 WidgetParam widgetParam;
84 widgetParam.title = "使用密码验证";
85 widgetParam.navigationButtonText = "确定";
86 ContextFactory::AuthWidgetContextPara para;
87 para.userId = 1;
88 std::vector<AuthType> validType = authParam.authTypes;
89 EXPECT_FALSE(AuthWidgetHelper::InitWidgetContextParam(authParam, validType, widgetParam, para));
90 }
91
92 HWTEST_F(AuthWidgetHelperTest, AuthWidgetHelperTestCheckValidSolution, TestSize.Level0)
93 {
94 int32_t userId = 1;
95 std::vector<AuthType> authTypeList;
96 authTypeList.push_back(FACE);
97 authTypeList.push_back(ALL);
98 authTypeList.push_back(PIN);
99 authTypeList.push_back(FINGERPRINT);
100 AuthTrustLevel atl = ATL3;
101 std::vector<AuthType> validTypeList;
102 EXPECT_FALSE(AuthWidgetHelper::CheckValidSolution(userId, authTypeList, atl, validTypeList));
103 }
104
105 HWTEST_F(AuthWidgetHelperTest, AuthWidgetHelperTestCheckReuseUnlockResult001, TestSize.Level0)
106 {
107 ContextFactory::AuthWidgetContextPara para;
108 para.userId = 1;
109 Attributes extraInfo;
110 AuthParamInner authParam;
111 authParam.reuseUnlockResult.isReuse = false;
112 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), INVALID_PARAMETERS);
113
114 authParam.reuseUnlockResult.isReuse = true;
115 authParam.reuseUnlockResult.reuseDuration = 0;
116 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), INVALID_PARAMETERS);
117
118 authParam.reuseUnlockResult.reuseDuration = 6 * 60 * 1000;
119 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), INVALID_PARAMETERS);
120 }
121
122 HWTEST_F(AuthWidgetHelperTest, AuthWidgetHelperTestCheckReuseUnlockResult002, TestSize.Level0)
123 {
124 ContextFactory::AuthWidgetContextPara para;
125 para.userId = 1;
126 Attributes extraInfo;
127 AuthParamInner authParam;
128 authParam.reuseUnlockResult.isReuse = true;
129 authParam.reuseUnlockResult.reuseDuration = 5 * 60 * 1000;
130 authParam.reuseUnlockResult.reuseMode = AUTH_TYPE_RELEVANT;
131
132 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
133 EXPECT_NE(mockHdi, nullptr);
134 EXPECT_CALL(*mockHdi, CheckReuseUnlockResult(_, _)).Times(3);
135 ON_CALL(*mockHdi, CheckReuseUnlockResult)
136 .WillByDefault(Return(HDF_FAILURE));
137 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), HDF_FAILURE);
138 ON_CALL(*mockHdi, CheckReuseUnlockResult)
139 .WillByDefault(
__anon1ea454880102(const HdiReuseUnlockParam &info, HdiReuseUnlockInfo &reuseInfo) 140 [](const HdiReuseUnlockParam &info, HdiReuseUnlockInfo &reuseInfo) {
141 std::vector<uint8_t> token;
142 token.push_back(1);
143 return HDF_SUCCESS;
144 }
145 );
146 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), HDF_SUCCESS);
147 ON_CALL(*mockHdi, CheckReuseUnlockResult)
148 .WillByDefault(Return(HDF_SUCCESS));
149 EXPECT_EQ(AuthWidgetHelper::CheckReuseUnlockResult(para, authParam, extraInfo), SUCCESS);
150 MockIUserAuthInterface::Holder::GetInstance().Reset();
151 }
152 } // namespace UserAuth
153 } // namespace UserIam
154 } // namespace OHOS