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