• 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 
16 #include <memory>
17 
18 #include <securec.h>
19 #include "gtest/gtest.h"
20 #define private public
21 #include "match_box.h"
22 #include "remove_box.h"
23 #include "distributed_server.h"
24 #include "distributed_client.h"
25 #include "distributed_send_adapter.h"
26 #include "analytics_util.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::DistributedHardware;
30 
31 namespace OHOS {
32 namespace Notification {
33 class SoftbusTest : public testing::Test {
34 public:
SetUp()35     void SetUp() override {};
TearDown()36     void TearDown() override {};
37 };
38 
39 /**
40  * @tc.name   : init socket server.
41  * @tc.number : SoftbusTest_0100
42  * @tc.desc   : test soft bus interface.
43  */
44 HWTEST_F(SoftbusTest, SoftbusTest_0100, Function | SmallTest | Level1)
45 {
46     // init server
47     int32_t result = DistributedServer::GetInstance().InitServer("phone",
48         DmDeviceType::DEVICE_TYPE_PHONE);
49     EXPECT_EQ(result, 0);
50     auto sockets = DistributedServer::GetInstance().serverSocket_;
51     EXPECT_EQ((int32_t)sockets.size(), 1);
52 
53     // release server
54     DistributedServer::GetInstance().ReleaseServer();
55     sockets = DistributedServer::GetInstance().serverSocket_;
56     EXPECT_EQ((int32_t)sockets.size(), 0);
57 
58     // init server
59     result = DistributedServer::GetInstance().InitServer("watch",
60         DmDeviceType::DEVICE_TYPE_WATCH);
61     EXPECT_EQ(result, 0);
62     sockets = DistributedServer::GetInstance().serverSocket_;
63     EXPECT_EQ((int32_t)sockets.size(), 2);
64 
65     // receive peer bind
66     char name[10] = { "name" };
67     char networkId[10] = { "networkId" };
68     char pkgName[10] = { "pkgName" };
69     PeerSocketInfo info;
70     info.name = name;
71     info.networkId = networkId;
72     info.pkgName = pkgName;
73     DistributedServer::GetInstance().OnBind(10, info);
74     DistributedServer::GetInstance().OnBind(11, info);
75 
76     char data[10] = { "0" };
77     // receive peer byte
78     DistributedServer::GetInstance().OnBytes(10, data, 10);
79     // receive peer message
80     DistributedServer::GetInstance().OnMessage(10, data, 10);
81 
82     auto peerSockets = DistributedServer::GetInstance().peerSockets_;
83     EXPECT_EQ((int32_t)peerSockets.size(), 2);
84 
85     // receive peer shotdwon
86     DistributedServer::GetInstance().OnShutdown(11, ShutdownReason::SHUTDOWN_REASON_PEER);
87     peerSockets = DistributedServer::GetInstance().peerSockets_;
88     EXPECT_EQ((int32_t)peerSockets.size(), 1);
89 }
90 
91 
92 /**
93  * @tc.name   : init socket client.
94  * @tc.number : SoftbusTest_0101
95  * @tc.desc   : test soft bus interface.
96  */
97 HWTEST_F(SoftbusTest, SoftbusTest_0101, Function | SmallTest | Level1)
98 {
99     DistributedServer::GetInstance().serverSocket_.clear();
100     // add device
101     DistributedDeviceInfo peerDevice;
102     peerDevice.deviceId_ = "deviceId";
103     peerDevice.networkId_ = "networkId";
104     peerDevice.deviceType_ = DmDeviceType::DEVICE_TYPE_WATCH;
105     DistributedClient::GetInstance().AddDevice(peerDevice);
106     auto sockets = DistributedServer::GetInstance().serverSocket_;
107     EXPECT_EQ((int32_t)sockets.size(), 0);
108 
109     // get socket id
110     auto data = std::make_shared<NotifticationMatchBox>();
111     data->SetLocalDeviceType(16);
112     data->SetLocalDeviceId("local_device");
113     data->Serialize();
114 
115     int32_t socketId = 0;
116     int32_t result = DistributedClient::GetInstance().SendMessage(data,
117         TransDataType::DATA_TYPE_MESSAGE, "deviceId", 0);
118     EXPECT_EQ(result, 0);
119     result = DistributedClient::GetInstance().SendMessage(data,
120         TransDataType::DATA_TYPE_BYTES, "deviceId", 0);
121     EXPECT_EQ(result, 0);
122 
123     auto remove = std::make_shared<NotificationRemoveBox>();
124     remove->SetNotificationHashCode("hashcode");
125     remove->Serialize();
126     result = DistributedClient::GetInstance().SendMessage(remove,
127         TransDataType::DATA_TYPE_BYTES, "deviceId", 0);
128     EXPECT_EQ(result, 0);
129 
130     auto socketsId = DistributedClient::GetInstance().socketsId_;
131     EXPECT_EQ((int32_t)socketsId.size(), 2);
132 
133     // release device
134     DistributedClient::GetInstance().ReleaseDevice("deviceId", DmDeviceType::DEVICE_TYPE_WATCH);
135 }
136 
137 /**
138  * @tc.name   : test send adapter.
139  * @tc.number : SoftbusTest_0102
140  * @tc.desc   : test soft bus adapter.
141  */
142 HWTEST_F(SoftbusTest, SoftbusTest_0102, Function | SmallTest | Level1)
143 {
144     DistributedClient::GetInstance().socketsId_.clear();
145 
146     DistributedDeviceInfo peerDevice;
147     peerDevice.deviceId_ = "deviceId";
148     peerDevice.networkId_ = "networkId";
149     peerDevice.deviceType_ = DmDeviceType::DEVICE_TYPE_WATCH;
150     DistributedClient::GetInstance().AddDevice(peerDevice);
151 
152     // make data
153     auto data = std::make_shared<NotifticationMatchBox>();
154     data->SetLocalDeviceType(16);
155     data->SetLocalDeviceId("local_device");
156     data->Serialize();
157 
158     std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(data, peerDevice,
159         TransDataType::DATA_TYPE_MESSAGE, MODIFY_ERROR_EVENT_CODE);
160     DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
161     sleep(2);
162     auto socketsId = DistributedClient::GetInstance().socketsId_;
163     EXPECT_EQ((int32_t)socketsId.size(), 1);
164 }
165 
166 }  // namespace Notification
167 }  // namespace OHOS
168