File: floss_socket_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (632 lines) | stat: -rw-r--r-- 23,702 bytes parent folder | download | duplicates (9)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/floss/floss_socket_manager.h"

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_exported_object.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/floss/floss_manager_client.h"
#include "device/bluetooth/floss/test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using testing::_;

}  // namespace

namespace floss {

using Security = FlossSocketManager::Security;
using BtifStatus = FlossDBusClient::BtifStatus;

class FlossSocketManagerTest : public testing::Test {
 public:
  FlossSocketManagerTest() = default;

  base::Version GetCurrVersion() {
    return floss::version::GetMaximalSupportedVersion();
  }

  void SetUpMocks() {
    adapter_path_ = FlossDBusClient::GenerateAdapterPath(adapter_index_);
    sockmgr_proxy_ = base::MakeRefCounted<::dbus::MockObjectProxy>(
        bus_.get(), kSocketManagerInterface, adapter_path_);
    exported_callbacks_ = base::MakeRefCounted<::dbus::MockExportedObject>(
        bus_.get(),
        ::dbus::ObjectPath(FlossSocketManager::kExportedCallbacksPath));

    EXPECT_CALL(*bus_.get(),
                GetObjectProxy(kSocketManagerInterface, adapter_path_))
        .WillRepeatedly(::testing::Return(sockmgr_proxy_.get()));
    EXPECT_CALL(*bus_.get(), GetExportedObject)
        .WillRepeatedly(::testing::Return(exported_callbacks_.get()));

    // Make sure we export all callbacks. This will need to be updated once new
    // callbacks are added.
    EXPECT_CALL(*exported_callbacks_.get(), ExportMethod).Times(4);

    // Handle method calls on the object proxy.
    ON_CALL(*sockmgr_proxy_.get(),
            DoCallMethodWithErrorResponse(
                HasMemberOf(socket_manager::kRegisterCallback), _, _))
        .WillByDefault(
            Invoke(this, &FlossSocketManagerTest::HandleRegisterCallback));
  }

  void SetUp() override {
    ::dbus::Bus::Options options;
    options.bus_type = ::dbus::Bus::BusType::SYSTEM;
    bus_ = base::MakeRefCounted<::dbus::MockBus>(options);
    sockmgr_ = FlossSocketManager::Create();

    SetUpMocks();
  }

  void TearDown() override {
    // Expected call to UnregisterCallback when client is destroyed
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(
                    HasMemberOf(socket_manager::kUnregisterCallback), _, _))
        .WillOnce([this](::dbus::MethodCall* method_call, int timeout_ms,
                         ::dbus::ObjectProxy::ResponseOrErrorCallback* cb) {
          dbus::MessageReader msg(method_call);
          // D-Bus method call should have 1 parameter.
          uint32_t param1;
          ASSERT_TRUE(FlossDBusClient::ReadAllDBusParams(&msg, &param1));
          EXPECT_EQ(this->callback_id_ctr_ - 1, param1);
          EXPECT_FALSE(msg.HasMoreData());
        });

    // Clean up the socket manager first to get rid of all references to various
    // buses, object proxies, etc.
    sockmgr_.reset();
  }

  void Init() {
    sockmgr_->Init(bus_.get(), kSocketManagerInterface, adapter_index_,
                   GetCurrVersion(), base::DoNothing());
  }

