OLD | NEW |
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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/timer/mock_timer.h" | 9 #include "base/timer/mock_timer.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "net/base/completion_callback.h" | 26 #include "net/base/completion_callback.h" |
27 #include "net/base/ip_address.h" | 27 #include "net/base/ip_address.h" |
28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
29 #include "net/log/test_net_log.h" | 29 #include "net/log/test_net_log.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gmock_mutant.h" | 31 #include "testing/gmock_mutant.h" |
32 | 32 |
33 // TODO(mfoltz): Mock out the ApiResourceManager to resolve threading issues | 33 // TODO(mfoltz): Mock out the ApiResourceManager to resolve threading issues |
34 // (crbug.com/398242) and simulate unloading of the extension. | 34 // (crbug.com/398242) and simulate unloading of the extension. |
35 | 35 |
36 namespace cast_channel = extensions::api::cast_channel; | 36 using ::cast_channel::ChannelAuthType; |
37 using cast_channel::CastMessage; | 37 using ::cast_channel::ChannelError; |
38 using cast_channel::CastSocket; | 38 using ::cast_channel::ReadyState; |
39 using cast_channel::CastTransport; | 39 |
40 using cast_channel::ChannelAuthType; | 40 using extensions::api::cast_channel::CastMessage; |
41 using cast_channel::ChannelError; | 41 using extensions::api::cast_channel::CastSocket; |
42 using cast_channel::CreateIPEndPointForTest; | 42 using extensions::api::cast_channel::CastTransport; |
43 using cast_channel::ErrorInfo; | 43 using extensions::api::cast_channel::CreateIPEndPointForTest; |
44 using cast_channel::LastErrors; | 44 using extensions::api::cast_channel::ErrorInfo; |
45 using cast_channel::Logger; | 45 using extensions::api::cast_channel::LastErrors; |
46 using cast_channel::MessageInfo; | 46 using extensions::api::cast_channel::Logger; |
47 using cast_channel::MockCastSocket; | 47 using extensions::api::cast_channel::MessageInfo; |
48 using cast_channel::MockCastTransport; | 48 using extensions::api::cast_channel::MockCastSocket; |
49 using cast_channel::ReadyState; | 49 using extensions::api::cast_channel::MockCastTransport; |
50 using extensions::Extension; | 50 using extensions::Extension; |
51 | 51 |
52 namespace utils = extension_function_test_utils; | 52 namespace utils = extension_function_test_utils; |
53 | 53 |
54 using ::testing::_; | 54 using ::testing::_; |
55 using ::testing::A; | 55 using ::testing::A; |
56 using ::testing::DoAll; | 56 using ::testing::DoAll; |
57 using ::testing::Invoke; | 57 using ::testing::Invoke; |
58 using ::testing::InSequence; | 58 using ::testing::InSequence; |
59 using ::testing::NotNull; | 59 using ::testing::NotNull; |
(...skipping 22 matching lines...) Expand all Loading... |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 class CastChannelAPITest : public ExtensionApiTest { | 84 class CastChannelAPITest : public ExtensionApiTest { |
85 public: | 85 public: |
86 CastChannelAPITest() : ip_endpoint_(CreateIPEndPointForTest()) {} | 86 CastChannelAPITest() : ip_endpoint_(CreateIPEndPointForTest()) {} |
87 | 87 |
88 void SetUpCommandLine(base::CommandLine* command_line) override { | 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
89 ExtensionApiTest::SetUpCommandLine(command_line); | 89 ExtensionApiTest::SetUpCommandLine(command_line); |
90 command_line->AppendSwitchASCII( | 90 command_line->AppendSwitchASCII( |
91 extensions::switches::kWhitelistedExtensionID, | 91 extensions::switches::kWhitelistedExtensionID, |
92 cast_channel::kTestExtensionId); | 92 extensions::api::cast_channel::kTestExtensionId); |
93 } | 93 } |
94 | 94 |
95 void SetUpMockCastSocket() { | 95 void SetUpMockCastSocket() { |
96 extensions::CastChannelAPI* api = GetApi(); | 96 extensions::CastChannelAPI* api = GetApi(); |
97 timeout_timer_ = new base::MockTimer(true, false); | 97 timeout_timer_ = new base::MockTimer(true, false); |
98 api->SetPingTimeoutTimerForTest(base::WrapUnique(timeout_timer_)); | 98 api->SetPingTimeoutTimerForTest(base::WrapUnique(timeout_timer_)); |
99 | 99 |
100 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); | 100 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); |
101 mock_cast_socket_ = new MockCastSocket; | 101 mock_cast_socket_ = new MockCastSocket; |
102 // Transfers ownership of the socket. | 102 // Transfers ownership of the socket. |
103 api->SetSocketForTest(base::WrapUnique<CastSocket>(mock_cast_socket_)); | 103 api->SetSocketForTest(base::WrapUnique<CastSocket>(mock_cast_socket_)); |
104 ON_CALL(*mock_cast_socket_, set_id(_)) | 104 ON_CALL(*mock_cast_socket_, set_id(_)) |
105 .WillByDefault(SaveArg<0>(&channel_id_)); | 105 .WillByDefault(SaveArg<0>(&channel_id_)); |
106 ON_CALL(*mock_cast_socket_, id()) | 106 ON_CALL(*mock_cast_socket_, id()) |
107 .WillByDefault(ReturnPointee(&channel_id_)); | 107 .WillByDefault(ReturnPointee(&channel_id_)); |
108 ON_CALL(*mock_cast_socket_, ip_endpoint()) | 108 ON_CALL(*mock_cast_socket_, ip_endpoint()) |
109 .WillByDefault(ReturnRef(ip_endpoint_)); | 109 .WillByDefault(ReturnRef(ip_endpoint_)); |
110 ON_CALL(*mock_cast_socket_, channel_auth()) | 110 ON_CALL(*mock_cast_socket_, channel_auth()) |
111 .WillByDefault(Return(cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED)); | 111 .WillByDefault(Return(ChannelAuthType::SSL_VERIFIED)); |
112 ON_CALL(*mock_cast_socket_, keep_alive()).WillByDefault(Return(false)); | 112 ON_CALL(*mock_cast_socket_, keep_alive()).WillByDefault(Return(false)); |
113 } | 113 } |
114 | 114 |
115 void SetUpOpenSendClose() { | 115 void SetUpOpenSendClose() { |
116 SetUpMockCastSocket(); | 116 SetUpMockCastSocket(); |
117 EXPECT_CALL(*mock_cast_socket_, error_state()) | 117 EXPECT_CALL(*mock_cast_socket_, error_state()) |
118 .WillRepeatedly(Return(cast_channel::CHANNEL_ERROR_NONE)); | 118 .WillRepeatedly(Return(ChannelError::NONE)); |
119 { | 119 { |
120 InSequence sequence; | 120 InSequence sequence; |
121 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) | 121 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) |
122 .WillOnce( | 122 .WillOnce(InvokeCompletionCallback<1>(ChannelError::NONE)); |
123 InvokeCompletionCallback<1>(cast_channel::CHANNEL_ERROR_NONE)); | |
124 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 123 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
125 .WillOnce(Return(cast_channel::READY_STATE_OPEN)); | 124 .WillOnce(Return(ReadyState::OPEN)); |
126 EXPECT_CALL(*mock_cast_socket_->mock_transport(), | 125 EXPECT_CALL(*mock_cast_socket_->mock_transport(), |
127 SendMessage(A<const CastMessage&>(), _)) | 126 SendMessage(A<const CastMessage&>(), _)) |
128 .WillOnce(InvokeCompletionCallback<1>(net::OK)); | 127 .WillOnce(InvokeCompletionCallback<1>(net::OK)); |
129 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 128 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
130 .WillOnce(Return(cast_channel::READY_STATE_OPEN)); | 129 .WillOnce(Return(ReadyState::OPEN)); |
131 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 130 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
132 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 131 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
133 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 132 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
134 .WillOnce(Return(cast_channel::READY_STATE_CLOSED)); | 133 .WillOnce(Return(ReadyState::CLOSED)); |
135 } | 134 } |
136 } | 135 } |
137 | 136 |
138 void SetUpOpenPingTimeout() { | 137 void SetUpOpenPingTimeout() { |
139 SetUpMockCastSocket(); | 138 SetUpMockCastSocket(); |
140 EXPECT_CALL(*mock_cast_socket_, error_state()) | 139 EXPECT_CALL(*mock_cast_socket_, error_state()) |
141 .WillRepeatedly(Return(cast_channel::CHANNEL_ERROR_NONE)); | 140 .WillRepeatedly(Return(ChannelError::NONE)); |
142 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true)); | 141 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true)); |
143 { | 142 { |
144 InSequence sequence; | 143 InSequence sequence; |
145 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) | 144 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) |
146 .WillOnce(DoAll( | 145 .WillOnce(DoAll(SaveArg<0>(&message_delegate_), |
147 SaveArg<0>(&message_delegate_), | 146 InvokeCompletionCallback<1>(ChannelError::NONE))); |
148 InvokeCompletionCallback<1>(cast_channel::CHANNEL_ERROR_NONE))); | |
149 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 147 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
150 .WillOnce(Return(cast_channel::READY_STATE_OPEN)) | 148 .WillOnce(Return(ReadyState::OPEN)) |
151 .RetiresOnSaturation(); | 149 .RetiresOnSaturation(); |
152 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 150 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
153 .WillOnce(Return(cast_channel::READY_STATE_CLOSED)); | 151 .WillOnce(Return(ReadyState::CLOSED)); |
154 } | 152 } |
155 } | 153 } |
156 | 154 |
157 extensions::CastChannelAPI* GetApi() { | 155 extensions::CastChannelAPI* GetApi() { |
158 return extensions::CastChannelAPI::Get(profile()); | 156 return extensions::CastChannelAPI::Get(profile()); |
159 } | 157 } |
160 | 158 |
161 // Logs some bogus error details and calls the OnError handler. | 159 // Logs some bogus error details and calls the OnError handler. |
162 void DoCallOnError(extensions::CastChannelAPI* api) { | 160 void DoCallOnError(extensions::CastChannelAPI* api) { |
163 api->GetLogger()->LogSocketEventWithRv(mock_cast_socket_->id(), | 161 api->GetLogger()->LogSocketEventWithRv( |
164 cast_channel::proto::SOCKET_WRITE, | 162 mock_cast_socket_->id(), |
165 net::ERR_FAILED); | 163 extensions::api::cast_channel::proto::SOCKET_WRITE, net::ERR_FAILED); |
166 message_delegate_->OnError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); | 164 message_delegate_->OnError(ChannelError::CONNECT_ERROR); |
167 } | 165 } |
168 | 166 |
169 protected: | 167 protected: |
170 void CallOnMessage(const std::string& message) { | 168 void CallOnMessage(const std::string& message) { |
171 content::BrowserThread::PostTask( | 169 content::BrowserThread::PostTask( |
172 content::BrowserThread::IO, FROM_HERE, | 170 content::BrowserThread::IO, FROM_HERE, |
173 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), | 171 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), |
174 GetApi(), mock_cast_socket_, message)); | 172 GetApi(), mock_cast_socket_, message)); |
175 } | 173 } |
176 | 174 |
177 void DoCallOnMessage(extensions::CastChannelAPI* api, | 175 void DoCallOnMessage(extensions::CastChannelAPI* api, |
178 MockCastSocket* cast_socket, | 176 MockCastSocket* cast_socket, |
179 const std::string& message) { | 177 const std::string& message) { |
180 CastMessage cast_message; | 178 CastMessage cast_message; |
181 FillCastMessage(message, &cast_message); | 179 FillCastMessage(message, &cast_message); |
182 message_delegate_->OnMessage(cast_message); | 180 message_delegate_->OnMessage(cast_message); |
183 } | 181 } |
184 | 182 |
185 // Starts the read delegate on the IO thread. | 183 // Starts the read delegate on the IO thread. |
186 void StartDelegate() { | 184 void StartDelegate() { |
187 CHECK(message_delegate_); | 185 CHECK(message_delegate_); |
188 content::BrowserThread::PostTask( | 186 content::BrowserThread::PostTask( |
189 content::BrowserThread::IO, FROM_HERE, | 187 content::BrowserThread::IO, FROM_HERE, |
190 base::Bind(&cast_channel::CastTransport::Delegate::Start, | 188 base::Bind(&CastTransport::Delegate::Start, |
191 base::Unretained(message_delegate_))); | 189 base::Unretained(message_delegate_))); |
192 } | 190 } |
193 | 191 |
194 // Fires a timer on the IO thread. | 192 // Fires a timer on the IO thread. |
195 void FireTimeout() { | 193 void FireTimeout() { |
196 content::BrowserThread::PostTask( | 194 content::BrowserThread::PostTask( |
197 content::BrowserThread::IO, FROM_HERE, | 195 content::BrowserThread::IO, FROM_HERE, |
198 base::Bind(&base::MockTimer::Fire, base::Unretained(timeout_timer_))); | 196 base::Bind(&base::MockTimer::Fire, base::Unretained(timeout_timer_))); |
199 } | 197 } |
200 | 198 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 #if defined(OS_WIN) && !defined(NDEBUG) | 294 #if defined(OS_WIN) && !defined(NDEBUG) |
297 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose | 295 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose |
298 #else | 296 #else |
299 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose | 297 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose |
300 #endif | 298 #endif |
301 // Test loading extension, opening a channel, adding a listener, | 299 // Test loading extension, opening a channel, adding a listener, |
302 // writing, reading, and closing. | 300 // writing, reading, and closing. |
303 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { | 301 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { |
304 SetUpMockCastSocket(); | 302 SetUpMockCastSocket(); |
305 EXPECT_CALL(*mock_cast_socket_, error_state()) | 303 EXPECT_CALL(*mock_cast_socket_, error_state()) |
306 .WillRepeatedly(Return(cast_channel::CHANNEL_ERROR_NONE)); | 304 .WillRepeatedly(Return(ChannelError::NONE)); |
307 | 305 |
308 { | 306 { |
309 InSequence sequence; | 307 InSequence sequence; |
310 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) | 308 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) |
311 .WillOnce(DoAll( | 309 .WillOnce(DoAll(SaveArg<0>(&message_delegate_), |
312 SaveArg<0>(&message_delegate_), | 310 InvokeCompletionCallback<1>(ChannelError::NONE))); |
313 InvokeCompletionCallback<1>(cast_channel::CHANNEL_ERROR_NONE))); | |
314 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 311 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
315 .Times(3) | 312 .Times(3) |
316 .WillRepeatedly(Return(cast_channel::READY_STATE_OPEN)); | 313 .WillRepeatedly(Return(ReadyState::OPEN)); |
317 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 314 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
318 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 315 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
319 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 316 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
320 .WillOnce(Return(cast_channel::READY_STATE_CLOSED)); | 317 .WillOnce(Return(ReadyState::CLOSED)); |
321 } | 318 } |
322 | 319 |
323 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 320 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
324 "test_open_receive_close.html")); | 321 "test_open_receive_close.html")); |
325 | 322 |
326 extensions::ResultCatcher catcher; | 323 extensions::ResultCatcher catcher; |
327 CallOnMessage("some-message"); | 324 CallOnMessage("some-message"); |
328 CallOnMessage("some-message"); | 325 CallOnMessage("some-message"); |
329 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 326 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
330 } | 327 } |
331 | 328 |
332 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest | 329 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest |
333 // always return true without actually running the test. Remove when fixed. | 330 // always return true without actually running the test. Remove when fixed. |
334 #if defined(OS_WIN) && !defined(NDEBUG) | 331 #if defined(OS_WIN) && !defined(NDEBUG) |
335 #define MAYBE_TestOpenError DISABLED_TestOpenError | 332 #define MAYBE_TestOpenError DISABLED_TestOpenError |
336 #else | 333 #else |
337 #define MAYBE_TestOpenError TestOpenError | 334 #define MAYBE_TestOpenError TestOpenError |
338 #endif | 335 #endif |
339 // Test the case when socket open results in an error. | 336 // Test the case when socket open results in an error. |
340 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { | 337 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { |
341 SetUpMockCastSocket(); | 338 SetUpMockCastSocket(); |
342 | 339 |
343 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) | 340 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) |
344 .WillOnce(DoAll(SaveArg<0>(&message_delegate_), | 341 .WillOnce(DoAll( |
345 InvokeDelegateOnError(this, GetApi()), | 342 SaveArg<0>(&message_delegate_), InvokeDelegateOnError(this, GetApi()), |
346 InvokeCompletionCallback<1>( | 343 InvokeCompletionCallback<1>(ChannelError::CONNECT_ERROR))); |
347 cast_channel::CHANNEL_ERROR_CONNECT_ERROR))); | |
348 EXPECT_CALL(*mock_cast_socket_, error_state()) | 344 EXPECT_CALL(*mock_cast_socket_, error_state()) |
349 .WillRepeatedly(Return(cast_channel::CHANNEL_ERROR_CONNECT_ERROR)); | 345 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR)); |
350 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 346 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
351 .WillRepeatedly(Return(cast_channel::READY_STATE_CLOSED)); | 347 .WillRepeatedly(Return(ReadyState::CLOSED)); |
352 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 348 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
353 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 349 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
354 | 350 |
355 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 351 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
356 "test_open_error.html")); | 352 "test_open_error.html")); |
357 } | 353 } |
358 | 354 |
359 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { | 355 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { |
360 scoped_refptr<Extension> empty_extension = | 356 scoped_refptr<Extension> empty_extension = |
361 extensions::test_util::CreateEmptyExtension(); | 357 extensions::test_util::CreateEmptyExtension(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 "\"keepAlive\": true, " | 434 "\"keepAlive\": true, " |
439 "\"audioOnly\": false, " | 435 "\"audioOnly\": false, " |
440 "\"connectInfo\": " | 436 "\"connectInfo\": " |
441 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | 437 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
442 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " | 438 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " |
443 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | 439 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
444 "\"destinationId\": \"\", \"data\": \"data\"}]", | 440 "\"destinationId\": \"\", \"data\": \"data\"}]", |
445 browser()); | 441 browser()); |
446 EXPECT_EQ(error, "message_info.destination_id is required"); | 442 EXPECT_EQ(error, "message_info.destination_id is required"); |
447 } | 443 } |
OLD | NEW |