• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: SoftbusSession_001
108  * @tc.desc: set SoftbusSession to make a new pointer, and it not nullptr
109  * @tc.type: FUNC
110  * @tc.require: AR000GHSJK
111  */
112 HWTEST_F(SoftbusSessionTest, SoftbusSession_001, testing::ext::TestSize.Level0)
113 {
114     std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
115     ASSERT_NE(m_SoftbusSession, nullptr);
116 }
117 
118 /**
119  * @tc.name: SoftbusSession_002
120  * @tc.desc: set SoftbusSession to make a new pointer, it not nullptr and delete it
121  * @tc.type: FUNC
122  * @tc.require: AR000GHSJK
123  */
124 HWTEST_F(SoftbusSessionTest, SoftbusSession_002, testing::ext::TestSize.Level0)
125 {
126     std::shared_ptr<SoftbusSession> m_SoftbusSession = std::make_shared<SoftbusSession>();
127     m_SoftbusSession.reset();
128     EXPECT_EQ(m_SoftbusSession, nullptr);
129 }
130 
131 /**
132  * @tc.name: CloseAuthSession_001
133  * @tc.desc: set sessionId = 3, and return DM_OK
134  * @tc.type: FUNC
135  * @tc.require: AR000GHSJK
136  */
137 HWTEST_F(SoftbusSessionTest, CloseAuthSession_001, testing::ext::TestSize.Level0)
138 {
139     int32_t sessionId = 3;
140     int ret = softbusSession->CloseAuthSession(sessionId);
141     EXPECT_EQ(ret, DM_OK);
142 }
143 
144 /**
145  * @tc.name: GetPeerDeviceId_001
146  * @tc.desc: set sessionId = 3 and return DM_OK
147  * @tc.type: FUNC
148  * @tc.require: AR000GHSJK
149  */
150 HWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0)
151 {
152     int32_t sessionId = 3;
153     std::string peerDevId;
154     int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId);
155     EXPECT_EQ(ret, DM_OK);
156 }
157 
158 /**
159  * @tc.name: RegisterSessionCallback_001
160  * @tc.desc: set info to null and return DM_OK
161  * @tc.type: FUNC
162  * @tc.require: AR000GHSJK
163  */
164 HWTEST_F(SoftbusSessionTest, RegisterSessionCallback_001, testing::ext::TestSize.Level0)
165 {
166     std::shared_ptr<ISoftbusSessionCallback> callback;
167     int ret = softbusSession->RegisterSessionCallback(callback);
168     EXPECT_EQ(ret, DM_OK);
169 }
170 
171 /**
172  * @tc.name: UnRegisterSessionCallback_001
173  * @tc.desc: set info to null and return ERR_DM_FAILED
174  * @tc.type: FUNC
175  * @tc.require: AR000GHSJK
176  */
177 HWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0)
178 {
179     int ret = softbusSession->UnRegisterSessionCallback();
180     EXPECT_EQ(ret, DM_OK);
181 }
182 } // namespace
183 } // namespace DistributedHardware
184 } // namespace OHOS
185