forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_socket_bluez_unittest.cc
704 lines (597 loc) · 24.8 KB
/
bluetooth_socket_bluez_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/bluez/bluetooth_socket_bluez.h"
#include <memory>
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_thread.h"
#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
#include "device/bluetooth/bluez/bluetooth_device_bluez.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_input_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
using device::BluetoothAdapter;
using device::BluetoothDevice;
using device::BluetoothSocket;
using device::BluetoothSocketThread;
using device::BluetoothUUID;
namespace {
void DoNothingDBusErrorCallback(const std::string& error_name,
const std::string& error_message) {}
} // namespace
namespace bluez {
class BluetoothSocketBlueZTest : public testing::Test {
public:
BluetoothSocketBlueZTest()
: success_callback_count_(0),
error_callback_count_(0),
last_bytes_sent_(0),
last_bytes_received_(0),
last_reason_(BluetoothSocket::kSystemError) {}
void SetUp() override {
std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
bluez::BluezDBusManager::GetSetterForTesting();
dbus_setter->SetBluetoothAdapterClient(
std::unique_ptr<bluez::BluetoothAdapterClient>(
new bluez::FakeBluetoothAdapterClient));
dbus_setter->SetBluetoothAgentManagerClient(
std::unique_ptr<bluez::BluetoothAgentManagerClient>(
new bluez::FakeBluetoothAgentManagerClient));
dbus_setter->SetBluetoothDeviceClient(
std::unique_ptr<bluez::BluetoothDeviceClient>(
new bluez::FakeBluetoothDeviceClient));
dbus_setter->SetBluetoothGattServiceClient(
std::unique_ptr<bluez::BluetoothGattServiceClient>(
new bluez::FakeBluetoothGattServiceClient));
dbus_setter->SetBluetoothInputClient(
std::unique_ptr<bluez::BluetoothInputClient>(
new bluez::FakeBluetoothInputClient));
dbus_setter->SetBluetoothProfileManagerClient(
std::unique_ptr<bluez::BluetoothProfileManagerClient>(
new bluez::FakeBluetoothProfileManagerClient));
BluetoothSocketThread::Get();
// Grab a pointer to the adapter.
{
base::RunLoop run_loop;
device::BluetoothAdapterFactory::GetAdapter(
base::Bind(&BluetoothSocketBlueZTest::AdapterCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
ASSERT_TRUE(adapter_.get() != nullptr);
ASSERT_TRUE(adapter_->IsInitialized());
ASSERT_TRUE(adapter_->IsPresent());
// Turn on the adapter.
adapter_->SetPowered(true, base::Bind(&base::DoNothing),
base::Bind(&base::DoNothing));
ASSERT_TRUE(adapter_->IsPowered());
}
void TearDown() override {
adapter_ = nullptr;
BluetoothSocketThread::CleanupForTesting();
bluez::BluezDBusManager::Shutdown();
}
void AdapterCallback(base::OnceClosure continuation,
scoped_refptr<BluetoothAdapter> adapter) {
adapter_ = adapter;
std::move(continuation).Run();
}
void SuccessCallback(base::OnceClosure continuation) {
++success_callback_count_;
std::move(continuation).Run();
}
void ErrorCallback(base::OnceClosure continuation,
const std::string& message) {
++error_callback_count_;
last_message_ = message;
std::move(continuation).Run();
}
void ConnectToServiceSuccessCallback(base::OnceClosure continuation,
scoped_refptr<BluetoothSocket> socket) {
++success_callback_count_;
last_socket_ = socket;
std::move(continuation).Run();
}
void SendSuccessCallback(base::OnceClosure continuation, int bytes_sent) {
++success_callback_count_;
last_bytes_sent_ = bytes_sent;
std::move(continuation).Run();
}
void ReceiveSuccessCallback(base::OnceClosure continuation,
int bytes_received,
scoped_refptr<net::IOBuffer> io_buffer) {
++success_callback_count_;
last_bytes_received_ = bytes_received;
last_io_buffer_ = io_buffer;
std::move(continuation).Run();
}
void ReceiveErrorCallback(base::OnceClosure continuation,
BluetoothSocket::ErrorReason reason,
const std::string& error_message) {
++error_callback_count_;
last_reason_ = reason;
last_message_ = error_message;
std::move(continuation).Run();
}
void CreateServiceSuccessCallback(base::OnceClosure continuation,
scoped_refptr<BluetoothSocket> socket) {
++success_callback_count_;
last_socket_ = socket;
std::move(continuation).Run();
}
void AcceptSuccessCallback(base::OnceClosure continuation,
const BluetoothDevice* device,
scoped_refptr<BluetoothSocket> socket) {
++success_callback_count_;
last_device_ = device;
last_socket_ = socket;
std::move(continuation).Run();
}
void ImmediateSuccessCallback() { ++success_callback_count_; }
protected:
base::test::ScopedTaskEnvironment scoped_task_environment_;
scoped_refptr<BluetoothAdapter> adapter_;
unsigned int success_callback_count_;
unsigned int error_callback_count_;
std::string last_message_;
scoped_refptr<BluetoothSocket> last_socket_;
int last_bytes_sent_;
int last_bytes_received_;
scoped_refptr<net::IOBuffer> last_io_buffer_;
BluetoothSocket::ErrorReason last_reason_;
const BluetoothDevice* last_device_;
};
TEST_F(BluetoothSocketBlueZTest, Connect) {
BluetoothDevice* device = adapter_->GetDevice(
bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != nullptr);
{
base::RunLoop run_loop;
device->ConnectToService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the socket for the remainder of the test.
scoped_refptr<BluetoothSocket> socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Send data to the socket, expect all of the data to be sent.
scoped_refptr<net::StringIOBuffer> write_buffer(
new net::StringIOBuffer("test"));
{
base::RunLoop run_loop;
socket->Send(
write_buffer.get(), write_buffer->size(),
base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_EQ(last_bytes_sent_, write_buffer->size());
success_callback_count_ = 0;
error_callback_count_ = 0;
// Receive data from the socket, and fetch the buffer from the callback; since
// the fake is an echo server, we expect to receive what we wrote.
{
base::RunLoop run_loop;
socket->Receive(
4096,
base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_EQ(4, last_bytes_received_);
EXPECT_TRUE(last_io_buffer_.get() != nullptr);
// Take ownership of the received buffer.
scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_;
last_io_buffer_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
std::string data = std::string(read_buffer->data(), last_bytes_received_);
EXPECT_EQ("test", data);
read_buffer = nullptr;
// Receive data again; the socket will have been closed, this should cause a
// disconnected error to be returned via the error callback.
{
base::RunLoop run_loop;
socket->Receive(
4096,
base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(0U, success_callback_count_);
EXPECT_EQ(1U, error_callback_count_);
EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_);
EXPECT_EQ(net::ErrorToString(net::OK), last_message_);
success_callback_count_ = 0;
error_callback_count_ = 0;
// Send data again; since the socket is closed we should get a system error
// equivalent to the connection reset error.
write_buffer = new net::StringIOBuffer("second test");
{
base::RunLoop run_loop;
socket->Send(
write_buffer.get(), write_buffer->size(),
base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(0U, success_callback_count_);
EXPECT_EQ(1U, error_callback_count_);
EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_);
success_callback_count_ = 0;
error_callback_count_ = 0;
// Close our end of the socket.
{
base::RunLoop run_loop;
socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback,
base::Unretained(this),
run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
}
TEST_F(BluetoothSocketBlueZTest, Listen) {
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the socket for the remainder of the test.
scoped_refptr<BluetoothSocket> server_socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Simulate an incoming connection by just calling the ConnectProfile method
// of the underlying fake device client (from the BlueZ point of view,
// outgoing and incoming look the same).
//
// This is done before the Accept() call to simulate a pending call at the
// point that Accept() is called.
bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client =
static_cast<bluez::FakeBluetoothDeviceClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient());
BluetoothDevice* device = adapter_->GetDevice(
bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != nullptr);
{
base::RunLoop run_loop;
fake_bluetooth_device_client->ConnectProfile(
static_cast<BluetoothDeviceBlueZ*>(device)->object_path(),
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback));
run_loop.RunUntilIdle();
}
{
base::RunLoop run_loop;
server_socket->Accept(
base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the client socket for the remainder of the test.
scoped_refptr<BluetoothSocket> client_socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Close our end of the client socket.
{
base::RunLoop run_loop;
client_socket->Disconnect(
base::Bind(&BluetoothSocketBlueZTest::SuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
client_socket = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Run a second connection test, this time calling Accept() before the
// incoming connection comes in.
{
base::RunLoop run_loop1;
// |run_loop2| is expected to be quit in ConnectProfile() through the quit
// closures saved in the Accept() call.
base::RunLoop run_loop2;
server_socket->Accept(
base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
base::Unretained(this), run_loop2.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop2.QuitWhenIdleClosure()));
run_loop1.RunUntilIdle();
fake_bluetooth_device_client->ConnectProfile(
static_cast<BluetoothDeviceBlueZ*>(device)->object_path(),
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback));
run_loop2.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the client socket for the remainder of the test.
client_socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Close our end of the client socket.
{
base::RunLoop run_loop;
client_socket->Disconnect(
base::Bind(&BluetoothSocketBlueZTest::SuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
client_socket = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Now close the server socket.
{
base::RunLoop run_loop;
server_socket->Disconnect(
base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
base::Unretained(this)));
run_loop.RunUntilIdle();
}
EXPECT_EQ(1U, success_callback_count_);
}
TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) {
// Start off with an invisible adapter, register the profile, then make
// the adapter visible.
bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
static_cast<bluez::FakeBluetoothAdapterClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
fake_bluetooth_adapter_client->SetVisible(false);
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the socket for the remainder of the test.
scoped_refptr<BluetoothSocket> socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// But there shouldn't be a profile registered yet.
bluez::FakeBluetoothProfileManagerClient*
fake_bluetooth_profile_manager_client =
static_cast<bluez::FakeBluetoothProfileManagerClient*>(
bluez::BluezDBusManager::Get()
->GetBluetoothProfileManagerClient());
bluez::FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider == nullptr);
// Make the adapter visible. This should register a profile.
{
base::RunLoop run_loop;
fake_bluetooth_adapter_client->SetVisible(true);
run_loop.RunUntilIdle();
}
profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider != nullptr);
// Cleanup the socket.
{
base::RunLoop run_loop;
socket->Disconnect(
base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
base::Unretained(this)));
run_loop.RunUntilIdle();
}
EXPECT_EQ(1U, success_callback_count_);
}
TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) {
// The fake adapter starts off visible by default.
bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
static_cast<bluez::FakeBluetoothAdapterClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take ownership of the socket for the remainder of the test.
scoped_refptr<BluetoothSocket> socket = last_socket_;
last_socket_ = nullptr;
success_callback_count_ = 0;
error_callback_count_ = 0;
// Make sure the profile was registered with the daemon.
bluez::FakeBluetoothProfileManagerClient*
fake_bluetooth_profile_manager_client =
static_cast<bluez::FakeBluetoothProfileManagerClient*>(
bluez::BluezDBusManager::Get()
->GetBluetoothProfileManagerClient());
bluez::FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider != nullptr);
// Make the adapter invisible, and fiddle with the profile fake to unregister
// the profile since this doesn't happen automatically.
{
base::RunLoop run_loop;
fake_bluetooth_adapter_client->SetVisible(false);
run_loop.RunUntilIdle();
}
// Then make the adapter visible again. This should re-register the profile.
{
base::RunLoop run_loop;
fake_bluetooth_adapter_client->SetVisible(true);
run_loop.RunUntilIdle();
}
profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider != nullptr);
// Cleanup the socket.
{
base::RunLoop run_loop;
socket->Disconnect(
base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
base::Unretained(this)));
run_loop.RunUntilIdle();
}
EXPECT_EQ(1U, success_callback_count_);
}
TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) {
BluetoothDevice* device = adapter_->GetDevice(
bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress);
ASSERT_TRUE(device != nullptr);
{
base::RunLoop run_loop;
device->ConnectToService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(0U, success_callback_count_);
EXPECT_EQ(1U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() == nullptr);
{
base::RunLoop run_loop;
device->ConnectToService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(0U, success_callback_count_);
EXPECT_EQ(2U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() == nullptr);
}
TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) {
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(0U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take control of this socket.
scoped_refptr<BluetoothSocket> server_socket;
server_socket.swap(last_socket_);
{
base::RunLoop run_loop;
server_socket->Accept(
base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
server_socket->Close();
server_socket = nullptr;
run_loop.RunUntilIdle();
}
EXPECT_EQ(1U, success_callback_count_);
EXPECT_EQ(1U, error_callback_count_);
{
base::RunLoop run_loop;
adapter_->CreateRfcommService(
BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
BluetoothAdapter::ServiceOptions(),
base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
run_loop.Run();
}
EXPECT_EQ(2U, success_callback_count_);
EXPECT_EQ(1U, error_callback_count_);
EXPECT_TRUE(last_socket_.get() != nullptr);
// Take control of this socket.
server_socket.swap(last_socket_);
{
base::RunLoop run_loop;
server_socket->Accept(
base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()),
base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
server_socket->Close();
server_socket = nullptr;
run_loop.RunUntilIdle();
}
EXPECT_EQ(2U, success_callback_count_);
EXPECT_EQ(2U, error_callback_count_);
}
} // namespace bluez