• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "uri.h"
25 
26 #include "common_func.h"
27 #include "file_share.h"
28 #include "log.h"
29 
30 using namespace std;
31 using namespace OHOS::Security::AccessToken;
32 using namespace OHOS::AppFileService;
33 
34 namespace {
35     const string bundleA = "com.ohos.systemui";
36 }
37 
GetSelfBundleName()38 string CommonFunc::GetSelfBundleName()
39 {
40     return bundleA;
41 }
42 
43 namespace OHOS::AppFileService::ModuleFileUri {
44     const string PATH_SHARE = "/data/storage/el2/share";
45     const string MODE_RW = "/rw/";
46     const string MODE_R = "/r/";
47     const int E_OK = 0;
48 
49     class FileUriTest : public testing::Test {
50     public:
SetUpTestCase(void)51         static void SetUpTestCase(void) {};
TearDownTestCase()52         static void TearDownTestCase() {};
SetUp()53         void SetUp() {};
TearDown()54         void TearDown() {};
55     };
56 
57     /**
58      * @tc.name: file_uri_test_0000
59      * @tc.desc: Test function of ToString() interface for SUCCESS.
60      * @tc.size: MEDIUM
61      * @tc.type: FUNC
62      * @tc.level Level 1
63      * @tc.require: I7LW57
64      */
65     HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1)
66     {
67         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000";
68 
69         string fileStr = "/data/storage/el2/base/files/test.txt";
70         string uri = "file://" + bundleA + fileStr;
71         FileUri fileUri(fileStr);
72         EXPECT_EQ(fileUri.ToString(), uri);
73 
74         FileUri fileUri2(uri);
75         EXPECT_EQ(fileUri2.ToString(), uri);
76         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000";
77     }
78 
79     /**
80      * @tc.name: file_uri_test_0001
81      * @tc.desc: Test function of GetName() interface for SUCCESS.
82      * @tc.size: MEDIUM
83      * @tc.type: FUNC
84      * @tc.level Level 1
85      * @tc.require: I7LW57
86      */
87     HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1)
88     {
89         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000";
90 
91         string fileStr = "/data/storage/el2/base/files/test.txt";
92         string uri = "file://" + bundleA + fileStr;
93         FileUri fileUri(fileStr);
94         string name = fileUri.GetName();
95         EXPECT_EQ(name, "test.txt");
96         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000";
97     }
98 
99     /**
100      * @tc.name: file_uri_test_0002
101      * @tc.desc: Test function of GetPath() interface for SUCCESS.
102      * @tc.size: MEDIUM
103      * @tc.type: FUNC
104      * @tc.level Level 1
105      * @tc.require: I7LW57
106      */
107     HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1)
108     {
109         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000";
110         string fileStr = "/data/storage/el2/base/files/test.txt";
111         string uri = "file://" + bundleA + fileStr;
112         FileUri fileUri(uri);
113         string path = fileUri.GetPath();
114         EXPECT_EQ(path, fileStr);
115         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
116     }
117 
118     /**
119      * @tc.name: file_uri_test_0003
120      * @tc.desc: Test function of GetPath() interface for SUCCESS.
121      * @tc.size: MEDIUM
122      * @tc.type: FUNC
123      * @tc.level Level 1
124      * @tc.require: I7LW57
125      */
126     HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1)
127     {
128         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001";
129         string fileStr = "/Documents/test.txt";
130         string uri = "file://" + bundleA + fileStr;
131         FileUri fileUri(uri);
132         string path = fileUri.GetRealPath();
133         EXPECT_EQ(path, fileStr);
134         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001";
135     }
136 
137     /**
138      * @tc.name: file_uri_test_0004
139      * @tc.desc: Test function of GetPath() interface for SUCCESS.
140      * @tc.size: MEDIUM
141      * @tc.type: FUNC
142      * @tc.level Level 1
143      * @tc.require: I7LW57
144      */
145     HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1)
146     {
147         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002";
148         string fileStr = "/data/storage/el2/base/files/test.txt";
149         string bundleB = "com.demo.b";
150         string uri = "file://" + bundleB + fileStr;
151         string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr;
152         FileUri fileUri(uri);
153         string path = fileUri.GetRealPath();
154         EXPECT_EQ(path, rltStr);
155         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002";
156     }
157 
158     /**
159      * @tc.name: file_uri_test_0005
160      * @tc.desc: Test function of GetPath() interface for SUCCESS.
161      * @tc.size: MEDIUM
162      * @tc.type: FUNC
163      * @tc.level Level 1
164      * @tc.require: I7LW57
165      */
166     HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1)
167     {
168         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003";
169         int32_t uid = -1;
170         uid = OHOS::IPCSkeleton::GetCallingUid();
171         string bundleB = "com.ohos.settingsdata";
172         string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt";
173         int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
174         ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!";
175 
176         string actStr = "/data/storage/el2/base/files/test.txt";
177         string uri = "file://" + bundleB + actStr;
178         uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleA, 0);
179 
180         int32_t flag = 3;
181         int32_t ret = CreateShareFile(uri, tokenId, flag);
182         EXPECT_EQ(ret, E_OK);
183 
184         string rltStr = PATH_SHARE + MODE_R + bundleB + actStr;
185         FileUri fileUri(uri);
186         string path = fileUri.GetRealPath();
187         EXPECT_EQ(path, rltStr);
188 
189         vector<string> sharePathList;
190         sharePathList.push_back(uri);
191         ret = DeleteShareFile(tokenId, sharePathList);
192         EXPECT_EQ(ret, E_OK);
193         close(fd);
194         GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003";
195     }
196 
197         /**
198      * @tc.name: file_uri_test_0006
199      * @tc.desc: Test function of GetPath() interface for SUCCESS.
200      * @tc.size: MEDIUM
201      * @tc.type: FUNC
202      * @tc.level Level 1
203      * @tc.require: I7LW57
204      */
205     HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1)
206     {
207         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
208         string fileStr = "/data/storage/el2/base/files/test.txt";
209         string uri = "file://" + fileStr;
210         FileUri fileUri(uri);
211         EXPECT_EQ(fileUri.ToString(), uri);
212         EXPECT_EQ(fileUri.GetName(), "test.txt");
213         EXPECT_EQ(fileUri.GetPath(), fileStr);
214         GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
215     }
216 }