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

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

Powered by Google App Engine
This is Rietveld 408576698