  void SetupListeningSocket() {
    // First listen on something. This will push the socket callbacks into a
    // map.
    EXPECT_CALL(
        *sockmgr_proxy_.get(),
        DoCallMethodWithErrorResponse(
            HasMemberOf(socket_manager::kListenUsingRfcommWithServiceRecord), _,
            _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    sockmgr_->ListenUsingRfcomm(
        "Foo", device::BluetoothUUID("F00D"), Security::kSecure,
        base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                       weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionStateChanged,
                            weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionAccepted,
                            weak_ptr_factory_.GetWeakPtr()));

    // We should call accept here but that state is tracked on the daemon side.
    // Opting not to simply because we have it mocked away...
  }

  void HandleRegisterCallback(
      ::dbus::MethodCall* method_call,
      int timeout_ms,
      ::dbus::ObjectProxy::ResponseOrErrorCallback* cb) {
    auto response = ::dbus::Response::CreateEmpty();
    ::dbus::MessageWriter msg(response.get());
    FlossDBusClient::WriteAllDBusParams(&msg, callback_id_ctr_);

    // Increment callback counter for next call.
    callback_id_ctr_++;

    std::move(*cb).Run(response.get(), nullptr);
  }

  void HandleReturnSocketResult(
      ::dbus::MethodCall* method_call,
      int timeout_ms,
      ::dbus::ObjectProxy::ResponseOrErrorCallback* cb) {
    auto response = ::dbus::Response::CreateEmpty();
    ::dbus::MessageWriter msg(response.get());
    FlossSocketManager::SocketResult result = {
        .status = BtifStatus::kSuccess,
        .id = socket_id_ctr_,
    };
    FlossDBusClient::WriteAllDBusParams(&msg, result);

    socket_id_ctr_++;

    std::move(*cb).Run(response.get(), nullptr);
  }

  void HandleReturnSuccess(::dbus::MethodCall* method_call,
                           int timeout_ms,
                           ::dbus::ObjectProxy::ResponseOrErrorCallback* cb) {
    auto response = ::dbus::Response::CreateEmpty();
    ::dbus::MessageWriter msg(response.get());

    BtifStatus status = BtifStatus::kSuccess;
    FlossDBusClient::WriteAllDBusParams(&msg, status);

    std::move(*cb).Run(response.get(), nullptr);
  }

  void SendOutgoingConnectionResult(
      FlossSocketManager::SocketId id,
      BtifStatus status,
      const std::optional<FlossSocketManager::FlossSocket>& socket,
      dbus::ExportedObject::ResponseSender response) {
    dbus::MethodCall method_call(socket_manager::kCallbackInterface,
                                 socket_manager::kOnOutgoingConnectionResult);
    method_call.SetSerial(serial_++);
    dbus::MessageWriter writer(&method_call);
    FlossDBusClient::WriteAllDBusParams(&writer, id, status, socket);

    sockmgr_->OnOutgoingConnectionResult(&method_call, std::move(response));
  }

  void SendIncomingSocketReady(
      const FlossSocketManager::FlossListeningSocket& server_socket,
      BtifStatus status,
      dbus::ExportedObject::ResponseSender response) {
    dbus::MethodCall method_call(socket_manager::kCallbackInterface,
                                 socket_manager::kOnIncomingSocketReady);
    method_call.SetSerial(serial_++);
    dbus::MessageWriter writer(&method_call);
    FlossDBusClient::WriteAllDBusParams(&writer, server_socket, status);

    sockmgr_->OnIncomingSocketReady(&method_call, std::move(response));
  }

  void SendIncomingSocketClosed(FlossSocketManager::SocketId id,
                                BtifStatus status,
                                dbus::ExportedObject::ResponseSender response) {
    dbus::MethodCall method_call(socket_manager::kCallbackInterface,
                                 socket_manager::kOnIncomingSocketReady);
    method_call.SetSerial(serial_++);
    dbus::MessageWriter writer(&method_call);
    FlossDBusClient::WriteAllDBusParams(&writer, id, status);

    sockmgr_->OnIncomingSocketClosed(&method_call, std::move(response));
  }

  void SendIncomingConnection(FlossSocketManager::SocketId id,
                              const FlossSocketManager::FlossSocket& socket,
                              dbus::ExportedObject::ResponseSender response) {
    dbus::MethodCall method_call(socket_manager::kCallbackInterface,
                                 socket_manager::kOnIncomingSocketReady);
    method_call.SetSerial(serial_++);
    dbus::MessageWriter writer(&method_call);
    FlossDBusClient::WriteAllDBusParams(&writer, id, socket);

    sockmgr_->OnHandleIncomingConnection(&method_call, std::move(response));
  }

  void SockStatusCb(DBusResult<BtifStatus> result) {
    if (!result.has_value()) {
      last_status_ = BtifStatus::kFail;
    } else {
      last_status_ = *result;
    }
  }

  void SockConnectionStateChanged(
      FlossSocketManager::ServerSocketState state,
      FlossSocketManager::FlossListeningSocket socket,
      BtifStatus status) {
    last_state_ = state;
    last_server_socket_ = socket;
    last_status_ = status;
  }

  void SockConnectionAccepted(FlossSocketManager::FlossSocket&& socket) {
    last_incoming_socket_ = std::move(socket);
  }

  void ExpectNormalResponse(std::unique_ptr<dbus::Response> response) {
    EXPECT_NE(response->GetMessageType(),
              dbus::Message::MessageType::MESSAGE_ERROR);
  }

  int adapter_index_ = 2;
  int serial_ = 1;
  dbus::ObjectPath adapter_path_;

  FlossSocketManager::ServerSocketState last_state_;
  FlossSocketManager::FlossListeningSocket last_server_socket_;
  BtifStatus last_status_;
  FlossSocketManager::FlossSocket last_incoming_socket_;

  uint32_t callback_id_ctr_ = 1;
  uint64_t socket_id_ctr_ = 1;

  scoped_refptr<::dbus::MockBus> bus_;
  scoped_refptr<::dbus::MockExportedObject> exported_callbacks_;
  scoped_refptr<::dbus::MockObjectProxy> sockmgr_proxy_;
  std::unique_ptr<FlossSocketManager> sockmgr_;

  base::test::TaskEnvironment task_environment_;
  base::WeakPtrFactory<FlossSocketManagerTest> weak_ptr_factory_{this};
};

// Tests for good path

TEST_F(FlossSocketManagerTest, ListenOnSockets) {
  Init();

  std::map<std::string, Security> l2cap_apis = {
      {socket_manager::kListenUsingInsecureL2capChannel, Security::kInsecure},
      {socket_manager::kListenUsingL2capChannel, Security::kSecure},
  };
  std::map<std::string, Security> l2cap_le_apis = {
      {socket_manager::kListenUsingInsecureL2capLeChannel, Security::kInsecure},
      {socket_manager::kListenUsingL2capLeChannel, Security::kSecure},
  };
  std::map<std::string, Security> rfcomm_apis = {
      {socket_manager::kListenUsingInsecureRfcommWithServiceRecord,
       Security::kInsecure},
      {socket_manager::kListenUsingRfcommWithServiceRecord, Security::kSecure},
  };

  // Exercise all security paths.
  for (auto kv : l2cap_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    last_status_ = BtifStatus::kNotReady;
    sockmgr_->ListenUsingL2cap(
        kv.second,
        base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                       weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionStateChanged,
                            weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionAccepted,
                            weak_ptr_factory_.GetWeakPtr()));

    EXPECT_EQ(BtifStatus::kSuccess, last_status_);
  }
  for (auto kv : l2cap_le_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    last_status_ = BtifStatus::kNotReady;
    sockmgr_->ListenUsingL2capLe(
        kv.second,
        base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                       weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionStateChanged,
                            weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionAccepted,
                            weak_ptr_factory_.GetWeakPtr()));

