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 "i_daemon_mock.h"
25 #include "utils_log.h"
26
27 namespace OHOS::Storage::DistributedFile {
GetInstance()28 sptr<IDaemon> DistributedFileDaemonProxy::GetInstance()
29 {
30 daemonProxy_ = iface_cast<IDaemon>(sptr(new DaemonServiceMock()));
31 return daemonProxy_;
32 }
33 namespace Test {
34 using namespace OHOS::FileManagement;
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace std;
38
39 namespace {
40 DistributedHardware::DmDeviceInfo deviceInfo = {
41 .deviceId = "testdevid",
42 .deviceName = "testdevname",
43 .networkId = "testnetworkid",
44 };
45 }
46
47 class DistributedDaemonManagerImplTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 std::shared_ptr<DistributedFileDaemonManagerImpl> distributedDaemonManagerImpl_;
54 };
55
SetUpTestCase(void)56 void DistributedDaemonManagerImplTest::SetUpTestCase(void)
57 {
58 GTEST_LOG_(INFO) << "SetUpTestCase";
59 }
60
TearDownTestCase(void)61 void DistributedDaemonManagerImplTest::TearDownTestCase(void)
62 {
63 GTEST_LOG_(INFO) << "TearDownTestCase";
64 }
65
SetUp(void)66 void DistributedDaemonManagerImplTest::SetUp(void)
67 {
68 if (distributedDaemonManagerImpl_ == nullptr) {
69 distributedDaemonManagerImpl_ = std::make_shared<DistributedFileDaemonManagerImpl>();
70 ASSERT_TRUE(distributedDaemonManagerImpl_ != nullptr) << "CallbackManager failed";
71 }
72 GTEST_LOG_(INFO) << "SetUp";
73 }
74
TearDown(void)75 void DistributedDaemonManagerImplTest::TearDown(void)
76 {
77 GTEST_LOG_(INFO) << "TearDown";
78 }
79
80 /**
81 * @tc.name: GetInstanceTest
82 * @tc.desc: Verify the GetInstance function
83 * @tc.type: FUNC
84 * @tc.require: I7M6L1
85 */
86 HWTEST_F(DistributedDaemonManagerImplTest, GetInstanceTest, TestSize.Level1)
87 {
88 GTEST_LOG_(INFO) << "GetInstanceTest Start";
89 try {
90 DistributedFileDaemonManagerImpl::GetInstance();
91 EXPECT_TRUE(true);
92 } catch (...) {
93 EXPECT_TRUE(false);
94 GTEST_LOG_(INFO) << "GetInstanceTest ERROR";
95 }
96 GTEST_LOG_(INFO) << "GetInstanceTest End";
97 }
98
99 /**
100 * @tc.name: OpenP2PConnectionTest
101 * @tc.desc: Verify the OpenP2PConnection function
102 * @tc.type: FUNC
103 * @tc.require: I7M6L1
104 */
105 HWTEST_F(DistributedDaemonManagerImplTest, OpenP2PConnectionTest, TestSize.Level1)
106 {
107 GTEST_LOG_(INFO) << "OpenP2PConnectionTest Start";
108 try {
109 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
110 EXPECT_NE(distributedFileDaemonProxy, nullptr);
111 auto res = distributedDaemonManagerImpl_->OpenP2PConnection(deviceInfo);
112 EXPECT_NE(res, E_SA_LOAD_FAILED);
113 } catch (...) {
114 EXPECT_TRUE(false);
115 GTEST_LOG_(INFO) << "OpenP2PConnectionTest ERROR";
116 }
117 GTEST_LOG_(INFO) << "OpenP2PConnectionTest End";
118 }
119
120 /**
121 * @tc.name: CloseP2PConnectionTest
122 * @tc.desc: Verify the CloseP2PConnection function
123 * @tc.type: FUNC
124 * @tc.require: I7M6L1
125 */
126 HWTEST_F(DistributedDaemonManagerImplTest, CloseP2PConnectionTest, TestSize.Level1)
127 {
128 GTEST_LOG_(INFO) << "CloseP2PConnectionTest Start";
129 try {
130 auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
131 EXPECT_NE(distributedFileDaemonProxy, nullptr);
132 auto res = distributedDaemonManagerImpl_->CloseP2PConnection(deviceInfo);
133 EXPECT_NE(res, E_SA_LOAD_FAILED);
134 } catch (...) {
135 EXPECT_TRUE(false);
136 GTEST_LOG_(INFO) << "CloseP2PConnectionTest ERROR";
137 }
138 GTEST_LOG_(INFO) << "CloseP2PConnectionTest End";
139 }
140 } // namespace Test
141 } // namespace OHOS::Storage::DistributedFile