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 #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 class KernelTalkerTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
SetUp()43 void SetUp() {};
TearDown()44 void TearDown() {};
45 };
46
SetUpTestCase(void)47 void KernelTalkerTest::SetUpTestCase(void)
48 {
49 g_smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT));
50 g_wmp = g_smp;
51 g_talker = std::make_shared<KernelTalker>(g_wmp, [](NotifyParam ¶m) {}, [](const std::string &cid) {});
52 };
53
TearDownTestCase(void)54 void KernelTalkerTest::TearDownTestCase(void)
55 {
56 g_talker = nullptr;
57 g_smp = nullptr;
58 };
59
60 /**
61 * @tc.name: KernelTalkerTest_SinkSessionTokernel_0100
62 * @tc.desc: Verify the SinkSessionTokernel function.
63 * @tc.type: FUNC
64 * @tc.require: SR000H0387
65 */
66 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkSessionTokernel_0100, TestSize.Level1)
67 {
68 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkSessionTokernel_0100 start";
69 bool res = true;
70 std::shared_ptr<SoftbusSession> session = make_shared<SoftbusSession>(TEST_SESSION_ID);
71 try {
72 g_talker->SinkSessionTokernel(session);
73 } catch (const exception &e) {
74 res = false;
75 LOGE("%{public}s", e.what());
76 }
77 EXPECT_TRUE(res == true);
78 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkSessionTokernel_0100 end";
79 }
80
81 /**
82 * @tc.name: KernelTalkerTest_SinkDevslTokernel_0100
83 * @tc.desc: Verify the SinkDevslTokernel function.
84 * @tc.type: FUNC
85 * @tc.require: SR000H0387
86 */
87 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkDevslTokernel_0100, TestSize.Level1)
88 {
89 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkDevslTokernel_0100 start";
90 bool res = true;
91 try {
92 g_talker->SinkDevslTokernel(SESSION_CID, 1);
93 } catch (const exception &e) {
94 res = false;
95 LOGE("%{public}s", e.what());
96 }
97 EXPECT_TRUE(res == true);
98 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkDevslTokernel_0100 end";
99 }
100
101 /**
102 * @tc.name: KernelTalkerTest_SinkOfflineCmdToKernel_0100
103 * @tc.desc: Verify the SinkOfflineCmdToKernel function.
104 * @tc.type: FUNC
105 * @tc.require: SR000H0387
106 */
107 HWTEST_F(KernelTalkerTest, KernelTalkerTest_SinkOfflineCmdToKernel_0100, TestSize.Level1)
108 {
109 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkOfflineCmdToKernel_0100 start";
110 bool res = true;
111 try {
112 g_talker->SinkOfflineCmdToKernel("testSession");
113 } catch (const exception &e) {
114 res = false;
115 LOGE("%{public}s", e.what());
116 }
117 EXPECT_TRUE(res == true);
118 GTEST_LOG_(INFO) << "KernelTalkerTest_SinkOfflineCmdToKernel_0100 end";
119 }
120
121 /**
122 * @tc.name: KernelTalkerTest_CreatePollThread_0100
123 * @tc.desc: Verify the CreatePollThread function.
124 * @tc.type: FUNC
125 * @tc.require: SR000H0387
126 */
127 HWTEST_F(KernelTalkerTest, KernelTalkerTest_CreatePollThread_0100, TestSize.Level1)
128 {
129 GTEST_LOG_(INFO) << "KernelTalkerTest_CreatePollThread_0100 start";
130 bool res = true;
131 try {
132 g_talker->CreatePollThread();
133 } catch (const exception &e) {
134 res = false;
135 LOGE("%{public}s", e.what());
136 }
137 EXPECT_TRUE(res == true);
138 GTEST_LOG_(INFO) << "KernelTalkerTest_CreatePollThread_0100 end";
139 }
140
141 /**
142 * @tc.name: KernelTalkerTest_WaitForPollThreadExited_0100
143 * @tc.desc: Verify the WaitForPollThreadExited function.
144 * @tc.type: FUNC
145 * @tc.require: SR000H0387
146 */
147 HWTEST_F(KernelTalkerTest, KernelTalkerTest_WaitForPollThreadExited_0100, TestSize.Level1)
148 {
149 GTEST_LOG_(INFO) << "KernelTalkerTest_WaitForPollThreadExited_0100 start";
150 int res = true;
151 try {
152 g_talker->WaitForPollThreadExited();
153 } catch (const exception &e) {
154 res = false;
155 LOGE("%{public}s", e.what());
156 }
157 EXPECT_TRUE(res == true);
158 GTEST_LOG_(INFO) << "KernelTalkerTest_WaitForPollThreadExited_0100 end";
159 }
160 } // namespace Test
161 } // namespace DistributedFile
162 } // namespace Storage
163 } // namespace OHOS
164