OLD | NEW |
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/gnubby_extension_session.h" | 5 #include "remoting/host/security_key/gnubby_extension_session.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/timer/mock_timer.h" | 18 #include "base/timer/mock_timer.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
23 #include "net/socket/unix_domain_client_socket_posix.h" | 23 #include "net/socket/unix_domain_client_socket_posix.h" |
| 24 #include "remoting/host/client_session_details.h" |
24 #include "remoting/host/host_mock_objects.h" | 25 #include "remoting/host/host_mock_objects.h" |
25 #include "remoting/host/security_key/gnubby_auth_handler.h" | 26 #include "remoting/host/security_key/gnubby_auth_handler.h" |
26 #include "remoting/proto/internal.pb.h" | 27 #include "remoting/proto/internal.pb.h" |
27 #include "remoting/protocol/client_stub.h" | 28 #include "remoting/protocol/client_stub.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
31 namespace remoting { | 32 namespace remoting { |
32 | 33 |
33 namespace { | 34 namespace { |
(...skipping 12 matching lines...) Expand all Loading... |
46 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, | 47 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, |
47 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, | 48 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, |
48 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, | 49 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, |
49 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, | 50 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, |
50 0xc8, 0xaa}; | 51 0xc8, 0xaa}; |
51 | 52 |
52 } // namespace | 53 } // namespace |
53 | 54 |
54 class TestClientStub : public protocol::ClientStub { | 55 class TestClientStub : public protocol::ClientStub { |
55 public: | 56 public: |
56 TestClientStub() : run_loop_(new base::RunLoop) {} | 57 TestClientStub(); |
57 ~TestClientStub() override {} | 58 ~TestClientStub() override; |
58 | 59 |
59 // protocol::ClientStub implementation. | 60 // protocol::ClientStub implementation. |
60 void SetCapabilities(const protocol::Capabilities& capabilities) override {} | 61 void SetCapabilities(const protocol::Capabilities& capabilities) override; |
61 | |
62 void SetPairingResponse( | 62 void SetPairingResponse( |
63 const protocol::PairingResponse& pairing_response) override {} | 63 const protocol::PairingResponse& pairing_response) override; |
64 | 64 void DeliverHostMessage(const protocol::ExtensionMessage& message) override; |
65 void DeliverHostMessage(const protocol::ExtensionMessage& message) override { | 65 void SetVideoLayout(const protocol::VideoLayout& layout) override; |
66 message_ = message; | |
67 run_loop_->Quit(); | |
68 } | |
69 | |
70 void SetVideoLayout(const protocol::VideoLayout& layout) override {} | |
71 | 66 |
72 // protocol::ClipboardStub implementation. | 67 // protocol::ClipboardStub implementation. |
73 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {} | 68 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; |
74 | 69 |
75 // protocol::CursorShapeStub implementation. | 70 // protocol::CursorShapeStub implementation. |
76 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {} | 71 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override; |
77 | 72 |
78 void WaitForDeliverHostMessage(base::TimeDelta max_timeout) { | 73 void WaitForDeliverHostMessage(base::TimeDelta max_timeout); |
79 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
80 FROM_HERE, run_loop_->QuitClosure(), max_timeout); | |
81 run_loop_->Run(); | |
82 run_loop_.reset(new base::RunLoop); | |
83 } | |
84 | 74 |
85 void CheckHostDataMessage(int id, const std::string& data) { | 75 void CheckHostDataMessage(int id, const std::string& data); |
86 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); | |
87 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str()); | |
88 | |
89 ASSERT_TRUE(message_.type() == "gnubby-auth" || | |
90 message_.type() == "auth-v1"); | |
91 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos); | |
92 ASSERT_NE(message_.data().find(connection_id), std::string::npos); | |
93 ASSERT_NE(message_.data().find(data_message), std::string::npos); | |
94 } | |
95 | 76 |
96 private: | 77 private: |
97 protocol::ExtensionMessage message_; | 78 protocol::ExtensionMessage message_; |
98 std::unique_ptr<base::RunLoop> run_loop_; | 79 std::unique_ptr<base::RunLoop> run_loop_; |
99 | 80 |
100 DISALLOW_COPY_AND_ASSIGN(TestClientStub); | 81 DISALLOW_COPY_AND_ASSIGN(TestClientStub); |
101 }; | 82 }; |
102 | 83 |
| 84 TestClientStub::TestClientStub() : run_loop_(new base::RunLoop) {} |
| 85 |
| 86 TestClientStub::~TestClientStub() {} |
| 87 |
| 88 void TestClientStub::SetCapabilities( |
| 89 const protocol::Capabilities& capabilities) {} |
| 90 |
| 91 void TestClientStub::SetPairingResponse( |
| 92 const protocol::PairingResponse& pairing_response) {} |
| 93 |
| 94 void TestClientStub::DeliverHostMessage( |
| 95 const protocol::ExtensionMessage& message) { |
| 96 message_ = message; |
| 97 run_loop_->Quit(); |
| 98 } |
| 99 |
| 100 void TestClientStub::SetVideoLayout(const protocol::VideoLayout& layout) {} |
| 101 |
| 102 void TestClientStub::InjectClipboardEvent( |
| 103 const protocol::ClipboardEvent& event) {} |
| 104 |
| 105 void TestClientStub::SetCursorShape( |
| 106 const protocol::CursorShapeInfo& cursor_shape) {} |
| 107 |
| 108 void TestClientStub::WaitForDeliverHostMessage(base::TimeDelta max_timeout) { |
| 109 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 110 FROM_HERE, run_loop_->QuitClosure(), max_timeout); |
| 111 run_loop_->Run(); |
| 112 run_loop_.reset(new base::RunLoop); |
| 113 } |
| 114 |
| 115 void TestClientStub::CheckHostDataMessage(int id, const std::string& data) { |
| 116 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); |
| 117 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str()); |
| 118 |
| 119 ASSERT_TRUE(message_.type() == "gnubby-auth" || message_.type() == "auth-v1"); |
| 120 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos); |
| 121 ASSERT_NE(message_.data().find(connection_id), std::string::npos); |
| 122 ASSERT_NE(message_.data().find(data_message), std::string::npos); |
| 123 } |
| 124 |
| 125 class TestClientSessionDetails : public ClientSessionDetails { |
| 126 public: |
| 127 TestClientSessionDetails(); |
| 128 ~TestClientSessionDetails() override; |
| 129 |
| 130 // ClientSessionDetails interface. |
| 131 uint32_t desktop_session_id() const override { return desktop_session_id_; } |
| 132 ClientSessionControl* session_control() override { return nullptr; } |
| 133 |
| 134 void set_desktop_session_id(uint32_t new_id) { desktop_session_id_ = new_id; } |
| 135 |
| 136 private: |
| 137 uint32_t desktop_session_id_ = UINT32_MAX; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(TestClientSessionDetails); |
| 140 }; |
| 141 |
| 142 TestClientSessionDetails::TestClientSessionDetails() {} |
| 143 |
| 144 TestClientSessionDetails::~TestClientSessionDetails() {} |
| 145 |
103 class GnubbyExtensionSessionTest : public testing::Test { | 146 class GnubbyExtensionSessionTest : public testing::Test { |
104 public: | 147 public: |
105 GnubbyExtensionSessionTest() | 148 GnubbyExtensionSessionTest(); |
106 : gnubby_extension_session_(new GnubbyExtensionSession(&client_stub_)) { | 149 ~GnubbyExtensionSessionTest() override; |
107 // We want to retain ownership of mock object so we can use it to inject | |
108 // events into the extension session. The mock object should not be used | |
109 // once |gnubby_extension_session_| is destroyed. | |
110 mock_gnubby_auth_handler_ = new MockGnubbyAuthHandler(); | |
111 gnubby_extension_session_->SetGnubbyAuthHandlerForTesting( | |
112 base::WrapUnique(mock_gnubby_auth_handler_)); | |
113 } | |
114 | 150 |
115 void WaitForAndVerifyHostMessage() { | 151 void WaitForAndVerifyHostMessage(); |
116 client_stub_.WaitForDeliverHostMessage( | |
117 base::TimeDelta::FromMilliseconds(500)); | |
118 base::ListValue expected_data; | |
119 | 152 |
120 // Skip first four bytes. | 153 void CreateGnubbyConnection(); |
121 for (size_t i = 4; i < sizeof(kRequestData); ++i) { | |
122 expected_data.AppendInteger(kRequestData[i]); | |
123 } | |
124 | |
125 std::string expected_data_json; | |
126 base::JSONWriter::Write(expected_data, &expected_data_json); | |
127 client_stub_.CheckHostDataMessage(1, expected_data_json); | |
128 } | |
129 | |
130 void CreateGnubbyConnection() { | |
131 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(1); | |
132 | |
133 protocol::ExtensionMessage message; | |
134 message.set_type("gnubby-auth"); | |
135 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | |
136 | |
137 ASSERT_TRUE(gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, | |
138 message)); | |
139 } | |
140 | 154 |
141 protected: | 155 protected: |
142 base::MessageLoopForIO message_loop_; | 156 base::MessageLoopForIO message_loop_; |
143 | 157 |
144 // Object under test. | 158 // Object under test. |
145 std::unique_ptr<GnubbyExtensionSession> gnubby_extension_session_; | 159 std::unique_ptr<GnubbyExtensionSession> gnubby_extension_session_; |
146 | 160 |
147 MockGnubbyAuthHandler* mock_gnubby_auth_handler_ = nullptr; | 161 MockGnubbyAuthHandler* mock_gnubby_auth_handler_ = nullptr; |
148 | 162 |
149 TestClientStub client_stub_; | 163 TestClientStub client_stub_; |
| 164 TestClientSessionDetails client_details_; |
150 | 165 |
151 private: | 166 private: |
152 DISALLOW_COPY_AND_ASSIGN(GnubbyExtensionSessionTest); | 167 DISALLOW_COPY_AND_ASSIGN(GnubbyExtensionSessionTest); |
153 }; | 168 }; |
154 | 169 |
| 170 GnubbyExtensionSessionTest::GnubbyExtensionSessionTest() |
| 171 : gnubby_extension_session_( |
| 172 new GnubbyExtensionSession(&client_details_, &client_stub_)) { |
| 173 // We want to retain ownership of mock object so we can use it to inject |
| 174 // events into the extension session. The mock object should not be used |
| 175 // once |gnubby_extension_session_| is destroyed. |
| 176 mock_gnubby_auth_handler_ = new MockGnubbyAuthHandler(); |
| 177 gnubby_extension_session_->SetGnubbyAuthHandlerForTesting( |
| 178 base::WrapUnique(mock_gnubby_auth_handler_)); |
| 179 } |
| 180 |
| 181 GnubbyExtensionSessionTest::~GnubbyExtensionSessionTest() {} |
| 182 |
| 183 void GnubbyExtensionSessionTest::WaitForAndVerifyHostMessage() { |
| 184 client_stub_.WaitForDeliverHostMessage( |
| 185 base::TimeDelta::FromMilliseconds(500)); |
| 186 base::ListValue expected_data; |
| 187 |
| 188 // Skip first four bytes. |
| 189 for (size_t i = 4; i < sizeof(kRequestData); ++i) { |
| 190 expected_data.AppendInteger(kRequestData[i]); |
| 191 } |
| 192 |
| 193 std::string expected_data_json; |
| 194 base::JSONWriter::Write(expected_data, &expected_data_json); |
| 195 client_stub_.CheckHostDataMessage(1, expected_data_json); |
| 196 } |
| 197 |
| 198 void GnubbyExtensionSessionTest::CreateGnubbyConnection() { |
| 199 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(1); |
| 200 |
| 201 protocol::ExtensionMessage message; |
| 202 message.set_type("gnubby-auth"); |
| 203 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); |
| 204 |
| 205 ASSERT_TRUE( |
| 206 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); |
| 207 } |
| 208 |
155 TEST_F(GnubbyExtensionSessionTest, GnubbyConnectionCreated_ValidMessage) { | 209 TEST_F(GnubbyExtensionSessionTest, GnubbyConnectionCreated_ValidMessage) { |
156 CreateGnubbyConnection(); | 210 CreateGnubbyConnection(); |
157 } | 211 } |
158 | 212 |
159 TEST_F(GnubbyExtensionSessionTest, NoGnubbyConnectionCreated_WrongMessageType) { | 213 TEST_F(GnubbyExtensionSessionTest, NoGnubbyConnectionCreated_WrongMessageType) { |
160 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(0); | 214 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(0); |
161 | 215 |
162 protocol::ExtensionMessage message; | 216 protocol::ExtensionMessage message; |
163 message.set_type("unsupported-gnubby-auth"); | 217 message.set_type("unsupported-gnubby-auth"); |
164 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | 218 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 mock_gnubby_auth_handler_->GetSendMessageCallback().Run(42, "test_msg"); | 426 mock_gnubby_auth_handler_->GetSendMessageCallback().Run(42, "test_msg"); |
373 | 427 |
374 client_stub_.WaitForDeliverHostMessage( | 428 client_stub_.WaitForDeliverHostMessage( |
375 base::TimeDelta::FromMilliseconds(500)); | 429 base::TimeDelta::FromMilliseconds(500)); |
376 | 430 |
377 // Expects a JSON array of the ASCII character codes for "test_msg". | 431 // Expects a JSON array of the ASCII character codes for "test_msg". |
378 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]"); | 432 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]"); |
379 } | 433 } |
380 | 434 |
381 } // namespace remoting | 435 } // namespace remoting |
OLD | NEW |