Chromium Code Reviews| 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 "base/basictypes.h" | |
| 6 #include "base/bind.h" | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/platform_file.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/values.h" | |
| 13 #include "remoting/host/setup/native_messaging_reader.h" | |
|
Sergey Ulanov
2013/05/18 01:59:31
nit: this should be first include in the file.
Lambros
2013/05/22 21:42:18
Done.
| |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 | |
| 18 class NativeMessagingReaderTest : public testing::Test { | |
| 19 public: | |
| 20 NativeMessagingReaderTest(); | |
| 21 virtual ~NativeMessagingReaderTest(); | |
| 22 | |
| 23 virtual void SetUp() OVERRIDE; | |
| 24 virtual void TearDown() OVERRIDE; | |
| 25 | |
| 26 // Starts the reader and runs the MessageLoop to completion. | |
| 27 void Run(); | |
| 28 | |
| 29 // MessageCallback passed to the Reader. Stores |message| so it can be | |
| 30 // verified by tests. | |
| 31 void OnMessage(scoped_ptr<base::Value> message); | |
| 32 | |
| 33 // Writes (or appends) a message (header+body) to the temp file. | |
| 34 void WriteMessage(std::string message); | |
| 35 | |
| 36 base::FilePath temp_file_path() const { return temp_file_path_; } | |
|
Sergey Ulanov
2013/05/18 01:59:31
nit: make the corresponding fields protected, then
Lambros
2013/05/22 21:42:18
Done.
| |
| 37 NativeMessagingReader* reader() const { return reader_.get(); } | |
| 38 base::Value* message() const { return message_.get(); } | |
| 39 | |
| 40 private: | |
| 41 // MessageLoop declared here, since the NativeMessageReader ctor requires a | |
| 42 // MessageLoop to have been created. | |
| 43 base::MessageLoop message_loop_; | |
| 44 base::RunLoop run_loop_; | |
| 45 scoped_ptr<NativeMessagingReader> reader_; | |
| 46 base::FilePath temp_file_path_; | |
| 47 base::PlatformFile temp_file_handle_; | |
| 48 scoped_ptr<base::Value> message_; | |
| 49 }; | |
| 50 | |
| 51 NativeMessagingReaderTest::NativeMessagingReaderTest() | |
| 52 : message_loop_(base::MessageLoop::TYPE_IO) { | |
| 53 } | |
| 54 | |
| 55 NativeMessagingReaderTest::~NativeMessagingReaderTest() {} | |
| 56 | |
| 57 void NativeMessagingReaderTest::SetUp() { | |
| 58 file_util::CreateTemporaryFile(&temp_file_path_); | |
| 59 temp_file_handle_ = base::CreatePlatformFile( | |
| 60 temp_file_path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
| 61 NULL, NULL); | |
| 62 reader_.reset(new NativeMessagingReader(temp_file_handle_)); | |
| 63 } | |
| 64 | |
| 65 void NativeMessagingReaderTest::TearDown() { | |
| 66 base::ClosePlatformFile(temp_file_handle_); | |
| 67 EXPECT_TRUE(file_util::Delete(temp_file_path_, false)); | |
| 68 } | |
| 69 | |
| 70 void NativeMessagingReaderTest::Run() { | |
| 71 // base::Unretained is safe since no further tasks can run after | |
| 72 // RunLoop::Run() returns. | |
| 73 reader()->Start( | |
| 74 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), | |
| 75 run_loop_.QuitClosure()); | |
| 76 run_loop_.Run(); | |
| 77 } | |
| 78 | |
| 79 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) { | |
| 80 message_ = message.Pass(); | |
| 81 } | |
| 82 | |
| 83 void NativeMessagingReaderTest::WriteMessage(std::string message) { | |
| 84 uint32 length = message.length(); | |
| 85 file_util::AppendToFile(temp_file_path(), reinterpret_cast<char*>(&length), | |
| 86 sizeof(length)); | |
| 87 file_util::AppendToFile(temp_file_path(), message.data(), length); | |
| 88 } | |
| 89 | |
| 90 TEST_F(NativeMessagingReaderTest, GoodMessage) { | |
| 91 WriteMessage("{\"foo\": 42}"); | |
| 92 Run(); | |
| 93 EXPECT_TRUE(message()); | |
| 94 base::DictionaryValue* message_dict; | |
| 95 EXPECT_TRUE(message()->GetAsDictionary(&message_dict)); | |
| 96 int result; | |
| 97 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | |
| 98 EXPECT_EQ(42, result); | |
| 99 } | |
| 100 | |
| 101 TEST_F(NativeMessagingReaderTest, InvalidLength) { | |
| 102 uint32 length = 0xffffffff; | |
| 103 file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), | |
| 104 sizeof(length)); | |
| 105 Run(); | |
| 106 EXPECT_FALSE(message()); | |
| 107 } | |
| 108 | |
| 109 TEST_F(NativeMessagingReaderTest, EmptyFile) { | |
| 110 Run(); | |
| 111 EXPECT_FALSE(message()); | |
| 112 } | |
| 113 | |
| 114 TEST_F(NativeMessagingReaderTest, ShortHeader) { | |
| 115 // Write only 3 bytes - the message length header is supposed to be 4 bytes. | |
| 116 file_util::WriteFile(temp_file_path(), "xxx", 3); | |
| 117 Run(); | |
| 118 EXPECT_FALSE(message()); | |
| 119 } | |
| 120 | |
| 121 TEST_F(NativeMessagingReaderTest, EmptyBody) { | |
| 122 uint32 length = 1; | |
| 123 file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), | |
| 124 sizeof(length)); | |
| 125 Run(); | |
| 126 EXPECT_FALSE(message()); | |
| 127 } | |
| 128 | |
| 129 TEST_F(NativeMessagingReaderTest, ShortBody) { | |
| 130 uint32 length = 2; | |
| 131 file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), | |
| 132 sizeof(length)); | |
| 133 // Only write 1 byte, where the header indicates there should be 2 bytes. | |
| 134 file_util::AppendToFile(temp_file_path(), "x", 1); | |
| 135 Run(); | |
| 136 EXPECT_FALSE(message()); | |
| 137 } | |
| 138 | |
| 139 TEST_F(NativeMessagingReaderTest, InvalidJSON) { | |
| 140 std::string text = "{"; | |
| 141 WriteMessage(text); | |
| 142 Run(); | |
| 143 EXPECT_FALSE(message()); | |
| 144 } | |
| 145 | |
| 146 TEST_F(NativeMessagingReaderTest, SecondMessage) { | |
| 147 WriteMessage("{}"); | |
| 148 WriteMessage("{\"foo\": 42}"); | |
| 149 Run(); | |
| 150 EXPECT_TRUE(message()); | |
| 151 base::DictionaryValue* message_dict; | |
| 152 EXPECT_TRUE(message()->GetAsDictionary(&message_dict)); | |
| 153 int result; | |
| 154 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | |
| 155 EXPECT_EQ(42, result); | |
| 156 } | |
| 157 | |
| 158 } // namespace remoting | |
| OLD | NEW |