summaryrefslogtreecommitdiffstats
path: root/remoting/host/it2me/it2me_native_messaging_host_unittest.cc
blob: 89702b13c952e993da67544e224cf01bbdf95016 (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
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
// Copyright 2013 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/host/it2me/it2me_native_messaging_host.h"

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/stringize_macros.h"
#include "base/values.h"
#include "net/base/file_stream.h"
#include "net/base/net_util.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/native_messaging/native_messaging_pipe.h"
#include "remoting/host/native_messaging/pipe_messaging_channel.h"
#include "remoting/host/setup/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

namespace {

const char kTestAccessCode[] = "888888";
const int kTestAccessCodeLifetimeInSeconds = 666;
const char kTestClientUsername[] = "some_user@gmail.com";

void VerifyId(scoped_ptr<base::DictionaryValue> response, int expected_value) {
  ASSERT_TRUE(response);

  int value;
  EXPECT_TRUE(response->GetInteger("id", &value));
  EXPECT_EQ(expected_value, value);
}

void VerifyStringProperty(scoped_ptr<base::DictionaryValue> response,
                          const std::string& name,
                          const std::string& expected_value) {
  ASSERT_TRUE(response);

  std::string value;
  EXPECT_TRUE(response->GetString(name, &value));
  EXPECT_EQ(expected_value, value);
}

// Verity the values of the "type" and "id" properties
void VerifyCommonProperties(scoped_ptr<base::DictionaryValue> response,
                            const std::string& type,
                            int id) {
  ASSERT_TRUE(response);

  std::string string_value;
  EXPECT_TRUE(response->GetString("type", &string_value));
  EXPECT_EQ(type, string_value);

  int int_value;
  EXPECT_TRUE(response->GetInteger("id", &int_value));
  EXPECT_EQ(id, int_value);
}

}  // namespace

class MockIt2MeHost : public It2MeHost {
 public:
  MockIt2MeHost(ChromotingHostContext* context,
                scoped_refptr<base::SingleThreadTaskRunner> task_runner,
                base::WeakPtr<It2MeHost::Observer> observer,
                const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
                const std::string& directory_bot_jid)
      : It2MeHost(context,
                  task_runner,
                  observer,
                  xmpp_server_config,
                  directory_bot_jid) {}

  // It2MeHost overrides
  void Connect() override;
  void Disconnect() override;
  void RequestNatPolicy() override;

 private:
  ~MockIt2MeHost() override {}

  void RunSetState(It2MeHostState state);

  DISALLOW_COPY_AND_ASSIGN(MockIt2MeHost);
};

void MockIt2MeHost::Connect() {
  if (!host_context()->ui_task_runner()->BelongsToCurrentThread()) {
    DCHECK(task_runner()->BelongsToCurrentThread());
    host_context()->ui_task_runner()->PostTask(
        FROM_HERE, base::Bind(&MockIt2MeHost::Connect, this));
    return;
  }

  RunSetState(kStarting);
  RunSetState(kRequestedAccessCode);

  std::string access_code(kTestAccessCode);
  base::TimeDelta lifetime =
      base::TimeDelta::FromSeconds(kTestAccessCodeLifetimeInSeconds);
  task_runner()->PostTask(FROM_HERE,
                          base::Bind(&It2MeHost::Observer::OnStoreAccessCode,
                                     observer(),
                                     access_code,
                                     lifetime));

  RunSetState(kReceivedAccessCode);

  std::string client_username(kTestClientUsername);
  task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&It2MeHost::Observer::OnClientAuthenticated,
                 observer(),
                 client_username));

  RunSetState(kConnected);
}

void MockIt2MeHost::Disconnect() {
  if (!host_context()->network_task_runner()->BelongsToCurrentThread()) {
    DCHECK(task_runner()->BelongsToCurrentThread());
    host_context()->network_task_runner()->PostTask(
        FROM_HERE, base::Bind(&MockIt2MeHost::Disconnect, this));
    return;
  }

  RunSetState(kDisconnecting);
  RunSetState(kDisconnected);
}

void MockIt2MeHost::RequestNatPolicy() {}

void MockIt2MeHost::RunSetState(It2MeHostState state) {
  if (!host_context()->network_task_runner()->BelongsToCurrentThread()) {
    host_context()->network_task_runner()->PostTask(
        FROM_HERE, base::Bind(&It2MeHost::SetStateForTesting, this, state));
  } else {
    SetStateForTesting(state);
  }
}

