summaryrefslogtreecommitdiffstats
path: root/remoting/host/setup/native_messaging_reader_unittest.cc
blob: a33ca3b34d1cc40b55a9d315714e6d671f5e885e (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
// 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/setup/native_messaging_reader.h"

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/platform_file.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "remoting/host/setup/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

class NativeMessagingReaderTest : public testing::Test {
 public:
  NativeMessagingReaderTest();
  virtual ~NativeMessagingReaderTest();

  virtual void SetUp() OVERRIDE;
  virtual void TearDown() OVERRIDE;

  // Starts the reader and runs the MessageLoop to completion.
  void Run();

  // MessageCallback passed to the Reader. Stores |message| so it can be
  // verified by tests.
  void OnMessage(scoped_ptr<base::Value> message);

  // Writes a message (header+body) to the write-end of the pipe.
  void WriteMessage(std::string message);

  // Writes some data to the write-end of the pipe.
  void WriteData(const char* data, int length);

 protected:
  scoped_ptr<NativeMessagingReader> reader_;
  base::PlatformFile read_handle_;
  base::PlatformFile write_handle_;
  scoped_ptr<base::Value> message_;

 private:
  // MessageLoop declared here, since the NativeMessageReader ctor requires a
  // MessageLoop to have been created.
  base::MessageLoop message_loop_;
  base::RunLoop run_loop_;
};

NativeMessagingReaderTest::NativeMessagingReaderTest()
    : message_loop_(base::MessageLoop::TYPE_IO) {
}

NativeMessagingReaderTest::~NativeMessagingReaderTest() {}

void NativeMessagingReaderTest::SetUp() {
  ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_));
  reader_.reset(new NativeMessagingReader(read_handle_));
}

void NativeMessagingReaderTest::TearDown() {
  // |read_handle_| is owned by NativeMessagingReader's FileStream, so don't
  // try to close it here. Also, |write_handle_| gets closed by Run().
}

void NativeMessagingReaderTest::Run() {
  // Close the write-end, so the reader doesn't block waiting for more data.
  base::ClosePlatformFile(write_handle_);

  // base::Unretained is safe since no further tasks can run after
  // RunLoop::Run() returns.
  reader_->Start(
      base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)),
      run_loop_.QuitClosure());
  run_loop_.Run();
}

void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) {
  message_ = message.Pass();
}

void NativeMessagingReaderTest::WriteMessage(std::string message) {
  uint32 length = message.length();
  WriteData(reinterpret_cast<char*>(&length), 4);
  WriteData(message.data(), length);
}

void NativeMessagingReaderTest::WriteData(const char* data, int length) {
  int written = base::WritePlatformFileAtCurrentPos(write_handle_, data,
                                                    length);
  ASSERT_EQ(length, written);
}

TEST_F(NativeMessagingReaderTest, GoodMessage) {
  WriteMessage("{\"foo\": 42}");
  Run();
  EXPECT_TRUE(message_);
  base::DictionaryValue* message_dict;
  EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
  int result;
  EXPECT_TRUE(message_dict->GetInteger("foo", &result));
  EXPECT_EQ(42, result);
}

TEST_F(NativeMessagingReaderTest, InvalidLength) {
  uint32 length = 0xffffffff;
  WriteData(reinterpret_cast<char*>(&length), 4);
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, EmptyFile) {
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, ShortHeader) {
  // Write only 3 bytes - the message length header is supposed to be 4 bytes.
  WriteData("xxx", 3);
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, EmptyBody) {
  uint32 length = 1;
  WriteData(reinterpret_cast<char*>(&length), 4);
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, ShortBody) {
  uint32 length = 2;
  WriteData(reinterpret_cast<char*>(&length), 4);

  // Only write 1 byte, where the header indicates there should be 2 bytes.
  WriteData("x", 1);
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, InvalidJSON) {
  std::string text = "{";
  WriteMessage(text);
  Run();
  EXPECT_FALSE(message_);
}

TEST_F(NativeMessagingReaderTest, SecondMessage) {
  WriteMessage("{}");
  WriteMessage("{\"foo\": 42}");
  Run();
  EXPECT_TRUE(message_);
  base::DictionaryValue* message_dict;
  EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
  int result;
  EXPECT_TRUE(message_dict->GetInteger("foo", &result));
  EXPECT_EQ(42, result);
}

}  // namespace remoting