1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/native_messaging/native_messaging_reader.h"
7 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/platform_file.h"
12 #include "base/run_loop.h"
13 #include "base/values.h"
14 #include "remoting/host/setup/test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class NativeMessagingReaderTest : public testing::Test {
21 NativeMessagingReaderTest();
22 virtual ~NativeMessagingReaderTest();
24 virtual void SetUp() OVERRIDE;
25 virtual void TearDown() OVERRIDE;
27 // Starts the reader and runs the MessageLoop to completion.
30 // MessageCallback passed to the Reader. Stores |message| so it can be
32 void OnMessage(scoped_ptr<base::Value> message);
34 // Writes a message (header+body) to the write-end of the pipe.
35 void WriteMessage(std::string message);
37 // Writes some data to the write-end of the pipe.
38 void WriteData(const char* data, int length);
41 scoped_ptr<NativeMessagingReader> reader_;
42 base::PlatformFile read_handle_;
43 base::PlatformFile write_handle_;
44 scoped_ptr<base::Value> message_;
47 // MessageLoop declared here, since the NativeMessageReader ctor requires a
48 // MessageLoop to have been created.
49 base::MessageLoop message_loop_;
50 base::RunLoop run_loop_;
53 NativeMessagingReaderTest::NativeMessagingReaderTest()
54 : message_loop_(base::MessageLoop::TYPE_IO) {
57 NativeMessagingReaderTest::~NativeMessagingReaderTest() {}
59 void NativeMessagingReaderTest::SetUp() {
60 ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_));
61 reader_.reset(new NativeMessagingReader(read_handle_));
64 void NativeMessagingReaderTest::TearDown() {
65 // |read_handle_| is owned by NativeMessagingReader's FileStream, so don't
66 // try to close it here. Also, |write_handle_| gets closed by Run().
69 void NativeMessagingReaderTest::Run() {
70 // Close the write-end, so the reader doesn't block waiting for more data.
71 base::ClosePlatformFile(write_handle_);
73 // base::Unretained is safe since no further tasks can run after
74 // RunLoop::Run() returns.
76 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)),
77 run_loop_.QuitClosure());
81 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) {
82 message_ = message.Pass();
85 void NativeMessagingReaderTest::WriteMessage(std::string message) {
86 uint32 length = message.length();
87 WriteData(reinterpret_cast<char*>(&length), 4);
88 WriteData(message.data(), length);
91 void NativeMessagingReaderTest::WriteData(const char* data, int length) {
92 int written = base::WritePlatformFileAtCurrentPos(write_handle_, data,
94 ASSERT_EQ(length, written);
97 TEST_F(NativeMessagingReaderTest, GoodMessage) {
98 WriteMessage("{\"foo\": 42}");
100 EXPECT_TRUE(message_);
101 base::DictionaryValue* message_dict;
102 EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
104 EXPECT_TRUE(message_dict->GetInteger("foo", &result));
105 EXPECT_EQ(42, result);
108 TEST_F(NativeMessagingReaderTest, InvalidLength) {
109 uint32 length = 0xffffffff;
110 WriteData(reinterpret_cast<char*>(&length), 4);
112 EXPECT_FALSE(message_);
115 TEST_F(NativeMessagingReaderTest, EmptyFile) {
117 EXPECT_FALSE(message_);
120 TEST_F(NativeMessagingReaderTest, ShortHeader) {
121 // Write only 3 bytes - the message length header is supposed to be 4 bytes.
124 EXPECT_FALSE(message_);
127 TEST_F(NativeMessagingReaderTest, EmptyBody) {
129 WriteData(reinterpret_cast<char*>(&length), 4);
131 EXPECT_FALSE(message_);
134 TEST_F(NativeMessagingReaderTest, ShortBody) {
136 WriteData(reinterpret_cast<char*>(&length), 4);
138 // Only write 1 byte, where the header indicates there should be 2 bytes.
141 EXPECT_FALSE(message_);
144 TEST_F(NativeMessagingReaderTest, InvalidJSON) {
145 std::string text = "{";
148 EXPECT_FALSE(message_);
151 TEST_F(NativeMessagingReaderTest, SecondMessage) {
153 WriteMessage("{\"foo\": 42}");
155 EXPECT_TRUE(message_);
156 base::DictionaryValue* message_dict;
157 EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
159 EXPECT_TRUE(message_dict->GetInteger("foo", &result));
160 EXPECT_EQ(42, result);
163 } // namespace remoting