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 #include "UTTest_softbus_session.h"
16
17 #include "dm_anonymous.h"
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "nlohmann/json.hpp"
21 #include "softbus_connector.h"
22 #include "softbus_session.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
SetUp()26 void SoftbusSessionTest::SetUp()
27 {
28 }
TearDown()29 void SoftbusSessionTest::TearDown()
30 {
31 }
SetUpTestCase()32 void SoftbusSessionTest::SetUpTestCase()
33 {
34 }
TearDownTestCase()35 void SoftbusSessionTest::TearDownTestCase()
36 {
37 }
38
39 namespace {
40 std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
41 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
42 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
43 std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
44 std::shared_ptr<DmAuthManager> discoveryMgr =
45 std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
46
47 /**
48 * @tc.name: OpenAuthSession_001
49 * @tc.desc: set deviceId =null, return sessionId(1)
50 * @tc.type: FUNC
51 * @tc.require: AR000GHSJK
52 */
53 HWTEST_F(SoftbusSessionTest, OpenAuthSession_001, testing::ext::TestSize.Level0)
54 {
55 std::string deviceId = "";
56 int ret = softbusSession->OpenAuthSession(deviceId);
57 EXPECT_EQ(ret, -1);
58 }
59
60 /**
61 * @tc.name: OpenAuthSession_002
62 * @tc.desc: set deviceId = "123456";and return sessionId
63 * @tc.type: FUNC
64 * @tc.require: AR000GHSJK
65 */
66 HWTEST_F(SoftbusSessionTest, OpenAuthSession_002, testing::ext::TestSize.Level0)
67 {
68 std::string deviceId = "123456";
69 int ret = softbusSession->OpenAuthSession(deviceId);
70 EXPECT_EQ(ret, -1);
71 }
72
73 /**
74 * @tc.name: SendData_001
75 * @tc.desc: set message null and return ERR_DM_FAILED
76 * @tc.type: FUNC
77 * @tc.require: AR000GHSJK
78 */
79 HWTEST_F(SoftbusSessionTest, SendData_001, testing::ext::TestSize.Level0)
80 {
81 std::string message = "";
82 int32_t sessionId = -1;
83 int ret = softbusSession->SendData(sessionId, message);
84 EXPECT_EQ(ret, ERR_DM_FAILED);
85 }
86
87 /**
88 * @tc.name: SendData_002
89 * @tc.desc: set sessionId = 0, go to the SendBytes'smaster and return ERR_DM_FAILED
90 * @tc.type: FUNC
91 * @tc.require: AR000GHSJK
92 */
93 HWTEST_F(SoftbusSessionTest, SendData_002, testing::ext::TestSize.Level0)
94 {
95 int32_t msgType = 2;
96 nlohmann::json jsonObj;
97 jsonObj[TAG_VER] = DM_ITF_VER;
98 jsonObj[TAG_MSG_TYPE] = msgType;
99 std::string message = jsonObj.dump();
100 int32_t sessionId = 0;
101 softbusSession->RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback>(discoveryMgr));
102 int ret = softbusSession->SendData(sessionId, message);
103 EXPECT_EQ(ret, ERR_DM_FAILED);
104 }
105
106 /**
107 * @tc.name: SendData_003
108 * @tc.desc: set jsonObject[TAG_MSG_TYPE] is string and return ERR_DM_FAILED
109 * @tc.type: FUNC
110 * @tc.require: AR000GHSJK
111 */
112 HWTEST_F(SoftbusSessionTest, SendData_003, testing::ext::TestSize.Level0)
113 {
114 std::string message = R"(
115 {
116 "MSG_TYPE": "messageTest"
117 }
118 )";
119 int32_t sessionId = 0;
120 int32_t ret = softbusSession->SendData(sessionId, message);
121 EXPECT_EQ(ret, ERR_DM_FAILED);
122 }
123
124 /**
125 * @tc.name: SoftbusSession_001
126 * @tc.desc: set SoftbusSession to make a new pointer, and it not nullptr
127 * @tc.type: FUNC
128 * @tc.require: AR000GHSJK
129 */
130 HWTEST_F(SoftbusSessionTest, SoftbusSession_001, testing::ext::TestSize.Level0)
131 {
132 std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
133 ASSERT_NE(m_SoftbusSession, nullptr);
134 }
135
136 /**
137 * @tc.name: SoftbusSession_002
138 * @tc.desc: set SoftbusSession to make a new pointer, it not nullptr and delete it
139 * @tc.type: FUNC
140 * @tc.require: AR000GHSJK
141 */
142 HWTEST_F(SoftbusSessionTest, SoftbusSession_002, testing::ext::TestSize.Level0)
143 {
144 std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
145 m_SoftbusSession.reset();
146 EXPECT_EQ(m_SoftbusSession, nullptr);
147 }
148
149 /**
150 * @tc.name: CloseAuthSession_001
151 * @tc.desc: set sessionId = 3, and return DM_OK
152 * @tc.type: FUNC
153 * @tc.require: AR000GHSJK
154 */
155 HWTEST_F(SoftbusSessionTest, CloseAuthSession_001, testing::ext::TestSize.Level0)
156 {
157 int32_t sessionId = 3;
158 int ret = softbusSession->CloseAuthSession(sessionId);
159 EXPECT_EQ(ret, DM_OK);
160 }
161
162 /**
163 * @tc.name: GetPeerDeviceId_001
164 * @tc.desc: set sessionId = 3 and return DM_OK
165 * @tc.type: FUNC
166 * @tc.require: AR000GHSJK
167 */
168 HWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0)
169 {
170 int32_t sessionId = 3;
171 std::string peerDevId;
172 int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId);
173 EXPECT_EQ(ret, DM_OK);
174 }
175
176 /**
177 * @tc.name: RegisterSessionCallback_001
178 * @tc.desc: set info to null and return DM_OK
179 * @tc.type: FUNC
180 * @tc.require: AR000GHSJK
181 */
182 HWTEST_F(SoftbusSessionTest, RegisterSessionCallback_001, testing::ext::TestSize.Level0)
183 {
184 std::shared_ptr<ISoftbusSessionCallback> callback;
185 int ret = softbusSession->RegisterSessionCallback(callback);
186 EXPECT_EQ(ret, DM_OK);
187 }
188
189 /**
190 * @tc.name: UnRegisterSessionCallback_001
191 * @tc.desc: set info to null and return ERR_DM_FAILED
192 * @tc.type: FUNC
193 * @tc.require: AR000GHSJK
194 */
195 HWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0)
196 {
197 int ret = softbusSession->UnRegisterSessionCallback();
198 EXPECT_EQ(ret, DM_OK);
199 }
200
201 /**
202 * @tc.name: OnSessionOpened_001
203 * @tc.desc: return DM_OK
204 * @tc.type: FUNC
205 * @tc.require: AR000GHSJK
206 */
207 HWTEST_F(SoftbusSessionTest, OnSessionOpened_001, testing::ext::TestSize.Level0)
208 {
209 softbusSession->RegisterSessionCallback(discoveryMgr);
210 int sessionId = 1;
211 int result = 0;
212 void *data = nullptr;
213 unsigned int dataLen = 1;
214 softbusSession->OnBytesReceived(sessionId, data, dataLen);
215 int ret = softbusSession->OnSessionOpened(sessionId, result);
216 softbusSession->OnSessionClosed(sessionId);
217 EXPECT_EQ(ret, DM_OK);
218 }
219 } // namespace
220 } // namespace DistributedHardware
221 } // namespace OHOS
222