summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/pairing_registry_unittest.cc
blob: 523bdaa01965b642023fe201189cd4ce10b55b6d (plain)
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
// Copyright (c) 2012 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 "remoting/protocol/pairing_registry.h"

#include <stdlib.h>

#include "base/bind.h"
#include "base/compiler_specific.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {
namespace protocol {

class PairingRegistryTest : public testing::Test {
 public:
  virtual void SetUp() OVERRIDE {
    got_secret_ = false;
  }

  void CompareSecret(const std::string& expected,
                     PairingRegistry::Pairing actual) {
    EXPECT_EQ(actual.shared_secret, expected);
    got_secret_ = true;
  }

 protected:
  bool got_secret_;
};

TEST_F(PairingRegistryTest, GetPairing) {
  PairingRegistry::Pairing client_info = {
    "client_id",
    "client_name",
    "shared_secret"
  };
  MockPairingRegistryDelegate* mock_delegate =
      new MockPairingRegistryDelegate();
  mock_delegate->AddPairing(client_info);
  scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);

  scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));

  registry->GetPairing(client_info.client_id,
                       base::Bind(&PairingRegistryTest::CompareSecret,
                                  base::Unretained(this),
                                  client_info.shared_secret));
  mock_delegate->RunCallback();
  EXPECT_TRUE(got_secret_);
}

TEST_F(PairingRegistryTest, AddPairing) {
  MockPairingRegistryDelegate* mock_delegate =
      new MockPairingRegistryDelegate();
  scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);

  scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));

  // Verify that we can create pairings from two clients with the same name, but
  // that they aren't allocated the same client id.
  PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client_name");
  PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client_name");

  const PairingRegistry::PairedClients& clients =
      mock_delegate->paired_clients();
  ASSERT_EQ(clients.size(), 2u);
  PairingRegistry::Pairing first = clients.begin()->second;
  PairingRegistry::Pairing second = (++clients.begin())->second;
  EXPECT_EQ(first.client_name, second.client_name);
  EXPECT_NE(first.client_id, second.client_id);
}

}  // namespace protocol
}  // namespace remoting