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/file_util.h" |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/platform_file.h" |
| 10 #include "base/values.h" |
| 11 #include "remoting/host/setup/native_messaging_writer.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 #if defined(OS_POSIX) |
| 15 #include <unistd.h> |
| 16 #endif // defined(OS_POSIX) |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 class NativeMessagingWriterTest : public testing::Test { |
| 21 public: |
| 22 NativeMessagingWriterTest(); |
| 23 virtual ~NativeMessagingWriterTest(); |
| 24 |
| 25 virtual void SetUp() OVERRIDE; |
| 26 virtual void TearDown() OVERRIDE; |
| 27 |
| 28 NativeMessagingWriter* writer() const { return writer_.get(); } |
| 29 base::FilePath temp_file_path() const { return temp_file_path_; } |
| 30 base::PlatformFile temp_file_handle() const { return temp_file_handle_; } |
| 31 |
| 32 private: |
| 33 scoped_ptr<NativeMessagingWriter> writer_; |
| 34 base::FilePath temp_file_path_; |
| 35 base::PlatformFile temp_file_handle_; |
| 36 }; |
| 37 |
| 38 NativeMessagingWriterTest::NativeMessagingWriterTest() {} |
| 39 |
| 40 NativeMessagingWriterTest::~NativeMessagingWriterTest() {} |
| 41 |
| 42 void NativeMessagingWriterTest::SetUp() { |
| 43 file_util::CreateTemporaryFile(&temp_file_path_); |
| 44 temp_file_handle_ = base::CreatePlatformFile( |
| 45 temp_file_path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 46 NULL, NULL); |
| 47 writer_.reset(new NativeMessagingWriter(temp_file_handle_)); |
| 48 } |
| 49 |
| 50 void NativeMessagingWriterTest::TearDown() { |
| 51 EXPECT_TRUE(file_util::Delete(temp_file_path_, false)); |
| 52 } |
| 53 |
| 54 TEST_F(NativeMessagingWriterTest, GoodMessage) { |
| 55 base::DictionaryValue message; |
| 56 message.SetInteger("foo", 42); |
| 57 EXPECT_TRUE(writer()->WriteMessage(message)); |
| 58 base::ClosePlatformFile(temp_file_handle()); |
| 59 |
| 60 // Read the file and verify the content. |
| 61 std::string content; |
| 62 EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &content)); |
| 63 EXPECT_GT(content.length(), 4U); |
| 64 uint32 length = *reinterpret_cast<const uint32*>(content.data()); |
| 65 content.erase(0, 4); |
| 66 EXPECT_EQ(content.length(), length); |
| 67 |
| 68 // |content| should now contain serialized |message|. |
| 69 scoped_ptr<base::Value> written_message(base::JSONReader::Read(content)); |
| 70 EXPECT_TRUE(message.Equals(written_message.get())); |
| 71 } |
| 72 |
| 73 TEST_F(NativeMessagingWriterTest, SecondMessage) { |
| 74 base::DictionaryValue message1; |
| 75 base::DictionaryValue message2; |
| 76 message2.SetInteger("foo", 42); |
| 77 EXPECT_TRUE(writer()->WriteMessage(message1)); |
| 78 EXPECT_TRUE(writer()->WriteMessage(message2)); |
| 79 base::ClosePlatformFile(temp_file_handle()); |
| 80 |
| 81 // Read the file and verify the content. |
| 82 std::string content; |
| 83 EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &content)); |
| 84 EXPECT_GT(content.length(), 4U); |
| 85 uint32 length = *reinterpret_cast<const uint32*>(content.data()); |
| 86 content.erase(0, 4); // Remove header1 |
| 87 EXPECT_GT(content.length(), length); |
| 88 content.erase(0, length); // Remove body1 |
| 89 EXPECT_GT(content.length(), 4U); |
| 90 length = *reinterpret_cast<const uint32*>(content.data()); |
| 91 content.erase(0, 4); // Remove header2 |
| 92 EXPECT_EQ(content.length(), length); |
| 93 |
| 94 // |content| should now contain serialized |message2|. |
| 95 scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content)); |
| 96 EXPECT_TRUE(message2.Equals(written_message2.get())); |
| 97 } |
| 98 |
| 99 // TODO(lambroslambrou): Implement this test on OS_WIN as well. |
| 100 #if defined(OS_POSIX) |
| 101 |
| 102 TEST_F(NativeMessagingWriterTest, FailedWrite) { |
| 103 base::ClosePlatformFile(temp_file_handle()); |
| 104 |
| 105 int fds[2]; |
| 106 EXPECT_EQ(0, pipe(fds)); |
| 107 |
| 108 // Pass the write end to NativeMessagingWriter, and close the read end so |
| 109 // that writing fails. |
| 110 base::ClosePlatformFile(fds[0]); |
| 111 |
| 112 NativeMessagingWriter writer(fds[1]); |
| 113 base::DictionaryValue message; |
| 114 EXPECT_FALSE(writer.WriteMessage(message)); |
| 115 } |
| 116 |
| 117 #endif // defined(OS_POSIX) |
| 118 |
| 119 } // namespace remoting |
OLD | NEW |