Index: remoting/host/setup/native_messaging_writer_unittest.cc |
diff --git a/remoting/host/setup/native_messaging_writer_unittest.cc b/remoting/host/setup/native_messaging_writer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..17ea1866af8e5d68c2185f5dff2a968d49c87250 |
--- /dev/null |
+++ b/remoting/host/setup/native_messaging_writer_unittest.cc |
@@ -0,0 +1,119 @@ |
+// 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/file_util.h" |
+#include "base/json/json_reader.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/platform_file.h" |
+#include "base/values.h" |
+#include "remoting/host/setup/native_messaging_writer.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+#if defined(OS_POSIX) |
+#include <unistd.h> |
+#endif // defined(OS_POSIX) |
+ |
+namespace remoting { |
+ |
+class NativeMessagingWriterTest : public testing::Test { |
+ public: |
+ NativeMessagingWriterTest(); |
+ virtual ~NativeMessagingWriterTest(); |
+ |
+ virtual void SetUp() OVERRIDE; |
+ virtual void TearDown() OVERRIDE; |
+ |
+ NativeMessagingWriter* writer() const { return writer_.get(); } |
+ base::FilePath temp_file_path() const { return temp_file_path_; } |
+ base::PlatformFile temp_file_handle() const { return temp_file_handle_; } |
+ |
+ private: |
+ scoped_ptr<NativeMessagingWriter> writer_; |
+ base::FilePath temp_file_path_; |
+ base::PlatformFile temp_file_handle_; |
+}; |
+ |
+NativeMessagingWriterTest::NativeMessagingWriterTest() {} |
+ |
+NativeMessagingWriterTest::~NativeMessagingWriterTest() {} |
+ |
+void NativeMessagingWriterTest::SetUp() { |
+ file_util::CreateTemporaryFile(&temp_file_path_); |
+ temp_file_handle_ = base::CreatePlatformFile( |
+ temp_file_path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
+ NULL, NULL); |
+ writer_.reset(new NativeMessagingWriter(temp_file_handle_)); |
+} |
+ |
+void NativeMessagingWriterTest::TearDown() { |
+ EXPECT_TRUE(file_util::Delete(temp_file_path_, false)); |
+} |
+ |
+TEST_F(NativeMessagingWriterTest, GoodMessage) { |
+ base::DictionaryValue message; |
+ message.SetInteger("foo", 42); |
+ EXPECT_TRUE(writer()->WriteMessage(message)); |
+ base::ClosePlatformFile(temp_file_handle()); |
+ |
+ // Read the file and verify the content. |
+ std::string content; |
+ EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &content)); |
+ EXPECT_GT(content.length(), 4U); |
+ uint32 length = *reinterpret_cast<const uint32*>(content.data()); |
+ content.erase(0, 4); |
+ EXPECT_EQ(content.length(), length); |
+ |
+ // |content| should now contain serialized |message|. |
+ scoped_ptr<base::Value> written_message(base::JSONReader::Read(content)); |
+ EXPECT_TRUE(message.Equals(written_message.get())); |
+} |
+ |
+TEST_F(NativeMessagingWriterTest, SecondMessage) { |
+ base::DictionaryValue message1; |
+ base::DictionaryValue message2; |
+ message2.SetInteger("foo", 42); |
+ EXPECT_TRUE(writer()->WriteMessage(message1)); |
+ EXPECT_TRUE(writer()->WriteMessage(message2)); |
+ base::ClosePlatformFile(temp_file_handle()); |
+ |
+ // Read the file and verify the content. |
+ std::string content; |
+ EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &content)); |
+ EXPECT_GT(content.length(), 4U); |
+ uint32 length = *reinterpret_cast<const uint32*>(content.data()); |
+ content.erase(0, 4); // Remove header1 |
+ EXPECT_GT(content.length(), length); |
+ content.erase(0, length); // Remove body1 |
+ EXPECT_GT(content.length(), 4U); |
+ length = *reinterpret_cast<const uint32*>(content.data()); |
+ content.erase(0, 4); // Remove header2 |
+ EXPECT_EQ(content.length(), length); |
+ |
+ // |content| should now contain serialized |message2|. |
+ scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content)); |
+ EXPECT_TRUE(message2.Equals(written_message2.get())); |
+} |
+ |
+// TODO(lambroslambrou): Implement this test on OS_WIN as well. |
+#if defined(OS_POSIX) |
+ |
+TEST_F(NativeMessagingWriterTest, FailedWrite) { |
+ base::ClosePlatformFile(temp_file_handle()); |
+ |
+ int fds[2]; |
+ EXPECT_EQ(0, pipe(fds)); |
+ |
+ // Pass the write end to NativeMessagingWriter, and close the read end so |
+ // that writing fails. |
+ base::ClosePlatformFile(fds[0]); |
+ |
+ NativeMessagingWriter writer(fds[1]); |
+ base::DictionaryValue message; |
+ EXPECT_FALSE(writer.WriteMessage(message)); |
+} |
+ |
+#endif // defined(OS_POSIX) |
+ |
+} // namespace remoting |