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 <cerrno>
18 #include <string>
19 #include <vector>
20 
21 #include "remote_uri.h"
22 
23 namespace {
24     using namespace std;
25     using namespace OHOS::DistributedFS::ModuleRemoteUri;
26 
27     class RemoteUriTest : public testing::Test {
28     public:
SetUpTestCase(void)29         static void SetUpTestCase(void) {};
TearDownTestCase()30         static void TearDownTestCase() {};
SetUp()31         void SetUp() {};
TearDown()32         void TearDown() {};
33     };
34 
35     /**
36      * @tc.name: Remote_uri_ConvertUri_0000
37      * @tc.desc: Test function of ConvertUri() interface for SUCCESS.
38      * @tc.size: MEDIUM
39      * @tc.type: FUNC
40      * @tc.level Level 1
41      * @tc.require: AR000HG8M4
42      */
43     HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0000, testing::ext::TestSize.Level1)
44     {
45         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0000";
46         int fd = 10;
47         string remoteUri = "";
48         string expectUri = "datashare:////#fdFromBinder=10";
49         int ret = RemoteUri::ConvertUri(fd, remoteUri);
50         EXPECT_TRUE(ret == 0);
51         EXPECT_TRUE(remoteUri == expectUri);
52 
53         fd = -1;
54         ret = RemoteUri::ConvertUri(fd, remoteUri);
55         EXPECT_TRUE(ret == -EINVAL);
56         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0000";
57     }
58 
59     /**
60      * @tc.name: Remote_uri_ConvertUri_0001
61      * @tc.desc: Test function of ConvertUri() interface for SUCCESS.
62      * @tc.size: MEDIUM
63      * @tc.type: FUNC
64      * @tc.level Level 1
65      * @tc.require: AR000HG8M4
66      */
67     HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0001, testing::ext::TestSize.Level1)
68     {
69         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0001";
70         const string fileStr = "/data/test/remote_uri_test.txt";
71         int maxUriSize = 128;
72         vector<string>remoteUriRecords;
73         vector<int>fdRecords;
74         for (int i = 0; i < maxUriSize + 1; i++) {
75             int fd = open(fileStr.c_str(), O_RDWR);
76             string remoteUri = "";
77             int ret = RemoteUri::ConvertUri(fd, remoteUri);
78             EXPECT_TRUE(ret == 0);
79             remoteUriRecords.emplace_back(remoteUri);
80             fdRecords.emplace_back(fd);
81         }
82         for (int i = 0; i < maxUriSize + 1; i++) {
83             int fd = 0;
84             bool ret = RemoteUri::IsRemoteUri(remoteUriRecords[i], fd);
85             EXPECT_TRUE(ret == true);
86             EXPECT_EQ(fd, fdRecords[i]);
87         }
88         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0001";
89     }
90 
91     /**
92      * @tc.name: Remote_uri_OpenRemoteUri_0002
93      * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS.
94      * @tc.size: MEDIUM
95      * @tc.type: FUNC
96      * @tc.level Level 1
97      * @tc.require: AR000HG8M4
98      */
99     HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0002, testing::ext::TestSize.Level1)
100     {
101         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0002";
102         string remoteUri = "datashar:////#fdFromBinder=10";
103         int ret = RemoteUri::OpenRemoteUri(remoteUri);
104         EXPECT_TRUE(ret == -1);
105         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0002";
106     }
107 
108     /**
109      * @tc.name: Remote_uri_OpenRemoteUri_0003
110      * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS.
111      * @tc.size: MEDIUM
112      * @tc.type: FUNC
113      * @tc.level Level 1
114      * @tc.require: AR000HG8M4
115      */
116     HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0003, testing::ext::TestSize.Level1)
117     {
118         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0003";
119         string remoteUri = "datashare:////fdFromBinder=10";
120         int ret = RemoteUri::OpenRemoteUri(remoteUri);
121         EXPECT_TRUE(ret == -1);
122         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0003";
123     }
124 
125     /**
126      * @tc.name: Remote_uri_OpenRemoteUri_0004
127      * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS.
128      * @tc.size: MEDIUM
129      * @tc.type: FUNC
130      * @tc.level Level 1
131      * @tc.require: AR000HG8M4
132      */
133     HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0004, testing::ext::TestSize.Level1)
134     {
135         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0004";
136         string remoteUri = "datashare:////#fdFromBinde=10";
137         int ret = RemoteUri::OpenRemoteUri(remoteUri);
138         EXPECT_TRUE(ret == -1);
139         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0004";
140     }
141 
142     /**
143      * @tc.name: Remote_uri_OpenRemoteUri_0005
144      * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS.
145      * @tc.size: MEDIUM
146      * @tc.type: FUNC
147      * @tc.level Level 1
148      * @tc.require: AR000HG8M4
149      */
150     HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0005, testing::ext::TestSize.Level1)
151     {
152         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri0005";
153         string remoteUri = "datashare:////#fdFromBinder=10abc";
154         int ret = RemoteUri::OpenRemoteUri(remoteUri);
155         EXPECT_TRUE(ret == -1);
156         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0005";
157     }
158 
159     /**
160      * @tc.name: Remote_uri_IsRemoteUri_006
161      * @tc.desc: Test function of IsRemoteUri() interface for SUCCESS.
162      * @tc.size: MEDIUM
163      * @tc.type: FUNC
164      * @tc.level Level 1
165      * @tc.require: AR000HG8M4
166      */
167     HWTEST_F(RemoteUriTest, Remote_uri_IsRemoteUri_006, testing::ext::TestSize.Level1)
168     {
169         GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_IsRemoteUri_0006";
170         string remoteUri = "datashare:////#fdFromBinder=10";
171         int fd = 0;
172         (void)RemoteUri::IsRemoteUri(remoteUri, fd, O_RDWR);
173         EXPECT_TRUE(fd == -1);
174         GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_IsRemoteUri_0006";
175     }
176 }
177