class MockIt2MeHostFactory : public It2MeHostFactory {
 public:
  MockIt2MeHostFactory() {}
  scoped_refptr<It2MeHost> CreateIt2MeHost(
      ChromotingHostContext* context,
      scoped_refptr<base::SingleThreadTaskRunner> task_runner,
      base::WeakPtr<It2MeHost::Observer> observer,
      const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
      const std::string& directory_bot_jid) override {
    return new MockIt2MeHost(
        context, task_runner, observer, xmpp_server_config, directory_bot_jid);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory);
};  // MockIt2MeHostFactory

class It2MeNativeMessagingHostTest : public testing::Test {
 public:
  It2MeNativeMessagingHostTest() {}
  ~It2MeNativeMessagingHostTest() override {}

  void SetUp() override;
  void TearDown() override;

 protected:
  scoped_ptr<base::DictionaryValue> ReadMessageFromOutputPipe();
  void WriteMessageToInputPipe(const base::Value& message);

  void VerifyHelloResponse(int request_id);
  void VerifyErrorResponse();
  void VerifyConnectResponses(int request_id);
  void VerifyDisconnectResponses(int request_id);

  // The Host process should shut down when it receives a malformed request.
  // This is tested by sending a known-good request, followed by |message|,
  // followed by the known-good request again. The response file should only
  // contain a single response from the first good request.
  void TestBadRequest(const base::Value& message, bool expect_error_response);
  void TestConnect();

 private:
  void StartHost();
  void StopHost();
  void ExitTest();

  // Each test creates two unidirectional pipes: "input" and "output".
  // It2MeNativeMessagingHost reads from input_read_file and writes to
  // output_write_file. The unittest supplies data to input_write_handle, and
  // verifies output from output_read_handle.
  //
  // unittest -> [input] -> It2MeNativeMessagingHost -> [output] -> unittest
  base::File input_write_file_;
  base::File output_read_file_;

  // Message loop of the test thread.
  scoped_ptr<base::MessageLoop> test_message_loop_;
  scoped_ptr<base::RunLoop> test_run_loop_;

  scoped_ptr<base::Thread> host_thread_;
  scoped_ptr<base::RunLoop> host_run_loop_;

  // Task runner of the host thread.
  scoped_refptr<AutoThreadTaskRunner> host_task_runner_;
  scoped_ptr<remoting::NativeMessagingPipe> pipe_;

  DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest);
};

void It2MeNativeMessagingHostTest::SetUp() {
  test_message_loop_.reset(new base::MessageLoop());
  test_run_loop_.reset(new base::RunLoop());

  // Run the host on a dedicated thread.
  host_thread_.reset(new base::Thread("host_thread"));
  host_thread_->Start();

  host_task_runner_ = new AutoThreadTaskRunner(
      host_thread_->message_loop_proxy(),
      base::Bind(&It2MeNativeMessagingHostTest::ExitTest,
                 base::Unretained(this)));

  host_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&It2MeNativeMessagingHostTest::StartHost,
                 base::Unretained(this)));

  // Wait until the host finishes starting.
  test_run_loop_->Run();
}

void It2MeNativeMessagingHostTest::TearDown() {
  // Closing the write-end of the input will send an EOF to the native
  // messaging reader. This will trigger a host shutdown.
  input_write_file_.Close();

  // Start a new RunLoop and Wait until the host finishes shutting down.
  test_run_loop_.reset(new base::RunLoop());
  test_run_loop_->Run();

  // Verify there are no more message in the output pipe.
  scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
  EXPECT_FALSE(response);

  // The It2MeNativeMessagingHost dtor closes the handles that are passed to it.
  // So the only handle left to close is |output_read_file_|.
  output_read_file_.Close();
}

scoped_ptr<base::DictionaryValue>
It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() {
  uint32 length;
  int read_result = output_read_file_.ReadAtCurrentPos(
      reinterpret_cast<char*>(&length), sizeof(length));
  if (read_result != sizeof(length)) {
    // The output pipe has been closed, return an empty message.
    return nullptr;
  }

  std::string message_json(length, '\0');
  read_result = output_read_file_.ReadAtCurrentPos(
      string_as_array(&message_json), length);
  if (read_result != static_cast<int>(length)) {
    LOG(ERROR) << "Message size (" << read_result
               << ") doesn't match the header (" << length << ").";
    return nullptr;
  }

  scoped_ptr<base::Value> message(base::JSONReader::Read(message_json));
  if (!message || !message->IsType(base::Value::TYPE_DICTIONARY)) {
    LOG(ERROR) << "Malformed message:" << message_json;
    return nullptr;
  }

  return make_scoped_ptr(
      static_cast<base::DictionaryValue*>(message.release()));
}

