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 <gtest/gtest.h>
17 #include <memory>
18 
19 #include "dfs_error.h"
20 #include "distributed_file_daemon_proxy.h"
21 #include "ipc/hmdfs_info.h"
22 #include "i_daemon_mock.h"
23 #include "utils_log.h"
24 
25 namespace OHOS::Storage::DistributedFile::Test {
26 using namespace OHOS::FileManagement;
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 
31 namespace {
32     DistributedHardware::DmDeviceInfo deviceInfo = {
33         .deviceId = "testdevid",
34         .deviceName = "testdevname",
35         .networkId = "testnetworkid",
36     };
37 }
38 
39 class DistributedDaemonProxyTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp();
44     void TearDown();
45     shared_ptr<DistributedFileDaemonProxy> proxy_ = nullptr;
46     sptr<DaemonServiceMock> mock_ = nullptr;
47 };
48 
SetUpTestCase(void)49 void DistributedDaemonProxyTest::SetUpTestCase(void)
50 {
51     GTEST_LOG_(INFO) << "SetUpTestCase";
52 }
53 
TearDownTestCase(void)54 void DistributedDaemonProxyTest::TearDownTestCase(void)
55 {
56     GTEST_LOG_(INFO) << "TearDownTestCase";
57 }
58 
SetUp(void)59 void DistributedDaemonProxyTest::SetUp(void)
60 {
61     mock_ = sptr(new DaemonServiceMock());
62     proxy_ = make_shared<DistributedFileDaemonProxy>(mock_);
63     GTEST_LOG_(INFO) << "SetUp";
64 }
65 
TearDown(void)66 void DistributedDaemonProxyTest::TearDown(void)
67 {
68     mock_ = nullptr;
69     proxy_ = nullptr;
70     GTEST_LOG_(INFO) << "TearDown";
71 }
72 
73 /**
74  * @tc.name: OpenP2PConnectionTest
75  * @tc.desc: Verify the OpenP2PConnection function.
76  * @tc.type: FUNC
77  * @tc.require: I7M6L1
78  */
79 HWTEST_F(DistributedDaemonProxyTest, OpenP2PConnectionTest, TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "OpenP2PConnectionTest Start";
82     EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
83     int ret = proxy_->OpenP2PConnection(deviceInfo);
84     EXPECT_EQ(ret, E_OK);
85     GTEST_LOG_(INFO) << "OpenP2PConnectionTest End";
86 }
87 
88 /**
89  * @tc.name: CloseP2PConnectionTest
90  * @tc.desc: Verify the CloseP2PConnection function.
91  * @tc.type: FUNC
92  * @tc.require: I7M6L1
93  */
94 HWTEST_F(DistributedDaemonProxyTest, CloseP2PConnectionTest, TestSize.Level1)
95 {
96     GTEST_LOG_(INFO) << "CloseP2PConnectionTest Start";
97     EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
98     int ret = proxy_->OpenP2PConnection(deviceInfo);
99     EXPECT_EQ(ret, E_OK);
100     GTEST_LOG_(INFO) << "CloseP2PConnectionTest End";
101 }
102 
103 /**
104  * @tc.name: PrepareSessionTest
105  * @tc.desc: Verify the PrepareSession function
106  * @tc.type: FUNC
107  * @tc.require: I7M6L1
108  */
109 HWTEST_F(DistributedDaemonProxyTest, PrepareSessionTest, TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "PrepareSessionTest Start";
112     EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
113     auto listener = sptr(new DaemonServiceMock());
114     const std::string srcUri = "file://docs/storage/Users/currentUser/Documents?networkId=xxxxx";
115     const std::string dstUri = "file://docs/storage/Users/currentUser/Documents";
116     const std::string srcDeviceId = "testSrcDeviceId";
117     const std::string copyPath = "tmpDir";
118     HmdfsInfo fileInfo = {
119         .copyPath = copyPath,
120         .dirExistFlag = false,
121     };
122     int ret = proxy_->PrepareSession(srcUri, dstUri, srcDeviceId, listener, fileInfo);
123     EXPECT_EQ(ret, E_INVAL_ARG);
124     GTEST_LOG_(INFO) << "PrepareSessionTest End";
125 }
126 } // namespace OHOS::Storage::DistributedFile::Test