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 #define LOG_TAG "SoftbusAdapterStandardTest"
16 
17 #include "app_device_change_listener.h"
18 #include <cstdint>
19 #include "gtest/gtest.h"
20 #include <iostream>
21 #include "log_print.h"
22 #include "softbus_adapter.h"
23 #include "types.h"
24 #include <unistd.h>
25 #include <vector>
26 
27 namespace OHOS::Test {
28 using namespace testing::ext;
29 using namespace OHOS::AppDistributedKv;
30 using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo;
31 class AppDataChangeListenerImpl : public AppDataChangeListener {
32     struct ServerSocketInfo {
33         std::string name;      /**< Peer socket name */
34         std::string networkId; /**< Peer network ID */
35         std::string pkgName;   /**< Peer package name */
36     };
37 
38     void OnMessage(const OHOS::AppDistributedKv::DeviceInfo &info, const uint8_t *ptr, const int size,
39         const struct PipeInfo &id) const override;
40 };
41 
OnMessage(const OHOS::AppDistributedKv::DeviceInfo & info,const uint8_t * ptr,const int size,const struct PipeInfo & id) const42 void AppDataChangeListenerImpl::OnMessage(const OHOS::AppDistributedKv::DeviceInfo &info,
43     const uint8_t *ptr, const int size, const struct PipeInfo &id) const
44 {
45     ZLOGI("data  %{public}s  %s", info.deviceName.c_str(), ptr);
46 }
47 
48 class SoftbusAdapterStandardTest : public testing::Test {
49 public:
SetUpTestCase(void)50     static void SetUpTestCase(void) {}
TearDownTestCase(void)51     static void TearDownTestCase(void) {}
SetUp()52     void SetUp() {}
TearDown()53     void TearDown() {}
54 protected:
55     static constexpr uint32_t DEFAULT_MTU_SIZE = 4096 * 1024u;
56     static constexpr uint32_t DEFAULT_TIMEOUT = 30 * 1000;
57 };
58 
59 /**
60 * @tc.name: StartWatchDeviceChange
61 * @tc.desc: start watch data change
62 * @tc.type: FUNC
63 * @tc.require:
64 * @tc.author: nhj
65  */
66 HWTEST_F(SoftbusAdapterStandardTest, StartWatchDeviceChange, TestSize.Level0)
67 {
68     auto status = SoftBusAdapter::GetInstance()->StartWatchDataChange(nullptr, {});
69     EXPECT_EQ(status, Status::INVALID_ARGUMENT);
70 }
71 
72 /**
73 * @tc.name: StartWatchDeviceChange
74 * @tc.desc: start watch data change
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author: nhj
78  */
79 HWTEST_F(SoftbusAdapterStandardTest, StartWatchDeviceChange01, TestSize.Level0)
80 {
81     PipeInfo appId;
82     appId.pipeId = "appId";
83     appId.userId = "groupId";
84     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
85     auto status = SoftBusAdapter::GetInstance()->StartWatchDataChange(dataListener, appId);
86     EXPECT_EQ(status, Status::SUCCESS);
87 }
88 
89 /**
90 * @tc.name: StartWatchDeviceChange
91 * @tc.desc: start watch data change
92 * @tc.type: FUNC
93 * @tc.require:
94 * @tc.author: nhj
95  */
96 HWTEST_F(SoftbusAdapterStandardTest, StartWatchDeviceChange02, TestSize.Level0)
97 {
98     PipeInfo appId;
99     appId.pipeId = "";
100     appId.userId = "groupId";
101     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
102     auto status = SoftBusAdapter::GetInstance()->StartWatchDataChange(dataListener, appId);
103     EXPECT_EQ(status, Status::SUCCESS);
104 }
105 
106 /**
107 * @tc.name: StopWatchDataChange
108 * @tc.desc: stop watch data change
109 * @tc.type: FUNC
110 * @tc.require:
111 * @tc.author: nhj
112  */
113 HWTEST_F(SoftbusAdapterStandardTest, StopWatchDataChange, TestSize.Level0)
114 {
115     PipeInfo appId;
116     appId.pipeId = "appId";
117     appId.userId = "groupId";
118     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
119     auto status = SoftBusAdapter::GetInstance()->StopWatchDataChange(dataListener, appId);
120     EXPECT_EQ(status, Status::SUCCESS);
121 }
122 
123 /**
124 * @tc.name: StopWatchDataChange
125 * @tc.desc: stop watch data change
126 * @tc.type: FUNC
127 * @tc.require:
128 * @tc.author: nhj
129  */
130 HWTEST_F(SoftbusAdapterStandardTest, StopWatchDataChange01, TestSize.Level0)
131 {
132     PipeInfo appId;
133     appId.pipeId = "";
134     appId.userId = "groupId";
135     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
136     auto status = SoftBusAdapter::GetInstance()->StopWatchDataChange(dataListener, appId);
137     EXPECT_EQ(status, Status::SUCCESS);
138 }
139 
140 /**
141 * @tc.name: SendData
142 * @tc.desc: parse sent data
143 * @tc.type: FUNC
144 * @tc.author: nhj
145 */
146 HWTEST_F(SoftbusAdapterStandardTest, SendData, TestSize.Level1)
147 {
148     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
149     PipeInfo id;
150     id.pipeId = "appId";
151     id.userId = "groupId";
152     auto secRegister = SoftBusAdapter::GetInstance()->StartWatchDataChange(dataListener, id);
153     EXPECT_EQ(Status::SUCCESS, secRegister);
154     std::string content = "Helloworlds";
155     const uint8_t *t = reinterpret_cast<const uint8_t*>(content.c_str());
156     DeviceId di = {"DeviceId"};
157     DataInfo data = { const_cast<uint8_t *>(t), static_cast<uint32_t>(content.length())};
158     auto status = SoftBusAdapter::GetInstance()->SendData(id, di, data, 11, { MessageType::DEFAULT });
159     EXPECT_NE(status.first, Status::SUCCESS);
160     SoftBusAdapter::GetInstance()->StopWatchDataChange(dataListener, id);
161     delete dataListener;
162 }
163 
164 /**
165 * @tc.name: GetMtuSize
166 * @tc.desc: get size
167 * @tc.type: FUNC
168 * @tc.author: nhj
169 */
170 HWTEST_F(SoftbusAdapterStandardTest, GetMtuSize, TestSize.Level1)
171 {
172     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
173     PipeInfo id;
174     id.pipeId = "appId";
175     id.userId = "groupId";
176     SoftBusAdapter::GetInstance()->StartWatchDataChange(dataListener, id);
177     DeviceId di = {"DeviceId"};
178     auto size = SoftBusAdapter::GetInstance()->GetMtuSize(di);
179     EXPECT_EQ(size, DEFAULT_MTU_SIZE);
180     SoftBusAdapter::GetInstance()->GetCloseSessionTask();
181     SoftBusAdapter::GetInstance()->StopWatchDataChange(dataListener, id);
182     delete dataListener;
183 }
184 
185 /**
186 * @tc.name: GetTimeout
187 * @tc.desc: get timeout
188 * @tc.type: FUNC
189 * @tc.author: nhj
190 */
191 HWTEST_F(SoftbusAdapterStandardTest, GetTimeout, TestSize.Level1)
192 {
193     const AppDataChangeListenerImpl *dataListener = new AppDataChangeListenerImpl();
194     PipeInfo id;
195     id.pipeId = "appId01";
196     id.userId = "groupId01";
197     SoftBusAdapter::GetInstance()->StartWatchDataChange(dataListener, id);
198     DeviceId di = {"DeviceId"};
199     auto time = SoftBusAdapter::GetInstance()->GetTimeout(di);
200     EXPECT_EQ(time, DEFAULT_TIMEOUT);
201     SoftBusAdapter::GetInstance()->StopWatchDataChange(dataListener, id);
202     delete dataListener;
203 }
204 
205 /**
206 * @tc.name: IsSameStartedOnPeer
207 * @tc.desc: get size
208 * @tc.type: FUNC
209 * @tc.author: nhj
210 */
211 HWTEST_F(SoftbusAdapterStandardTest, IsSameStartedOnPeer, TestSize.Level1)
212 {
213     PipeInfo id;
214     id.pipeId = "appId01";
215     id.userId = "groupId01";
216     DeviceId di = {"DeviceId"};
217     SoftBusAdapter::GetInstance()->SetMessageTransFlag(id, true);
218     auto status = SoftBusAdapter::GetInstance()->IsSameStartedOnPeer(id, di);
219     EXPECT_EQ(status, true);
220 }
221 } // namespace OHOS::Test