• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 #include <sys/mount.h>
22 #include <system_error>
23 #include <unistd.h>
24 
25 #include "device/device_info.h"
26 #include "device/device_manager_agent.h"
27 #include "mountpoint/mount_point.h"
28 #include "network/kernel_talker.h"
29 #include "network/session_pool.h"
30 #include "network/softbus/softbus_session.h"
31 #include "securec.h"
32 #include "utils_directory.h"
33 #include "utils_log.h"
34 
35 namespace OHOS {
36 namespace Storage {
37 namespace DistributedFile {
38 namespace Test {
39 using namespace testing::ext;
40 using namespace std;
41 
42 static const string srcHead = "/data/service/el2/";
43 static const string dstHead = "/mnt/hmdfs/";
44 static const string cacheHead = "/data/service/el2/";
45 static const string SAME_ACCOUNT = "account";
46 std::mutex cmdMutex_;
47 
48 const int KEY_MAX_LEN = 32;
49 const int CID_MAX_LEN = 64;
50 struct UpdateSocketParam {
51     int32_t cmd;
52     int32_t newfd;
53     uint8_t status;
54     uint8_t protocol;
55     uint16_t udpPort;
56     uint8_t deviceType;
57     uint8_t masterKey[KEY_MAX_LEN];
58     char cid[CID_MAX_LEN];
59     int32_t linkType;
60     int32_t binderFd;
61 } __attribute__((packed));
62 
63 class DistributedFileDaemonServiceTest : public testing::Test {
64 public:
65     static void SetUpTestCase(void);
66     static void TearDownTestCase(void);
67     void SetUp();
68     void TearDown();
69 };
70 
SetUpTestCase(void)71 void DistributedFileDaemonServiceTest::SetUpTestCase(void)
72 {
73     // input testsuit setup step,setup invoked before all testcases
74 }
75 
TearDownTestCase(void)76 void DistributedFileDaemonServiceTest::TearDownTestCase(void)
77 {
78     // input testsuit teardown step,teardown invoked after all testcases
79 }
80 
SetUp(void)81 void DistributedFileDaemonServiceTest::SetUp(void)
82 {
83     // input testcase setup step,setup invoked before each testcases
84 }
85 
TearDown(void)86 void DistributedFileDaemonServiceTest::TearDown(void)
87 {
88     // input testcase teardown step,teardown invoked after each testcases
89 }
90 
91 
92 /**
93  * @tc.name: mount_test_001
94  * @tc.desc: Verify the mount/umount function.
95  * @tc.type: FUNC
96  * @tc.require: Issue Number
97  */
98 HWTEST_F(DistributedFileDaemonServiceTest, mount_umount_test_001, TestSize.Level1)
99 {
100     auto mp = make_unique<OHOS::Storage::DistributedFile::MountPoint>(
101         Utils::DfsuMountArgumentDescriptors::Alpha(100, SAME_ACCOUNT));
102 
103     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp = move(mp);
104 
105     try {
106         smp->Mount();
107         smp->Umount();
108         LOGE("testcase run OK");
109     } catch (const exception &e) {
110         LOGE("%{public}s", e.what());
111         EXPECT_EQ(0, 1);
112     }
113     EXPECT_EQ(0, 0);
114 }
115 
116 /**
117  * @tc.name: distributedFileDaemon_service_test_006_repeats_init_register
118  * @tc.desc: Verify the Start/Stop function.
119  * @tc.type: FUNC
120  * @tc.require: Issue Number
121  */
122 HWTEST_F(DistributedFileDaemonServiceTest,
123          distributedFileDaemon_service_test_006_repeats_init_register,
124          TestSize.Level1)
125 {
126     auto dm = DeviceManagerAgent::GetInstance();
127     shared_ptr<OHOS::Storage::DistributedFile::DeviceManagerAgent> sdm = move(dm);
128 
129     try {
130         sdm->Start();
131         sdm->Stop();
132         LOGE("testcase distributedFileDaemon_service_test_006_repeats_init_register run end");
133     } catch (const exception &e) {
134         LOGE("Error:%{public}s", e.what());
135         EXPECT_EQ(0, 1);
136     }
137     EXPECT_EQ(0, 0);
138 }
139 /**
140  * @tc.name: distributedFileDaemon_service_test_007_repeats_create_net_instance
141  * @tc.desc: Verify the JoinGroup/QuitGroup function.
142  * @tc.type: FUNC
143  * @tc.require: Issue Number
144  */
145 HWTEST_F(DistributedFileDaemonServiceTest,
146          distributedFileDaemon_service_test_007_repeats_create_net_instance,
147          TestSize.Level1)
148 {
149     const int userId = 4200;
150     auto mountArgument = Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT);
151     auto mp = make_unique<OHOS::Storage::DistributedFile::MountPoint>(mountArgument);
152     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp = move(mp);
153 
154     try {
155         auto sdm = DeviceManagerAgent::GetInstance();
156 
157         smp->Mount();
158         sdm->Start();
159         sdm->JoinGroup(smp);
160         smp->Umount();
161         sdm->QuitGroup(smp);
162         sdm->Stop();
163         LOGE("testcase distributedFileDaemon_service_test_007_repeats_create_net_instance run end");
164     } catch (const exception &e) {
165         LOGE("Error:%{public}s", e.what());
166         EXPECT_EQ(0, 1);
167     }
168     EXPECT_EQ(0, 0);
169 }
170 /**
171  * @tc.name: distributedFileDaemon_service_test_008_repeats_repeat_create_net_instance
172  * @tc.desc: Verify the JoinGroup/QuitGroup function.
173  * @tc.type: FUNC
174  * @tc.require: Issue Number
175  */
176 HWTEST_F(DistributedFileDaemonServiceTest,
177          distributedFileDaemon_service_test_008_repeats_repeat_create_net_instance,
178          TestSize.Level1)
179 {
180     const int userId = 4201;
181     auto mountArgument = Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT);
182     auto mp = make_unique<OHOS::Storage::DistributedFile::MountPoint>(mountArgument);
183     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp = move(mp);
184 
185     try {
186         auto sdm = DeviceManagerAgent::GetInstance();
187 
188         smp->Mount();
189         sdm->Start();
190         sdm->JoinGroup(smp);
191         try {
192             sdm->JoinGroup(smp);
193         } catch (const exception &e) {
194             string err = e.what();
195             string out = "Mountpoint existed";
196 
197             if (err.find(out) == string::npos) {
198                 LOGE("008-Error:%{public}s", e.what());
199                 EXPECT_EQ(0, 1);
200             }
201         }
202         smp->Umount();
203         sdm->QuitGroup(smp);
204         sdm->Stop();
205         LOGE("testcase distributedFileDaemon_service_test_008_repeats_repeat_create_net_instance run end");
206     } catch (const exception &e) {
207         LOGE("Error:%{public}s", e.what());
208         EXPECT_EQ(0, 1);
209     }
210     EXPECT_EQ(0, 0);
211 }
212 /**
213  * @tc.name: distributedFileDaemon_service_test_009_repeats_create_multipe_net_instance
214  * @tc.desc: Verify the JoinGroup/QuitGroup function.
215  * @tc.type: FUNC
216  * @tc.require: Issue Number
217  */
218 HWTEST_F(DistributedFileDaemonServiceTest,
219          distributedFileDaemon_service_test_009_repeats_create_multipe_net_instance,
220          TestSize.Level1)
221 {
222     const int userId1 = 4202;
223     const int userId2 = 4203;
224     auto mountArgument1 = Utils::DfsuMountArgumentDescriptors::Alpha(userId1, SAME_ACCOUNT);
225     auto mountArgument2 = Utils::DfsuMountArgumentDescriptors::Alpha(userId2, SAME_ACCOUNT);
226     auto mp1 = make_unique<OHOS::Storage::DistributedFile::MountPoint>(mountArgument1);
227     auto mp2 = make_unique<OHOS::Storage::DistributedFile::MountPoint>(mountArgument2);
228     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp1 = move(mp1);
229     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp2 = move(mp2);
230 
231     try {
232         auto sdm = DeviceManagerAgent::GetInstance();
233 
234         smp1->Mount();
235         smp2->Mount();
236         sdm->Start();
237         sdm->JoinGroup(smp1);
238         sdm->JoinGroup(smp2);
239         smp1->Umount();
240         smp2->Umount();
241         sdm->QuitGroup(smp1);
242         sdm->QuitGroup(smp2);
243         sdm->Stop();
244         LOGE("testcase distributedFileDaemon_service_test_009_repeats_create_multipe_net_instance run end");
245     } catch (const exception &e) {
246         LOGE("Error:%{public}s", e.what());
247         EXPECT_EQ(0, 1);
248     }
249     EXPECT_EQ(0, 0);
250 }
251 /**
252  * @tc.name: distributedFileDaemon_service_test_010_repeats_repeat_del_net_instance
253  * @tc.desc: Verify the JoinGroup/QuitGroup function.
254  * @tc.type: FUNC
255  * @tc.require: Issue Number
256  */
257 HWTEST_F(DistributedFileDaemonServiceTest,
258          distributedFileDaemon_service_test_010_repeats_repeat_del_net_instance,
259          TestSize.Level1)
260 {
261     const int userId = 4204;
262     auto mountArgument = Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT);
263     auto mp = make_unique<OHOS::Storage::DistributedFile::MountPoint>(mountArgument);
264     shared_ptr<OHOS::Storage::DistributedFile::MountPoint> smp = move(mp);
265 
266     try {
267         auto sdm = DeviceManagerAgent::GetInstance();
268 
269         smp->Mount();
270         sdm->Start();
271         sdm->JoinGroup(smp);
272         smp->Umount();
273         sdm->QuitGroup(smp);
274         try {
275             sdm->QuitGroup(smp);
276         } catch (const exception &e) {
277             string err = e.what();
278             string out = "Mountpoint didn't exist";
279 
280             if (err.find(out) == string::npos) {
281                 LOGE("010-Error:%{public}s", e.what());
282                 EXPECT_EQ(0, 1);
283             }
284         }
285         sdm->Stop();
286         LOGE("testcase distributedFileDaemon_service_test_010_repeats_repeat_del_net_instance run end");
287     } catch (const exception &e) {
288         LOGE("Error:%{public}s", e.what());
289         EXPECT_EQ(0, 1);
290     }
291     EXPECT_EQ(0, 0);
292 }
293 } // namespace Test
294 } // namespace DistributedFile
295 } // namespace Storage
296 } // namespace OHOS
297