1 /*
2  * Copyright (c) 2022-2024 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 #include <unistd.h>
18 
19 #include "gtest/gtest.h"
20 #include "network/kernel_talker.h"
21 #include "network/softbus/softbus_session.h"
22 #include "utils_log.h"
23 
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 namespace Test {
28 using namespace testing::ext;
29 using namespace std;
30 
31 constexpr int TEST_SESSION_ID = 10;
32 constexpr int USER_ID = 100;
33 static const string SESSION_CID = "testSession";
34 static const string SAME_ACCOUNT = "account";
35 shared_ptr<MountPoint> g_smp;
36 weak_ptr<MountPoint> g_wmp;
37 shared_ptr<KernelTalker> g_talker;
38 
39 enum Notify {
40     NOTIFY_GET_SESSION = 0,
41     NOTIFY_OFFLINE,
42     NOTIFY_NONE,
43     NOTIFY_CNT,
44 };
45 
46 class KernelTalkerTest : public testing::Test {
47 public:
48     static void SetUpTestCase(void);
49     static void TearDownTestCase(void);
SetUp()50     void SetUp() {};
TearDown()51     void TearDown() {};
52 };
53 
SetUpTestCase(void)54 void KernelTalkerTest::SetUpTestCase(void)
55 {
56     g_smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT));
57     g_wmp = g_smp;
58     g_talker = std::make_shared<KernelTalker>(g_wmp, [](NotifyParam &param) {}, [](const std::string &cid) {});
59 };
60 
TearDownTestCase(void)61 void KernelTalkerTest::TearDownTestCase(void)
62 {
63     g_talker = nullptr;
64     g_smp = nullptr;
65 };
66 
67 /**
68  * @tc.name: KernelTalkerTest_SinkSessionTokernel_0100
69  * @tc.desc: Verify the SinkSessionTokernel function.
70  * @tc.type: FUNC
71  * @tc.require: SR000H0387
72  */
73 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkSessionTokernel_0100, TestSize.Level1)
74 {
75     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkSessionTokernel_0100 start";
76     bool res = true;
77     std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
78     std::shared_ptr<SoftbusSession> session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
79     try {
80         g_talker->SinkSessionTokernel(session, "Server");
81     } catch (const exception &e) {
82         res = false;
83         LOGE("%{public}s", e.what());
84     }
85     EXPECT_TRUE(res == true);
86     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkSessionTokernel_0100 end";
87 }
88 
89 /**
90  * @tc.name: KernelTalkerTest_SinkDevslTokernel_0100
91  * @tc.desc: Verify the SinkDevslTokernel function.
92  * @tc.type: FUNC
93  * @tc.require: SR000H0387
94  */
95 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkDevslTokernel_0100, TestSize.Level1)
96 {
97     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkDevslTokernel_0100 start";
98     bool res = true;
99     try {
100         g_talker->SinkDevslTokernel(SESSION_CID, 1);
101     } catch (const exception &e) {
102         res = false;
103         LOGE("%{public}s", e.what());
104     }
105     EXPECT_TRUE(res == true);
106     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkDevslTokernel_0100 end";
107 }
108 
109 /**
110  * @tc.name: KernelTalkerTest_SinkOfflineCmdToKernel_0100
111  * @tc.desc: Verify the SinkOfflineCmdToKernel function.
112  * @tc.type: FUNC
113  * @tc.require: SR000H0387
114  */
115 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkOfflineCmdToKernel_0100, TestSize.Level1)
116 {
117     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkOfflineCmdToKernel_0100 start";
118     bool res = true;
119     try {
120         g_talker->SinkOfflineCmdToKernel("testSession");
121     } catch (const exception &e) {
122         res = false;
123         LOGE("%{public}s", e.what());
124     }
125     EXPECT_TRUE(res == true);
126     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkOfflineCmdToKernel_0100 end";
127 }
128 
129 /**
130  * @tc.name: KernelTalkerTest_CreatePollThread_0100
131  * @tc.desc: Verify the CreatePollThread function.
132  * @tc.type: FUNC
133  * @tc.require: SR000H0387
134  */
135 HWTEST_F(KernelTalkerTest, KernelTalkerTest_CreatePollThread_0100, TestSize.Level1)
136 {
137     GTEST_LOG_(INFO) << "KernelTalkerTest_CreatePollThread_0100 start";
138     bool res = true;
139     try {
140         g_talker->CreatePollThread();
141     } catch (const exception &e) {
142         res = false;
143         LOGE("%{public}s", e.what());
144     }
145     EXPECT_TRUE(res == true);
146     GTEST_LOG_(INFO) << "KernelTalkerTest_CreatePollThread_0100 end";
147 }
148 
149 /**
150  * @tc.name: KernelTalkerTest_WaitForPollThreadExited_0100
151  * @tc.desc: Verify the WaitForPollThreadExited function.
152  * @tc.type: FUNC
153  * @tc.require: SR000H0387
154  */
155 HWTEST_F(KernelTalkerTest, KernelTalkerTest_WaitForPollThreadExited_0100, TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "KernelTalkerTest_WaitForPollThreadExited_0100 start";
158     int res = true;
159     try {
160         g_talker->WaitForPollThreadExited();
161     } catch (const exception &e) {
162         res = false;
163         LOGE("%{public}s", e.what());
164     }
165     EXPECT_TRUE(res == true);
166     GTEST_LOG_(INFO) << "KernelTalkerTest_WaitForPollThreadExited_0100 end";
167 }
168 
169 /**
170  * @tc.name: KernelTalkerTest_PollRun_0100
171  * @tc.desc: Verify the PollRun function.
172  * @tc.type: FUNC
173  * @tc.require: SR000H0387
174  */
175 HWTEST_F(KernelTalkerTest, KernelTalkerTest_PollRun_0100, TestSize.Level1)
176 {
177     GTEST_LOG_(INFO) << "KernelTalkerTest_PollRun_0100 start";
178     int res = true;
179     try {
180         g_talker->PollRun();
181     } catch (const exception &e) {
182         res = false;
183         LOGE("%{public}s", e.what());
184     }
185     EXPECT_TRUE(res == true);
186     GTEST_LOG_(INFO) << "KernelTalkerTest_PollRun_0100 end";
187 }
188 
189 /**
190  * @tc.name: KernelTalkerTest_HandleAllNotify_0100
191  * @tc.desc: Verify the HandleAllNotify function.
192  * @tc.type: FUNC
193  * @tc.require: SR000H0387
194  */
195 HWTEST_F(KernelTalkerTest, KernelTalkerTest_HandleAllNotify_0100, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "KernelTalkerTest_HandleAllNotify_0100 start";
198     bool res = true;
199     int fd = -1;
200     try {
201         g_talker->HandleAllNotify(fd);
202     } catch (const exception &e) {
203         res = false;
204         LOGE("%{public}s", e.what());
205     }
206     EXPECT_TRUE(res == true);
207     GTEST_LOG_(INFO) << "KernelTalkerTest_HandleAllNotify_0100 end";
208 }
209 
210 /**
211  * @tc.name: KernelTalkerTest_NotifyHandler_0100
212  * @tc.desc: Verify the NotifyHandler function.
213  * @tc.type: FUNC
214  * @tc.require: SR000H0387
215  */
216 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0100, TestSize.Level1)
217 {
218     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0100 start";
219     bool res = true;
220     NotifyParam param;
221     param.notify = NOTIFY_GET_SESSION;
222     try {
223         g_talker->NotifyHandler(param);
224     } catch (const exception &e) {
225         res = false;
226         LOGE("%{public}s", e.what());
227     }
228     EXPECT_TRUE(res == true);
229     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0100 end";
230 }
231 
232 /**
233  * @tc.name: KernelTalkerTest_NotifyHandler_0200
234  * @tc.desc: Verify the NotifyHandler function.
235  * @tc.type: FUNC
236  * @tc.require: SR000H0387
237  */
238 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0200, TestSize.Level1)
239 {
240     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0200 start";
241     bool res = true;
242     NotifyParam param;
243     param.notify = NOTIFY_OFFLINE;
244     try {
245         g_talker->NotifyHandler(param);
246     } catch (const exception &e) {
247         res = false;
248         LOGE("%{public}s", e.what());
249     }
250     EXPECT_TRUE(res == true);
251     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0200 end";
252 }
253 
254 /**
255  * @tc.name: KernelTalkerTest_NotifyHandler_0300
256  * @tc.desc: Verify the NotifyHandler function.
257  * @tc.type: FUNC
258  * @tc.require: SR000H0387
259  */
260 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0300, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0300 start";
263     bool res = true;
264     NotifyParam param;
265     param.notify = NOTIFY_NONE;
266     try {
267         g_talker->NotifyHandler(param);
268     } catch (const exception &e) {
269         res = false;
270         LOGE("%{public}s", e.what());
271     }
272     EXPECT_TRUE(res == true);
273     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0300 end";
274 }
275 } // namespace Test
276 } // namespace DistributedFile
277 } // namespace Storage
278 } // namespace OHOS
279