• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SinkSessionTokernel_0101
91  * @tc.desc: Verify the SinkSessionTokernel function.
92  * @tc.type: FUNC
93  * @tc.require: SR000H0387
94  */
95 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkSessionTokernel_0101, TestSize.Level1)
96 {
97     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkSessionTokernel_0101 start";
98     bool res = true;
99     try {
100         g_talker->SinkSessionTokernel(nullptr, "Server");
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_SinkSessionTokernel_0101 end";
107 }
108 
109 /**
110  * @tc.name: KernelTalkerTest_SinkDevslTokernel_0100
111  * @tc.desc: Verify the SinkDevslTokernel function.
112  * @tc.type: FUNC
113  * @tc.require: SR000H0387
114  */
115 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkDevslTokernel_0100, TestSize.Level1)
116 {
117     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkDevslTokernel_0100 start";
118     bool res = true;
119     try {
120         g_talker->SinkDevslTokernel(SESSION_CID, 1);
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_SinkDevslTokernel_0100 end";
127 }
128 
129 /**
130  * @tc.name: KernelTalkerTest_SinkOfflineCmdToKernel_0100
131  * @tc.desc: Verify the SinkOfflineCmdToKernel function.
132  * @tc.type: FUNC
133  * @tc.require: SR000H0387
134  */
135 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkOfflineCmdToKernel_0100, TestSize.Level1)
136 {
137     GTEST_LOG_(INFO) << "KernelTalkerTest_SinkOfflineCmdToKernel_0100 start";
138     bool res = true;
139     try {
140         g_talker->SinkOfflineCmdToKernel("testSession");
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_SinkOfflineCmdToKernel_0100 end";
147 }
148 
149 /**
150  * @tc.name: KernelTalkerTest_CreatePollThread_0100
151  * @tc.desc: Verify the CreatePollThread function.
152  * @tc.type: FUNC
153  * @tc.require: SR000H0387
154  */
155 HWTEST_F(KernelTalkerTest, KernelTalkerTest_CreatePollThread_0100, TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "KernelTalkerTest_CreatePollThread_0100 start";
158     bool res = true;
159     try {
160         g_talker->CreatePollThread();
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_CreatePollThread_0100 end";
167 }
168 
169 /**
170  * @tc.name: KernelTalkerTest_WaitForPollThreadExited_0100
171  * @tc.desc: Verify the WaitForPollThreadExited function.
172  * @tc.type: FUNC
173  * @tc.require: SR000H0387
174  */
175 HWTEST_F(KernelTalkerTest, KernelTalkerTest_WaitForPollThreadExited_0100, TestSize.Level1)
176 {
177     GTEST_LOG_(INFO) << "KernelTalkerTest_WaitForPollThreadExited_0100 start";
178     int res = true;
179     try {
180         g_talker->WaitForPollThreadExited();
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_WaitForPollThreadExited_0100 end";
187 }
188 
189 /**
190  * @tc.name: KernelTalkerTest_PollRun_0100
191  * @tc.desc: Verify the PollRun function.
192  * @tc.type: FUNC
193  * @tc.require: SR000H0387
194  */
195 HWTEST_F(KernelTalkerTest, KernelTalkerTest_PollRun_0100, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "KernelTalkerTest_PollRun_0100 start";
198     int res = true;
199     try {
200         g_talker->PollRun();
201     } catch (const exception &e) {
202         res = false;
203         LOGE("%{public}s", e.what());
204     }
205     EXPECT_TRUE(res == true);
206     GTEST_LOG_(INFO) << "KernelTalkerTest_PollRun_0100 end";
207 }
208 
209 /**
210  * @tc.name: KernelTalkerTest_HandleAllNotify_0100
211  * @tc.desc: Verify the HandleAllNotify function.
212  * @tc.type: FUNC
213  * @tc.require: SR000H0387
214  */
215 HWTEST_F(KernelTalkerTest, KernelTalkerTest_HandleAllNotify_0100, TestSize.Level1)
216 {
217     GTEST_LOG_(INFO) << "KernelTalkerTest_HandleAllNotify_0100 start";
218     bool res = true;
219     int fd = -1;
220     try {
221         g_talker->HandleAllNotify(fd);
222     } catch (const exception &e) {
223         res = false;
224         LOGE("%{public}s", e.what());
225     }
226     EXPECT_TRUE(res == true);
227     GTEST_LOG_(INFO) << "KernelTalkerTest_HandleAllNotify_0100 end";
228 }
229 
230 /**
231  * @tc.name: KernelTalkerTest_NotifyHandler_0100
232  * @tc.desc: Verify the NotifyHandler function.
233  * @tc.type: FUNC
234  * @tc.require: SR000H0387
235  */
236 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0100, TestSize.Level1)
237 {
238     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0100 start";
239     bool res = true;
240     NotifyParam param;
241     param.notify = NOTIFY_GET_SESSION;
242     try {
243         g_talker->NotifyHandler(param);
244     } catch (const exception &e) {
245         res = false;
246         LOGE("%{public}s", e.what());
247     }
248     EXPECT_TRUE(res == true);
249     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0100 end";
250 }
251 
252 /**
253  * @tc.name: KernelTalkerTest_NotifyHandler_0200
254  * @tc.desc: Verify the NotifyHandler function.
255  * @tc.type: FUNC
256  * @tc.require: SR000H0387
257  */
258 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0200, TestSize.Level1)
259 {
260     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0200 start";
261     bool res = true;
262     NotifyParam param;
263     param.notify = NOTIFY_OFFLINE;
264     try {
265         g_talker->NotifyHandler(param);
266     } catch (const exception &e) {
267         res = false;
268         LOGE("%{public}s", e.what());
269     }
270     EXPECT_TRUE(res == true);
271     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0200 end";
272 }
273 
274 /**
275  * @tc.name: KernelTalkerTest_NotifyHandler_0300
276  * @tc.desc: Verify the NotifyHandler function.
277  * @tc.type: FUNC
278  * @tc.require: SR000H0387
279  */
280 HWTEST_F(KernelTalkerTest, KernelTalkerTest_NotifyHandler_0300, TestSize.Level1)
281 {
282     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0300 start";
283     bool res = true;
284     NotifyParam param;
285     param.notify = NOTIFY_NONE;
286     try {
287         g_talker->NotifyHandler(param);
288     } catch (const exception &e) {
289         res = false;
290         LOGE("%{public}s", e.what());
291     }
292     EXPECT_TRUE(res == true);
293     GTEST_LOG_(INFO) << "KernelTalkerTest_NotifyHandler_0300 end";
294 }
295 } // namespace Test
296 } // namespace DistributedFile
297 } // namespace Storage
298 } // namespace OHOS
299