| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "remoting/host/setup/native_messaging_reader.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.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" | |
| 16 | |
| 17 namespace remoting { | |
| 18 | |
| 19 class NativeMessagingReaderTest : public testing::Test { | |
| 20 public: | |
| 21 NativeMessagingReaderTest(); | |
| 22 virtual ~NativeMessagingReaderTest(); | |
| 23 | |
| 24 virtual void SetUp() OVERRIDE; | |
| 25 virtual void TearDown() OVERRIDE; | |
| 26 | |
| 27 // Starts the reader and runs the MessageLoop to completion. | |
| 28 void Run(); | |
| 29 | |
| 30 // MessageCallback passed to the Reader. Stores |message| so it can be | |
| 31 // verified by tests. | |
| 32 void OnMessage(scoped_ptr<base::Value> message); | |
| 33 | |
| 34 // Writes a message (header+body) to the write-end of the pipe. | |
| 35 void WriteMessage(std::string message); | |
| 36 | |
| 37 // Writes some data to the write-end of the pipe. | |
| 38 void WriteData(const char* data, int length); | |
| 39 | |
| 40 protected: | |
| 41 scoped_ptr<NativeMessagingReader> reader_; | |
| 42 base::PlatformFile read_handle_; | |
| 43 base::PlatformFile write_handle_; | |
| 44 scoped_ptr<base::Value> message_; | |
| 45 | |
| 46 private: | |
| 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_; | |
| 51 }; | |
| 52 | |
| 53 NativeMessagingReaderTest::NativeMessagingReaderTest() | |
| 54 : message_loop_(base::MessageLoop::TYPE_IO) { | |
| 55 } | |
| 56 | |
| 57 NativeMessagingReaderTest::~NativeMessagingReaderTest() {} | |
| 58 | |
| 59 void NativeMessagingReaderTest::SetUp() { | |
| 60 ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_)); | |
| 61 reader_.reset(new NativeMessagingReader(read_handle_)); | |
| 62 } | |
| 63 | |
| 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(). | |
| 67 } | |
| 68 | |
| 69 void NativeMessagingReaderTest::Run() { | |
| 70 // Close the write-end, so the reader doesn't block waiting for more data. | |
| 71 base::ClosePlatformFile(write_handle_); | |
| 72 | |
| 73 // base::Unretained is safe since no further tasks can run after | |
| 74 // RunLoop::Run() returns. | |
| 75 reader_->Start( | |
| 76 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), | |
| 77 run_loop_.QuitClosure()); | |
| 78 run_loop_.Run(); | |
| 79 } | |
| 80 | |
| 81 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) { | |
| 82 message_ = message.Pass(); | |
| 83 } | |
| 84 | |
| 85 void NativeMessagingReaderTest::WriteMessage(std::string message) { | |
| 86 uint32 length = message.length(); | |
| 87 WriteData(reinterpret_cast<char*>(&length), 4); | |
| 88 WriteData(message.data(), length); | |
| 89 } | |
| 90 | |
| 91 void NativeMessagingReaderTest::WriteData(const char* data, int length) { | |
| 92 int written = base::WritePlatformFileAtCurrentPos(write_handle_, data, | |
| 93 length); | |
| 94 ASSERT_EQ(length, written); | |
| 95 } | |
| 96 | |
| 97 TEST_F(NativeMessagingReaderTest, GoodMessage) { | |
| 98 WriteMessage("{\"foo\": 42}"); | |
| 99 Run(); | |
| 100 EXPECT_TRUE(message_); | |
| 101 base::DictionaryValue* message_dict; | |
| 102 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); | |
| 103 int result; | |
| 104 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | |
| 105 EXPECT_EQ(42, result); | |
| 106 } | |
| 107 | |
| 108 TEST_F(NativeMessagingReaderTest, InvalidLength) { | |
| 109 uint32 length = 0xffffffff; | |
| 110 WriteData(reinterpret_cast<char*>(&length), 4); | |
| 111 Run(); | |
| 112 EXPECT_FALSE(message_); | |
| 113 } | |
| 114 | |
| 115 TEST_F(NativeMessagingReaderTest, EmptyFile) { | |
| 116 Run(); | |
| 117 EXPECT_FALSE(message_); | |
| 118 } | |
| 119 | |
| 120 TEST_F(NativeMessagingReaderTest, ShortHeader) { | |
| 121 // Write only 3 bytes - the message length header is supposed to be 4 bytes. | |
| 122 WriteData("xxx", 3); | |
| 123 Run(); | |
| 124 EXPECT_FALSE(message_); | |
| 125 } | |
| 126 | |
| 127 TEST_F(NativeMessagingReaderTest, EmptyBody) { | |
| 128 uint32 length = 1; | |
| 129 WriteData(reinterpret_cast<char*>(&length), 4); | |
| 130 Run(); | |
| 131 EXPECT_FALSE(message_); | |
| 132 } | |
| 133 | |
| 134 TEST_F(NativeMessagingReaderTest, ShortBody) { | |
| 135 uint32 length = 2; | |
| 136 WriteData(reinterpret_cast<char*>(&length), 4); | |
| 137 | |
| 138 // Only write 1 byte, where the header indicates there should be 2 bytes. | |
| 139 WriteData("x", 1); | |
| 140 Run(); | |
| 141 EXPECT_FALSE(message_); | |
| 142 } | |
| 143 | |
| 144 TEST_F(NativeMessagingReaderTest, InvalidJSON) { | |
| 145 std::string text = "{"; | |
| 146 WriteMessage(text); | |
| 147 Run(); | |
| 148 EXPECT_FALSE(message_); | |
| 149 } | |
| 150 | |
| 151 TEST_F(NativeMessagingReaderTest, SecondMessage) { | |
| 152 WriteMessage("{}"); | |
| 153 WriteMessage("{\"foo\": 42}"); | |
| 154 Run(); | |
| 155 EXPECT_TRUE(message_); | |
| 156 base::DictionaryValue* message_dict; | |
| 157 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); | |
| 158 int result; | |
| 159 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | |
| 160 EXPECT_EQ(42, result); | |
| 161 } | |
| 162 | |
| 163 } // namespace remoting | |
| OLD | NEW |