Chromium Code Reviews| Index: remoting/host/security_key/remote_security_key_message_writer_unittest.cc |
| diff --git a/remoting/host/security_key/remote_security_key_message_writer_unittest.cc b/remoting/host/security_key/remote_security_key_message_writer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..07ec3a21de3a4b1df4b0355dd2def6abd375e28c |
| --- /dev/null |
| +++ b/remoting/host/security_key/remote_security_key_message_writer_unittest.cc |
| @@ -0,0 +1,189 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/security_key/remote_security_key_message_writer.h" |
| + |
| +#include <stdint.h> |
| + |
| +#include <utility> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/stl_util.h" |
| +#include "remoting/host/security_key/security_key_message.h" |
| +#include "remoting/host/setup/test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| +const uint8_t kTestMessageType = 42; |
| +const char kTestMessageTypeChar = static_cast<char>(kTestMessageType); |
| +const char kTestMessagePayload[] = "Super-test-payload!"; |
| +const unsigned int kBufferSizeBytes = 40000; |
| +} // namespace |
| + |
| +namespace remoting { |
| + |
| +class RemoteSecurityKeyMessageWriterTest : public testing::Test { |
| + public: |
| + RemoteSecurityKeyMessageWriterTest(); |
| + ~RemoteSecurityKeyMessageWriterTest() override; |
| + |
| + protected: |
| + // testing::Test interface. |
| + void SetUp() override; |
| + |
| + scoped_ptr<RemoteSecurityKeyMessageWriter> writer_; |
| + base::File read_file_; |
| + base::File write_file_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageWriterTest); |
| +}; |
| + |
| +RemoteSecurityKeyMessageWriterTest::RemoteSecurityKeyMessageWriterTest() {} |
| + |
| +RemoteSecurityKeyMessageWriterTest::~RemoteSecurityKeyMessageWriterTest() {} |
| + |
| +void RemoteSecurityKeyMessageWriterTest::SetUp() { |
| + ASSERT_TRUE(MakePipe(&read_file_, &write_file_, kBufferSizeBytes)); |
|
Sergey Ulanov
2016/03/28 18:26:43
Why do you need to set buffer size for this pipe?
joedow
2016/03/29 22:14:14
The short version is that the buffer size can vary
|
| + writer_.reset(new RemoteSecurityKeyMessageWriter(std::move(write_file_))); |
| +} |
| + |
| +TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithoutPayload) { |
| + ASSERT_TRUE(writer_->WriteMessage( |
| + static_cast<RemoteSecurityKeyMessageType>(kTestMessageType))); |
| + |
| + // Retrieve and verify the message header. |
| + uint32_t length; |
| + int bytes_read = |
| + read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
| + ASSERT_EQ(4, bytes_read); |
| + ASSERT_EQ(sizeof(kTestMessageType), length); |
| + |
| + // Retrieve and verify the message type. |
| + std::string message_type(length, '\0'); |
| + bytes_read = |
| + read_file_.ReadAtCurrentPos(string_as_array(&message_type), length); |
| + ASSERT_EQ(static_cast<int>(length), bytes_read); |
| + ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1)); |
| + |
| + // Destroy the writer and verify the other end of the pipe is clean. |
| + writer_.reset(); |
| + char unused; |
| + ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); |
| +} |
| + |
| +TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithPayload) { |
| + std::string test_message_payload(kTestMessagePayload); |
| + ASSERT_TRUE(writer_->WriteMessage( |
| + static_cast<RemoteSecurityKeyMessageType>(kTestMessageType), |
| + test_message_payload)); |
| + |
| + // Retrieve and verify the message header. |
| + uint32_t total_length; |
| + int bytes_read = |
| + read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&total_length), 4); |
| + ASSERT_EQ(4, bytes_read); |
| + ASSERT_EQ(sizeof(kTestMessageType) + test_message_payload.size(), |
| + total_length); |
| + |
| + // Retrieve and verify the message type. |
| + int message_type_length = 1; |
| + std::string message_type(message_type_length, '\0'); |
| + bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_type), |
| + message_type_length); |
| + ASSERT_EQ(message_type_length, bytes_read); |
| + ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1)); |
| + |
| + // Retrieve and verify the message payload. |
| + int message_payload_length = total_length - message_type_length; |
| + std::string message_payload(message_payload_length, '\0'); |
| + bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_payload), |
| + message_payload_length); |
| + ASSERT_EQ(message_payload_length, bytes_read); |
| + ASSERT_EQ(message_payload, test_message_payload); |
| + |
| + // Destroy the writer and verify the other end of the pipe is clean. |
| + writer_.reset(); |
| + char unused; |
| + ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); |
| +} |
| + |
| +TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithLargePayload) { |
| + std::string test_message_payload(kBufferSizeBytes - |
| + sizeof(RemoteSecurityKeyMessageType) - |
| + SecurityKeyMessage::kHeaderSizeBytes, |
|
Sergey Ulanov
2016/03/28 18:26:43
What if the message is larger than the buffer? I t
joedow
2016/03/29 22:14:14
Done.
|
| + 'Y'); |
| + ASSERT_TRUE(writer_->WriteMessage( |
| + static_cast<RemoteSecurityKeyMessageType>(kTestMessageType), |
| + test_message_payload)); |
| + |
| + // Retrieve and verify the message header. |
| + uint32_t total_length; |
| + int bytes_read = |
| + read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&total_length), 4); |
| + ASSERT_EQ(4, bytes_read); |
| + ASSERT_EQ(sizeof(kTestMessageType) + test_message_payload.size(), |
| + total_length); |
| + |
| + // Retrieve and verify the message type. |
| + int message_type_length = 1; |
| + std::string message_type(message_type_length, '\0'); |
| + bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_type), |
| + message_type_length); |
| + ASSERT_EQ(message_type_length, bytes_read); |
| + ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1)); |
| + |
| + // Retrieve and verify the message payload. |
| + int message_payload_length = total_length - message_type_length; |
| + std::string message_payload(message_payload_length, '\0'); |
| + bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_payload), |
| + message_payload_length); |
| + ASSERT_EQ(message_payload_length, bytes_read); |
| + ASSERT_EQ(message_payload, test_message_payload); |
| + |
| + // Destroy the writer and verify the other end of the pipe is clean. |
| + writer_.reset(); |
| + char unused; |
| + ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); |
| +} |
| + |
| +TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMultipleMessages) { |
| + int total_messages_to_write = 10; |
| + for (int i = 0; i < total_messages_to_write; i++) { |
| + ASSERT_TRUE(writer_->WriteMessage( |
| + static_cast<RemoteSecurityKeyMessageType>(kTestMessageType + i))); |
| + } |
| + |
| + for (int i = 0; i < total_messages_to_write; i++) { |
| + // Retrieve and verify the message header. |
| + uint32_t length; |
| + int bytes_read = |
| + read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4); |
| + ASSERT_EQ(4, bytes_read); |
| + ASSERT_EQ(sizeof(kTestMessageType), length); |
| + |
| + // Retrieve and verify the message type. |
| + std::string message_type(length, '\0'); |
| + bytes_read = |
| + read_file_.ReadAtCurrentPos(string_as_array(&message_type), length); |
| + ASSERT_EQ(static_cast<int>(length), bytes_read); |
| + char current_char = kTestMessageType + i; |
| + ASSERT_EQ(message_type, std::string(¤t_char, 1)); |
| + } |
| + |
| + // Destroy the writer and verify the other end of the pipe is clean. |
| + writer_.reset(); |
| + char unused; |
| + ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); |
| +} |
| + |
| +TEST_F(RemoteSecurityKeyMessageWriterTest, EnsureWriteFailsWhenPipeClosed) { |
| + // Close the read end so that writing fails immediately. |
| + read_file_.Close(); |
| + |
| + EXPECT_FALSE(writer_->WriteMessage( |
| + static_cast<RemoteSecurityKeyMessageType>(kTestMessageType))); |
| +} |
| + |
| +} // namespace remoting |