| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/native_messaging/native_messaging_writer.h" | 5 #include "remoting/host/native_messaging/native_messaging_writer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "remoting/host/setup/test_util.h" | 15 #include "remoting/host/setup/test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace remoting { | 18 namespace remoting { |
| 19 | 19 |
| 20 class NativeMessagingWriterTest : public testing::Test { | 20 class NativeMessagingWriterTest : public testing::Test { |
| 21 public: | 21 public: |
| 22 NativeMessagingWriterTest(); | 22 NativeMessagingWriterTest(); |
| 23 ~NativeMessagingWriterTest() override; | 23 ~NativeMessagingWriterTest() override; |
| 24 | 24 |
| 25 void SetUp() override; | 25 void SetUp() override; |
| 26 | 26 |
| 27 protected: | 27 protected: |
| 28 scoped_ptr<NativeMessagingWriter> writer_; | 28 std::unique_ptr<NativeMessagingWriter> writer_; |
| 29 base::File read_file_; | 29 base::File read_file_; |
| 30 base::File write_file_; | 30 base::File write_file_; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 NativeMessagingWriterTest::NativeMessagingWriterTest() {} | 33 NativeMessagingWriterTest::NativeMessagingWriterTest() {} |
| 34 NativeMessagingWriterTest::~NativeMessagingWriterTest() {} | 34 NativeMessagingWriterTest::~NativeMessagingWriterTest() {} |
| 35 | 35 |
| 36 void NativeMessagingWriterTest::SetUp() { | 36 void NativeMessagingWriterTest::SetUp() { |
| 37 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); | 37 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); |
| 38 writer_.reset(new NativeMessagingWriter(std::move(write_file_))); | 38 writer_.reset(new NativeMessagingWriter(std::move(write_file_))); |
| 39 } | 39 } |
| 40 | 40 |
| 41 TEST_F(NativeMessagingWriterTest, GoodMessage) { | 41 TEST_F(NativeMessagingWriterTest, GoodMessage) { |
| 42 base::DictionaryValue message; | 42 base::DictionaryValue message; |
| 43 message.SetInteger("foo", 42); | 43 message.SetInteger("foo", 42); |
| 44 EXPECT_TRUE(writer_->WriteMessage(message)); | 44 EXPECT_TRUE(writer_->WriteMessage(message)); |
| 45 | 45 |
| 46 // Read from the pipe and verify the content. | 46 // Read from the pipe and verify the content. |
| 47 uint32_t length; | 47 uint32_t length; |
| 48 int read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); | 48 int read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
| 49 EXPECT_EQ(4, read); | 49 EXPECT_EQ(4, read); |
| 50 std::string content(length, '\0'); | 50 std::string content(length, '\0'); |
| 51 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); | 51 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); |
| 52 EXPECT_EQ(static_cast<int>(length), read); | 52 EXPECT_EQ(static_cast<int>(length), read); |
| 53 | 53 |
| 54 // |content| should now contain serialized |message|. | 54 // |content| should now contain serialized |message|. |
| 55 scoped_ptr<base::Value> written_message = base::JSONReader::Read(content); | 55 std::unique_ptr<base::Value> written_message = |
| 56 base::JSONReader::Read(content); |
| 56 EXPECT_TRUE(message.Equals(written_message.get())); | 57 EXPECT_TRUE(message.Equals(written_message.get())); |
| 57 | 58 |
| 58 // Nothing more should have been written. Close the write-end of the pipe, | 59 // Nothing more should have been written. Close the write-end of the pipe, |
| 59 // and verify the read end immediately hits EOF. | 60 // and verify the read end immediately hits EOF. |
| 60 writer_.reset(nullptr); | 61 writer_.reset(nullptr); |
| 61 char unused; | 62 char unused; |
| 62 read = read_file_.ReadAtCurrentPos(&unused, 1); | 63 read = read_file_.ReadAtCurrentPos(&unused, 1); |
| 63 EXPECT_LE(read, 0); | 64 EXPECT_LE(read, 0); |
| 64 } | 65 } |
| 65 | 66 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 std::string content; | 78 std::string content; |
| 78 for (int i = 0; i < 2; i++) { | 79 for (int i = 0; i < 2; i++) { |
| 79 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); | 80 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
| 80 EXPECT_EQ(4, read) << "i = " << i; | 81 EXPECT_EQ(4, read) << "i = " << i; |
| 81 content.resize(length); | 82 content.resize(length); |
| 82 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); | 83 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); |
| 83 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; | 84 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; |
| 84 } | 85 } |
| 85 | 86 |
| 86 // |content| should now contain serialized |message2|. | 87 // |content| should now contain serialized |message2|. |
| 87 scoped_ptr<base::Value> written_message2 = base::JSONReader::Read(content); | 88 std::unique_ptr<base::Value> written_message2 = |
| 89 base::JSONReader::Read(content); |
| 88 EXPECT_TRUE(message2.Equals(written_message2.get())); | 90 EXPECT_TRUE(message2.Equals(written_message2.get())); |
| 89 } | 91 } |
| 90 | 92 |
| 91 TEST_F(NativeMessagingWriterTest, FailedWrite) { | 93 TEST_F(NativeMessagingWriterTest, FailedWrite) { |
| 92 // Close the read end so that writing fails immediately. | 94 // Close the read end so that writing fails immediately. |
| 93 read_file_.Close(); | 95 read_file_.Close(); |
| 94 | 96 |
| 95 base::DictionaryValue message; | 97 base::DictionaryValue message; |
| 96 EXPECT_FALSE(writer_->WriteMessage(message)); | 98 EXPECT_FALSE(writer_->WriteMessage(message)); |
| 97 } | 99 } |
| 98 | 100 |
| 99 } // namespace remoting | 101 } // namespace remoting |
| OLD | NEW |