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

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

Issue 1830433002: Adding the message writing class used for remote_security_key STDOUT communication. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@messages
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 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.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "remoting/host/security_key/security_key_message.h"
14 #include "remoting/host/setup/test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18 const uint8_t kTestMessageType = 42;
19 const char kTestMessageTypeChar = static_cast<char>(kTestMessageType);
20 const char kTestMessagePayload[] = "Super-test-payload!";
21 const unsigned int kBufferSizeBytes = 40000;
22 } // namespace
23
24 namespace remoting {
25
26 class RemoteSecurityKeyMessageWriterTest : public testing::Test {
27 public:
28 RemoteSecurityKeyMessageWriterTest();
29 ~RemoteSecurityKeyMessageWriterTest() override;
30
31 protected:
32 // testing::Test interface.
33 void SetUp() override;
34
35 scoped_ptr<RemoteSecurityKeyMessageWriter> writer_;
36 base::File read_file_;
37 base::File write_file_;
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageWriterTest);
41 };
42
43 RemoteSecurityKeyMessageWriterTest::RemoteSecurityKeyMessageWriterTest() {}
44
45 RemoteSecurityKeyMessageWriterTest::~RemoteSecurityKeyMessageWriterTest() {}
46
47 void RemoteSecurityKeyMessageWriterTest::SetUp() {
48 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
49 writer_.reset(new RemoteSecurityKeyMessageWriter(std::move(write_file_)));
50 }
51
52 TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithoutPayload) {
53 ASSERT_TRUE(writer_->WriteMessage(
54 static_cast<RemoteSecurityKeyMessageType>(kTestMessageType)));
55
56 // Retrieve and verify the message header.
57 uint32_t length;
58 int bytes_read =
59 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
60 ASSERT_EQ(4, bytes_read);
61 ASSERT_EQ(sizeof(kTestMessageType), length);
62
63 // Retrieve and verify the message type.
64 std::string message_type(length, '\0');
65 bytes_read =
66 read_file_.ReadAtCurrentPos(string_as_array(&message_type), length);
67 ASSERT_EQ(static_cast<int>(length), bytes_read);
68 ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1));
69
70 // Destroy the writer and verify the other end of the pipe is clean.
71 writer_.reset();
72 char unused;
73 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
74 }
75
76 TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithPayload) {
77 std::string test_message_payload(kTestMessagePayload);
78 ASSERT_TRUE(writer_->WriteMessage(
79 static_cast<RemoteSecurityKeyMessageType>(kTestMessageType),
80 test_message_payload));
81
82 // Retrieve and verify the message header.
83 uint32_t total_length;
84 int bytes_read =
85 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&total_length), 4);
86 ASSERT_EQ(4, bytes_read);
87 ASSERT_EQ(sizeof(kTestMessageType) + test_message_payload.size(),
88 total_length);
89
90 // Retrieve and verify the message type.
91 int message_type_length = 1;
92 std::string message_type(message_type_length, '\0');
93 bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_type),
94 message_type_length);
95 ASSERT_EQ(message_type_length, bytes_read);
96 ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1));
97
98 // Retrieve and verify the message payload.
99 int message_payload_length = total_length - message_type_length;
100 std::string message_payload(message_payload_length, '\0');
101 bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_payload),
102 message_payload_length);
103 ASSERT_EQ(message_payload_length, bytes_read);
104 ASSERT_EQ(message_payload, test_message_payload);
105
106 // Destroy the writer and verify the other end of the pipe is clean.
107 writer_.reset();
108 char unused;
109 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
110 }
111
112 TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMessageWithLargePayload) {
113 std::string test_message_payload(kBufferSizeBytes -
114 sizeof(RemoteSecurityKeyMessageType) -
115 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.
116 'Y');
117 ASSERT_TRUE(writer_->WriteMessage(
118 static_cast<RemoteSecurityKeyMessageType>(kTestMessageType),
119 test_message_payload));
120
121 // Retrieve and verify the message header.
122 uint32_t total_length;
123 int bytes_read =
124 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&total_length), 4);
125 ASSERT_EQ(4, bytes_read);
126 ASSERT_EQ(sizeof(kTestMessageType) + test_message_payload.size(),
127 total_length);
128
129 // Retrieve and verify the message type.
130 int message_type_length = 1;
131 std::string message_type(message_type_length, '\0');
132 bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_type),
133 message_type_length);
134 ASSERT_EQ(message_type_length, bytes_read);
135 ASSERT_EQ(message_type, std::string(&kTestMessageTypeChar, 1));
136
137 // Retrieve and verify the message payload.
138 int message_payload_length = total_length - message_type_length;
139 std::string message_payload(message_payload_length, '\0');
140 bytes_read = read_file_.ReadAtCurrentPos(string_as_array(&message_payload),
141 message_payload_length);
142 ASSERT_EQ(message_payload_length, bytes_read);
143 ASSERT_EQ(message_payload, test_message_payload);
144
145 // Destroy the writer and verify the other end of the pipe is clean.
146 writer_.reset();
147 char unused;
148 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
149 }
150
151 TEST_F(RemoteSecurityKeyMessageWriterTest, WriteMultipleMessages) {
152 int total_messages_to_write = 10;
153 for (int i = 0; i < total_messages_to_write; i++) {
154 ASSERT_TRUE(writer_->WriteMessage(
155 static_cast<RemoteSecurityKeyMessageType>(kTestMessageType + i)));
156 }
157
158 for (int i = 0; i < total_messages_to_write; i++) {
159 // Retrieve and verify the message header.
160 uint32_t length;
161 int bytes_read =
162 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
163 ASSERT_EQ(4, bytes_read);
164 ASSERT_EQ(sizeof(kTestMessageType), length);
165
166 // Retrieve and verify the message type.
167 std::string message_type(length, '\0');
168 bytes_read =
169 read_file_.ReadAtCurrentPos(string_as_array(&message_type), length);
170 ASSERT_EQ(static_cast<int>(length), bytes_read);
171 char current_char = kTestMessageType + i;
172 ASSERT_EQ(message_type, std::string(&current_char, 1));
173 }
174
175 // Destroy the writer and verify the other end of the pipe is clean.
176 writer_.reset();
177 char unused;
178 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0);
179 }
180
181 TEST_F(RemoteSecurityKeyMessageWriterTest, EnsureWriteFailsWhenPipeClosed) {
182 // Close the read end so that writing fails immediately.
183 read_file_.Close();
184
185 EXPECT_FALSE(writer_->WriteMessage(
186 static_cast<RemoteSecurityKeyMessageType>(kTestMessageType)));
187 }
188
189 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698