1 /*
2 * Copyright (c) 2022 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 "checker/checker_manager.h"
18 #include "accesstoken_kit.h"
19 #include "bootstrap.h"
20 #include "hap_token_info.h"
21 #include "nativetoken_kit.h"
22 #include "utils/crypto.h"
23 using namespace testing::ext;
24 using namespace OHOS::DistributedData;
25 using namespace OHOS::Security::AccessToken;
26 class CheckerManagerTest : public testing::Test {
27 public:
SetUpTestCase(void)28     static void SetUpTestCase(void) {}
TearDownTestCase(void)29     static void TearDownTestCase(void) {}
30     void SetUp();
31     void TearDown();
32     NativeTokenInfoParams infoInstance{0};
33 };
34 
SetUp(void)35 void CheckerManagerTest::SetUp(void)
36 {
37     infoInstance.dcapsNum = 0;
38     infoInstance.permsNum = 0;
39     infoInstance.aclsNum = 0;
40     infoInstance.dcaps = nullptr;
41     infoInstance.perms = nullptr;
42     infoInstance.acls = nullptr;
43     infoInstance.processName = "CheckerManagerTest";
44     infoInstance.aplStr = "system_core";
45 
46     HapInfoParams info = {
47         .userID = 100,
48         .bundleName = "ohos.test.demo",
49         .instIndex = 0,
50         .appIDDesc = "ohos.test.demo"
51     };
52     PermissionDef infoManagerTestPermDef = {
53         .permissionName = "ohos.permission.test",
54         .bundleName = "ohos.test.demo",
55         .grantMode = 1,
56         .availableLevel = APL_NORMAL,
57         .label = "label",
58         .labelId = 1,
59         .description = "open the door",
60         .descriptionId = 1
61     };
62     PermissionStateFull infoManagerTestState = {
63         .permissionName = "ohos.permission.test",
64         .isGeneral = true,
65         .resDeviceID = {"local"},
66         .grantStatus = {PermissionState::PERMISSION_GRANTED},
67         .grantFlags = {1}
68     };
69     HapPolicyParams policy = {
70         .apl = APL_NORMAL,
71         .domain = "test.domain",
72         .permList = {infoManagerTestPermDef},
73         .permStateList = {infoManagerTestState}
74     };
75     AccessTokenKit::AllocHapToken(info, policy);
76 
77     Bootstrap::GetInstance().LoadComponents();
78     Bootstrap::GetInstance().LoadDirectory();
79     Bootstrap::GetInstance().LoadCheckers();
80     Bootstrap::GetInstance().LoadNetworks();
81 }
82 
TearDown()83 void CheckerManagerTest::TearDown()
84 {
85     auto tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0);
86     AccessTokenKit::DeleteToken(tokenId);
87 }
88 /**
89 * @tc.name: checkers
90 * @tc.desc: checker the bundle name of the system abilities.
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author: Sven Wang
94 */
95 HWTEST_F(CheckerManagerTest, Checkers, TestSize.Level0)
96 {
97     auto *checker = CheckerManager::GetInstance().GetChecker("SystemChecker");
98     ASSERT_NE(checker, nullptr);
99     checker = CheckerManager::GetInstance().GetChecker("BundleChecker");
100     ASSERT_NE(checker, nullptr);
101     checker = CheckerManager::GetInstance().GetChecker("OtherChecker");
102     ASSERT_EQ(checker, nullptr);
103 }
104 
105 /**
106 * @tc.name: SystemChecker bms
107 * @tc.desc: checker the bundle name of the system abilities.
108 * @tc.type: FUNC
109 * @tc.require:
110 * @tc.author: Sven Wang
111 */
112 HWTEST_F(CheckerManagerTest, SystemCheckerBMS, TestSize.Level0)
113 {
114     CheckerManager::StoreInfo info;
115     info.uid = 1000;
116     info.tokenId = GetAccessTokenId(&infoInstance);
117     info.bundleName = "bundle_manager_service";
118     ASSERT_EQ("bundle_manager_service", CheckerManager::GetInstance().GetAppId(info));
119     ASSERT_TRUE(CheckerManager::GetInstance().IsValid(info));
120 }
121 
122 /**
123 * @tc.name: SystemChecker form
124 * @tc.desc: checker the bundle name of the system abilities.
125 * @tc.type: FUNC
126 * @tc.require:
127 * @tc.author: Sven Wang
128 */
129 HWTEST_F(CheckerManagerTest, SystemCheckerForm, TestSize.Level0)
130 {
131     CheckerManager::StoreInfo info;
132     info.uid = 1000;
133     info.tokenId = GetAccessTokenId(&infoInstance);
134     info.bundleName = "form_storage";
135     ASSERT_EQ("form_storage", CheckerManager::GetInstance().GetAppId(info));
136     ASSERT_TRUE(CheckerManager::GetInstance().IsValid(info));
137 }
138 
139 /**
140 * @tc.name: SystemChecker ivi
141 * @tc.desc: checker the bundle name of the system abilities.
142 * @tc.type: FUNC
143 * @tc.require:
144 * @tc.author: Sven Wang
145 */
146 HWTEST_F(CheckerManagerTest, SystemCheckerIVI, TestSize.Level0)
147 {
148     CheckerManager::StoreInfo info;
149     info.uid = 1000;
150     info.tokenId = GetAccessTokenId(&infoInstance);
151     info.bundleName = "ivi_config_manager";
152     ASSERT_EQ("ivi_config_manager", CheckerManager::GetInstance().GetAppId(info));
153     ASSERT_TRUE(CheckerManager::GetInstance().IsValid(info));
154 }
155 
156 /**
157 * @tc.name: BundleChecker
158 * @tc.desc: checker the bundle name of the bundle abilities.
159 * @tc.type: FUNC
160 * @tc.require:
161 * @tc.author: Sven Wang
162 */
163 HWTEST_F(CheckerManagerTest, BundleChecker, TestSize.Level0)
164 {
165     CheckerManager::StoreInfo storeInfo;
166     storeInfo.uid = 2000000;
167     storeInfo.tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0);
168     storeInfo.bundleName = "ohos.test.demo";
169     HapTokenInfo tokenInfo;
170     AccessTokenKit::GetHapTokenInfo(storeInfo.tokenId, tokenInfo);
171     ASSERT_EQ(Crypto::Sha256(tokenInfo.appID), CheckerManager::GetInstance().GetAppId(storeInfo));
172     ASSERT_TRUE(CheckerManager::GetInstance().IsValid(storeInfo));
173 }
174 
175 /**
176 * @tc.name: IsDynamic
177 * @tc.desc: checker data type.
178 * @tc.type: FUNC
179 * @tc.require:
180 * @tc.author:
181 */
182 HWTEST_F(CheckerManagerTest, IsDynamic, TestSize.Level0)
183 {
184     CheckerManager::StoreInfo storeInfo;
185     storeInfo.uid = 2000000;
186     storeInfo.tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0);
187     storeInfo.bundleName = "ohos.test.demo";
188     ASSERT_FALSE(CheckerManager::GetInstance().IsDynamic(storeInfo));
189 }
190 
191 /**
192 * @tc.name: IsStatic
193 * @tc.desc: checker data type.
194 * @tc.type: FUNC
195 * @tc.require:
196 * @tc.author:
197 */
198 HWTEST_F(CheckerManagerTest, IsStatic, TestSize.Level0)
199 {
200     CheckerManager::StoreInfo storeInfo;
201     storeInfo.uid = 2000000;
202     storeInfo.tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0);
203     storeInfo.bundleName = "ohos.test.demo";
204     ASSERT_FALSE(CheckerManager::GetInstance().IsStatic(storeInfo));
205 }