• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "ability_manager_errors.h"
19 #include "event_report.h"
20 #include "system_ability_definition.h"
21 #include "tokenid_kit.h"
22 #define private public
23 #include "uri_permission_utils.h"
24 #undef private
25 #include "mock_my_flag.h"
26 #include "accesstoken_kit.h"
27 #include "uri.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace AAFwk {
34 class UriPermissionUtilsNewTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40 };
41 
SetUpTestCase()42 void UriPermissionUtilsNewTest::SetUpTestCase() {}
43 
TearDownTestCase()44 void UriPermissionUtilsNewTest::TearDownTestCase() {}
45 
SetUp()46 void UriPermissionUtilsNewTest::SetUp() {}
47 
TearDown()48 void UriPermissionUtilsNewTest::TearDown() {}
49 
50 /*
51  * Feature: UPMSUtils
52  * Function: ConnectManager
53  * SubFunction: NA
54  * FunctionPoints: UPMSUtils SendShareUnPrivilegeUriEvent
55  */
56 HWTEST_F(UriPermissionUtilsNewTest, Upms_SendShareUnPrivilegeUriEvent_001, TestSize.Level1)
57 {
58     uint32_t callerTokenId = 1;
59     uint32_t targetTokenId = 2;
60     MyFlag::tokenInfos_.clear();
61     MyFlag::tokenInfos_[callerTokenId] = TokenInfo(callerTokenId, Security::AccessToken::ATokenTypeEnum::TOKEN_HAP,
62         "com.test.test", "com.test.test");
63     MyFlag::tokenInfos_[targetTokenId] = TokenInfo(targetTokenId, Security::AccessToken::ATokenTypeEnum::TOKEN_HAP,
64         "com.test.test", "com.test.test");
65     auto result = UPMSUtils::SendShareUnPrivilegeUriEvent(callerTokenId, targetTokenId);
66     EXPECT_TRUE(result);
67 }
68 
69 /*
70  * Feature: UPMSUtils
71  * Function: ConnectManager
72  * SubFunction: NA
73  * FunctionPoints: UPMSUtils CheckAndCreateEventInfo
74  */
75 HWTEST_F(UriPermissionUtilsNewTest, Upms_CheckAndCreateEventInfo_001, TestSize.Level1)
76 {
77     uint32_t callerTokenId = 1;
78     uint32_t targetTokenId = 2;
79     EventInfo eventInfo;
80     MyFlag::tokenInfos_.clear();
81     MyFlag::tokenInfos_[callerTokenId] = TokenInfo(callerTokenId, Security::AccessToken::ATokenTypeEnum::TOKEN_HAP,
82         "com.test.ohos", "com.test.ohos");
83     MyFlag::tokenInfos_[targetTokenId] = TokenInfo(targetTokenId, Security::AccessToken::ATokenTypeEnum::TOKEN_HAP,
84         "com.test.ohos", "com.test.ohos");
85     auto result = UPMSUtils::CheckAndCreateEventInfo(callerTokenId, targetTokenId, eventInfo);
86     EXPECT_FALSE(result);
87 }
88 
89 /*
90  * Feature: UPMSUtils
91  * Function: ConnectManager
92  * SubFunction: NA
93  * FunctionPoints: UPMSUtils GetAlterableBundleNameByTokenId
94  */
95 HWTEST_F(UriPermissionUtilsNewTest, Upms_GetAlterableBundleNameByTokenId_001, TestSize.Level1)
96 {
97     uint32_t tokenId = 1;
98     std::string bundleName = "";
99     MyFlag::tokenInfos_.clear();
100     MyFlag::tokenInfos_[tokenId] = TokenInfo(tokenId, Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
101     MyFlag::retHapSuccValue_ = Security::AccessToken::AccessTokenKitRet::RET_FAILED;
102     auto result = UPMSUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName);
103     EXPECT_FALSE(result);
104 }
105 
106 /*
107  * Feature: UPMSUtils
108  * Function: ConnectManager
109  * SubFunction: NA
110  * FunctionPoints: UPMSUtils GetAppIdByBundleName
111  */
112 HWTEST_F(UriPermissionUtilsNewTest, Upms_GetAppIdByBundleName_001, TestSize.Level1)
113 {
114     std::string bundleName = "";
115     std::string appId = "";
116     auto result = UPMSUtils::GetAppIdByBundleName(bundleName, appId);
117     EXPECT_EQ(result, INNER_ERR);
118 }
119 
120 /*
121  * Feature: UPMSUtils
122  * Function: ConnectManager
123  * SubFunction: NA
124  * FunctionPoints: UPMSUtils GetTokenIdByBundleName
125  */
126 HWTEST_F(UriPermissionUtilsNewTest, Upms_GetTokenIdByBundleName_001, TestSize.Level1)
127 {
128     std::string bundleName = "";
129     int32_t appIndex = 1001;
130     uint32_t tokenId = 1;
131     auto result = UPMSUtils::GetTokenIdByBundleName(bundleName, appIndex, tokenId);
132     EXPECT_EQ(result, ERR_GET_TARGET_BUNDLE_INFO_FAILED);
133 }
134 
135 /*
136  * Feature: UPMSUtils
137  * Function: ConnectManagerHelper
138  * SubFunction: NA
139  * FunctionPoints: UPMSUtils ConnectManagerHelper
140  */
141 HWTEST_F(UriPermissionUtilsNewTest, Upms_ConnectManagerHelper_001, TestSize.Level1)
142 {
143     auto manager = UPMSUtils::ConnectManagerHelper();
144     EXPECT_TRUE(manager != nullptr);
145 }
146 
147 /*
148  * Feature: UPMSUtils
149  * Function: IsDocsCloudUri
150  * SubFunction: NA
151  * FunctionPoints: UPMSUtils IsDocsCloudUri
152  */
153 HWTEST_F(UriPermissionUtilsNewTest, Upms_IsDocsCloudUri_001, TestSize.Level1)
154 {
155     Uri uri("ccc");
156     EXPECT_FALSE(UPMSUtils::IsDocsCloudUri(uri));
157 }
158 
159 /*
160  * Feature: UPMSUtils
161  * Function: CheckUriTypeIsValid
162  * SubFunction: NA
163  * FunctionPoints: UPMSUtils CheckUriTypeIsValid
164  */
165 HWTEST_F(UriPermissionUtilsNewTest, Upms_CheckUriTypeIsValid_001, TestSize.Level1)
166 {
167     Uri uri("ccc");
168     EXPECT_FALSE(UPMSUtils::CheckUriTypeIsValid(uri));
169 }
170 
171 /*
172  * Feature: UPMSUtils
173  * Function: CheckUriTypeIsValid
174  * SubFunction: NA
175  * FunctionPoints: UPMSUtils CheckUriTypeIsValid
176  */
177 HWTEST_F(UriPermissionUtilsNewTest, Upms_CheckUriTypeIsValid_002, TestSize.Level1)
178 {
179     Uri uri("file:///path/to/file");
180     EXPECT_TRUE(UPMSUtils::CheckUriTypeIsValid(uri));
181 }
182 
183 /*
184  * Feature: UPMSUtils
185  * Function: GetBundleApiTargetVersion
186  * SubFunction: NA
187  * FunctionPoints: UPMSUtils GetBundleApiTargetVersion
188  */
189 HWTEST_F(UriPermissionUtilsNewTest, Upms_GetBundleApiTargetVersion_001, TestSize.Level1)
190 {
191     std::string bundleName = "ohos.bundle.test";
192     int32_t targetApiVersion = 0;
193     EXPECT_FALSE(UPMSUtils::GetBundleApiTargetVersion(bundleName, targetApiVersion));
194 }
195 
196 /*
197  * Feature: UPMSUtils
198  * Function: GetDirByBundleNameAndAppIndex
199  * SubFunction: NA
200  * FunctionPoints: UPMSUtils GetDirByBundleNameAndAppIndex
201  */
202 HWTEST_F(UriPermissionUtilsNewTest, Upms_GetDirByBundleNameAndAppIndex_001, TestSize.Level1)
203 {
204     std::string bundleName = "ohos.bundle.test";
205     std::string dirName = "/Data";
206     EXPECT_TRUE(UPMSUtils::GetDirByBundleNameAndAppIndex(bundleName, 0, dirName));
207 }
208 }  // namespace AAFwk
209 }  // namespace OHOS