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

Side by Side Diff: remoting/host/security_key/security_key_message_reader_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_reader_impl.h" 5 #include "remoting/host/security_key/security_key_message_reader_impl.h"
6 6
7 #include <cstdint> 7 #include <cstdint>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "remoting/host/security_key/security_key_message.h" 15 #include "remoting/host/security_key/security_key_message.h"
16 #include "remoting/host/setup/test_util.h" 16 #include "remoting/host/setup/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 namespace { 19 namespace {
20 const remoting::RemoteSecurityKeyMessageType kTestMessageType = 20 const remoting::SecurityKeyMessageType kTestMessageType =
21 remoting::RemoteSecurityKeyMessageType::CONNECT; 21 remoting::SecurityKeyMessageType::CONNECT;
22 const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024; 22 const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024;
23 } // namespace 23 } // namespace
24 24
25 namespace remoting { 25 namespace remoting {
26 26
27 class RemoteSecurityKeyMessageReaderImplTest : public testing::Test { 27 class SecurityKeyMessageReaderImplTest : public testing::Test {
28 public: 28 public:
29 RemoteSecurityKeyMessageReaderImplTest(); 29 SecurityKeyMessageReaderImplTest();
30 ~RemoteSecurityKeyMessageReaderImplTest() override; 30 ~SecurityKeyMessageReaderImplTest() override;
31 31
32 // SecurityKeyMessageCallback passed to the Reader. Stores |message| so it can 32 // SecurityKeyMessageCallback passed to the Reader. Stores |message| so it can
33 // be verified by tests. 33 // be verified by tests.
34 void OnMessage(std::unique_ptr<SecurityKeyMessage> message); 34 void OnMessage(std::unique_ptr<SecurityKeyMessage> message);
35 35
36 // Used as a callback to signal completion. 36 // Used as a callback to signal completion.
37 void OperationComplete(); 37 void OperationComplete();
38 38
39 protected: 39 protected:
40 // testing::Test interface. 40 // testing::Test interface.
41 void SetUp() override; 41 void SetUp() override;
42 42
43 // Runs the MessageLoop until the reader has completed and called back. 43 // Runs the MessageLoop until the reader has completed and called back.
44 void RunLoop(); 44 void RunLoop();
45 45
46 // Closes |write_file_| and runs the MessageLoop until the reader has 46 // Closes |write_file_| and runs the MessageLoop until the reader has
47 // completed and called back. 47 // completed and called back.
48 void CloseWriteFileAndRunLoop(); 48 void CloseWriteFileAndRunLoop();
49 49
50 // Writes a message (header+code+body) to the write-end of the pipe. 50 // Writes a message (header+code+body) to the write-end of the pipe.
51 void WriteMessage(RemoteSecurityKeyMessageType message_type, 51 void WriteMessage(SecurityKeyMessageType message_type,
52 const std::string& message_payload); 52 const std::string& message_payload);
53 53
54 // Writes some data to the write-end of the pipe. 54 // Writes some data to the write-end of the pipe.
55 void WriteData(const char* data, int length); 55 void WriteData(const char* data, int length);
56 56
57 std::unique_ptr<RemoteSecurityKeyMessageReader> reader_; 57 std::unique_ptr<SecurityKeyMessageReader> reader_;
58 base::File read_file_; 58 base::File read_file_;
59 base::File write_file_; 59 base::File write_file_;
60 60
61 std::vector<std::unique_ptr<SecurityKeyMessage>> messages_received_; 61 std::vector<std::unique_ptr<SecurityKeyMessage>> messages_received_;
62 62
63 private: 63 private:
64 base::MessageLoopForIO message_loop_; 64 base::MessageLoopForIO message_loop_;
65 std::unique_ptr<base::RunLoop> run_loop_; 65 std::unique_ptr<base::RunLoop> run_loop_;
66 66
67 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderImplTest); 67 DISALLOW_COPY_AND_ASSIGN(SecurityKeyMessageReaderImplTest);
68 }; 68 };
69 69
70 RemoteSecurityKeyMessageReaderImplTest::RemoteSecurityKeyMessageReaderImplTest() 70 SecurityKeyMessageReaderImplTest::SecurityKeyMessageReaderImplTest()
71 : run_loop_(new base::RunLoop()) {} 71 : run_loop_(new base::RunLoop()) {}
72 72
73 RemoteSecurityKeyMessageReaderImplTest:: 73 SecurityKeyMessageReaderImplTest::~SecurityKeyMessageReaderImplTest() {}
74 ~RemoteSecurityKeyMessageReaderImplTest() {}
75 74
76 void RemoteSecurityKeyMessageReaderImplTest::SetUp() { 75 void SecurityKeyMessageReaderImplTest::SetUp() {
77 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); 76 ASSERT_TRUE(MakePipe(&read_file_, &write_file_));
78 reader_.reset(new RemoteSecurityKeyMessageReaderImpl(std::move(read_file_))); 77 reader_.reset(new SecurityKeyMessageReaderImpl(std::move(read_file_)));
79 78
80 // base::Unretained is safe since no further tasks can run after 79 // base::Unretained is safe since no further tasks can run after
81 // RunLoop::Run() returns. 80 // RunLoop::Run() returns.
82 reader_->Start( 81 reader_->Start(
83 base::Bind(&RemoteSecurityKeyMessageReaderImplTest::OnMessage, 82 base::Bind(&SecurityKeyMessageReaderImplTest::OnMessage,
84 base::Unretained(this)), 83 base::Unretained(this)),
85 base::Bind(&RemoteSecurityKeyMessageReaderImplTest::OperationComplete, 84 base::Bind(&SecurityKeyMessageReaderImplTest::OperationComplete,
86 base::Unretained(this))); 85 base::Unretained(this)));
87 } 86 }
88 87
89 void RemoteSecurityKeyMessageReaderImplTest::RunLoop() { 88 void SecurityKeyMessageReaderImplTest::RunLoop() {
90 run_loop_->Run(); 89 run_loop_->Run();
91 run_loop_.reset(new base::RunLoop()); 90 run_loop_.reset(new base::RunLoop());
92 } 91 }
93 92
94 void RemoteSecurityKeyMessageReaderImplTest::CloseWriteFileAndRunLoop() { 93 void SecurityKeyMessageReaderImplTest::CloseWriteFileAndRunLoop() {
95 write_file_.Close(); 94 write_file_.Close();
96 run_loop_->Run(); 95 run_loop_->Run();
97 run_loop_.reset(new base::RunLoop()); 96 run_loop_.reset(new base::RunLoop());
98 } 97 }
99 98
100 void RemoteSecurityKeyMessageReaderImplTest::OnMessage( 99 void SecurityKeyMessageReaderImplTest::OnMessage(
101 std::unique_ptr<SecurityKeyMessage> message) { 100 std::unique_ptr<SecurityKeyMessage> message) {
102 messages_received_.push_back(std::move(message)); 101 messages_received_.push_back(std::move(message));
103 OperationComplete(); 102 OperationComplete();
104 } 103 }
105 104
106 void RemoteSecurityKeyMessageReaderImplTest::OperationComplete() { 105 void SecurityKeyMessageReaderImplTest::OperationComplete() {
107 run_loop_->Quit(); 106 run_loop_->Quit();
108 } 107 }
109 108
110 void RemoteSecurityKeyMessageReaderImplTest::WriteMessage( 109 void SecurityKeyMessageReaderImplTest::WriteMessage(
111 RemoteSecurityKeyMessageType message_type, 110 SecurityKeyMessageType message_type,
112 const std::string& message_payload) { 111 const std::string& message_payload) {
113 uint32_t length = 112 uint32_t length =
114 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload.size(); 113 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload.size();
115 WriteData(reinterpret_cast<char*>(&length), 114 WriteData(reinterpret_cast<char*>(&length),
116 SecurityKeyMessage::kHeaderSizeBytes); 115 SecurityKeyMessage::kHeaderSizeBytes);
117 WriteData(reinterpret_cast<char*>(&message_type), 116 WriteData(reinterpret_cast<char*>(&message_type),
118 SecurityKeyMessage::kMessageTypeSizeBytes); 117 SecurityKeyMessage::kMessageTypeSizeBytes);
119 if (!message_payload.empty()) { 118 if (!message_payload.empty()) {
120 WriteData(message_payload.data(), message_payload.size()); 119 WriteData(message_payload.data(), message_payload.size());
121 } 120 }
122 } 121 }
123 122
124 void RemoteSecurityKeyMessageReaderImplTest::WriteData(const char* data, 123 void SecurityKeyMessageReaderImplTest::WriteData(const char* data, int length) {
125 int length) {
126 int written = write_file_.WriteAtCurrentPos(data, length); 124 int written = write_file_.WriteAtCurrentPos(data, length);
127 ASSERT_EQ(length, written); 125 ASSERT_EQ(length, written);
128 } 126 }
129 127
130 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithNoPayload) { 128 TEST_F(SecurityKeyMessageReaderImplTest, SingleMessageWithNoPayload) {
131 WriteMessage(kTestMessageType, std::string()); 129 WriteMessage(kTestMessageType, std::string());
132 RunLoop(); 130 RunLoop();
133 ASSERT_EQ(1u, messages_received_.size()); 131 ASSERT_EQ(1u, messages_received_.size());
134 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); 132 ASSERT_EQ(kTestMessageType, messages_received_[0]->type());
135 ASSERT_EQ("", messages_received_[0]->payload()); 133 ASSERT_EQ("", messages_received_[0]->payload());
136 134
137 CloseWriteFileAndRunLoop(); 135 CloseWriteFileAndRunLoop();
138 } 136 }
139 137
140 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithPayload) { 138 TEST_F(SecurityKeyMessageReaderImplTest, SingleMessageWithPayload) {
141 std::string payload("I AM A VALID MESSAGE PAYLOAD!!!!!!!!!!!!!!!!!!!!!!"); 139 std::string payload("I AM A VALID MESSAGE PAYLOAD!!!!!!!!!!!!!!!!!!!!!!");
142 WriteMessage(kTestMessageType, payload); 140 WriteMessage(kTestMessageType, payload);
143 RunLoop(); 141 RunLoop();
144 ASSERT_EQ(1u, messages_received_.size()); 142 ASSERT_EQ(1u, messages_received_.size());
145 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); 143 ASSERT_EQ(kTestMessageType, messages_received_[0]->type());
146 ASSERT_EQ(payload, messages_received_[0]->payload()); 144 ASSERT_EQ(payload, messages_received_[0]->payload());
147 145
148 CloseWriteFileAndRunLoop(); 146 CloseWriteFileAndRunLoop();
149 } 147 }
150 148
151 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageViaSingleWrite) { 149 TEST_F(SecurityKeyMessageReaderImplTest, SingleMessageViaSingleWrite) {
152 // All other tests write in 2-3 chunks, this writes the message in one shot. 150 // All other tests write in 2-3 chunks, this writes the message in one shot.
153 std::string payload("LLLLTI am the best payload in the history of testing."); 151 std::string payload("LLLLTI am the best payload in the history of testing.");
154 // Overwite the 'L' values with the actual length. 152 // Overwite the 'L' values with the actual length.
155 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes; 153 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes;
156 payload[0] = static_cast<char>(length); 154 payload[0] = static_cast<char>(length);
157 payload[1] = 0; 155 payload[1] = 0;
158 payload[2] = 0; 156 payload[2] = 0;
159 payload[3] = 0; 157 payload[3] = 0;
160 // Overwite the 'T' value with the actual type. 158 // Overwite the 'T' value with the actual type.
161 payload[4] = static_cast<char>(kTestMessageType); 159 payload[4] = static_cast<char>(kTestMessageType);
162 WriteData(payload.data(), payload.size()); 160 WriteData(payload.data(), payload.size());
163 RunLoop(); 161 RunLoop();
164 ASSERT_EQ(1u, messages_received_.size()); 162 ASSERT_EQ(1u, messages_received_.size());
165 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); 163 ASSERT_EQ(kTestMessageType, messages_received_[0]->type());
166 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload()); 164 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload());
167 165
168 CloseWriteFileAndRunLoop(); 166 CloseWriteFileAndRunLoop();
169 } 167 }
170 168
171 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageViaMultipleWrites) { 169 TEST_F(SecurityKeyMessageReaderImplTest, SingleMessageViaMultipleWrites) {
172 // All other tests write in 2-3 chunks, this writes the message byte by byte. 170 // All other tests write in 2-3 chunks, this writes the message byte by byte.
173 std::string payload("LLLLTI am the worst payload in the history of testing."); 171 std::string payload("LLLLTI am the worst payload in the history of testing.");
174 // Overwite the 'L' values with the actual length. 172 // Overwite the 'L' values with the actual length.
175 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes; 173 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes;
176 payload[0] = static_cast<char>(length); 174 payload[0] = static_cast<char>(length);
177 payload[1] = 0; 175 payload[1] = 0;
178 payload[2] = 0; 176 payload[2] = 0;
179 payload[3] = 0; 177 payload[3] = 0;
180 // Overwite the 'T' value with the actual type. 178 // Overwite the 'T' value with the actual type.
181 payload[4] = static_cast<char>(kTestMessageType); 179 payload[4] = static_cast<char>(kTestMessageType);
182 180
183 for (uint32_t i = 0; i < payload.size(); i++) { 181 for (uint32_t i = 0; i < payload.size(); i++) {
184 WriteData(&payload[i], 1); 182 WriteData(&payload[i], 1);
185 } 183 }
186 RunLoop(); 184 RunLoop();
187 ASSERT_EQ(1u, messages_received_.size()); 185 ASSERT_EQ(1u, messages_received_.size());
188 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); 186 ASSERT_EQ(kTestMessageType, messages_received_[0]->type());
189 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload()); 187 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload());
190 188
191 CloseWriteFileAndRunLoop(); 189 CloseWriteFileAndRunLoop();
192 } 190 }
193 191
194 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithLargePayload) { 192 TEST_F(SecurityKeyMessageReaderImplTest, SingleMessageWithLargePayload) {
195 std::string payload(kMaxSecurityKeyMessageByteCount - 193 std::string payload(kMaxSecurityKeyMessageByteCount -
196 SecurityKeyMessage::kMessageTypeSizeBytes, 194 SecurityKeyMessage::kMessageTypeSizeBytes,
197 'Y'); 195 'Y');
198 WriteMessage(kTestMessageType, payload); 196 WriteMessage(kTestMessageType, payload);
199 RunLoop(); 197 RunLoop();
200 ASSERT_EQ(1u, messages_received_.size()); 198 ASSERT_EQ(1u, messages_received_.size());
201 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); 199 ASSERT_EQ(kTestMessageType, messages_received_[0]->type());
202 ASSERT_EQ(payload, messages_received_[0]->payload()); 200 ASSERT_EQ(payload, messages_received_[0]->payload());
203 201
204 CloseWriteFileAndRunLoop(); 202 CloseWriteFileAndRunLoop();
205 } 203 }
206 204
207 TEST_F(RemoteSecurityKeyMessageReaderImplTest, EmptyFile) { 205 TEST_F(SecurityKeyMessageReaderImplTest, EmptyFile) {
208 CloseWriteFileAndRunLoop(); 206 CloseWriteFileAndRunLoop();
209 ASSERT_EQ(0u, messages_received_.size()); 207 ASSERT_EQ(0u, messages_received_.size());
210 } 208 }
211 209
212 TEST_F(RemoteSecurityKeyMessageReaderImplTest, InvalidMessageLength) { 210 TEST_F(SecurityKeyMessageReaderImplTest, InvalidMessageLength) {
213 uint32_t length = kMaxSecurityKeyMessageByteCount + 1; 211 uint32_t length = kMaxSecurityKeyMessageByteCount + 1;
214 ASSERT_FALSE(SecurityKeyMessage::IsValidMessageSize(length)); 212 ASSERT_FALSE(SecurityKeyMessage::IsValidMessageSize(length));
215 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); 213 WriteData(reinterpret_cast<char*>(&length), sizeof(length));
216 CloseWriteFileAndRunLoop(); 214 CloseWriteFileAndRunLoop();
217 ASSERT_EQ(0u, messages_received_.size()); 215 ASSERT_EQ(0u, messages_received_.size());
218 } 216 }
219 217
220 TEST_F(RemoteSecurityKeyMessageReaderImplTest, ShortHeader) { 218 TEST_F(SecurityKeyMessageReaderImplTest, ShortHeader) {
221 // Write only 3 bytes - the message length header is supposed to be 4 bytes. 219 // Write only 3 bytes - the message length header is supposed to be 4 bytes.
222 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1); 220 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1);
223 CloseWriteFileAndRunLoop(); 221 CloseWriteFileAndRunLoop();
224 ASSERT_EQ(0u, messages_received_.size()); 222 ASSERT_EQ(0u, messages_received_.size());
225 } 223 }
226 224
227 TEST_F(RemoteSecurityKeyMessageReaderImplTest, ZeroLengthMessage) { 225 TEST_F(SecurityKeyMessageReaderImplTest, ZeroLengthMessage) {
228 uint32_t length = 0; 226 uint32_t length = 0;
229 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); 227 WriteData(reinterpret_cast<char*>(&length), sizeof(length));
230 CloseWriteFileAndRunLoop(); 228 CloseWriteFileAndRunLoop();
231 ASSERT_EQ(0u, messages_received_.size()); 229 ASSERT_EQ(0u, messages_received_.size());
232 } 230 }
233 231
234 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MissingControlCode) { 232 TEST_F(SecurityKeyMessageReaderImplTest, MissingControlCode) {
235 uint32_t length = 1; 233 uint32_t length = 1;
236 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); 234 WriteData(reinterpret_cast<char*>(&length), sizeof(length));
237 CloseWriteFileAndRunLoop(); 235 CloseWriteFileAndRunLoop();
238 ASSERT_EQ(0u, messages_received_.size()); 236 ASSERT_EQ(0u, messages_received_.size());
239 } 237 }
240 238
241 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MissingPayload) { 239 TEST_F(SecurityKeyMessageReaderImplTest, MissingPayload) {
242 uint32_t length = 2; 240 uint32_t length = 2;
243 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); 241 WriteData(reinterpret_cast<char*>(&length), sizeof(length));
244 242
245 char test_control_code = static_cast<char>(kTestMessageType); 243 char test_control_code = static_cast<char>(kTestMessageType);
246 WriteData(&test_control_code, sizeof(test_control_code)); 244 WriteData(&test_control_code, sizeof(test_control_code));
247 CloseWriteFileAndRunLoop(); 245 CloseWriteFileAndRunLoop();
248 ASSERT_EQ(0u, messages_received_.size()); 246 ASSERT_EQ(0u, messages_received_.size());
249 } 247 }
250 248
251 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MultipleMessages) { 249 TEST_F(SecurityKeyMessageReaderImplTest, MultipleMessages) {
252 std::vector<std::string> payloads({"", "S", // Really short 250 std::vector<std::string> payloads({"", "S", // Really short
253 "", "Short", "", "Medium Length", "", 251 "", "Short", "", "Medium Length", "",
254 "Longer than medium, but not super long", 252 "Longer than medium, but not super long",
255 "", std::string(2048, 'Y'), ""}); 253 "", std::string(2048, 'Y'), ""});
256 for (size_t i = 0; i < payloads.size(); i++) { 254 for (size_t i = 0; i < payloads.size(); i++) {
257 WriteMessage(kTestMessageType, payloads[i]); 255 WriteMessage(kTestMessageType, payloads[i]);
258 RunLoop(); 256 RunLoop();
259 ASSERT_EQ(i + 1, messages_received_.size()); 257 ASSERT_EQ(i + 1, messages_received_.size());
260 } 258 }
261 CloseWriteFileAndRunLoop(); 259 CloseWriteFileAndRunLoop();
262 260
263 for (size_t i = 0; i < payloads.size(); i++) { 261 for (size_t i = 0; i < payloads.size(); i++) {
264 ASSERT_EQ(kTestMessageType, messages_received_[i]->type()); 262 ASSERT_EQ(kTestMessageType, messages_received_[i]->type());
265 ASSERT_EQ(payloads[i], messages_received_[i]->payload()); 263 ASSERT_EQ(payloads[i], messages_received_[i]->payload());
266 } 264 }
267 } 265 }
268 266
269 } // namespace remoting 267 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698