1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 #include <gtest/hwext/gtest-multithread.h>
18 #include <new>
19 #include <string>
20 #include "os_account_info.h"
21 #define private public
22 #include "os_account_control_file_manager.h"
23 #undef private
24 
25 namespace OHOS {
26 namespace AccountSA {
27 using namespace testing;
28 using namespace testing::mt;
29 using namespace testing::ext;
30 using namespace OHOS::AccountSA;
31 using namespace OHOS;
32 using namespace AccountSA;
33 namespace {
34 const OsAccountType OS_ACCOUNT_TYPE = OsAccountType::ADMIN;
35 const std::string STRING_TEST_USER_NAME = "testuser";
36 const int64_t STRING_TEST_USER_SHELLNUMBER = 1000;
37 int32_t g_id = 0;
38 bool g_write = false;
39 const int32_t ID = 100;
40 const std::vector<std::string> CONSTRAINTS = {
41     "constraints.test",
42 };
43 OsAccountControlFileManager *g_controlManager = new (std::nothrow) OsAccountControlFileManager();
44 }  // namespace
45 class OsAccountControlFileManagerTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp();
50     void TearDown();
51 
52 public:
53     std::string storeID_ = "os_account_info";
54 };
55 
SetUpTestCase(void)56 void OsAccountControlFileManagerTest::SetUpTestCase(void)
57 {
58     ASSERT_NE(g_controlManager, nullptr);
59     g_controlManager->Init();
60     g_controlManager->GetAllowCreateId(g_id);
61     OsAccountInfo osAccountTestInfo(g_id, STRING_TEST_USER_NAME, OS_ACCOUNT_TYPE, STRING_TEST_USER_SHELLNUMBER);
62     osAccountTestInfo.SetIsCreateCompleted(true);
63     ASSERT_EQ(g_controlManager->InsertOsAccount(osAccountTestInfo), ERR_OK);
64     ASSERT_EQ(g_controlManager->UpdateBaseOAConstraints(std::to_string(ID), CONSTRAINTS, true), ERR_OK);
65     ASSERT_EQ(g_controlManager->UpdateGlobalOAConstraints(std::to_string(ID), CONSTRAINTS, true), ERR_OK);
66     ASSERT_EQ(g_controlManager->UpdateSpecificOAConstraints(std::to_string(ID), std::to_string(ID), CONSTRAINTS, true),
67         ERR_OK);
68 }
69 
TearDownTestCase(void)70 void OsAccountControlFileManagerTest::TearDownTestCase(void)
71 {
72     g_controlManager->DelOsAccount(g_id);
73     ASSERT_EQ(g_controlManager->UpdateBaseOAConstraints(std::to_string(ID), CONSTRAINTS, false), ERR_OK);
74     ASSERT_EQ(g_controlManager->UpdateGlobalOAConstraints(std::to_string(ID), CONSTRAINTS, false), ERR_OK);
75     ASSERT_EQ(g_controlManager->UpdateSpecificOAConstraints(std::to_string(ID), std::to_string(ID), CONSTRAINTS, false),
76         ERR_OK);
77 }
78 
SetUp(void)79 void OsAccountControlFileManagerTest::SetUp(void)
80 {}
81 
TearDown(void)82 void OsAccountControlFileManagerTest::TearDown(void)
83 {}
84 
TestWriteReadFileInfo()85 void TestWriteReadFileInfo()
86 {
87     g_write = !g_write;
88     int32_t i = 1000;
89     if (g_write) {
90         while (i--) {
91             std::string testName = STRING_TEST_USER_NAME + std::to_string(i);
92             OsAccountInfo osAccountInfo(g_id, testName, OS_ACCOUNT_TYPE, STRING_TEST_USER_SHELLNUMBER);
93             osAccountInfo.SetIsCreateCompleted(true);
94             EXPECT_EQ(g_controlManager->UpdateOsAccount(osAccountInfo), ERR_OK);
95         }
96     } else {
97         while (i--) {
98             OsAccountInfo osAccountInfo;
99             EXPECT_EQ(g_controlManager->GetOsAccountInfoById(g_id, osAccountInfo), ERR_OK);
100             EXPECT_EQ(osAccountInfo.GetIsCreateCompleted(), true);
101         }
102     }
103 }
104 
105 /**
106  * @tc.name: OsAccountControlFileManagerTest001
107  * @tc.desc: Test multiple thread file operate
108  * @tc.type: FUNC
109  * @tc.require:
110  */
111 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest001, TestSize.Level1)
112 {
113     GTEST_RUN_TASK(TestWriteReadFileInfo);
114 }
115 
TestIsFromBaseOAConstraintsList()116 void TestIsFromBaseOAConstraintsList()
117 {
118     int32_t i = 1000;
119     while (i--) {
120         bool isExits = false;
121         EXPECT_EQ(g_controlManager->IsFromBaseOAConstraintsList(ID, CONSTRAINTS[0], isExits), ERR_OK);
122         EXPECT_EQ(isExits, true);
123     }
124 }
125 
126 /**
127  * @tc.name: OsAccountControlFileManagerTest002
128  * @tc.desc: Test multiple thread file operate
129  * @tc.type: FUNC
130  * @tc.require:
131  */
132 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest002, TestSize.Level1)
133 {
134     GTEST_RUN_TASK(TestIsFromBaseOAConstraintsList);
135 }
136 
TestGetGlobalOAConstraintsList()137 void TestGetGlobalOAConstraintsList()
138 {
139     int32_t i = 1000;
140     while (i--) {
141         std::vector<std::string> constraintsList;
142         EXPECT_EQ(g_controlManager->GetGlobalOAConstraintsList(constraintsList), ERR_OK);
143         EXPECT_EQ(constraintsList.size(), 1);
144     }
145 }
146 
147 /**
148  * @tc.name: OsAccountControlFileManagerTest003
149  * @tc.desc: Test multiple thread file operate
150  * @tc.type: FUNC
151  * @tc.require:
152  */
153 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest003, TestSize.Level1)
154 {
155     GTEST_RUN_TASK(TestGetGlobalOAConstraintsList);
156 }
157 
TestIsFromGlobalOAConstraintsList()158 void TestIsFromGlobalOAConstraintsList()
159 {
160     int32_t i = 1000;
161     while (i--) {
162         std::vector<ConstraintSourceTypeInfo> globalSourceList;
163         EXPECT_EQ(g_controlManager->IsFromGlobalOAConstraintsList(ID, ID, CONSTRAINTS[0], globalSourceList), ERR_OK);
164         EXPECT_EQ(globalSourceList[0].localId, ID);
165         EXPECT_EQ(globalSourceList[0].typeInfo, ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER);
166     }
167 }
168 
169 /**
170  * @tc.name: OsAccountControlFileManagerTest004
171  * @tc.desc: Test multiple thread file operate
172  * @tc.type: FUNC
173  * @tc.require:
174  */
175 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest004, TestSize.Level1)
176 {
177     GTEST_RUN_TASK(TestIsFromGlobalOAConstraintsList);
178 }
179 
TestGetSpecificOAConstraintsList()180 void TestGetSpecificOAConstraintsList()
181 {
182     int32_t i = 1000;
183     while (i--) {
184         std::vector<std::string> constraintsList;
185         EXPECT_EQ(g_controlManager->GetSpecificOAConstraintsList(ID, constraintsList), ERR_OK);
186         EXPECT_EQ(constraintsList.size(), 1);
187     }
188 }
189 
190 /**
191  * @tc.name: OsAccountControlFileManagerTest005
192  * @tc.desc: Test multiple thread file operate
193  * @tc.type: FUNC
194  * @tc.require:
195  */
196 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest005, TestSize.Level1)
197 {
198     GTEST_RUN_TASK(TestGetSpecificOAConstraintsList);
199 }
200 
TestIsFromSpecificOAConstraintsList()201 void TestIsFromSpecificOAConstraintsList()
202 {
203     int32_t i = 1000;
204     while (i--) {
205         std::vector<ConstraintSourceTypeInfo> globalSourceList;
206         EXPECT_EQ(g_controlManager->IsFromSpecificOAConstraintsList(ID, ID, CONSTRAINTS[0], globalSourceList), ERR_OK);
207         EXPECT_EQ(globalSourceList[0].localId, ID);
208         EXPECT_EQ(globalSourceList[0].typeInfo, ConstraintSourceType::CONSTRAINT_TYPE_DEVICE_OWNER);
209     }
210 }
211 
212 /**
213  * @tc.name: OsAccountControlFileManagerTest006
214  * @tc.desc: Test multiple thread file operate
215  * @tc.type: FUNC
216  * @tc.require:
217  */
218 HWTEST_F(OsAccountControlFileManagerTest, OsAccountControlFileManagerTest006, TestSize.Level1)
219 {
220     GTEST_RUN_TASK(TestIsFromSpecificOAConstraintsList);
221 }
222 }  // namespace AccountSA
223 }  // namespace OHOS