    EXPECT_EQ(BtifStatus::kSuccess, last_status_);
  }

  for (auto kv : rfcomm_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    last_status_ = BtifStatus::kNotReady;
    sockmgr_->ListenUsingRfcomm(
        "Foo", device::BluetoothUUID(), kv.second,
        base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                       weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionStateChanged,
                            weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(&FlossSocketManagerTest::SockConnectionAccepted,
                            weak_ptr_factory_.GetWeakPtr()));

    EXPECT_EQ(BtifStatus::kSuccess, last_status_);
  }
}

TEST_F(FlossSocketManagerTest, ConnectToSockets) {
  Init();

  std::map<std::string, Security> l2cap_apis = {
      {socket_manager::kCreateInsecureL2capChannel, Security::kInsecure},
      {socket_manager::kCreateL2capChannel, Security::kSecure},
  };

  std::map<std::string, Security> l2cap_le_apis = {
      {socket_manager::kCreateInsecureL2capLeChannel, Security::kInsecure},
      {socket_manager::kCreateL2capLeChannel, Security::kSecure},
  };

  std::map<std::string, Security> rfcomm_apis = {
      {socket_manager::kCreateInsecureRfcommSocketToServiceRecord,
       Security::kInsecure},
      {socket_manager::kCreateRfcommSocketToServiceRecord, Security::kSecure},
  };

  FlossDeviceId remote_device = {
      .address = "00:11:22:33:44:55",
      .name = "Remote device",
  };

  int psm = 42;

  device::BluetoothUUID uuid("f0de");

  for (auto kv : l2cap_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    bool callback_completed = false;
    BtifStatus callback_status = BtifStatus::kNotReady;
    int found_psm = -1;

    sockmgr_->ConnectUsingL2cap(
        remote_device, psm, kv.second,
        base::BindOnce(
            [](bool* complete, BtifStatus* cb_status, int* fpsm,
               BtifStatus status,
               std::optional<FlossSocketManager::FlossSocket>&& socket) {
              *complete = true;
              *cb_status = status;
              if (socket) {
                *fpsm = socket->port;
              }
            },
            &callback_completed, &callback_status, &found_psm));

    // Status shouldn't be updated yet since we get callback update AFTER we
    // send outgoing result.
    EXPECT_FALSE(callback_completed);
    EXPECT_EQ(BtifStatus::kNotReady, callback_status);

    std::optional<FlossSocketManager::FlossSocket> sock =
        FlossSocketManager::FlossSocket();
    sock->id = socket_id_ctr_ - 1;
    sock->port = psm;

    // Trigger the callback completion. We don't care about socket itself.
    SendOutgoingConnectionResult(
        socket_id_ctr_ - 1, BtifStatus::kSuccess, std::move(sock),
        base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                       weak_ptr_factory_.GetWeakPtr()));

    EXPECT_TRUE(callback_completed);
    EXPECT_EQ(BtifStatus::kSuccess, callback_status);
    EXPECT_EQ(psm, found_psm);
  }

  for (auto kv : l2cap_le_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    bool callback_completed = false;
    BtifStatus callback_status = BtifStatus::kNotReady;
    int found_psm = -1;

    sockmgr_->ConnectUsingL2capLe(
        remote_device, psm, kv.second,
        base::BindOnce(
            [](bool* complete, BtifStatus* cb_status, int* fpsm,
               BtifStatus status,
               std::optional<FlossSocketManager::FlossSocket>&& socket) {
              *complete = true;
              *cb_status = status;
              if (socket) {
                *fpsm = socket->port;
              }
            },
            &callback_completed, &callback_status, &found_psm));

    // Status shouldn't be updated yet since we get callback update AFTER we
    // send outgoing result.
    EXPECT_FALSE(callback_completed);
    EXPECT_EQ(BtifStatus::kNotReady, callback_status);

    std::optional<FlossSocketManager::FlossSocket> sock =
        FlossSocketManager::FlossSocket();
    sock->id = socket_id_ctr_ - 1;
    sock->port = psm;

    // Trigger the callback completion. We don't care about socket itself.
    SendOutgoingConnectionResult(
        socket_id_ctr_ - 1, BtifStatus::kSuccess, std::move(sock),
        base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                       weak_ptr_factory_.GetWeakPtr()));

    EXPECT_TRUE(callback_completed);
    EXPECT_EQ(BtifStatus::kSuccess, callback_status);
    EXPECT_EQ(psm, found_psm);
  }

  for (auto kv : rfcomm_apis) {
    EXPECT_CALL(*sockmgr_proxy_.get(),
                DoCallMethodWithErrorResponse(HasMemberOf(kv.first), _, _))
        .WillOnce(
            Invoke(this, &FlossSocketManagerTest::HandleReturnSocketResult));

    bool callback_completed = false;
    BtifStatus callback_status = BtifStatus::kNotReady;
    device::BluetoothUUID found_uuid;

    sockmgr_->ConnectUsingRfcomm(
        remote_device, uuid, kv.second,
        base::BindOnce(
            [](bool* complete, BtifStatus* cb_status, device::BluetoothUUID* uu,
               BtifStatus status,
               std::optional<FlossSocketManager::FlossSocket>&& socket) {
              *complete = true;
              *cb_status = status;
              if (socket && socket->uuid) {
                *uu = *socket->uuid;
              }
            },
            &callback_completed, &callback_status, &found_uuid));

    // Status shouldn't be updated yet since we get callback update AFTER we
    // send outgoing result.
    EXPECT_FALSE(callback_completed);
    EXPECT_EQ(BtifStatus::kNotReady, callback_status);

    std::optional<FlossSocketManager::FlossSocket> sock =
        FlossSocketManager::FlossSocket();
    sock->id = socket_id_ctr_ - 1;
    sock->uuid = uuid;

    // Trigger the callback completion. We don't care about socket itself.
    SendOutgoingConnectionResult(
        socket_id_ctr_ - 1, BtifStatus::kSuccess, std::move(sock),
        base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                       weak_ptr_factory_.GetWeakPtr()));

    EXPECT_TRUE(callback_completed);
    EXPECT_EQ(BtifStatus::kSuccess, callback_status);
    EXPECT_EQ(uuid, found_uuid);
  }
}

