| 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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 std::string content(length, '\0'); | 48 std::string content(length, '\0'); |
| 49 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); | 49 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); |
| 50 EXPECT_EQ(static_cast<int>(length), read); | 50 EXPECT_EQ(static_cast<int>(length), read); |
| 51 | 51 |
| 52 // |content| should now contain serialized |message|. | 52 // |content| should now contain serialized |message|. |
| 53 scoped_ptr<base::Value> written_message(base::JSONReader::Read(content)); | 53 scoped_ptr<base::Value> written_message(base::JSONReader::Read(content)); |
| 54 EXPECT_TRUE(message.Equals(written_message.get())); | 54 EXPECT_TRUE(message.Equals(written_message.get())); |
| 55 | 55 |
| 56 // Nothing more should have been written. Close the write-end of the pipe, | 56 // Nothing more should have been written. Close the write-end of the pipe, |
| 57 // and verify the read end immediately hits EOF. | 57 // and verify the read end immediately hits EOF. |
| 58 writer_.reset(NULL); | 58 writer_.reset(nullptr); |
| 59 char unused; | 59 char unused; |
| 60 read = read_file_.ReadAtCurrentPos(&unused, 1); | 60 read = read_file_.ReadAtCurrentPos(&unused, 1); |
| 61 EXPECT_LE(read, 0); | 61 EXPECT_LE(read, 0); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST_F(NativeMessagingWriterTest, SecondMessage) { | 64 TEST_F(NativeMessagingWriterTest, SecondMessage) { |
| 65 base::DictionaryValue message1; | 65 base::DictionaryValue message1; |
| 66 base::DictionaryValue message2; | 66 base::DictionaryValue message2; |
| 67 message2.SetInteger("foo", 42); | 67 message2.SetInteger("foo", 42); |
| 68 EXPECT_TRUE(writer_->WriteMessage(message1)); | 68 EXPECT_TRUE(writer_->WriteMessage(message1)); |
| 69 EXPECT_TRUE(writer_->WriteMessage(message2)); | 69 EXPECT_TRUE(writer_->WriteMessage(message2)); |
| 70 writer_.reset(NULL); | 70 writer_.reset(nullptr); |
| 71 | 71 |
| 72 // Read two messages. | 72 // Read two messages. |
| 73 uint32 length; | 73 uint32 length; |
| 74 int read; | 74 int read; |
| 75 std::string content; | 75 std::string content; |
| 76 for (int i = 0; i < 2; i++) { | 76 for (int i = 0; i < 2; i++) { |
| 77 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); | 77 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
| 78 EXPECT_EQ(4, read) << "i = " << i; | 78 EXPECT_EQ(4, read) << "i = " << i; |
| 79 content.resize(length); | 79 content.resize(length); |
| 80 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); | 80 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); |
| 81 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; | 81 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // |content| should now contain serialized |message2|. | 84 // |content| should now contain serialized |message2|. |
| 85 scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content)); | 85 scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content)); |
| 86 EXPECT_TRUE(message2.Equals(written_message2.get())); | 86 EXPECT_TRUE(message2.Equals(written_message2.get())); |
| 87 } | 87 } |
| 88 | 88 |
| 89 TEST_F(NativeMessagingWriterTest, FailedWrite) { | 89 TEST_F(NativeMessagingWriterTest, FailedWrite) { |
| 90 // Close the read end so that writing fails immediately. | 90 // Close the read end so that writing fails immediately. |
| 91 read_file_.Close(); | 91 read_file_.Close(); |
| 92 | 92 |
| 93 base::DictionaryValue message; | 93 base::DictionaryValue message; |
| 94 EXPECT_FALSE(writer_->WriteMessage(message)); | 94 EXPECT_FALSE(writer_->WriteMessage(message)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 } // namespace remoting | 97 } // namespace remoting |
| OLD | NEW |