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 #include "UTTest_softbus_adapter.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_adapter.h"
22 #include "softbus_connector.h"
23 #include "softbus_error_code.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
SetUp()27 void SoftbusAdapterTest::SetUp()
28 {
29 }
TearDown()30 void SoftbusAdapterTest::TearDown()
31 {
32 }
SetUpTestCase()33 void SoftbusAdapterTest::SetUpTestCase()
34 {
35 }
TearDownTestCase()36 void SoftbusAdapterTest::TearDownTestCase()
37 {
38 }
39 
40 namespace {
41 HWTEST_F(SoftbusAdapterTest, CreateSoftbusSessionServer_001, testing::ext::TestSize.Level0)
42 {
43     std::string pkgName;
44     std::string sessionName;
45     int32_t ret = SoftbusAdapter::GetInstance().CreateSoftbusSessionServer(pkgName, sessionName);
46     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
47 }
48 
49 HWTEST_F(SoftbusAdapterTest, RemoveSoftbusSessionServer_001, testing::ext::TestSize.Level0)
50 {
51     std::string pkgName;
52     std::string sessionName;
53     int32_t ret = SoftbusAdapter::GetInstance().RemoveSoftbusSessionServer(pkgName, sessionName);
54     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
55 }
56 
57 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionOpened_001, testing::ext::TestSize.Level0)
58 {
59     int32_t socket = 1;
60     std::string str = "132131321345";
61     PeerSocketInfo info = {
62         .name = const_cast<char*>(str.c_str()),
63         .pkgName = const_cast<char*>(str.c_str()),
64         .networkId = const_cast<char*>(str.c_str()),
65     };
66     SoftbusAdapter::GetInstance().OnSoftbusSessionOpened(socket, info);
67     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
68 }
69 
70 HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionClosed_001, testing::ext::TestSize.Level0)
71 {
72     int32_t socket = 1;
73     ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
74     SoftbusAdapter::GetInstance().OnSoftbusSessionClosed(socket, reason);
75     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
76 }
77 
78 HWTEST_F(SoftbusAdapterTest, OnBytesReceived_001, testing::ext::TestSize.Level0)
79 {
80     int32_t socket = 1;
81     std::string data = "data";
82     SoftbusAdapter::GetInstance().OnBytesReceived(socket, data.c_str(), data.size());
83     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
84 }
85 
86 HWTEST_F(SoftbusAdapterTest, OnStreamReceived_001, testing::ext::TestSize.Level0)
87 {
88     int32_t socket = 1;
89     StreamData data;
90     StreamData exta;
91     StreamFrameInfo frameInfo;
92     SoftbusAdapter::GetInstance().OnStreamReceived(socket, &data, &exta, &frameInfo);
93     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
94 }
95 
96 HWTEST_F(SoftbusAdapterTest, OnMessageReceived_001, testing::ext::TestSize.Level0)
97 {
98     int32_t socket = 1;
99     std::string data = "data";
100     SoftbusAdapter::GetInstance().OnMessageReceived(socket, data.c_str(), data.size());
101     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
102 }
103 
104 HWTEST_F(SoftbusAdapterTest, OnQosEvent_001, testing::ext::TestSize.Level0)
105 {
106     int32_t socket = 1;
107     QoSEvent eventId = static_cast<QoSEvent>(1);
108     QosTV qosInfo[] = {
109         { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
110         { .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 },
111         { .qos = QOS_TYPE_MIN_LATENCY, .value = 2500 },
112     };
113     uint32_t qosCount = 1;
114     SoftbusAdapter::GetInstance().OnQosEvent(socket, eventId, qosInfo, qosCount);
115     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnError, nullptr);
116 }
117 
118 HWTEST_F(SoftbusAdapterTest, DmOnSoftbusSessionBind_001, testing::ext::TestSize.Level0)
119 {
120     int32_t socket = 1;
121     std::string str = "132131321345";
122     PeerSocketInfo info = {
123         .name = const_cast<char*>(str.c_str()),
124         .pkgName = const_cast<char*>(str.c_str()),
125         .networkId = const_cast<char*>(str.c_str()),
126     };
127     SoftbusAdapter::GetInstance().iSocketListener_.OnBind(socket, info);
128     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnError, nullptr);
129 }
130 
131 HWTEST_F(SoftbusAdapterTest, DmOnSoftbusSessionClosed_001, testing::ext::TestSize.Level0)
132 {
133     int32_t socket = 1;
134     ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
135     SoftbusAdapter::GetInstance().iSocketListener_.OnShutdown(socket, reason);
136     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
137 }
138 
139 HWTEST_F(SoftbusAdapterTest, DmOnBytesReceived_001, testing::ext::TestSize.Level0)
140 {
141     int32_t socket = 1;
142     std::string data = "data";
143     SoftbusAdapter::GetInstance().iSocketListener_.OnBytes(socket, data.c_str(), data.size());
144     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
145 }
146 
147 HWTEST_F(SoftbusAdapterTest, DmOnStreamReceived_001, testing::ext::TestSize.Level0)
148 {
149     int32_t socket = 1;
150     StreamData data;
151     StreamData exta;
152     StreamFrameInfo frameInfo;
153     SoftbusAdapter::GetInstance().iSocketListener_.OnStream(socket, &data, &exta, &frameInfo);
154     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
155 }
156 
157 HWTEST_F(SoftbusAdapterTest, DmOnMessageReceived_001, testing::ext::TestSize.Level0)
158 {
159     int32_t socket = 1;
160     std::string data = "data";
161     SoftbusAdapter::GetInstance().iSocketListener_.OnMessage(socket, data.c_str(), data.size());
162     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
163 }
164 
165 HWTEST_F(SoftbusAdapterTest, DmOnQosEvent_001, testing::ext::TestSize.Level0)
166 {
167     int32_t socket = 1;
168     QoSEvent eventId = static_cast<QoSEvent>(1);
169     QosTV qosInfo[] = {
170         { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
171         { .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 },
172         { .qos = QOS_TYPE_MIN_LATENCY, .value = 2500 },
173     };
174     uint32_t qosCount = 1;
175     SoftbusAdapter::GetInstance().iSocketListener_.OnQos(socket, eventId, qosInfo, qosCount);
176     EXPECT_EQ(SoftbusAdapter::GetInstance().iSocketListener_.OnFile, nullptr);
177 }
178 } // namespace
179 } // namespace DistributedHardware
180 } // namespace OHOS
181