1 /*
2 * Copyright (c) 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/softbus/softbus_session.h"
21 #include "utils_log.h"
22
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 namespace Test {
27 using namespace testing::ext;
28 using namespace std;
29
30 constexpr int TEST_SESSION_ID = 10;
31 std::shared_ptr<SoftbusSession> g_session;
32
33 class SoftbusSessionTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
SetUp()37 void SetUp() {};
TearDown()38 void TearDown() {};
39 };
40
SetUpTestCase(void)41 void SoftbusSessionTest::SetUpTestCase(void)
42 {
43 // input testsuit setup step,setup invoked before all testcases
44 std::string peerDeviceId = "f6d4c0864707aefte7a78f09473aa122ff57fc8";
45 g_session = make_shared<SoftbusSession>(TEST_SESSION_ID, peerDeviceId);
46 }
47
TearDownTestCase(void)48 void SoftbusSessionTest::TearDownTestCase(void)
49 {
50 // input testsuit setup step,setup invoked before all testcases
51 g_session = nullptr;
52 }
53
54 /**
55 * @tc.name: SoftbusSessionTest_IsFromServer_0100
56 * @tc.desc: Verify the IsFromServer function.
57 * @tc.type: FUNC
58 * @tc.require: SR000H0387
59 */
60 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_IsFromServer_0100, TestSize.Level1)
61 {
62 GTEST_LOG_(INFO) << "SoftbusSessionTest_IsFromServer_0100 start";
63 try {
64 bool res = g_session->IsFromServer();
65 EXPECT_TRUE(res == false);
66 } catch (const exception &e) {
67 LOGE("%{public}s", e.what());
68 }
69 GTEST_LOG_(INFO) << "SoftbusSessionTest_IsFromServer_0100 end";
70 }
71
72 /**
73 * @tc.name: SoftbusSessionTest_GetCid_0100
74 * @tc.desc: Verify the GetCid function.
75 * @tc.type: FUNC
76 * @tc.require: SR000H0387
77 */
78 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_GetCid_0100, TestSize.Level1)
79 {
80 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetCid_0100 start";
81 try {
82 string cid = g_session->GetCid();
83 EXPECT_TRUE(cid == "f6d4c0864707aefte7a78f09473aa122ff57fc8");
84 } catch (const exception &e) {
85 LOGE("%{public}s", e.what());
86 }
87 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetCid_0100 end";
88 }
89
90 /**
91 * @tc.name: SoftbusSessionTest_GetHandle_0100
92 * @tc.desc: Verify the GetHandle function.
93 * @tc.type: FUNC
94 * @tc.require: SR000H0387
95 */
96 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_GetHandle_0100, TestSize.Level1)
97 {
98 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetHandle_0100 start";
99 try {
100 int32_t socket = g_session->GetHandle();
101 EXPECT_TRUE(socket == INVALID_SOCKET_FD);
102 } catch (const exception &e) {
103 LOGE("%{public}s", e.what());
104 }
105 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetHandle_0100 end";
106 }
107
108 /**
109 * @tc.name: SoftbusSessionTest_GetKey_0100
110 * @tc.desc: Verify the GetKey function.
111 * @tc.type: FUNC
112 * @tc.require: SR000H0387
113 */
114 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_GetKey_0100, TestSize.Level1)
115 {
116 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetKey_0100 start";
117 bool res = true;
118 try {
119 (void)g_session->GetKey();
120 } catch (const exception &e) {
121 res = false;
122 LOGE("%{public}s", e.what());
123 }
124 EXPECT_TRUE(res == true);
125 GTEST_LOG_(INFO) << "SoftbusSessionTest_GetKey_0100 end";
126 }
127
128 /**
129 * @tc.name: SoftbusSessionTest_Release_0100
130 * @tc.desc: Verify the Release function.
131 * @tc.type: FUNC
132 * @tc.require: SR000H0387
133 */
134 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_Release_0100, TestSize.Level1)
135 {
136 GTEST_LOG_(INFO) << "SoftbusSessionTest_Release_0100 start";
137 try {
138 g_session->Release();
139 EXPECT_TRUE(true);
140 } catch (const exception &e) {
141 LOGE("%{public}s", e.what());
142 }
143 GTEST_LOG_(INFO) << "SoftbusSessionTest_Release_0100 end";
144 }
145
146 /**
147 * @tc.name: SoftbusSessionTest_DisableSessionListener_0100
148 * @tc.desc: Verify the DisableSessionListener function.
149 * @tc.type: FUNC
150 * @tc.require: SR000H0387
151 */
152 HWTEST_F(SoftbusSessionTest, SoftbusSessionTest_DisableSessionListener_0100, TestSize.Level1)
153 {
154 GTEST_LOG_(INFO) << "SoftbusSessionTest_DisableSessionListener_0100 start";
155 try {
156 g_session->DisableSessionListener();
157 EXPECT_TRUE(true);
158 } catch (const exception &e) {
159 LOGE("%{public}s", e.what());
160 }
161 GTEST_LOG_(INFO) << "SoftbusSessionTest_DisableSessionListener_0100 end";
162 }
163 } // namespace Test
164 } // namespace DistributedFile
165 } // namespace Storage
166 } // namespace OHOS
167