summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/v2_authenticator_unittest.cc
blob: 812ee1dfe32d24caadf4aaa7eeed5558baadb351 (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
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
// 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/v2_authenticator.h"

#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "crypto/rsa_private_key.h"
#include "net/base/net_errors.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/channel_authenticator.h"
#include "remoting/protocol/connection_tester.h"
#include "remoting/protocol/fake_session.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"

using testing::_;
using testing::DeleteArg;
using testing::SaveArg;

namespace remoting {
namespace protocol {

namespace {

const int kMessageSize = 100;
const int kMessages = 1;

const char kClientJid[] = "host2@gmail.com/321";

const char kTestSharedSecret[] = "1234-1234-5678";
const char kTestSharedSecretBad[] = "0000-0000-0001";

class MockChannelDoneCallback {
 public:
  MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket));
};

}  // namespace

class V2AuthenticatorTest : public testing::Test {
 public:
  V2AuthenticatorTest() {
  }
  virtual ~V2AuthenticatorTest() {
  }

 protected:
  virtual void SetUp() OVERRIDE {
    FilePath certs_dir;
    PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir);
    certs_dir = certs_dir.AppendASCII("net");
    certs_dir = certs_dir.AppendASCII("data");
    certs_dir = certs_dir.AppendASCII("ssl");
    certs_dir = certs_dir.AppendASCII("certificates");

    FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
    ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_));

    FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
    std::string key_string;
    ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
    std::vector<uint8> key_vector(
        reinterpret_cast<const uint8*>(key_string.data()),
        reinterpret_cast<const uint8*>(key_string.data() +
                                       key_string.length()));
    private_key_.reset(
        crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
  }

  void InitAuthenticators(const std::string& client_secret,
                          const std::string& host_secret) {
    host_.reset(V2Authenticator::CreateForHost(
        host_cert_, *private_key_, host_secret));
    client_.reset(V2Authenticator::CreateForClient(client_secret));
  }

  void RunAuthExchange() {
    do {
      scoped_ptr<buzz::XmlElement> message;

      // Pass message from client to host.
      ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state());
      message.reset(client_->GetNextMessage());
      ASSERT_TRUE(message.get());
      ASSERT_NE(Authenticator::MESSAGE_READY, client_->state());

      ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state());
      host_->ProcessMessage(message.get());
      ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state());

      // Are we done yet?
      if (host_->state() == Authenticator::ACCEPTED ||
          host_->state() == Authenticator::REJECTED) {
        break;
      }

      // Pass message from host to client.
      ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state());
      message.reset(host_->GetNextMessage());
      ASSERT_TRUE(message.get());
      ASSERT_NE(Authenticator::MESSAGE_READY, host_->state());

      ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state());
      client_->ProcessMessage(message.get());
      ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state());
    } while (client_->state() != Authenticator::ACCEPTED &&
             client_->state() != Authenticator::REJECTED);
  }

  void RunChannelAuth(bool expected_fail) {
    client_fake_socket_.reset(new FakeSocket());
    host_fake_socket_.reset(new FakeSocket());
    client_fake_socket_->PairWith(host_fake_socket_.get());

    client_auth_->SecureAndAuthenticate(
        client_fake_socket_.release(),
        base::Bind(&MockChannelDoneCallback::OnDone,
                   base::Unretained(&client_callback_)));

    host_auth_->SecureAndAuthenticate(
        host_fake_socket_.release(),
        base::Bind(&MockChannelDoneCallback::OnDone,
                   base::Unretained(&host_callback_)));

    net::StreamSocket* client_socket = NULL;
    net::StreamSocket* host_socket = NULL;

    EXPECT_CALL(client_callback_, OnDone(net::OK, _))
        .WillOnce(SaveArg<1>(&client_socket));
    if (expected_fail) {
      EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL));
    } else {
      EXPECT_CALL(host_callback_, OnDone(net::OK, _))
          .WillOnce(SaveArg<1>(&host_socket));
    }

    message_loop_.RunAllPending();

    client_socket_.reset(client_socket);
    host_socket_.reset(host_socket);
  }

  MessageLoop message_loop_;

  scoped_ptr<crypto::RSAPrivateKey> private_key_;
  std::string host_cert_;
  scoped_ptr<V2Authenticator> host_;
  scoped_ptr<V2Authenticator> client_;
  scoped_ptr<FakeSocket> client_fake_socket_;
  scoped_ptr<FakeSocket> host_fake_socket_;
  scoped_ptr<ChannelAuthenticator> client_auth_;
  scoped_ptr<ChannelAuthenticator> host_auth_;
  MockChannelDoneCallback client_callback_;
  MockChannelDoneCallback host_callback_;
  scoped_ptr<net::StreamSocket> client_socket_;
  scoped_ptr<net::StreamSocket> host_socket_;

  DISALLOW_COPY_AND_ASSIGN(V2AuthenticatorTest);
};

TEST_F(V2AuthenticatorTest, SuccessfulAuth) {
  ASSERT_NO_FATAL_FAILURE(
      InitAuthenticators(kTestSharedSecret, kTestSharedSecret));
  ASSERT_NO_FATAL_FAILURE(RunAuthExchange());

  ASSERT_EQ(Authenticator::ACCEPTED, host_->state());
  ASSERT_EQ(Authenticator::ACCEPTED, client_->state());

  client_auth_.reset(client_->CreateChannelAuthenticator());
  host_auth_.reset(host_->CreateChannelAuthenticator());
  RunChannelAuth(false);

  EXPECT_TRUE(client_socket_.get() != NULL);
  EXPECT_TRUE(host_socket_.get() != NULL);

  StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
                                kMessageSize, kMessages);

  tester.Start();
  message_loop_.Run();
  tester.CheckResults();
}

// Verify that connection is rejected when secrets don't match.
TEST_F(V2AuthenticatorTest, InvalidSecret) {
  ASSERT_NO_FATAL_FAILURE(
      InitAuthenticators(kTestSharedSecretBad, kTestSharedSecret));
  ASSERT_NO_FATAL_FAILURE(RunAuthExchange());

  ASSERT_EQ(Authenticator::REJECTED, client_->state());

  // Change |client_| so that we can get the laste message.
  client_->state_ = Authenticator::MESSAGE_READY;

  scoped_ptr<buzz::XmlElement> message(client_->GetNextMessage());
  ASSERT_TRUE(message.get());

  ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state());
  host_->ProcessMessage(message.get());
  ASSERT_EQ(Authenticator::REJECTED, host_->state());
}

}  // namespace protocol
}  // namespace remoting