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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "dfs_error.h"
20 #include "i_daemon_mock.h"
21 #include "i_file_trans_listener.h"
22 #include "ipc/daemon.h"
23 #include "iremote_object.h"
24 #include "system_ability_definition.h"
25 #include "utils_log.h"
26
27 namespace OHOS::Storage::DistributedFile::Test {
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace std;
31
32 class DaemonTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 std::shared_ptr<Daemon> daemon_;
39 };
40
41 class MockFileTransListener : public IRemoteStub<IFileTransListener> {
42 public:
43 MOCK_METHOD2(OnFileReceive, int32_t(uint64_t totalBytes, uint64_t processedBytes));
44 MOCK_METHOD2(OnFailed, int32_t(const std::string &sessionName, int32_t errorCode));
45 MOCK_METHOD1(OnFinished, int32_t(const std::string &sessionName));
46 };
47
48 DistributedHardware::DmDeviceInfo deviceInfo = {
49 .deviceId = "testdevid",
50 .networkId = "testnetworkid",
51 };
52
SetUpTestCase(void)53 void DaemonTest::SetUpTestCase(void)
54 {
55 GTEST_LOG_(INFO) << "SetUpTestCase";
56 }
57
TearDownTestCase(void)58 void DaemonTest::TearDownTestCase(void)
59 {
60 GTEST_LOG_(INFO) << "TearDownTestCase";
61 }
62
SetUp(void)63 void DaemonTest::SetUp(void)
64 {
65 int32_t saID = FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID;
66 bool runOnCreate = true;
67 daemon_ = std::make_shared<Daemon>(saID, runOnCreate);
68 GTEST_LOG_(INFO) << "SetUp";
69 }
70
TearDown(void)71 void DaemonTest::TearDown(void)
72 {
73 GTEST_LOG_(INFO) << "TearDown";
74 }
75
76 /**
77 * @tc.name: OnStopTest
78 * @tc.desc: Verify the OnStop function
79 * @tc.type: FUNC
80 * @tc.require: issueI7M6L1
81 */
82 HWTEST_F(DaemonTest, OnStopTest, TestSize.Level1)
83 {
84 GTEST_LOG_(INFO) << "OnStop Start";
85 try {
86 daemon_->state_ = ServiceRunningState::STATE_NOT_START;
87 daemon_->registerToService_ = false;
88 daemon_->OnStop();
89 EXPECT_TRUE(true);
90 } catch (...) {
91 EXPECT_TRUE(false);
92 GTEST_LOG_(INFO) << "OnStop ERROR";
93 }
94 GTEST_LOG_(INFO) << "OnStop End";
95 }
96
97 /**
98 * @tc.name: DaemonTest_PublishSA_0100
99 * @tc.desc: Verify the PublishSA function.
100 * @tc.type: FUNC
101 * @tc.require: issueI7SP3A
102 */
103 HWTEST_F(DaemonTest, DaemonTest_PublishSA_0100, TestSize.Level1)
104 {
105 GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 start";
106 try {
107 daemon_->registerToService_ = true;
108 daemon_->PublishSA();
109 EXPECT_TRUE(daemon_->registerToService_);
110 } catch (const exception &e) {
111 LOGE("Error:%{public}s", e.what());
112 EXPECT_TRUE(false);
113 }
114 GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 end";
115 }
116
117 /**
118 * @tc.name: DaemonTest_OnStart_0100
119 * @tc.desc: Verify the OnStart function.
120 * @tc.type: FUNC
121 * @tc.require: issueI7SP3A
122 */
123 HWTEST_F(DaemonTest, DaemonTest_OnStart_0100, TestSize.Level1)
124 {
125 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 start";
126 try {
127 daemon_->state_ = ServiceRunningState::STATE_RUNNING;
128 daemon_->OnStart();
129 EXPECT_EQ(daemon_->state_, ServiceRunningState::STATE_RUNNING);
130 } catch (const exception &e) {
131 LOGE("Error:%{public}s", e.what());
132 EXPECT_TRUE(false);
133 }
134 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 end";
135 }
136
137 /**
138 * @tc.name: DaemonTest_OnRemoveSystemAbility_0100
139 * @tc.desc: Verify the OnRemoveSystemAbility function.
140 * @tc.type: FUNC
141 * @tc.require: issueI7SP3A
142 */
143 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0100, TestSize.Level1)
144 {
145 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 start";
146 try {
147 daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID + 1, "");
148 EXPECT_TRUE(true);
149 } catch (const exception &e) {
150 LOGE("Error:%{public}s", e.what());
151 EXPECT_TRUE(false);
152 }
153 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 end";
154 }
155
156 /**
157 * @tc.name: DaemonTest_OnRemoveSystemAbility_0200
158 * @tc.desc: Verify the OnRemoveSystemAbility function.
159 * @tc.type: FUNC
160 * @tc.require: issueI7SP3A
161 */
162 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0200, TestSize.Level1)
163 {
164 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 start";
165 try {
166 daemon_->subScriber_ = nullptr;
167 daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "");
168 EXPECT_TRUE(true);
169 } catch (const exception &e) {
170 LOGE("Error:%{public}s", e.what());
171 EXPECT_TRUE(false);
172 }
173 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 end";
174 }
175
176 /**
177 * @tc.name: DaemonTest_PrepareSession_0100
178 * @tc.desc: Verify the PrepareSession function.
179 * @tc.type: FUNC
180 * @tc.require: issueI90MOB
181 */
182 HWTEST_F(DaemonTest, DaemonTest_PrepareSession_0100, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 start";
185 try {
186 const std::string srcUri = "file://docs/storage/Users/currentUser/Documents?networkid=xxxxx";
187 const std::string dstUri = "file://docs/storage/Users/currentUser/Documents";
188 const std::string srcDeviceId = "testSrcDeviceId";
189 auto listener = sptr<IRemoteObject>(new MockFileTransListener());
190 const std::string copyPath = "tmpDir";
191 const std::string sessionName = "DistributedDevice0";
192 HmdfsInfo fileInfo = {
193 .copyPath = copyPath,
194 .dirExistFlag = false,
195 .sessionName = sessionName,
196 };
197 EXPECT_EQ(daemon_->PrepareSession(srcUri, dstUri, srcDeviceId, listener, fileInfo),
198 FileManagement::E_SA_LOAD_FAILED);
199 } catch (const exception &e) {
200 LOGE("Error:%{public}s", e.what());
201 EXPECT_TRUE(false);
202 }
203 GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 end";
204 }
205 } // namespace OHOS::Storage::DistributedFile::Test