• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_METHOD1(OnFailed, int32_t(const std::string &sessionName));
45     MOCK_METHOD1(OnFinished, int32_t(const std::string &sessionName));
46 };
47 
SetUpTestCase(void)48 void DaemonTest::SetUpTestCase(void)
49 {
50     GTEST_LOG_(INFO) << "SetUpTestCase";
51 }
52 
TearDownTestCase(void)53 void DaemonTest::TearDownTestCase(void)
54 {
55     GTEST_LOG_(INFO) << "TearDownTestCase";
56 }
57 
SetUp(void)58 void DaemonTest::SetUp(void)
59 {
60     int32_t saID = FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID;
61     bool runOnCreate = true;
62     daemon_ = std::make_shared<Daemon>(saID, runOnCreate);
63     GTEST_LOG_(INFO) << "SetUp";
64 }
65 
TearDown(void)66 void DaemonTest::TearDown(void)
67 {
68     GTEST_LOG_(INFO) << "TearDown";
69 }
70 
71 /**
72  * @tc.name: OnStopTest
73  * @tc.desc: Verify the OnStop function
74  * @tc.type: FUNC
75  * @tc.require: issueI7M6L1
76  */
77 HWTEST_F(DaemonTest, OnStopTest, TestSize.Level1)
78 {
79     GTEST_LOG_(INFO) << "OnStop Start";
80     try {
81         daemon_->state_ = ServiceRunningState::STATE_NOT_START;
82         daemon_->registerToService_ = false;
83         daemon_->OnStop();
84         EXPECT_TRUE(true);
85     } catch (...) {
86         EXPECT_TRUE(false);
87         GTEST_LOG_(INFO) << "OnStop  ERROR";
88     }
89     GTEST_LOG_(INFO) << "OnStop End";
90 }
91 
92 /**
93  * @tc.name: DaemonTest_PublishSA_0100
94  * @tc.desc: Verify the PublishSA function.
95  * @tc.type: FUNC
96  * @tc.require: issueI7SP3A
97  */
98 HWTEST_F(DaemonTest, DaemonTest_PublishSA_0100, TestSize.Level1)
99 {
100     GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 start";
101     try {
102         daemon_->registerToService_ = true;
103         daemon_->PublishSA();
104         EXPECT_TRUE(daemon_->registerToService_);
105     } catch (const exception &e) {
106         LOGE("Error:%{public}s", e.what());
107         EXPECT_TRUE(false);
108     }
109     GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 end";
110 }
111 
112 /**
113  * @tc.name: DaemonTest_OnStart_0100
114  * @tc.desc: Verify the OnStart function.
115  * @tc.type: FUNC
116  * @tc.require: issueI7SP3A
117  */
118 HWTEST_F(DaemonTest, DaemonTest_OnStart_0100, TestSize.Level1)
119 {
120     GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 start";
121     try {
122         daemon_->state_ = ServiceRunningState::STATE_RUNNING;
123         daemon_->OnStart();
124         EXPECT_EQ(daemon_->state_, ServiceRunningState::STATE_RUNNING);
125     } catch (const exception &e) {
126         LOGE("Error:%{public}s", e.what());
127         EXPECT_TRUE(false);
128     }
129     GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 end";
130 }
131 
132 /**
133  * @tc.name: DaemonTest_OnRemoveSystemAbility_0100
134  * @tc.desc: Verify the OnRemoveSystemAbility function.
135  * @tc.type: FUNC
136  * @tc.require: issueI7SP3A
137  */
138 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0100, TestSize.Level1)
139 {
140     GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 start";
141     try {
142         daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID + 1, "");
143         EXPECT_TRUE(true);
144     } catch (const exception &e) {
145         LOGE("Error:%{public}s", e.what());
146         EXPECT_TRUE(false);
147     }
148     GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 end";
149 }
150 
151 /**
152  * @tc.name: DaemonTest_OnRemoveSystemAbility_0200
153  * @tc.desc: Verify the OnRemoveSystemAbility function.
154  * @tc.type: FUNC
155  * @tc.require: issueI7SP3A
156  */
157 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0200, TestSize.Level1)
158 {
159     GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 start";
160     try {
161         daemon_->subScriber_ = nullptr;
162         daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "");
163         EXPECT_TRUE(true);
164     } catch (const exception &e) {
165         LOGE("Error:%{public}s", e.what());
166         EXPECT_TRUE(false);
167     }
168     GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 end";
169 }
170 
171 /**
172  * @tc.name: DaemonTest_PrepareSession_0100
173  * @tc.desc: Verify the PrepareSession function.
174  * @tc.type: FUNC
175  * @tc.require: issueI90MOB
176  */
177 HWTEST_F(DaemonTest, DaemonTest_PrepareSession_0100, TestSize.Level1)
178 {
179     GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 start";
180     try {
181         const std::string srcUri = "file://docs/storage/Users/currentUser/Documents?networkid=xxxxx";
182         const std::string dstUri = "file://docs/storage/Users/currentUser/Documents";
183         const std::string srcDeviceId = "testSrcDeviceId";
184         auto listener = sptr<IRemoteObject>(new MockFileTransListener());
185         EXPECT_EQ(daemon_->PrepareSession(srcUri, dstUri, srcDeviceId, listener),
186                   FileManagement::E_SOFTBUS_SESSION_FAILED);
187     } catch (const exception &e) {
188         LOGE("Error:%{public}s", e.what());
189         EXPECT_TRUE(false);
190     }
191     GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 end";
192 }
193 } // namespace OHOS::Storage::DistributedFile::Test