1b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// found in the LICENSE file.
4b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "remoting/protocol/pairing_registry.h"
6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
7b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include <stdlib.h>
8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include <algorithm>
107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/bind.h"
12b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "base/compiler_specific.h"
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/memory/scoped_ptr.h"
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/message_loop/message_loop.h"
15a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/run_loop.h"
16a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/thread_task_runner_handle.h"
177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/values.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "remoting/protocol/protocol_mock_objects.h"
19b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
22a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)using testing::Sequence;
23a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochnamespace {
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochusing remoting::protocol::PairingRegistry;
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)class MockPairingRegistryCallbacks {
29a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) public:
30a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MockPairingRegistryCallbacks() {}
31a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual ~MockPairingRegistryCallbacks() {}
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MOCK_METHOD1(DoneCallback, void(bool));
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MOCK_METHOD1(GetAllPairingsCallbackPtr, void(base::ListValue*));
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MOCK_METHOD1(GetPairingCallback, void(PairingRegistry::Pairing));
36a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
37a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void GetAllPairingsCallback(scoped_ptr<base::ListValue> pairings) {
38a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    GetAllPairingsCallbackPtr(pairings.get());
39a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
40a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
41a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) private:
42a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockPairingRegistryCallbacks);
43a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)};
44a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Verify that a pairing Dictionary has correct entries, but doesn't include
467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// any shared secret.
477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid VerifyPairing(PairingRegistry::Pairing expected,
487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   const base::DictionaryValue& actual) {
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string value;
507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(actual.GetString(PairingRegistry::kClientNameKey, &value));
517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(expected.client_name(), value);
527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(actual.GetString(PairingRegistry::kClientIdKey, &value));
537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(expected.client_id(), value);
547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(actual.HasKey(PairingRegistry::kSharedSecretKey));
567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace
597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace remoting {
61b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace protocol {
62b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
63b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class PairingRegistryTest : public testing::Test {
64b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) public:
65ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  virtual void SetUp() OVERRIDE {
66ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    callback_count_ = 0;
67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
68b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void set_pairings(scoped_ptr<base::ListValue> pairings) {
707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    pairings_ = pairings.Pass();
717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
73ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void ExpectSecret(const std::string& expected,
74ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                    PairingRegistry::Pairing actual) {
75ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    EXPECT_EQ(expected, actual.shared_secret());
76ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    ++callback_count_;
77ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
78ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
79ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void ExpectSaveSuccess(bool success) {
80ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    EXPECT_TRUE(success);
81ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    ++callback_count_;
82ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
83ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
84b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) protected:
85a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::MessageLoop message_loop_;
86a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::RunLoop run_loop_;
87a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
88ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  int callback_count_;
897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  scoped_ptr<base::ListValue> pairings_;
90b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)};
91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(PairingRegistryTest, CreateAndGetPairings) {
93a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
94a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<PairingRegistry::Delegate>(new MockPairingRegistryDelegate()));
957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("my_client");
967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("my_client");
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
98ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  EXPECT_NE(pairing_1.shared_secret(), pairing_2.shared_secret());
99ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  registry->GetPairing(pairing_1.client_id(),
101ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                       base::Bind(&PairingRegistryTest::ExpectSecret,
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                  base::Unretained(this),
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  pairing_1.shared_secret()));
104ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  EXPECT_EQ(1, callback_count_);
105b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Check that the second client is paired with a different shared secret.
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  registry->GetPairing(pairing_2.client_id(),
108ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                       base::Bind(&PairingRegistryTest::ExpectSecret,
109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  base::Unretained(this),
110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  pairing_2.shared_secret()));
111ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  EXPECT_EQ(2, callback_count_);
112b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1147dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(PairingRegistryTest, GetAllPairings) {
115a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
116a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<PairingRegistry::Delegate>(new MockPairingRegistryDelegate()));
1177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  registry->GetAllPairings(
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(&PairingRegistryTest::set_pairings,
1227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                 base::Unretained(this)));
1237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_EQ(2u, pairings_->GetSize());
1257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const base::DictionaryValue* actual_pairing_1;
1267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const base::DictionaryValue* actual_pairing_2;
1277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_1));
1287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(pairings_->GetDictionary(1, &actual_pairing_2));
1297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Ordering is not guaranteed, so swap if necessary.
1317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string actual_client_id;
1327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(actual_pairing_1->GetString(PairingRegistry::kClientIdKey,
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                          &actual_client_id));
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (actual_client_id != pairing_1.client_id()) {
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    std::swap(actual_pairing_1, actual_pairing_2);
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  VerifyPairing(pairing_1, *actual_pairing_1);
1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  VerifyPairing(pairing_2, *actual_pairing_2);
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1427dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(PairingRegistryTest, DeletePairing) {
143a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
144a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<PairingRegistry::Delegate>(new MockPairingRegistryDelegate()));
1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
148ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->DeletePairing(
149ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_1.client_id(),
150ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      base::Bind(&PairingRegistryTest::ExpectSaveSuccess,
151ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                 base::Unretained(this)));
1527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Re-read the list, and verify it only has the pairing_2 client.
1547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  registry->GetAllPairings(
1557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(&PairingRegistryTest::set_pairings,
1567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                 base::Unretained(this)));
1577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_EQ(1u, pairings_->GetSize());
1597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const base::DictionaryValue* actual_pairing_2;
1607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_2));
1617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string actual_client_id;
1627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(actual_pairing_2->GetString(PairingRegistry::kClientIdKey,
1637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                          &actual_client_id));
1647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(pairing_2.client_id(), actual_client_id);
1657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1677dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(PairingRegistryTest, ClearAllPairings) {
168a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<PairingRegistry::Delegate>(new MockPairingRegistryDelegate()));
1707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
1717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
1727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
173ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->ClearAllPairings(
174ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      base::Bind(&PairingRegistryTest::ExpectSaveSuccess,
175ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                 base::Unretained(this)));
1767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Re-read the list, and verify it is empty.
1787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  registry->GetAllPairings(
1797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(&PairingRegistryTest::set_pairings,
1807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                 base::Unretained(this)));
1817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(pairings_->empty());
1837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
185a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)ACTION_P(QuitMessageLoop, callback) {
186a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  callback.Run();
187a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
188a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
189a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)MATCHER_P(EqualsClientName, client_name, "") {
190a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return arg.client_name() == client_name;
191a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
192a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
193a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)MATCHER(NoPairings, "") {
194a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return arg->empty();
195a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
196ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
197a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(PairingRegistryTest, SerializedRequests) {
198a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MockPairingRegistryCallbacks callbacks;
199a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  Sequence s;
200a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client1")))
201a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
202a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client2")))
203a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
204a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, DoneCallback(true))
205a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
206a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client1")))
207a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
208a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("")))
209a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
210a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, DoneCallback(true))
211a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
212a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetAllPairingsCallbackPtr(NoPairings()))
213a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s);
214a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client3")))
215a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .InSequence(s)
216a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      .WillOnce(QuitMessageLoop(run_loop_.QuitClosure()));
217a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
218a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<PairingRegistry> registry = new PairingRegistry(
219a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::ThreadTaskRunnerHandle::Get(),
220a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<PairingRegistry::Delegate>(new MockPairingRegistryDelegate()));
221ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
222ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
223ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetPairing(
224ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_1.client_id(),
225a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
226a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
227ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetPairing(
228ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_2.client_id(),
229a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
230a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
231ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->DeletePairing(
232ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_2.client_id(),
233a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::DoneCallback,
234a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
235ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetPairing(
236ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_1.client_id(),
237a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
238a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
239ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetPairing(
240ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_2.client_id(),
241a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
242a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
243ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->ClearAllPairings(
244a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::DoneCallback,
245a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
246ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetAllPairings(
247a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetAllPairingsCallback,
248a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
249ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  PairingRegistry::Pairing pairing_3 = registry->CreatePairing("client3");
250ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->GetPairing(
251ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      pairing_3.client_id(),
252a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
253a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 base::Unretained(&callbacks)));
254ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
255a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  run_loop_.Run();
256ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
257ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
258b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}  // namespace protocol
259b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}  // namespace remoting
260