OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/security_key/security_key_message_writer_impl.h" | 5 #include "remoting/host/security_key/security_key_message_writer_impl.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 DISALLOW_COPY_AND_ASSIGN(SecurityKeyMessageWriterImplTest); | 59 DISALLOW_COPY_AND_ASSIGN(SecurityKeyMessageWriterImplTest); |
60 }; | 60 }; |
61 | 61 |
62 SecurityKeyMessageWriterImplTest::SecurityKeyMessageWriterImplTest() {} | 62 SecurityKeyMessageWriterImplTest::SecurityKeyMessageWriterImplTest() {} |
63 | 63 |
64 SecurityKeyMessageWriterImplTest::~SecurityKeyMessageWriterImplTest() {} | 64 SecurityKeyMessageWriterImplTest::~SecurityKeyMessageWriterImplTest() {} |
65 | 65 |
66 std::string SecurityKeyMessageWriterImplTest::ReadMessage( | 66 std::string SecurityKeyMessageWriterImplTest::ReadMessage( |
67 int payload_length_bytes) { | 67 int payload_length_bytes) { |
68 std::string message_header(SecurityKeyMessage::kHeaderSizeBytes, '\0'); | 68 std::string message_header(SecurityKeyMessage::kHeaderSizeBytes, '\0'); |
69 read_file_.ReadAtCurrentPos(string_as_array(&message_header), | 69 read_file_.ReadAtCurrentPos(base::string_as_array(&message_header), |
70 SecurityKeyMessage::kHeaderSizeBytes); | 70 SecurityKeyMessage::kHeaderSizeBytes); |
71 | 71 |
72 std::string message_type(SecurityKeyMessage::kMessageTypeSizeBytes, '\0'); | 72 std::string message_type(SecurityKeyMessage::kMessageTypeSizeBytes, '\0'); |
73 read_file_.ReadAtCurrentPos(string_as_array(&message_type), | 73 read_file_.ReadAtCurrentPos(base::string_as_array(&message_type), |
74 SecurityKeyMessage::kMessageTypeSizeBytes); | 74 SecurityKeyMessage::kMessageTypeSizeBytes); |
75 | 75 |
76 std::string message_data(payload_length_bytes, '\0'); | 76 std::string message_data(payload_length_bytes, '\0'); |
77 if (payload_length_bytes) { | 77 if (payload_length_bytes) { |
78 read_file_.ReadAtCurrentPos(string_as_array(&message_data), | 78 read_file_.ReadAtCurrentPos(base::string_as_array(&message_data), |
79 payload_length_bytes); | 79 payload_length_bytes); |
80 } | 80 } |
81 | 81 |
82 return message_header + message_type + message_data; | 82 return message_header + message_type + message_data; |
83 } | 83 } |
84 | 84 |
85 void SecurityKeyMessageWriterImplTest::OnReadComplete( | 85 void SecurityKeyMessageWriterImplTest::OnReadComplete( |
86 const base::Closure& done_callback, | 86 const base::Closure& done_callback, |
87 const std::string& result) { | 87 const std::string& result) { |
88 message_result_ = result; | 88 message_result_ = result; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 for (int i = 0; i < total_messages_to_write; i++) { | 167 for (int i = 0; i < total_messages_to_write; i++) { |
168 // Retrieve and verify the message header. | 168 // Retrieve and verify the message header. |
169 int length; | 169 int length; |
170 ASSERT_EQ(SecurityKeyMessage::kHeaderSizeBytes, | 170 ASSERT_EQ(SecurityKeyMessage::kHeaderSizeBytes, |
171 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4)); | 171 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4)); |
172 ASSERT_EQ(SecurityKeyMessage::kMessageTypeSizeBytes, length); | 172 ASSERT_EQ(SecurityKeyMessage::kMessageTypeSizeBytes, length); |
173 | 173 |
174 // Retrieve and verify the message type. | 174 // Retrieve and verify the message type. |
175 std::string message_type(length, '\0'); | 175 std::string message_type(length, '\0'); |
176 int bytes_read = | 176 int bytes_read = read_file_.ReadAtCurrentPos( |
177 read_file_.ReadAtCurrentPos(string_as_array(&message_type), length); | 177 base::string_as_array(&message_type), length); |
178 ASSERT_EQ(length, bytes_read); | 178 ASSERT_EQ(length, bytes_read); |
179 | 179 |
180 SecurityKeyMessageType type = | 180 SecurityKeyMessageType type = |
181 SecurityKeyMessage::MessageTypeFromValue(message_type[0]); | 181 SecurityKeyMessage::MessageTypeFromValue(message_type[0]); |
182 if (i % 2 == 0) { | 182 if (i % 2 == 0) { |
183 ASSERT_EQ(SecurityKeyMessageType::CONNECT, type); | 183 ASSERT_EQ(SecurityKeyMessageType::CONNECT, type); |
184 } else { | 184 } else { |
185 ASSERT_EQ(SecurityKeyMessageType::REQUEST, type); | 185 ASSERT_EQ(SecurityKeyMessageType::REQUEST, type); |
186 } | 186 } |
187 } | 187 } |
188 | 188 |
189 // Destroy the writer and verify the other end of the pipe is clean. | 189 // Destroy the writer and verify the other end of the pipe is clean. |
190 writer_.reset(); | 190 writer_.reset(); |
191 char unused; | 191 char unused; |
192 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); | 192 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); |
193 } | 193 } |
194 | 194 |
195 TEST_F(SecurityKeyMessageWriterImplTest, EnsureWriteFailsWhenPipeClosed) { | 195 TEST_F(SecurityKeyMessageWriterImplTest, EnsureWriteFailsWhenPipeClosed) { |
196 // Close the read end so that writing fails immediately. | 196 // Close the read end so that writing fails immediately. |
197 read_file_.Close(); | 197 read_file_.Close(); |
198 | 198 |
199 EXPECT_FALSE(writer_->WriteMessage(kTestMessageType)); | 199 EXPECT_FALSE(writer_->WriteMessage(kTestMessageType)); |
200 } | 200 } |
201 | 201 |
202 } // namespace remoting | 202 } // namespace remoting |
OLD | NEW |