• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <gtest/gtest.h>
16 #include "key_crypto_utils.h"
17 #include "os_account_manager.h"
18 #include "screenlock_manager.h"
19 using namespace testing;
20 using namespace OHOS::AccountSA;
21 int g_idNum = 0;
22 int g_authNum = 0;
23 int g_lockNum = 0;
24 int g_accountExist = 0;
25 bool g_accountIsExist = true;
26 static int g_nonExist = 2;
27 std::vector<int32_t> temp;
28 namespace OHOS::AccountSA {
QueryActiveOsAccountIds(std::vector<int32_t> & ids)29 ErrCode OsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
30 {
31     ids = temp;
32     return g_idNum;
33 }
34 
IsOsAccountExists(const int id,bool & isOsAccountExists)35 ErrCode OsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExists)
36 {
37     isOsAccountExists = g_accountIsExist;
38     return g_accountExist;
39 }
40 }
41 
42 namespace OHOS::ScreenLock {
RequestStrongAuth(int reasonFlag,int32_t userId)43 int32_t ScreenLockManager::RequestStrongAuth(int reasonFlag, int32_t userId)
44 {
45     return g_authNum;
46 }
47 
Lock(int32_t userId)48 int32_t ScreenLockManager::Lock(int32_t userId)
49 {
50     return g_lockNum;
51 }
52 }
53 
54 // Test suite class
55 namespace OHOS::StorageDaemon {
56 class ForceLockUserScreenTest : public ::testing::Test {
57     /**
58     * Define common variable
59     * Implement Setup TearDown methods
60     */
61     public:
62     static void SetUpTestCase(void);
63     static void TearDownTestCase(void);
64     void SetUp();
65     void TearDown();
66 };
67 
SetUpTestCase(void)68 void ForceLockUserScreenTest::SetUpTestCase(void)
69 {
70     GTEST_LOG_(INFO) << "SetUpTestCase";
71 }
72 
TearDownTestCase(void)73 void ForceLockUserScreenTest::TearDownTestCase(void)
74 {
75     GTEST_LOG_(INFO) << "TearDownTestCase";
76 }
77 
SetUp(void)78 void ForceLockUserScreenTest::SetUp(void)
79 {
80     GTEST_LOG_(INFO) << "SetUp";
81 }
82 
TearDown(void)83 void ForceLockUserScreenTest::TearDown(void)
84 {
85     GTEST_LOG_(INFO) << "TearDown";
86 }
87 
88 /**
89  * @tc.number: ForceLockUserScreen_QueryActiveOsAccountIdsFailed
90  * @tc.name: ForceLockUserScreen_QueryActiveOsAccountIdsFailed
91  * @tc.desc: Test function of ForceLockUserScreen interface.
92  * @tc.size: MEDIUM
93  * @tc.type: FUNC
94  * @tc.level Level 1
95  * @tc.require: issuesIC35N9
96  */
97 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_QueryActiveOsAccountIdsFailed, testing::ext::TestSize.Level1)
98 {
99     // Mock the QueryActiveOsAccountIds function to return an error
100     // Call the function under test
101     g_idNum = 1;
102     temp = {1, 2, 3};
103     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
104     g_accountExist = ERR_OK;
105     bool isOsAccountExists = g_accountIsExist;
106     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(1, isOsAccountExists);
107     EXPECT_EQ(ret, ERR_OK);
108 }
109 
110 /**
111  * @tc.number: ForceLockUserScreen_NoActiveOsAccounts
112  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
113  * @tc.desc: Test function of ForceLockUserScreen interface.
114  * @tc.size: MEDIUM
115  * @tc.type: FUNC
116  * @tc.level Level 1
117  * @tc.require: issuesIC35N9
118  */
119 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_NoActiveOsAccounts, testing::ext::TestSize.Level1)
120 {
121     // Mock the QueryActiveOsAccountIds function to return success but an empty list
122     // Call the function under test
123     g_idNum = ERR_OK;
124     temp = {};
125     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
126     g_accountExist = g_nonExist;
127     bool isOsAccountExists = g_accountIsExist;
128     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(2, isOsAccountExists);
129     EXPECT_EQ(ret, g_nonExist);
130 }
131 
132 /**
133  * @tc.number: ForceLockUserScreen_RequestStrongAuthFailed
134  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
135  * @tc.desc: Test function of ForceLockUserScreen interface.
136  * @tc.size: MEDIUM
137  * @tc.type: FUNC
138  * @tc.level Level 1
139  * @tc.require: issuesIC35N9
140  */
141 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_RequestStrongAuthFailed, testing::ext::TestSize.Level1)
142 {
143     // Mock the QueryActiveOsAccountIds and RequestStrongAuth functions to return specific results
144     g_idNum = 1;
145     temp = {};
146     // Call the function under test
147     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
148     g_accountExist = ERR_OK;
149     bool isOsAccountExists = g_accountIsExist;
150     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(3, isOsAccountExists);
151     EXPECT_EQ(ret, ERR_OK);
152 }
153 
154 /**
155  * @tc.number: ForceLockUserScreen_LockFailed
156  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
157  * @tc.desc: Test function of ForceLockUserScreen interface.
158  * @tc.size: MEDIUM
159  * @tc.type: FUNC
160  * @tc.level Level 1
161  * @tc.require: issuesIC35N9
162  */
163 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_LockFailed, testing::ext::TestSize.Level1)
164 {
165     // Mock the QueryActiveOsAccountIds, RequestStrongAuth, and Lock functions to return specific results
166     g_idNum = ERR_OK;
167     temp = {1, 2, 3};
168     // Call the function under test
169     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
170     g_accountExist = ERR_OK;
171     bool isOsAccountExists = g_accountIsExist;
172     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(5, isOsAccountExists);
173     EXPECT_EQ(ret, ERR_OK);
174 }
175 
176 /**
177  * @tc.number: ForceLockUserScreen_Success_1
178  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
179  * @tc.desc: Test function of ForceLockUserScreen interface.
180  * @tc.size: MEDIUM
181  * @tc.type: FUNC
182  * @tc.level Level 1
183  * @tc.require: issuesIC35N9
184  */
185 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_Success_1, testing::ext::TestSize.Level1)
186 {
187     // Mock the QueryActiveOsAccountIds, RequestStrongAuth, and Lock functions to return success
188     g_idNum = ERR_OK;
189     temp = {1, 2, 3};
190     g_authNum = g_nonExist;
191     // Call the function under test
192     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
193     g_accountExist = g_nonExist;
194     bool isOsAccountExists = g_accountIsExist;
195     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(999, isOsAccountExists);
196     EXPECT_EQ(ret, g_nonExist);
197 }
198 
199 /**
200  * @tc.number: ForceLockUserScreen_Success_2
201  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
202  * @tc.desc: Test function of ForceLockUserScreen interface.
203  * @tc.size: MEDIUM
204  * @tc.type: FUNC
205  * @tc.level Level 1
206  * @tc.require: issuesIC35N9
207  */
208 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_Success_2, testing::ext::TestSize.Level1)
209 {
210     // Mock the QueryActiveOsAccountIds, RequestStrongAuth, and Lock functions to return success
211     g_idNum = ERR_OK;
212     temp = {1, 2, 3};
213     g_authNum = ScreenLock::E_SCREENLOCK_OK;
214     // Call the function under test
215     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
216     g_accountExist = ERR_OK;
217     bool isOsAccountExists = g_accountIsExist;
218     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(111, isOsAccountExists);
219     EXPECT_EQ(ret, ERR_OK);
220 }
221 
222 /**
223  * @tc.number: ForceLockUserScreen_Success_3
224  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
225  * @tc.desc: Test function of ForceLockUserScreen interface.
226  * @tc.size: MEDIUM
227  * @tc.type: FUNC
228  * @tc.level Level 1
229  * @tc.require: issuesIC35N9
230  */
231 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_Success_3, testing::ext::TestSize.Level1)
232 {
233     // Mock the QueryActiveOsAccountIds, RequestStrongAuth, and Lock functions to return success
234     g_idNum = ERR_OK;
235     temp = {1, 2, 3};
236     g_authNum = ScreenLock::E_SCREENLOCK_OK;
237     g_lockNum = g_nonExist;
238     // Call the function under test
239     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
240     g_accountExist = ERR_OK;
241     bool isOsAccountExists = g_accountIsExist;
242     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(111, isOsAccountExists);
243     EXPECT_EQ(ret, ERR_OK);
244 }
245 
246 /**
247  * @tc.number: ForceLockUserScreen_Success_4
248  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
249  * @tc.desc: Test function of ForceLockUserScreen interface.
250  * @tc.size: MEDIUM
251  * @tc.type: FUNC
252  * @tc.level Level 1
253  * @tc.require: issuesIC35N9
254  */
255 HWTEST_F(ForceLockUserScreenTest, ForceLockUserScreen_Success_4, testing::ext::TestSize.Level1)
256 {
257     // Mock the QueryActiveOsAccountIds, RequestStrongAuth, and Lock functions to return success
258     g_idNum = ERR_OK;
259     temp = {1, 2, 3};
260     g_authNum = ScreenLock::E_SCREENLOCK_OK;
261     g_lockNum = ScreenLock::E_SCREENLOCK_OK;
262     // Call the function under test
263     OHOS::StorageService::KeyCryptoUtils::ForceLockUserScreen();
264     g_accountExist = ERR_OK;
265     bool isOsAccountExists = g_accountIsExist;
266     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(12345, isOsAccountExists);
267     EXPECT_EQ(ret, ERR_OK);
268 }
269 
270 /**
271  * @tc.number: CheckAccountExists_Failed
272  * @tc.name: ForceLockUserScreen_NoActiveOsAccounts
273  * @tc.desc: Test function of ForceLockUserScreen interface.
274  * @tc.size: MEDIUM
275  * @tc.type: FUNC
276  * @tc.level Level 1
277  * @tc.require: issuesIC35N9
278  */
279 HWTEST_F(ForceLockUserScreenTest, CheckAccountExists_Failed, testing::ext::TestSize.Level1)
280 {
281     // Mock the IsOsAccountExists function to return an error
282     // Call the function under test
283     g_accountExist = g_nonExist;
284     bool isOsAccountExists = g_accountIsExist;
285     int32_t ret = OHOS::StorageService::KeyCryptoUtils::CheckAccountExists(12345, isOsAccountExists);
286     EXPECT_EQ(ret, g_accountExist);
287 }
288 }
289