void It2MeNativeMessagingHostTest::WriteMessageToInputPipe(
    const base::Value& message) {
  std::string message_json;
  base::JSONWriter::Write(&message, &message_json);

  uint32 length = message_json.length();
  input_write_file_.WriteAtCurrentPos(reinterpret_cast<char*>(&length),
                                      sizeof(length));
  input_write_file_.WriteAtCurrentPos(message_json.data(), length);
}

void It2MeNativeMessagingHostTest::VerifyHelloResponse(int request_id) {
  scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
  VerifyCommonProperties(response.Pass(), "helloResponse", request_id);
}

void It2MeNativeMessagingHostTest::VerifyErrorResponse() {
  scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
  VerifyStringProperty(response.Pass(), "type", "error");
}

void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) {
  bool connect_response_received = false;
  bool starting_received = false;
  bool requestedAccessCode_received = false;
  bool receivedAccessCode_received = false;
  bool connected_received = false;

  // We expect a total of 5 messages: 1 connectResponse and 4 hostStateChanged.
  for (int i = 0; i < 5; ++i) {
    scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
    ASSERT_TRUE(response);

    std::string type;
    ASSERT_TRUE(response->GetString("type", &type));

    if (type == "connectResponse") {
      EXPECT_FALSE(connect_response_received);
      connect_response_received = true;
      VerifyId(response.Pass(), request_id);
    } else if (type == "hostStateChanged") {
      std::string state;
      ASSERT_TRUE(response->GetString("state", &state));

      std::string value;
      if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) {
        EXPECT_FALSE(starting_received);
        starting_received = true;
      } else if (state == It2MeNativeMessagingHost::HostStateToString(
                              kRequestedAccessCode)) {
        EXPECT_FALSE(requestedAccessCode_received);
        requestedAccessCode_received = true;
      } else if (state == It2MeNativeMessagingHost::HostStateToString(
                              kReceivedAccessCode)) {
        EXPECT_FALSE(receivedAccessCode_received);
        receivedAccessCode_received = true;

        EXPECT_TRUE(response->GetString("accessCode", &value));
        EXPECT_EQ(kTestAccessCode, value);

        int accessCodeLifetime;
        EXPECT_TRUE(
            response->GetInteger("accessCodeLifetime", &accessCodeLifetime));
        EXPECT_EQ(kTestAccessCodeLifetimeInSeconds, accessCodeLifetime);
      } else if (state ==
                 It2MeNativeMessagingHost::HostStateToString(kConnected)) {
        EXPECT_FALSE(connected_received);
        connected_received = true;

        EXPECT_TRUE(response->GetString("client", &value));
        EXPECT_EQ(kTestClientUsername, value);
      } else {
        ADD_FAILURE() << "Unexpected host state: " << state;
      }
    } else {
      ADD_FAILURE() << "Unexpected message type: " << type;
    }
  }
}

void It2MeNativeMessagingHostTest::VerifyDisconnectResponses(int request_id) {
  bool disconnect_response_received = false;
  bool disconnecting_received = false;
  bool disconnected_received = false;

  // We expect a total of 3 messages: 1 connectResponse and 2 hostStateChanged.
  for (int i = 0; i < 3; ++i) {
    scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
    ASSERT_TRUE(response);

    std::string type;
    ASSERT_TRUE(response->GetString("type", &type));

    if (type == "disconnectResponse") {
      EXPECT_FALSE(disconnect_response_received);
      disconnect_response_received = true;
      VerifyId(response.Pass(), request_id);
    } else if (type == "hostStateChanged") {
      std::string state;
      ASSERT_TRUE(response->GetString("state", &state));
      if (state ==
          It2MeNativeMessagingHost::HostStateToString(kDisconnecting)) {
        EXPECT_FALSE(disconnecting_received);
        disconnecting_received = true;
      } else if (state ==
                 It2MeNativeMessagingHost::HostStateToString(kDisconnected)) {
        EXPECT_FALSE(disconnected_received);
        disconnected_received = true;
      } else {
        ADD_FAILURE() << "Unexpected host state: " << state;
      }
    } else {
      ADD_FAILURE() << "Unexpected message type: " << type;
    }
  }
}

void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message,
                                                  bool expect_error_response) {
  base::DictionaryValue good_message;
  good_message.SetString("type", "hello");
  good_message.SetInteger("id", 1);

  WriteMessageToInputPipe(good_message);
  WriteMessageToInputPipe(message);
  WriteMessageToInputPipe(good_message);

  VerifyHelloResponse(1);

  if (expect_error_response)
    VerifyErrorResponse();

  scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
  EXPECT_FALSE(response);
}

