Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(786)

Unified Diff: remoting/host/security_key/remote_security_key_message_reader_unittest.cc

Issue 1818233005: Adding the message reading class used for remote_security_key STDIN communication. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@writer
Patch Set: Integrating security message changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/host/security_key/remote_security_key_message_reader_unittest.cc
diff --git a/remoting/host/security_key/remote_security_key_message_reader_unittest.cc b/remoting/host/security_key/remote_security_key_message_reader_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c20b5dfa5ee60046057752ad3e7933cb8f0bdc7b
--- /dev/null
+++ b/remoting/host/security_key/remote_security_key_message_reader_unittest.cc
@@ -0,0 +1,199 @@
+// 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_reader.h"
+
+#include <stdint.h>
+
+#include <string>
+#include <utility>
+
+#include "base/bind.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.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 kTestControlCode = 42;
+const unsigned int kBufferSizeBytes = 40000;
+const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024;
+} // namespace
+
+namespace remoting {
+
+class RemoteSecurityKeyMessageReaderTest : public testing::Test {
+ public:
+ RemoteSecurityKeyMessageReaderTest();
+ ~RemoteSecurityKeyMessageReaderTest() override;
+
+ // Starts the reader and runs the MessageLoop to completion.
+ void Run();
+
+ // SecurityKeyMessage::Callback passed to the Reader. Stores |message| so it
+ // can be verified by tests.
+ void OnMessage(scoped_ptr<SecurityKeyMessage> message);
+
+ // Writes a message (header+code+body) to the write-end of the pipe.
+ void WriteMessage(uint8_t message_type, const std::string& message_payload);
+
+ // Writes some data to the write-end of the pipe.
+ void WriteData(const char* data, int length);
+
+ protected:
+ // testing::Test interface.
+ void SetUp() override;
+
+ scoped_ptr<RemoteSecurityKeyMessageReader> reader_;
+ base::File read_file_;
+ base::File write_file_;
+ uint8_t last_message_type_ = 0;
+ std::string last_message_payload_;
+
+ std::vector<uint8_t> message_types_;
+ std::vector<std::string> message_payloads_;
+
+ private:
+ base::MessageLoopForIO message_loop_;
+ base::RunLoop run_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderTest);
+};
+
+RemoteSecurityKeyMessageReaderTest::RemoteSecurityKeyMessageReaderTest() {}
+RemoteSecurityKeyMessageReaderTest::~RemoteSecurityKeyMessageReaderTest() {}
+
+void RemoteSecurityKeyMessageReaderTest::SetUp() {
+ ASSERT_TRUE(MakePipe(&read_file_, &write_file_, kBufferSizeBytes));
+ reader_.reset(new RemoteSecurityKeyMessageReader(std::move(read_file_)));
+}
+
+void RemoteSecurityKeyMessageReaderTest::Run() {
+ // Close the write-end, so the reader doesn't block waiting for more data.
+ write_file_.Close();
+
+ // base::Unretained is safe since no further tasks can run after
+ // RunLoop::Run() returns.
+ reader_->Start(base::Bind(&RemoteSecurityKeyMessageReaderTest::OnMessage,
+ base::Unretained(this)),
+ run_loop_.QuitClosure());
+ run_loop_.Run();
+}
+
+void RemoteSecurityKeyMessageReaderTest::OnMessage(
+ scoped_ptr<SecurityKeyMessage> message) {
+ last_message_type_ = static_cast<uint8_t>(message->type());
+ last_message_payload_ = message->payload();
+
+ message_types_.push_back(last_message_type_);
+ message_payloads_.push_back(last_message_payload_);
+}
+
+void RemoteSecurityKeyMessageReaderTest::WriteMessage(
+ uint8_t message_type,
+ const std::string& message_payload) {
+ uint32_t length = sizeof(message_type) + message_payload.size();
+ WriteData(reinterpret_cast<char*>(&length),
+ SecurityKeyMessage::kHeaderSizeBytes);
+ WriteData(reinterpret_cast<char*>(&message_type), sizeof(message_type));
+ if (!message_payload.empty()) {
+ WriteData(message_payload.data(), message_payload.size());
+ }
+}
+
+void RemoteSecurityKeyMessageReaderTest::WriteData(const char* data,
+ int length) {
+ int written = write_file_.WriteAtCurrentPos(data, length);
+ ASSERT_EQ(length, written);
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithNoPayload) {
+ WriteMessage(kTestControlCode, std::string());
+ Run();
+ EXPECT_EQ(last_message_type_, kTestControlCode);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithPayload) {
+ std::string payload("I AM A VALID MESSAGE PAYLOAD!!!!!!!!!!!!!!!!!!!!!!");
+ WriteMessage(kTestControlCode, payload);
+ Run();
+ EXPECT_EQ(last_message_type_, kTestControlCode);
+ EXPECT_EQ(last_message_payload_, payload);
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithLargePayload) {
+ std::string payload(kBufferSizeBytes - sizeof(RemoteSecurityKeyMessageType) -
+ SecurityKeyMessage::kHeaderSizeBytes,
+ 'Y');
+ WriteMessage(kTestControlCode, payload);
+ Run();
+ EXPECT_EQ(last_message_type_, kTestControlCode);
+ EXPECT_EQ(last_message_payload_, payload);
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, EmptyFile) {
+ Run();
+ EXPECT_EQ(last_message_type_, 0);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, InvalidMessageLength) {
+ uint32_t length = kMaxSecurityKeyMessageByteCount + 1;
+ WriteData(reinterpret_cast<char*>(&length), sizeof(length));
+ Run();
+ EXPECT_EQ(last_message_type_, 0);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, ShortHeader) {
+ // Write only 3 bytes - the message length header is supposed to be 4 bytes.
+ WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1);
+ Run();
+ EXPECT_EQ(last_message_type_, 0);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, MissingControlCode) {
+ uint32_t length = 1;
+ WriteData(reinterpret_cast<char*>(&length), sizeof(length));
+ Run();
+ EXPECT_EQ(last_message_type_, 0);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, MissingPayload) {
+ uint32_t length = 2;
+ WriteData(reinterpret_cast<char*>(&length), sizeof(length));
+
+ char test_control_code = static_cast<char>(kTestControlCode);
+ WriteData(&test_control_code, sizeof(test_control_code));
+ Run();
+ EXPECT_EQ(last_message_type_, 0);
+ EXPECT_EQ(last_message_payload_, "");
+}
+
+TEST_F(RemoteSecurityKeyMessageReaderTest, MultipleMessages) {
+ const size_t kMessageCount = 11;
+ std::vector<std::string> payloads({"", "S", // Really short
+ "", "Short", "", "Medium Length", "",
+ "Longer than medium, but not super long",
+ "", std::string(2048, 'Y'), ""});
+ ASSERT_EQ(kMessageCount, payloads.size());
+
+ for (size_t i = 0; i < kMessageCount; i++) {
+ WriteMessage(kTestControlCode + i, payloads[i]);
+ }
+
+ Run();
+
+ for (size_t i = 0; i < kMessageCount; i++) {
+ ASSERT_EQ(message_types_[i], kTestControlCode + i);
+ ASSERT_EQ(message_payloads_[i], payloads[i]);
+ }
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698