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

Side by Side Diff: remoting/host/security_key/remote_security_key_message_writer_impl_unittest.cc

Issue 2162083003: Renaming Gnubby and RemoteSecurityKey files/classes/members (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing a GYP build error Created 4 years, 5 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "remoting/host/security_key/remote_security_key_message_writer_impl.h"
6
7 #include <cstdint>
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "base/task_runner_util.h"
16 #include "base/threading/thread.h"
17 #include "base/time/time.h"
18 #include "remoting/host/security_key/security_key_message.h"
19 #include "remoting/host/setup/test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace {
23 const remoting::RemoteSecurityKeyMessageType kTestMessageType =
24 remoting::RemoteSecurityKeyMessageType::CONNECT;
25 const unsigned int kLargeMessageSizeBytes = 200000;
26 } // namespace
27
28 namespace remoting {
29
30 class RemoteSecurityKeyMessageWriterImplTest : public testing::Test {
31 public:
32 RemoteSecurityKeyMessageWriterImplTest();
33 ~RemoteSecurityKeyMessageWriterImplTest() override;
34
35 // Run on a separate thread, this method reads the message written to the
36 // output stream and returns the result.
37 std::string ReadMessage(int payload_length_bytes);
38
39 // Called back once the read operation has completed.
40 void OnReadComplete(const base::Closure& done_callback,
41 const std::string& result);
42
43 protected:
44 // testing::Test interface.
45 void SetUp() override;
46
47 // Writes |kTestMessageType| and |payload| to the output stream and verifies
48 // they were written correctly.
49 void WriteMessageToOutput(const std::string& payload);
50
51 std::unique_ptr<RemoteSecurityKeyMessageWriter> writer_;
52 base::File read_file_;
53 base::File write_file_;
54
55 // Stores the result of the last read operation.
56 std::string message_result_;
57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageWriterImplTest);
60 };
61
62 RemoteSecurityKeyMessageWriterImplTest::
63 RemoteSecurityKeyMessageWriterImplTest() {}
64
65 RemoteSecurityKeyMessageWriterImplTest::
66 ~RemoteSecurityKeyMessageWriterImplTest() {}
67
68 std::string RemoteSecurityKeyMessageWriterImplTest::ReadMessage(
69 int payload_length_bytes) {
70 std::string message_header(SecurityKeyMessage::kHeaderSizeBytes, '\0');
71 read_file_.ReadAtCurrentPos(string_as_array(&message_header),
72 SecurityKeyMessage::kHeaderSizeBytes);
73
74 std::string message_type(SecurityKeyMessage::kMessageTypeSizeBytes, '\0');
75 read_file_.ReadAtCurrentPos(string_as_array(&message_type),
76 SecurityKeyMessage::kMessageTypeSizeBytes);
77
78 std::string message_data(payload_length_bytes, '\0');
79 if (payload_length_bytes) {
80 read_file_.ReadAtCurrentPos(string_as_array(&message_data),
81 payload_length_bytes);
82 }
83
84 return message_header + message_type + message_data;
85 }
86
87 void RemoteSecurityKeyMessageWriterImplTest::OnReadComplete(
88 const base::Closure& done_callback,
89 const std::string& result) {
90 message_result_ = result;
91 done_callback.Run();
92 }
93
94 void RemoteSecurityKeyMessageWriterImplTest::SetUp() {
95 ASSERT_TRUE(MakePipe(&read_file_, &write_file_));
96 writer_.reset(new RemoteSecurityKeyMessageWriterImpl(std::move(write_file_)));
97 }
98
99 void RemoteSecurityKeyMessageWriterImplTest::WriteMessageToOutput(
100 const std::string& payload) {
101 // Thread used for blocking IO operations.
102 base::Thread reader_thread("ReaderThread");
103
104 base::Thread::Options options;
105 options.message_loop_type = base::MessageLoop::TYPE_IO;
106 reader_thread.StartWithOptions(options);
107
108 // Used to block until the read complete callback is triggered.
109 base::MessageLoopForIO message_loop;
110 base::RunLoop run_loop;
111
112 ASSERT_TRUE(base::PostTaskAndReplyWithResult(
113 reader_thread.task_runner().get(), FROM_HERE,
114 base::Bind(&RemoteSecurityKeyMessageWriterImplTest::ReadMessage,
115 base::Unretained(this), payload.size()),
116 base::Bind(&RemoteSecurityKeyMessageWriterImplTest::OnReadComplete,
117 base::Unretained(this), run_loop.QuitClosure())));
118
119 if (payload.size()) {
120 ASSERT_TRUE(writer_->WriteMessageWithPayload(kTestMessageType, payload));
121 } else {
122 ASSERT_TRUE(writer_->WriteMessage(kTestMessageType));
123 }
124
125 run_loop.Run();
126
127 size_t total_size = SecurityKeyMessage::kHeaderSizeBytes +
128 SecurityKeyMessage::kMessageTypeSizeBytes +
129 payload.size();
130 ASSERT_EQ(message_result_.size(), total_size);
131
132 RemoteSecurityKeyMessageType type =
133 SecurityKeyMessage::MessageTypeFromValue(message_result_[4]);
134 ASSERT_EQ(kTestMessageType, type);
135
136 if (payload.size()) {
137 ASSERT_EQ(message_result_.substr(5), payload);
138 }
139
140 // Destroy the writer and verify the other end of the pipe is clean.
141 writer_.reset();
142 char unused;
143 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
144 }
145
146 TEST_F(RemoteSecurityKeyMessageWriterImplTest, WriteMessageWithoutPayload) {
147 std::string empty_payload;
148 WriteMessageToOutput(empty_payload);
149 }
150
151 TEST_F(RemoteSecurityKeyMessageWriterImplTest, WriteMessageWithPayload) {
152 WriteMessageToOutput("Super-test-payload!");
153 }
154
155 TEST_F(RemoteSecurityKeyMessageWriterImplTest, WriteMessageWithLargePayload) {
156 WriteMessageToOutput(std::string(kLargeMessageSizeBytes, 'Y'));
157 }
158
159 TEST_F(RemoteSecurityKeyMessageWriterImplTest, WriteMultipleMessages) {
160 int total_messages_to_write = 10;
161 for (int i = 0; i < total_messages_to_write; i++) {
162 if (i % 2 == 0) {
163 ASSERT_TRUE(writer_->WriteMessage(RemoteSecurityKeyMessageType::CONNECT));
164 } else {
165 ASSERT_TRUE(writer_->WriteMessage(RemoteSecurityKeyMessageType::REQUEST));
166 }
167 }
168
169 for (int i = 0; i < total_messages_to_write; i++) {
170 // Retrieve and verify the message header.
171 int length;
172 ASSERT_EQ(SecurityKeyMessage::kHeaderSizeBytes,
173 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4));
174 ASSERT_EQ(SecurityKeyMessage::kMessageTypeSizeBytes, length);
175
176 // Retrieve and verify the message type.
177 std::string message_type(length, '\0');
178 int bytes_read =
179 read_file_.ReadAtCurrentPos(string_as_array(&message_type), length);
180 ASSERT_EQ(length, bytes_read);
181
182 RemoteSecurityKeyMessageType type =
183 SecurityKeyMessage::MessageTypeFromValue(message_type[0]);
184 if (i % 2 == 0) {
185 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT, type);
186 } else {
187 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST, type);
188 }
189 }
190
191 // Destroy the writer and verify the other end of the pipe is clean.
192 writer_.reset();
193 char unused;
194 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
195 }
196
197 TEST_F(RemoteSecurityKeyMessageWriterImplTest, EnsureWriteFailsWhenPipeClosed) {
198 // Close the read end so that writing fails immediately.
199 read_file_.Close();
200
201 EXPECT_FALSE(writer_->WriteMessage(kTestMessageType));
202 }
203
204 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698