1 /*
2  * Copyright (c) 2023 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 "file_uri.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <fcntl.h>
21 
22 #include "accesstoken_kit.h"
23 #include "ipc_skeleton.h"
24 #include "parameter.h"
25 #include "uri.h"
26 
27 #include "common_func.h"
28 #include "file_share.h"
29 #include "log.h"
30 
31 using namespace std;
32 using namespace OHOS::Security::AccessToken;
33 using namespace OHOS::AppFileService;
34 
35 namespace {
36     const string BUNDLE_A = "com.example.filesharea";
37     const string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable";
38 }
39 
GetSelfBundleName()40 string CommonFunc::GetSelfBundleName()
41 {
42     return BUNDLE_A;
43 }
44 
CheckFileManagerFullMountEnable()45 static bool CheckFileManagerFullMountEnable()
46 {
47     char value[] = "false";
48     int retSystem = GetParameter(FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value));
49     if ((retSystem > 0) && (string_view(value) == "true")) {
50         return true;
51     }
52     LOGE("Not supporting all mounts");
53     return false;
54 }
55 
56 namespace OHOS::AppFileService::ModuleFileUri {
57     const string PATH_SHARE = "/data/storage/el2/share";
58     const string MODE_RW = "/rw/";
59     const string MODE_R = "/r/";
60     const int E_OK = 0;
61 
62     class FileUriTest : public testing::Test {
63     public:
SetUpTestCase(void)64         static void SetUpTestCase(void) {};
TearDownTestCase()65         static void TearDownTestCase() {};
SetUp()66         void SetUp() {};
TearDown()67         void TearDown() {};
68     };
69 
70     /**
71      * @tc.name: file_uri_test_0000
72      * @tc.desc: Test function of ToString() interface for SUCCESS.
73      * @tc.size: MEDIUM
74      * @tc.type: FUNC
75      * @tc.level Level 1
76      * @tc.require: I7LW57
77      */
78     HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1)
79     {
80         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000";
81 
82         string fileStr = "/data/storage/el2/base/files/test.txt";
83         string uri = "file://" + BUNDLE_A + fileStr;
84         FileUri fileUri(fileStr);
85         EXPECT_EQ(fileUri.ToString(), uri);
86 
87         FileUri fileUri2(uri);
88         EXPECT_EQ(fileUri2.ToString(), uri);
89         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000";
90     }
91 
92     /**
93      * @tc.name: file_uri_test_0001
94      * @tc.desc: Test function of GetName() interface for SUCCESS.
95      * @tc.size: MEDIUM
96      * @tc.type: FUNC
97      * @tc.level Level 1
98      * @tc.require: I7LW57
99      */
100     HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1)
101     {
102         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000";
103 
104         string fileStr = "/data/storage/el2/base/files/test.txt";
105         string uri = "file://" + BUNDLE_A + fileStr;
106         FileUri fileUri(fileStr);
107         string name = fileUri.GetName();
108         EXPECT_EQ(name, "test.txt");
109         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000";
110     }
111 
112     /**
113      * @tc.name: file_uri_test_0002
114      * @tc.desc: Test function of GetPath() interface for SUCCESS.
115      * @tc.size: MEDIUM
116      * @tc.type: FUNC
117      * @tc.level Level 1
118      * @tc.require: I7LW57
119      */
120     HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1)
121     {
122         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000";
123         string fileStr = "/data/storage/el2/base/files/test.txt";
124         string uri = "file://" + BUNDLE_A + fileStr;
125         FileUri fileUri(uri);
126         string path = fileUri.GetPath();
127         EXPECT_EQ(path, fileStr);
128 
129         string uri2 = "file://media/Photo/12/IMG_12345_999999/test.jpg";
130         FileUri fileUri2(uri2);
131         path = fileUri2.GetPath();
132         EXPECT_EQ(path, "/Photo/12/IMG_12345_999999");
133         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
134     }
135 
136     /**
137      * @tc.name: file_uri_test_0003
138      * @tc.desc: Test function of GetPath() interface for SUCCESS.
139      * @tc.size: MEDIUM
140      * @tc.type: FUNC
141      * @tc.level Level 1
142      * @tc.require: I7LW57
143      */
144     HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1)
145     {
146         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001";
147         string fileStr = "/Documents/test.txt";
148         string uri = "file://" + BUNDLE_A + fileStr;
149         FileUri fileUri(uri);
150         string path = fileUri.GetRealPath();
151         EXPECT_EQ(path, fileStr);
152 
153         string fileStrPath = "docs/storage/Users/currentUser/Documents/1.txt";
154         string fileStrRealPath = "/storage/Users/currentUser/Documents/1.txt";
155         string uri2 = "file://" + fileStrPath;
156         FileUri fileUri2(uri2);
157         path.clear();
158         path = fileUri2.GetRealPath();
159         if (CheckFileManagerFullMountEnable()) {
160             EXPECT_EQ(path, fileStrRealPath);
161         } else {
162             EXPECT_EQ(path, PATH_SHARE + MODE_R + fileStrPath);
163         };
164         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001";
165     }
166 
167     /**
168      * @tc.name: file_uri_test_0004
169      * @tc.desc: Test function of GetPath() interface for SUCCESS.
170      * @tc.size: MEDIUM
171      * @tc.type: FUNC
172      * @tc.level Level 1
173      * @tc.require: I7LW57
174      */
175     HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1)
176     {
177         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002";
178         string fileStr = "/data/storage/el2/base/files/test.txt";
179         string bundleB = "com.demo.b";
180         string uri = "file://" + bundleB + fileStr;
181         string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr;
182         FileUri fileUri(uri);
183         string path = fileUri.GetRealPath();
184         EXPECT_EQ(path, rltStr);
185         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002";
186     }
187 
188     /**
189      * @tc.name: file_uri_test_0005
190      * @tc.desc: Test function of GetPath() interface for SUCCESS.
191      * @tc.size: MEDIUM
192      * @tc.type: FUNC
193      * @tc.level Level 1
194      * @tc.require: I7LW57
195      */
196     HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1)
197     {
198         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003";
199         int32_t uid = 100;
200         string bundleB = "com.example.fileshareb";
201         string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt";
202         int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
203         ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!";
204 
205         string actStr = "/data/storage/el2/base/files/test.txt";
206         string uri = "file://" + bundleB + actStr;
207         uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, BUNDLE_A, 0);
208 
209         int32_t flag = 3;
210         vector<string> uriList(1, uri);
211         vector<int32_t> retList;
212         int32_t ret = FileShare::CreateShareFile(uriList, tokenId, flag, retList);
213         EXPECT_EQ(ret, E_OK);
214 
215         string rltStr = PATH_SHARE + MODE_R + bundleB + actStr;
216         FileUri fileUri(uri);
217         string path = fileUri.GetRealPath();
218         EXPECT_EQ(path, rltStr);
219 
220         vector<string> sharePathList;
221         sharePathList.push_back(uri);
222         ret = FileShare::DeleteShareFile(tokenId, sharePathList);
223         EXPECT_EQ(ret, E_OK);
224         close(fd);
225         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003";
226     }
227 
228     /**
229      * @tc.name: file_uri_test_0006
230      * @tc.desc: Test function of GetPath() interface for SUCCESS.
231      * @tc.size: MEDIUM
232      * @tc.type: FUNC
233      * @tc.level Level 1
234      * @tc.require: I7LW57
235      */
236     HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1)
237     {
238         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
239         string fileStr = "/data/storage/el2/base/files/test.txt";
240         string uri = "file://" + fileStr;
241         FileUri fileUri(uri);
242         EXPECT_EQ(fileUri.ToString(), uri);
243         EXPECT_EQ(fileUri.GetName(), "test.txt");
244         EXPECT_EQ(fileUri.GetPath(), fileStr);
245         EXPECT_EQ(fileUri.GetRealPath(), fileStr);
246         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
247     }
248 
249     /**
250      * @tc.name: file_uri_test_0007
251      * @tc.desc: Test function of GetFullDirectoryUri() interface for SUCCESS.
252      * @tc.size: MEDIUM
253      * @tc.type: FUNC
254      * @tc.level Level 1
255      * @tc.require:
256      */
257     HWTEST_F(FileUriTest, File_uri_GetFullDirectoryUri_0000, testing::ext::TestSize.Level1)
258     {
259         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetFullDirectoryUri_0000";
260         string fileStr = "/data/test/remote_file_share_test.txt";
261         FileUri fileUriObject(fileStr);
262         string fileDirectoryUri = "file://" + BUNDLE_A + "/data/test";
263         EXPECT_EQ(fileUriObject.GetFullDirectoryUri(), fileDirectoryUri);
264         string folderStr = "/data/test";
265         string folderUri = "file://" + BUNDLE_A + folderStr;
266         FileUri folderUriObject(folderUri);
267         string folderDirectoryUri = "file://" + BUNDLE_A + folderStr;
268         EXPECT_EQ(folderUriObject.GetFullDirectoryUri(), folderDirectoryUri);
269         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
270     }
271 }