void It2MeNativeMessagingHostTest::StartHost() {
  DCHECK(host_task_runner_->RunsTasksOnCurrentThread());

  base::File input_read_file;
  base::File output_write_file;

  ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_));
  ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file));

  // Creating a native messaging host with a mock It2MeHostFactory.
  scoped_ptr<It2MeHostFactory> factory(new MockIt2MeHostFactory());

  pipe_.reset(new NativeMessagingPipe());

  scoped_ptr<extensions::NativeMessagingChannel> channel(
      new PipeMessagingChannel(input_read_file.Pass(),
                               output_write_file.Pass()));

  scoped_ptr<extensions::NativeMessageHost> it2me_host(
      new It2MeNativeMessagingHost(
          host_task_runner_,
          factory.Pass()));
  it2me_host->Start(pipe_.get());

  pipe_->Start(it2me_host.Pass(),
               channel.Pass(),
               base::Bind(&It2MeNativeMessagingHostTest::StopHost,
                          base::Unretained(this)));

  // Notify the test that the host has finished starting up.
  test_message_loop_->message_loop_proxy()->PostTask(
      FROM_HERE, test_run_loop_->QuitClosure());
}

void It2MeNativeMessagingHostTest::StopHost() {
  DCHECK(host_task_runner_->RunsTasksOnCurrentThread());

  pipe_.reset();

  // Wait till all shutdown tasks have completed.
  base::RunLoop().RunUntilIdle();

  // Trigger a test shutdown via ExitTest().
  host_task_runner_ = NULL;
}

void It2MeNativeMessagingHostTest::ExitTest() {
  if (!test_message_loop_->message_loop_proxy()->RunsTasksOnCurrentThread()) {
    test_message_loop_->message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&It2MeNativeMessagingHostTest::ExitTest,
                   base::Unretained(this)));
    return;
  }
  test_run_loop_->Quit();
}

void It2MeNativeMessagingHostTest::TestConnect() {
  base::DictionaryValue connect_message;
  int next_id = 0;

  // Send the "connect" request.
  connect_message.SetInteger("id", ++next_id);
  connect_message.SetString("type", "connect");
  connect_message.SetString("xmppServerAddress", "talk.google.com:5222");
  connect_message.SetBoolean("xmppServerUseTls", true);
  connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com");
  connect_message.SetString("userName", "chromo.pyauto@gmail.com");
  connect_message.SetString("authServiceWithToken", "oauth2:sometoken");
  WriteMessageToInputPipe(connect_message);

  VerifyConnectResponses(next_id);

  base::DictionaryValue disconnect_message;
  disconnect_message.SetInteger("id", ++next_id);
  disconnect_message.SetString("type", "disconnect");
  WriteMessageToInputPipe(disconnect_message);

  VerifyDisconnectResponses(next_id);
}

// Test hello request.
TEST_F(It2MeNativeMessagingHostTest, Hello) {
  int next_id = 0;
  base::DictionaryValue message;
  message.SetInteger("id", ++next_id);
  message.SetString("type", "hello");
  WriteMessageToInputPipe(message);

  VerifyHelloResponse(next_id);
}

// Verify that response ID matches request ID.
TEST_F(It2MeNativeMessagingHostTest, Id) {
  base::DictionaryValue message;
  message.SetString("type", "hello");
  WriteMessageToInputPipe(message);
  message.SetString("id", "42");
  WriteMessageToInputPipe(message);

  scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
  EXPECT_TRUE(response);
  std::string value;
  EXPECT_FALSE(response->GetString("id", &value));

  response = ReadMessageFromOutputPipe();
  EXPECT_TRUE(response);
  EXPECT_TRUE(response->GetString("id", &value));
  EXPECT_EQ("42", value);
}

TEST_F(It2MeNativeMessagingHostTest, Connect) {
  // A new It2MeHost instance is created for every it2me session. The native
  // messaging host, on the other hand, is long lived. This test verifies
  // multiple It2Me host startup and shutdowns.
  for (int i = 0; i < 3; ++i)
    TestConnect();
}

// Verify non-Dictionary requests are rejected.
TEST_F(It2MeNativeMessagingHostTest, WrongFormat) {
  base::ListValue message;
  // No "error" response will be sent for non-Dictionary messages.
  TestBadRequest(message, false);
}

// Verify requests with no type are rejected.
TEST_F(It2MeNativeMessagingHostTest, MissingType) {
  base::DictionaryValue message;
  TestBadRequest(message, true);
}

// Verify rejection if type is unrecognized.
TEST_F(It2MeNativeMessagingHostTest, InvalidType) {
  base::DictionaryValue message;
  message.SetString("type", "xxx");
  TestBadRequest(message, true);
}

}  // namespace remoting