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 <cstdio>
17 #include <thread>
18 #include <unistd.h>
19
20 #include <gtest/gtest.h>
21 #include <nlohmann/json.hpp>
22
23 #include "external_file_access_test.h"
24 #include "accesstoken_kit.h"
25 #include "context_impl.h"
26 #include "file_access_framework_errno.h"
27 #include "file_info_shared_memory.h"
28 #include "iservice_registry.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31
32 namespace OHOS::FileAccessFwk {
33 static shared_ptr<FileAccessHelper> g_fah = nullptr;
34 static shared_ptr<OHOS::AbilityRuntime::Context> g_context = nullptr;
35
SetNativeToken()36 void SetNativeToken()
37 {
38 uint64_t tokenId;
39 const char *perms[] = {
40 "ohos.permission.FILE_ACCESS_MANAGER",
41 "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
42 "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION"
43 };
44 NativeTokenInfoParams infoInstance = {
45 .dcapsNum = 0,
46 .permsNum = 3,
47 .aclsNum = 0,
48 .dcaps = nullptr,
49 .perms = perms,
50 .acls = nullptr,
51 .aplStr = "system_core",
52 };
53
54 infoInstance.processName = "SetUpTestCase";
55 tokenId = GetAccessTokenId(&infoInstance);
56 const uint64_t systemAppMask = (static_cast<uint64_t>(1) << 32);
57 tokenId |= systemAppMask;
58 SetSelfTokenID(tokenId);
59 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
60 }
61
SetUpTestCase()62 void FileExtensionHelperTest::SetUpTestCase()
63 {
64 cout << "FileExtensionHelperTest code test" << endl;
65 SetNativeToken();
66 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67 if (saManager == nullptr) {
68 return;
69 }
70 auto systemAbilityObj = saManager->GetSystemAbility(ABILITY_ID);
71 g_context = make_shared<OHOS::AbilityRuntime::ContextImpl>();
72 g_context->SetToken(systemAbilityObj);
73 AAFwk::Want want;
74 vector<AAFwk::Want> wantVec;
75 setuid(UID_TRANSFORM_TMP);
76 int ret = FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wantVec);
77 EXPECT_EQ(ret, OHOS::FileAccessFwk::ERR_OK);
78 bool isFound = false;
79 for (size_t i = 0; i < wantVec.size(); i++) {
80 auto element = wantVec[i].GetElement();
81 if (element.GetBundleName() == "com.ohos.UserFile.ExternalFileManager" &&
82 element.GetAbilityName() == "FileExtensionAbility") {
83 want = wantVec[i];
84 isFound = true;
85 break;
86 }
87 }
88 EXPECT_TRUE(isFound);
89 vector<AAFwk::Want> wants{want};
90 g_fah = FileAccessHelper::Creator(systemAbilityObj, wants);
91 if (g_fah == nullptr) {
92 GTEST_LOG_(ERROR) << "external_file_access_test g_fah is nullptr";
93 exit(1);
94 }
95 setuid(UID_DEFAULT);
96 }
TearDownTestCase()97 void FileExtensionHelperTest::TearDownTestCase()
98 {
99 g_fah = nullptr;
100 g_context = nullptr;
101 }
102
SetUp()103 void FileExtensionHelperTest::SetUp()
104 {
105 }
106
TearDown()107 void FileExtensionHelperTest::TearDown()
108 {
109 }
110
GetFileAccessHelper()111 shared_ptr<FileAccessHelper> FileExtensionHelperTest::GetFileAccessHelper()
112 {
113 return g_fah;
114 }
115
GetContext()116 shared_ptr<OHOS::AbilityRuntime::Context> FileExtensionHelperTest::GetContext()
117 {
118 return g_context;
119 }
120
ReplaceBundleNameFromPath(std::string & path,const std::string & newName)121 bool FileExtensionHelperTest::ReplaceBundleNameFromPath(std::string &path, const std::string &newName)
122 {
123 Uri uri(path);
124 std::string scheme = uri.GetScheme();
125 if (scheme == FILE_SCHEME_NAME) {
126 std::string curName = uri.GetAuthority();
127 if (curName.empty()) {
128 return false;
129 }
130 path.replace(path.find(curName), curName.length(), newName);
131 return true;
132 }
133
134 std::string tPath = uri.GetPath();
135 if (tPath.empty()) {
136 GTEST_LOG_(INFO) << "Uri path error.";
137 return false;
138 }
139
140 if (tPath.front() != '/') {
141 GTEST_LOG_(INFO) << "Uri path format error.";
142 return false;
143 }
144
145 auto index = tPath.substr(1).find_first_of("/");
146 auto bundleName = tPath.substr(1, index);
147 if (bundleName.empty()) {
148 GTEST_LOG_(INFO) << "bundleName empty.";
149 return false;
150 }
151
152 path.replace(path.find(bundleName), bundleName.length(), newName);
153 return true;
154 }
155
ReplaceBundleName(std::string & str,const std::string & newBundleName)156 bool FileExtensionHelperTest::ReplaceBundleName(std::string& str, const std::string& newBundleName)
157 {
158 if (!ReplaceBundleNameFromPath(str, newBundleName)) {
159 GTEST_LOG_(ERROR) << "replace BundleName failed.";
160 return false;
161 }
162 return true;
163 }
164 }