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 
18 #include "device_policies_storage_rdb.h"
19 
20 using namespace testing::ext;
21 using namespace testing;
22 
23 namespace OHOS {
24 namespace EDM {
25 namespace TEST {
26 const int DEFAULT_USER_ID = 100;
27 const int TEST_USER_ID = 101;
28 const std::string TEST_POLICY_NAME = "policyName";
29 const std::string TEST_POLICY_NAME2 = "policyName2";
30 const std::string TEST_POLICY_VALUE = "policyValue";
31 const std::string TEST_POLICY_VALUE2 = "policyValue2";
32 const std::string ADMIN_PACKAGENAME_ABLILITY = "com.edm.test.MainAbility";
33 class DevicePoliciesStorageRdbTest : public testing::Test {};
34 /**
35  * @tc.name: TestQueryAllUserIdWithoutPolicy
36  * @tc.desc: Test AdminManager::QueryAllUserId.
37  * @tc.type: FUNC
38  */
39 HWTEST_F(DevicePoliciesStorageRdbTest, TestQueryAllUserIdWithoutPolicy, TestSize.Level1)
40 {
41     std::shared_ptr<DevicePoliciesStorageRdb> devicePoliciesStorageRdb = DevicePoliciesStorageRdb::GetInstance();
42     std::vector<int32_t> userIds;
43     devicePoliciesStorageRdb->QueryAllUserId(userIds);
44     ASSERT_TRUE(userIds.empty());
45 }
46 
47 /**
48  * @tc.name: TestQueryAllUserIdWithPolicy
49  * @tc.desc: Test AdminManager::QueryAllUserId.
50  * @tc.type: FUNC
51  */
52 HWTEST_F(DevicePoliciesStorageRdbTest, TestQueryAllUserIdWithPolicy, TestSize.Level1)
53 {
54     std::shared_ptr<DevicePoliciesStorageRdb> devicePoliciesStorageRdb = DevicePoliciesStorageRdb::GetInstance();
55     bool ret = devicePoliciesStorageRdb->InsertAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY,
56         TEST_POLICY_NAME, TEST_POLICY_VALUE);
57     ASSERT_TRUE(ret);
58 
59     std::vector<int32_t> userIds;
60     devicePoliciesStorageRdb->QueryAllUserId(userIds);
61     ASSERT_TRUE(userIds.size() == 1);
62 
63     ret = devicePoliciesStorageRdb->DeleteAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY, TEST_POLICY_NAME);
64     ASSERT_TRUE(ret);
65 
66     userIds.clear();
67     devicePoliciesStorageRdb->QueryAllUserId(userIds);
68     ASSERT_TRUE(userIds.empty());
69 }
70 
71 /**
72  * @tc.name: TestQueryAllUserIdWithMultiPolicy
73  * @tc.desc: Test AdminManager::QueryAllUserId.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(DevicePoliciesStorageRdbTest, TestQueryAllUserIdWithMultiPolicy, TestSize.Level1)
77 {
78     std::shared_ptr<DevicePoliciesStorageRdb> devicePoliciesStorageRdb = DevicePoliciesStorageRdb::GetInstance();
79     bool ret = devicePoliciesStorageRdb->InsertAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY,
80         TEST_POLICY_NAME, TEST_POLICY_VALUE);
81     ASSERT_TRUE(ret);
82     ret = devicePoliciesStorageRdb->InsertCombinedPolicy(DEFAULT_USER_ID, TEST_POLICY_NAME2, TEST_POLICY_VALUE2);
83     ASSERT_TRUE(ret);
84 
85     std::vector<int32_t> userIds;
86     devicePoliciesStorageRdb->QueryAllUserId(userIds);
87     ASSERT_TRUE(userIds.size() == 1);
88 
89     ret = devicePoliciesStorageRdb->DeleteAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY, TEST_POLICY_NAME);
90     ASSERT_TRUE(ret);
91 
92     ret = devicePoliciesStorageRdb->DeleteAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY, TEST_POLICY_NAME2);
93     ASSERT_TRUE(ret);
94 
95     userIds.clear();
96     devicePoliciesStorageRdb->QueryAllUserId(userIds);
97     ASSERT_TRUE(userIds.empty());
98 }
99 
100 /**
101  * @tc.name: TestQueryAllUserIdWithMultiUserId
102  * @tc.desc: Test AdminManager::QueryAllUserId.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(DevicePoliciesStorageRdbTest, TestQueryAllUserIdWithMultiUserId, TestSize.Level1)
106 {
107     std::shared_ptr<DevicePoliciesStorageRdb> devicePoliciesStorageRdb = DevicePoliciesStorageRdb::GetInstance();
108     bool ret = devicePoliciesStorageRdb->InsertAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY,
109         TEST_POLICY_NAME, TEST_POLICY_VALUE);
110     ASSERT_TRUE(ret);
111     ret = devicePoliciesStorageRdb->InsertAdminPolicy(TEST_USER_ID, ADMIN_PACKAGENAME_ABLILITY,
112         TEST_POLICY_NAME, TEST_POLICY_VALUE);
113     ASSERT_TRUE(ret);
114 
115     std::vector<int32_t> userIds;
116     devicePoliciesStorageRdb->QueryAllUserId(userIds);
117     ASSERT_TRUE(userIds.size() == 2);
118 
119     ret = devicePoliciesStorageRdb->DeleteAdminPolicy(DEFAULT_USER_ID, ADMIN_PACKAGENAME_ABLILITY, TEST_POLICY_NAME);
120     ASSERT_TRUE(ret);
121 
122     ret = devicePoliciesStorageRdb->DeleteAdminPolicy(TEST_USER_ID, ADMIN_PACKAGENAME_ABLILITY, TEST_POLICY_NAME);
123     ASSERT_TRUE(ret);
124 
125     userIds.clear();
126     devicePoliciesStorageRdb->QueryAllUserId(userIds);
127     ASSERT_TRUE(userIds.empty());
128 }
129 } // namespace TEST
130 } // namespace EDM
131 } // namespace OHOS
132