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 <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 30 matching lines...) Expand all Loading... |
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(base::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 std::unique_ptr<base::Value> written_message = | 55 std::unique_ptr<base::Value> written_message = |
56 base::JSONReader::Read(content); | 56 base::JSONReader::Read(content); |
57 EXPECT_TRUE(message.Equals(written_message.get())); | 57 EXPECT_TRUE(message.Equals(written_message.get())); |
58 | 58 |
59 // 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, |
60 // and verify the read end immediately hits EOF. | 60 // and verify the read end immediately hits EOF. |
61 writer_.reset(nullptr); | 61 writer_.reset(nullptr); |
(...skipping 11 matching lines...) Expand all Loading... |
73 writer_.reset(nullptr); | 73 writer_.reset(nullptr); |
74 | 74 |
75 // Read two messages. | 75 // Read two messages. |
76 uint32_t length; | 76 uint32_t length; |
77 int read; | 77 int read; |
78 std::string content; | 78 std::string content; |
79 for (int i = 0; i < 2; i++) { | 79 for (int i = 0; i < 2; i++) { |
80 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); | 80 read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
81 EXPECT_EQ(4, read) << "i = " << i; | 81 EXPECT_EQ(4, read) << "i = " << i; |
82 content.resize(length); | 82 content.resize(length); |
83 read = read_file_.ReadAtCurrentPos(string_as_array(&content), length); | 83 read = read_file_.ReadAtCurrentPos(base::string_as_array(&content), length); |
84 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; | 84 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; |
85 } | 85 } |
86 | 86 |
87 // |content| should now contain serialized |message2|. | 87 // |content| should now contain serialized |message2|. |
88 std::unique_ptr<base::Value> written_message2 = | 88 std::unique_ptr<base::Value> written_message2 = |
89 base::JSONReader::Read(content); | 89 base::JSONReader::Read(content); |
90 EXPECT_TRUE(message2.Equals(written_message2.get())); | 90 EXPECT_TRUE(message2.Equals(written_message2.get())); |
91 } | 91 } |
92 | 92 |
93 TEST_F(NativeMessagingWriterTest, FailedWrite) { | 93 TEST_F(NativeMessagingWriterTest, FailedWrite) { |
94 // Close the read end so that writing fails immediately. | 94 // Close the read end so that writing fails immediately. |
95 read_file_.Close(); | 95 read_file_.Close(); |
96 | 96 |
97 base::DictionaryValue message; | 97 base::DictionaryValue message; |
98 EXPECT_FALSE(writer_->WriteMessage(message)); | 98 EXPECT_FALSE(writer_->WriteMessage(message)); |
99 } | 99 } |
100 | 100 |
101 } // namespace remoting | 101 } // namespace remoting |
OLD | NEW |