1 /*
2 * Copyright (c) 2022 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 <memory>
17
18 #include "gtest/gtest.h"
19 #include "ipc/daemon.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 namespace Test {
26 using namespace testing::ext;
27
28 std::shared_ptr<Daemon> g_daemon = nullptr;
29
30 class DaemonTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase(void)38 void DaemonTest::SetUpTestCase(void)
39 {
40 // input testsuit setup step,setup invoked before all testcases
41 if (g_daemon == nullptr) {
42 int32_t saId = 10;
43 g_daemon = std::make_shared<Daemon>(saId);
44 ASSERT_TRUE(g_daemon != nullptr) << "SystemAbility failed";
45 }
46 }
47
TearDownTestCase(void)48 void DaemonTest::TearDownTestCase(void)
49 {
50 // input testsuit teardown step,teardown invoked after all testcases
51 g_daemon = nullptr;
52 }
53
SetUp(void)54 void DaemonTest::SetUp(void)
55 {
56 // input testcase setup step,setup invoked before each testcases
57 }
58
TearDown(void)59 void DaemonTest::TearDown(void)
60 {
61 // input testcase teardown step,teardown invoked after each testcases
62 }
63
64 /**
65 * @tc.name: DaemonTest_OnStart_0100
66 * @tc.desc: Verify the OnStart function.
67 * @tc.type: FUNC
68 * @tc.require: SR000H0387
69 */
70 HWTEST_F(DaemonTest, DaemonTest_OnStart_0100, TestSize.Level1)
71 {
72 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 start";
73 bool res = true;
74 try {
75 g_daemon->OnStart();
76 }
77 catch(const std::exception& e) {
78 res = false;
79 LOGE("DaemonTest_OnStart_0100 : %{public}s", e.what());
80 }
81 EXPECT_TRUE(res == true);
82 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 end";
83 }
84
85 /**
86 * @tc.name: DaemonTest_OnAddSystemAbility_0200
87 * @tc.desc: Verify the OnAddSystemAbility function.
88 * @tc.type: FUNC
89 * @tc.require: SR000H0387
90 */
91 HWTEST_F(DaemonTest, DaemonTest_OnAddSystemAbility_0200, TestSize.Level1)
92 {
93 GTEST_LOG_(INFO) << "DaemonTest_OnAddSystemAbility_0200 start";
94 bool res = true;
95 try {
96 int32_t saId = 10;
97 g_daemon->OnAddSystemAbility(saId, "");
98 }
99 catch(const std::exception& e) {
100 res = false;
101 LOGE("DaemonTest_OnAddSystemAbility_0200 : %{public}s", e.what());
102 }
103 EXPECT_TRUE(res == true);
104 GTEST_LOG_(INFO) << "DaemonTest_OnAddSystemAbility_0200 end";
105 }
106
107 /**
108 * @tc.name: DaemonTest_OnRemoveSystemAbility_0300
109 * @tc.desc: Verify the OnRemoveSystemAbility function.
110 * @tc.type: FUNC
111 * @tc.require: SR000H0387
112 */
113 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0300, TestSize.Level1)
114 {
115 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0300 start";
116 bool res = true;
117 try {
118 int32_t saId = 10;
119 g_daemon->OnRemoveSystemAbility(saId, "");
120 }
121 catch(const std::exception& e) {
122 res = false;
123 LOGE("DaemonTest_OnRemoveSystemAbility_0300 : %{public}s", e.what());
124 }
125 EXPECT_TRUE(res == true);
126 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0300 end";
127 }
128
129 /**
130 * @tc.name: DaemonTest_QueryServiceState_0400
131 * @tc.desc: Verify the QueryServiceState function.
132 * @tc.type: FUNC
133 * @tc.require: SR000H0387
134 */
135 HWTEST_F(DaemonTest, DaemonTest_QueryServiceState_0400, TestSize.Level1)
136 {
137 GTEST_LOG_(INFO) << "DaemonTest_QueryServiceState_0400 start";
138 ServiceRunningState state { ServiceRunningState::STATE_NOT_START };
139 state = g_daemon->QueryServiceState();
140 EXPECT_EQ(state, ServiceRunningState::STATE_RUNNING);
141 GTEST_LOG_(INFO) << "DaemonTest_QueryServiceState_0400 end";
142 }
143
144 /**
145 * @tc.name: DaemonTest_EchoServerDemo_0500
146 * @tc.desc: Verify the EchoServerDemo function.
147 * @tc.type: FUNC
148 * @tc.require: SR000H0387
149 */
150 HWTEST_F(DaemonTest, DaemonTest_EchoServerDemo_0500, TestSize.Level1)
151 {
152 GTEST_LOG_(INFO) << "DaemonTest_EchoServerDemo_0500 start";
153 int32_t res = 1;
154 try {
155 res = g_daemon->EchoServerDemo("");
156 }
157 catch(const std::exception& e) {
158 LOGE("DaemonTest_EchoServerDemo_0500 : %{public}s", e.what());
159 }
160 EXPECT_EQ(res, 0);
161 GTEST_LOG_(INFO) << "DaemonTest_EchoServerDemo_0500 end";
162 }
163
164 /**
165 * @tc.name: DaemonTest_OnStop_0600
166 * @tc.desc: Verify the OnStop function.
167 * @tc.type: FUNC
168 * @tc.require: SR000H0387
169 */
170 HWTEST_F(DaemonTest, DaemonTest_OnStop_0600, TestSize.Level1)
171 {
172 GTEST_LOG_(INFO) << "DaemonTest_OnStop_0600 start";
173 bool res = true;
174 try {
175 g_daemon->OnStop();
176 }
177 catch(const std::exception& e) {
178 res = false;
179 LOGE("DaemonTest_OnStop_0600 : %{public}s", e.what());
180 }
181 EXPECT_TRUE(res == true);
182 GTEST_LOG_(INFO) << "DaemonTest_OnStop_0600 end";
183 }
184 } // namespace Test
185 } // namespace DistributedFile
186 } // namespace Storage
187 } // namespace OHOS