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 <fcntl.h>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <sys/stat.h>
20
21 #include "dfs_error.h"
22 #include "distributed_file_daemon_manager_impl.h"
23 #include "distributed_file_daemon_proxy.h"
24 #include "ipc/hmdfs_info.h"
25 #include "i_daemon_mock.h"
26 #include "utils_log.h"
27
28 namespace OHOS::Storage::DistributedFile {
GetInstance()29 sptr<IDaemon> DistributedFileDaemonProxy::GetInstance()
30 {
31 daemonProxy_ = iface_cast<IDaemon>(sptr(new DaemonServiceMock()));
32 return daemonProxy_;
33 }
34 namespace Test {
35 using namespace OHOS::FileManagement;
36 using namespace testing;
37 using namespace testing::ext;
38 using namespace std;
39
40 namespace {
41 DistributedHardware::DmDeviceInfo deviceInfo = {
42 .deviceId = "testdevid",
43 .deviceName = "testdevname",
44 .networkId = "testnetworkid",
45 };
46 }
47
48 class DistributedDaemonManagerImplTest : public testing::Test {
49 public:
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp();
53 void TearDown();
54 std::shared_ptr<DistributedFileDaemonManagerImpl> distributedDaemonManagerImpl_;
55 };
56
SetUpTestCase(void)57 void DistributedDaemonManagerImplTest::SetUpTestCase(void)
58 {
59 GTEST_LOG_(INFO) << "SetUpTestCase";
60 }
61
TearDownTestCase(void)62 void DistributedDaemonManagerImplTest::TearDownTestCase(void)
63 {
64 GTEST_LOG_(INFO) << "TearDownTestCase";
65 }
66
SetUp(void)67 void DistributedDaemonManagerImplTest::SetUp(void)
68 {
69 if (distributedDaemonManagerImpl_ == nullptr) {
70 distributedDaemonManagerImpl_ = std::make_shared<DistributedFileDaemonManagerImpl>();
71 ASSERT_TRUE(distributedDaemonManagerImpl_ != nullptr) << "CallbackManager failed";
72 }
73 GTEST_LOG_(INFO) << "SetUp";
74 }
75
TearDown(void)76 void DistributedDaemonManagerImplTest::TearDown(void)
77 {
78 GTEST_LOG_(INFO) << "TearDown";
79 }
80
81 /**
82 * @tc.name: GetInstanceTest
83 * @tc.desc: Verify the GetInstance function
84 * @tc.type: FUNC
85 * @tc.require: I7M6L1
86 */
87 HWTEST_F(DistributedDaemonManagerImplTest, GetInstanceTest, TestSize.Level1)
88 {
89 GTEST_LOG_(INFO) << "GetInstanceTest Start";
90 try {
91 DistributedFileDaemonManagerImpl::GetInstance();
92 EXPECT_TRUE(true);
93 } catch (...) {
94 EXPECT_TRUE(false);
95 GTEST_LOG_(INFO) << "GetInstanceTest ERROR";
96 }
97 GTEST_LOG_(INFO) << "GetInstanceTest End";
98 }
99
100 /**
101 * @tc.name: OpenP2PConnectionTest
102 * @tc.desc: Verify the OpenP2PConnection function
103 * @tc.type: FUNC
104 * @tc.require: I7M6L1
105 */
106 HWTEST_F(DistributedDaemonManagerImplTest, OpenP2PConnectionTest, TestSize.Level1)
107 {
108 GTEST_LOG_(INFO) << "OpenP2PConnectionTest Start";
109 try {
110 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
111 EXPECT_NE(distributedFileDaemonProxy, nullptr);
112 auto res = distributedDaemonManagerImpl_->OpenP2PConnection(deviceInfo);
113 EXPECT_NE(res, E_SA_LOAD_FAILED);
114 } catch (...) {
115 EXPECT_TRUE(false);
116 GTEST_LOG_(INFO) << "OpenP2PConnectionTest ERROR";
117 }
118 GTEST_LOG_(INFO) << "OpenP2PConnectionTest End";
119 }
120
121 /**
122 * @tc.name: CloseP2PConnectionTest
123 * @tc.desc: Verify the CloseP2PConnection function
124 * @tc.type: FUNC
125 * @tc.require: I7M6L1
126 */
127 HWTEST_F(DistributedDaemonManagerImplTest, CloseP2PConnectionTest, TestSize.Level1)
128 {
129 GTEST_LOG_(INFO) << "CloseP2PConnectionTest Start";
130 try {
131 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
132 EXPECT_NE(distributedFileDaemonProxy, nullptr);
133 auto res = distributedDaemonManagerImpl_->CloseP2PConnection(deviceInfo);
134 EXPECT_NE(res, E_SA_LOAD_FAILED);
135 } catch (...) {
136 EXPECT_TRUE(false);
137 GTEST_LOG_(INFO) << "CloseP2PConnectionTest ERROR";
138 }
139 GTEST_LOG_(INFO) << "CloseP2PConnectionTest End";
140 }
141
142 /**
143 * @tc.name: PrepareSessionTest
144 * @tc.desc: Verify the PrepareSession function
145 * @tc.type: FUNC
146 * @tc.require: I7M6L1
147 */
148 HWTEST_F(DistributedDaemonManagerImplTest, PrepareSessionTest, TestSize.Level1)
149 {
150 const std::string srcUri = "file://docs/storage/Users/currentUser/Documents?networkId=xxxxx";
151 const std::string dstUri = "file://docs/storage/Users/currentUser/Documents";
152 const std::string srcDeviceId = "testSrcDeviceId";
153 const sptr<IRemoteObject> listener = sptr(new DaemonServiceMock());
154 const std::string copyPath = "tmpDir";
155 const std::string sessionName = "DistributedDevice0";
156 HmdfsInfo fileInfo = {
157 .copyPath = copyPath,
158 .dirExistFlag = false,
159 .sessionName = sessionName,
160 };
161 GTEST_LOG_(INFO) << "PrepareSessionTest Start";
162 try {
163 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
164 EXPECT_NE(distributedFileDaemonProxy, nullptr);
165 auto res = distributedDaemonManagerImpl_->PrepareSession(srcUri, dstUri, srcDeviceId, listener, fileInfo);
166 EXPECT_NE(res, E_SA_LOAD_FAILED);
167 } catch (...) {
168 EXPECT_TRUE(false);
169 GTEST_LOG_(INFO) << "PrepareSessionTest ERROR";
170 }
171 GTEST_LOG_(INFO) << "PrepareSessionTest End";
172 }
173 } // namespace Test
174 } // namespace OHOS::Storage::DistributedFile