// Really basic calls to accept and close
TEST_F(FlossSocketManagerTest, AcceptAndCloseConnection) {
  Init();

  EXPECT_CALL(
      *sockmgr_proxy_.get(),
      DoCallMethodWithErrorResponse(HasMemberOf(socket_manager::kAccept), _, _))
      .WillOnce(Invoke(this, &FlossSocketManagerTest::HandleReturnSuccess));

  last_status_ = BtifStatus::kNotReady;
  sockmgr_->Accept(socket_id_ctr_, 42,
                   base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                                  weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kSuccess, last_status_);

  EXPECT_CALL(
      *sockmgr_proxy_.get(),
      DoCallMethodWithErrorResponse(HasMemberOf(socket_manager::kClose), _, _))
      .WillOnce(Invoke(this, &FlossSocketManagerTest::HandleReturnSuccess));

  last_status_ = BtifStatus::kNotReady;
  sockmgr_->Close(socket_id_ctr_,
                  base::BindOnce(&FlossSocketManagerTest::SockStatusCb,
                                 weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kSuccess, last_status_);
}

// Handle state changes from calling accept and close.
TEST_F(FlossSocketManagerTest, IncomingStateChanges) {
  Init();
  SetupListeningSocket();

  // With a bad id, callbacks will never be dispatched.
  FlossSocketManager::FlossListeningSocket bad_socket;
  bad_socket.id = 123456789;

  // Good id is the last socket ctr we used.
  FlossSocketManager::FlossListeningSocket good_socket;
  good_socket.id = socket_id_ctr_ - 1;
  good_socket.name = "Foo";
  good_socket.uuid = device::BluetoothUUID("F00D");

  // Empty out the last seen status and socket.
  last_status_ = BtifStatus::kNotReady;
  last_server_socket_ = FlossSocketManager::FlossListeningSocket();
  last_state_ = FlossSocketManager::ServerSocketState::kClosed;

  EXPECT_FALSE(last_server_socket_.is_valid());
  // Send an invalid update. Should result in no callbacks being called.
  SendIncomingSocketReady(
      bad_socket, BtifStatus::kSuccess,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kNotReady, last_status_);
  EXPECT_FALSE(last_server_socket_.is_valid());

  // Send a successful ready to a valid socket.
  SendIncomingSocketReady(
      good_socket, BtifStatus::kSuccess,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kSuccess, last_status_);
  EXPECT_EQ(last_state_, FlossSocketManager::ServerSocketState::kReady);
  EXPECT_TRUE(last_server_socket_.is_valid());

  // Empty out the last seen status and socket.
  last_status_ = BtifStatus::kNotReady;
  last_server_socket_ = FlossSocketManager::FlossListeningSocket();

  // Send an invalid update. Should result in no callbacks being called.
  SendIncomingSocketClosed(
      bad_socket.id, BtifStatus::kSuccess,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kNotReady, last_status_);
  EXPECT_FALSE(last_server_socket_.is_valid());

  // Send a successful close to a valid socket.
  SendIncomingSocketClosed(
      good_socket.id, BtifStatus::kSuccess,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kSuccess, last_status_);
  EXPECT_EQ(last_server_socket_.id, good_socket.id);
  EXPECT_EQ(last_state_, FlossSocketManager::ServerSocketState::kClosed);

  // Try sending a ready to the same socket and nothing should happen.
  last_status_ = BtifStatus::kNotReady;
  SendIncomingSocketReady(
      good_socket, BtifStatus::kSuccess,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_EQ(BtifStatus::kNotReady, last_status_);
}

// Handle incoming socket connections.
TEST_F(FlossSocketManagerTest, IncomingConnections) {
  Init();
  SetupListeningSocket();

  // With a bad id, callbacks will never be dispatched.
  FlossSocketManager::FlossSocket bad_socket;
  bad_socket.id = 123456789;

  // Good id is the last socket ctr we used.
  FlossSocketManager::FlossSocket good_socket;
  good_socket.id = socket_id_ctr_ - 1;

  last_incoming_socket_ = FlossSocketManager::FlossSocket();
  EXPECT_FALSE(last_incoming_socket_.is_valid());

  SendIncomingConnection(
      bad_socket.id, bad_socket,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_FALSE(last_incoming_socket_.is_valid());

  SendIncomingConnection(
      good_socket.id, good_socket,
      base::BindOnce(&FlossSocketManagerTest::ExpectNormalResponse,
                     weak_ptr_factory_.GetWeakPtr()));
  EXPECT_TRUE(last_incoming_socket_.is_valid());
  EXPECT_EQ(last_incoming_socket_.id, good_socket.id);
}

}  // namespace floss