1 /*
2 * Copyright (C) 2021 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 <gtest/gtest.h>
17 #include "bluetooth_socket.h"
18 #include "bluetooth_gatt_characteristic.h"
19 #include "uuid.h"
20 #include "bluetooth_remote_device.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Bluetooth {
26 class SocketTest : public testing::Test {
27 public:
SocketTest()28 SocketTest()
29 {}
~SocketTest()30 ~SocketTest()
31 {}
32
33 std::shared_ptr<ClientSocket> sppClientSocket_ = nullptr;
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38
39 BluetoothRemoteDevice *pbluetoothRomote_ = nullptr;
40 UUID randomUuid_;
41 UUID insecureUuid_;
42
43 std::shared_ptr<ServerSocket> sppServerSocket_ = nullptr;
44 UUID uuid_;
45 };
46
SetUpTestCase(void)47 void SocketTest::SetUpTestCase(void)
48 {}
TearDownTestCase(void)49 void SocketTest::TearDownTestCase(void)
50 {}
SetUp()51 void SocketTest::SetUp()
52 {
53 pbluetoothRomote_ = new BluetoothRemoteDevice();
54 randomUuid_ = UUID::RandomUUID();
55 sppClientSocket_ = std::make_shared<ClientSocket>(*pbluetoothRomote_, randomUuid_, TYPE_RFCOMM, false);
56 insecureUuid_ = UUID::FromString("00001101-0000-1000-8000-00805F9B34FB");
57
58 uuid_ = UUID::FromString("11111111-0000-1000-8000-00805F9B34FB");
59 if (!sppServerSocket_)
60 GTEST_LOG_(INFO) << "SocketFactory::DataListenRfcommByServiceRecord starts";
61 sppServerSocket_ = SocketFactory::DataListenRfcommByServiceRecord("server", uuid_);
62 }
63
TearDown()64 void SocketTest::TearDown()
65 {
66 delete pbluetoothRomote_;
67 pbluetoothRomote_ = nullptr;
68 }
69
70 /**
71 * @tc.number:Spp_UnitTest001
72 * @tc.name: Connect
73 * @tc.desc:
74 */
75 HWTEST_F(SocketTest, Spp_UnitTest_Connect, TestSize.Level1)
76 {
77 GTEST_LOG_(INFO) << "SppClientSocket::Connect starts";
78 sppClientSocket_->Connect(-1);
79 GTEST_LOG_(INFO) << "SppClientSocket::Connect ends";
80 }
81
82 /**
83 * @tc.number:Spp_UnitTest002
84 * @tc.name: Close
85 * @tc.desc:
86 */
87 HWTEST_F(SocketTest, Spp_UnitTest_Close, TestSize.Level1)
88 {
89 GTEST_LOG_(INFO) << "SppClientSocket::Close starts";
90 sppClientSocket_->Close();
91 GTEST_LOG_(INFO) << "SppClientSocket::Close ends";
92 }
93
94 /**
95 * @tc.number:Spp_UnitTest003
96 * @tc.name: GetInputStream
97 * @tc.desc:
98 */
99 HWTEST_F(SocketTest, Spp_UnitTest_GetInputStream, TestSize.Level1)
100 {
101 int fd = 37;
102 BluetoothRemoteDevice device_;
103 ClientSocket *pfd_SppClientSocket = new ClientSocket(fd, device_.GetDeviceAddr(), TYPE_RFCOMM);
104
105 uint8_t receive[512];
106 int DATASIZE = 1024;
107 ssize_t returnInput = 0;
108 GTEST_LOG_(INFO) << "SppClientSocket::GetInputStream starts";
109 std::shared_ptr<InputStream> input = pfd_SppClientSocket->GetInputStream();
110 returnInput = input->Read(receive, DATASIZE);
111 GTEST_LOG_(INFO) << "SppClientSocket::GetInputStream ends";
112 }
113
114 /**
115 * @tc.number:Spp_UnitTest004
116 * @tc.name: GetOutputStream
117 * @tc.desc:
118 */
119 HWTEST_F(SocketTest, Spp_UnitTest_GetOutputStream, TestSize.Level1)
120 {
121 GTEST_LOG_(INFO) << "SppClientSocket::GetOutputStream starts";
122 uint8_t multiChar[10] = {'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'};
123 int fd = 37;
124 BluetoothRemoteDevice device_;
125 ClientSocket *pfd_SppClientSocket = new ClientSocket(fd, device_.GetDeviceAddr(), TYPE_RFCOMM);
126 std::shared_ptr<OutputStream> output = pfd_SppClientSocket->GetOutputStream();
127 size_t returnOutput = 0;
128 returnOutput = output->Write(multiChar, 10);
129 GTEST_LOG_(INFO) << "SppClientSocket::GetOutputStream ends";
130 }
131
132 /**
133 * @tc.number:Spp_UnitTest005
134 * @tc.name: GetRemoteDevice
135 * @tc.desc:
136 */
137 HWTEST_F(SocketTest, Spp_UnitTest_GetRemoteDevice, TestSize.Level1)
138 {
139 GTEST_LOG_(INFO) << "SppClientSocket::GetRemoteDevice starts";
140 int fd = 37;
141 BluetoothRemoteDevice device_;
142 ClientSocket *pfd_SppClientSocket = new ClientSocket(fd, device_.GetDeviceAddr(), TYPE_RFCOMM);
143 BluetoothRemoteDevice tempRemoteDevice = pfd_SppClientSocket->GetRemoteDevice();
144 GTEST_LOG_(INFO) << "SppClientSocket::GetRemoteDevice ends";
145 }
146
147 /**
148 * @tc.number:Spp_UnitTest006
149 * @tc.name: IsConnected
150 * @tc.desc:
151 */
152 HWTEST_F(SocketTest, Spp_UnitTest_IsConnected, TestSize.Level1)
153 {
154 GTEST_LOG_(INFO) << "SppClientSocket::IsConnected starts";
155 int fd = 37;
156 BluetoothRemoteDevice device_;
157 ClientSocket *pfd_SppClientSocket = new ClientSocket(fd, device_.GetDeviceAddr(), TYPE_RFCOMM);
158 bool IsConnected = false;
159 IsConnected = pfd_SppClientSocket->IsConnected();
160 EXPECT_EQ(IsConnected, true);
161 GTEST_LOG_(INFO) << "SppClientSocket::IsConnected ends";
162 }
163
164 /**
165 * @tc.number:Spp_UnitTest007
166 * @tc.name: GetStringTag
167 * @tc.desc:
168 */
169 HWTEST_F(SocketTest, Spp_UnitTest_GetStringTag, TestSize.Level1)
170 {
171 GTEST_LOG_(INFO) << "SppServerSocket::GetStringTag starts";
172 std::string returnStr{""};
173 returnStr = sppServerSocket_->GetStringTag();
174 GTEST_LOG_(INFO) << "SppServerSocket::GetStringTag ends";
175 }
176
177 /**
178 * @tc.number:Spp_UnitTest008
179 * @tc.name: Accept
180 * @tc.desc:
181 */
182 HWTEST_F(SocketTest, Spp_UnitTest_Accept, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO) << "SppServerSocket::Accept starts";
185
186 int SERTIMEOUT = 10;
187 std::shared_ptr<ClientSocket> preturn_SppClientSocket = sppServerSocket_->Accept(SERTIMEOUT);
188 GTEST_LOG_(INFO) << "SppServerSocket::Accept ends";
189 }
190
191
192
193 } // namespace Bluetooth
194 } // namespace OHOS