Index: remoting/host/setup/native_messaging_reader_unittest.cc |
diff --git a/remoting/host/setup/native_messaging_reader_unittest.cc b/remoting/host/setup/native_messaging_reader_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0ed5b03fea26305a618171227aa59f3fcfe467e1 |
--- /dev/null |
+++ b/remoting/host/setup/native_messaging_reader_unittest.cc |
@@ -0,0 +1,158 @@ |
+// 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 "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/file_util.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/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.
|
+#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 (or appends) a message (header+body) to the temp file. |
+ void WriteMessage(std::string message); |
+ |
+ 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.
|
+ NativeMessagingReader* reader() const { return reader_.get(); } |
+ base::Value* message() const { return message_.get(); } |
+ |
+ private: |
+ // MessageLoop declared here, since the NativeMessageReader ctor requires a |
+ // MessageLoop to have been created. |
+ base::MessageLoop message_loop_; |
+ base::RunLoop run_loop_; |
+ scoped_ptr<NativeMessagingReader> reader_; |
+ base::FilePath temp_file_path_; |
+ base::PlatformFile temp_file_handle_; |
+ scoped_ptr<base::Value> message_; |
+}; |
+ |
+NativeMessagingReaderTest::NativeMessagingReaderTest() |
+ : message_loop_(base::MessageLoop::TYPE_IO) { |
+} |
+ |
+NativeMessagingReaderTest::~NativeMessagingReaderTest() {} |
+ |
+void NativeMessagingReaderTest::SetUp() { |
+ file_util::CreateTemporaryFile(&temp_file_path_); |
+ temp_file_handle_ = base::CreatePlatformFile( |
+ temp_file_path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
+ NULL, NULL); |
+ reader_.reset(new NativeMessagingReader(temp_file_handle_)); |
+} |
+ |
+void NativeMessagingReaderTest::TearDown() { |
+ base::ClosePlatformFile(temp_file_handle_); |
+ EXPECT_TRUE(file_util::Delete(temp_file_path_, false)); |
+} |
+ |
+void NativeMessagingReaderTest::Run() { |
+ // 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(); |
+ file_util::AppendToFile(temp_file_path(), reinterpret_cast<char*>(&length), |
+ sizeof(length)); |
+ file_util::AppendToFile(temp_file_path(), message.data(), length); |
+} |
+ |
+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; |
+ file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), |
+ sizeof(length)); |
+ 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. |
+ file_util::WriteFile(temp_file_path(), "xxx", 3); |
+ Run(); |
+ EXPECT_FALSE(message()); |
+} |
+ |
+TEST_F(NativeMessagingReaderTest, EmptyBody) { |
+ uint32 length = 1; |
+ file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), |
+ sizeof(length)); |
+ Run(); |
+ EXPECT_FALSE(message()); |
+} |
+ |
+TEST_F(NativeMessagingReaderTest, ShortBody) { |
+ uint32 length = 2; |
+ file_util::WriteFile(temp_file_path(), reinterpret_cast<char*>(&length), |
+ sizeof(length)); |
+ // Only write 1 byte, where the header indicates there should be 2 bytes. |
+ file_util::AppendToFile(temp_file_path(), "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 |