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

Side by Side Diff: remoting/host/gnubby_auth_handler_posix_unittest.cc

Issue 1234003002: Fix GnubbyAuthHandlerPosix to handle requests correctly. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@allowIo
Patch Set: Rebase Created 5 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
« no previous file with comments | « remoting/host/gnubby_auth_handler_posix.cc ('k') | remoting/host/gnubby_socket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.h"
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 30 matching lines...) Expand all
41 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, 41 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6,
42 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, 42 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa,
43 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, 43 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7,
44 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, 44 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd,
45 0xc8, 0xaa}; 45 0xc8, 0xaa};
46 46
47 } // namespace 47 } // namespace
48 48
49 class TestClientStub : public protocol::ClientStub { 49 class TestClientStub : public protocol::ClientStub {
50 public: 50 public:
51 TestClientStub() {} 51 TestClientStub() : loop_(new base::RunLoop) {}
52 ~TestClientStub() override {} 52 ~TestClientStub() override {}
53 53
54 // protocol::ClientStub implementation. 54 // protocol::ClientStub implementation.
55 void SetCapabilities(const protocol::Capabilities& capabilities) override {} 55 void SetCapabilities(const protocol::Capabilities& capabilities) override {}
56 56
57 void SetPairingResponse( 57 void SetPairingResponse(
58 const protocol::PairingResponse& pairing_response) override {} 58 const protocol::PairingResponse& pairing_response) override {}
59 59
60 void DeliverHostMessage(const protocol::ExtensionMessage& message) override { 60 void DeliverHostMessage(const protocol::ExtensionMessage& message) override {
61 message_ = message; 61 message_ = message;
62 loop_.Quit(); 62 loop_->Quit();
63 } 63 }
64 64
65 // protocol::ClipboardStub implementation. 65 // protocol::ClipboardStub implementation.
66 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {} 66 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {}
67 67
68 // protocol::CursorShapeStub implementation. 68 // protocol::CursorShapeStub implementation.
69 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {} 69 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {}
70 70
71 void WaitForDeliverHostMessage() { loop_.Run(); } 71 void WaitForDeliverHostMessage() {
72 loop_->Run();
73 loop_.reset(new base::RunLoop);
74 }
72 75
73 void CheckHostDataMessage(int id, const std::string& data) { 76 void CheckHostDataMessage(int id, const std::string& data) {
74 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); 77 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id);
75 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str()); 78 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str());
76 79
77 ASSERT_TRUE(message_.type() == "gnubby-auth" || 80 ASSERT_TRUE(message_.type() == "gnubby-auth" ||
78 message_.type() == "auth-v1"); 81 message_.type() == "auth-v1");
79 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos); 82 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos);
80 ASSERT_NE(message_.data().find(connection_id), std::string::npos); 83 ASSERT_NE(message_.data().find(connection_id), std::string::npos);
81 ASSERT_NE(message_.data().find(data_message), std::string::npos); 84 ASSERT_NE(message_.data().find(data_message), std::string::npos);
82 } 85 }
83 86
84 private: 87 private:
85 protocol::ExtensionMessage message_; 88 protocol::ExtensionMessage message_;
86 base::RunLoop loop_; 89 scoped_ptr<base::RunLoop> loop_;
87 90
88 DISALLOW_COPY_AND_ASSIGN(TestClientStub); 91 DISALLOW_COPY_AND_ASSIGN(TestClientStub);
89 }; 92 };
90 93
91 class GnubbyAuthHandlerPosixTest : public testing::Test { 94 class GnubbyAuthHandlerPosixTest : public testing::Test {
92 public: 95 public:
93 GnubbyAuthHandlerPosixTest() { 96 GnubbyAuthHandlerPosixTest() {
94 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); 97 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
95 socket_path_ = temp_dir_.path().Append(kSocketFilename); 98 socket_path_ = temp_dir_.path().Append(kSocketFilename);
96 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_)); 99 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_));
97 auth_handler_ = auth_handler_posix_.get(); 100 auth_handler_ = auth_handler_posix_.get();
98 auth_handler_->SetGnubbySocketName(socket_path_); 101 auth_handler_->SetGnubbySocketName(socket_path_);
99 } 102 }
100 103
104 void WriteRequestData(net::UnixDomainClientSocket* client_socket) {
105 int request_len = sizeof(kRequestData);
106 scoped_refptr<net::DrainableIOBuffer> request_buffer(
107 new net::DrainableIOBuffer(
108 new net::WrappedIOBuffer(
109 reinterpret_cast<const char*>(kRequestData)),
110 request_len));
111 net::TestCompletionCallback write_callback;
112 int bytes_written = 0;
113 while (bytes_written < request_len) {
114 int write_result = client_socket->Write(request_buffer.get(),
115 request_buffer->BytesRemaining(),
116 write_callback.callback());
117 write_result = write_callback.GetResult(write_result);
118 ASSERT_GT(write_result, 0);
119 bytes_written += write_result;
120 ASSERT_LE(bytes_written, request_len);
121 request_buffer->DidConsume(write_result);
122 }
123 ASSERT_EQ(request_len, bytes_written);
124 }
125
126 void WaitForAndVerifyHostMessage() {
127 client_stub_.WaitForDeliverHostMessage();
128 base::ListValue expected_data;
129 // Skip first four bytes.
130 for (size_t i = 4; i < sizeof(kRequestData); ++i) {
131 expected_data.AppendInteger(kRequestData[i]);
132 }
133
134 std::string expected_data_json;
135 base::JSONWriter::Write(expected_data, &expected_data_json);
136 client_stub_.CheckHostDataMessage(1, expected_data_json);
137 }
138
101 protected: 139 protected:
102 // Object under test. 140 // Object under test.
103 scoped_ptr<GnubbyAuthHandlerPosix> auth_handler_posix_; 141 scoped_ptr<GnubbyAuthHandlerPosix> auth_handler_posix_;
104 142
105 // GnubbyAuthHandler interface for |auth_handler_posix_|. 143 // GnubbyAuthHandler interface for |auth_handler_posix_|.
106 GnubbyAuthHandler* auth_handler_; 144 GnubbyAuthHandler* auth_handler_;
107 145
108 base::MessageLoopForIO message_loop_; 146 base::MessageLoopForIO message_loop_;
109 TestClientStub client_stub_; 147 TestClientStub client_stub_;
110 base::ScopedTempDir temp_dir_; 148 base::ScopedTempDir temp_dir_;
111 base::FilePath socket_path_; 149 base::FilePath socket_path_;
112 base::Closure accept_callback_; 150 base::Closure accept_callback_;
113 }; 151 };
114 152
115 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) { 153 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) {
116 auth_handler_->DeliverHostDataMessage(42, "test_msg"); 154 auth_handler_->DeliverHostDataMessage(42, "test_msg");
117 client_stub_.WaitForDeliverHostMessage(); 155 client_stub_.WaitForDeliverHostMessage();
118 // Expects a JSON array of the ASCII character codes for "test_msg". 156 // Expects a JSON array of the ASCII character codes for "test_msg".
119 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]"); 157 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]");
120 } 158 }
121 159
122 TEST_F(GnubbyAuthHandlerPosixTest, DidReadAndClose) { 160 TEST_F(GnubbyAuthHandlerPosixTest, NotClosedAfterRequest) {
123 std::string message_json = "{\"type\":\"control\",\"option\":\"auth-v1\"}"; 161 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
124 162
125 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest()); 163 const char message_json[] = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
126 auth_handler_->DeliverClientMessage(message_json); 164 auth_handler_->DeliverClientMessage(message_json);
165
127 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); 166 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
128 net::TestCompletionCallback connect_callback; 167 net::TestCompletionCallback connect_callback;
168
129 int rv = client_socket.Connect(connect_callback.callback()); 169 int rv = client_socket.Connect(connect_callback.callback());
130 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); 170 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
131 int request_len = sizeof(kRequestData);
132 scoped_refptr<net::DrainableIOBuffer> request_buffer(
133 new net::DrainableIOBuffer(
134 new net::WrappedIOBuffer(reinterpret_cast<const char*>(kRequestData)),
135 request_len));
136 net::TestCompletionCallback write_callback;
137 int bytes_written = 0;
138 while (bytes_written < request_len) {
139 int write_result = client_socket.Write(request_buffer.get(),
140 request_buffer->BytesRemaining(),
141 write_callback.callback());
142 write_result = write_callback.GetResult(write_result);
143 ASSERT_GT(write_result, 0);
144 bytes_written += write_result;
145 ASSERT_LE(bytes_written, request_len);
146 request_buffer->DidConsume(write_result);
147 }
148 ASSERT_EQ(request_len, bytes_written);
149 171
150 client_stub_.WaitForDeliverHostMessage(); 172 // Write the request and verify the response.
151 base::ListValue expected_data; 173 WriteRequestData(&client_socket);
152 // Skip first four bytes. 174 WaitForAndVerifyHostMessage();
153 for (size_t i = 4; i < sizeof(kRequestData); ++i) {
154 expected_data.AppendInteger(kRequestData[i]);
155 }
156 175
157 std::string expected_data_json; 176 // Verify that completing a request/response cycle didn't close the socket.
158 base::JSONWriter::Write(expected_data, &expected_data_json); 177 ASSERT_EQ(1u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
159 client_stub_.CheckHostDataMessage(1, expected_data_json); 178 }
179
180 TEST_F(GnubbyAuthHandlerPosixTest, HandleTwoRequests) {
160 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest()); 181 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
182
183 const char message_json[] = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
184 auth_handler_->DeliverClientMessage(message_json);
185
186 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
187 net::TestCompletionCallback connect_callback;
188
189 int rv = client_socket.Connect(connect_callback.callback());
190 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
191
192 // Write the request and verify the response.
193 WriteRequestData(&client_socket);
194 WaitForAndVerifyHostMessage();
195
196 // Repeat the request/response cycle.
197 WriteRequestData(&client_socket);
198 WaitForAndVerifyHostMessage();
199
200 // Verify that completing two request/response cycles didn't close the socket.
201 ASSERT_EQ(1u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
161 } 202 }
162 203
163 TEST_F(GnubbyAuthHandlerPosixTest, DidReadTimeout) { 204 TEST_F(GnubbyAuthHandlerPosixTest, DidReadTimeout) {
164 std::string message_json = "{\"type\":\"control\",\"option\":\"auth-v1\"}"; 205 std::string message_json = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
165 206
166 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest()); 207 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
167 auth_handler_->DeliverClientMessage(message_json); 208 auth_handler_->DeliverClientMessage(message_json);
168 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); 209 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
169 net::TestCompletionCallback connect_callback; 210 net::TestCompletionCallback connect_callback;
170 int rv = client_socket.Connect(connect_callback.callback()); 211 int rv = client_socket.Connect(connect_callback.callback());
(...skipping 11 matching lines...) Expand all
182 net::TestCompletionCallback connect_callback; 223 net::TestCompletionCallback connect_callback;
183 int rv = client_socket.Connect(connect_callback.callback()); 224 int rv = client_socket.Connect(connect_callback.callback());
184 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); 225 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
185 226
186 std::string error_json = "{\"type\":\"error\",\"connectionId\":1}"; 227 std::string error_json = "{\"type\":\"error\",\"connectionId\":1}";
187 auth_handler_->DeliverClientMessage(error_json); 228 auth_handler_->DeliverClientMessage(error_json);
188 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest()); 229 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
189 } 230 }
190 231
191 } // namespace remoting 232 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/gnubby_auth_handler_posix.cc ('k') | remoting/host/gnubby_socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698