OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" | 8 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" |
9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
| 11 #include "chrome/browser/extensions/extension_function_test_utils.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/common/extensions/api/cast_channel.h" | 14 #include "chrome/common/extensions/api/cast_channel.h" |
13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
14 #include "extensions/common/switches.h" | 16 #include "extensions/common/switches.h" |
15 #include "net/base/capturing_net_log.h" | 17 #include "net/base/capturing_net_log.h" |
16 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gmock_mutant.h" | 21 #include "testing/gmock_mutant.h" |
20 | 22 |
21 namespace cast_channel = extensions::api::cast_channel; | 23 namespace cast_channel = extensions::api::cast_channel; |
22 using cast_channel::CastSocket; | 24 using cast_channel::CastSocket; |
23 using cast_channel::ChannelError; | 25 using cast_channel::ChannelError; |
24 using cast_channel::MessageInfo; | 26 using cast_channel::MessageInfo; |
25 using cast_channel::ReadyState; | 27 using cast_channel::ReadyState; |
| 28 using extensions::Extension; |
| 29 |
| 30 namespace utils = extension_function_test_utils; |
26 | 31 |
27 using ::testing::_; | 32 using ::testing::_; |
28 using ::testing::A; | 33 using ::testing::A; |
29 using ::testing::DoAll; | 34 using ::testing::DoAll; |
30 using ::testing::Invoke; | 35 using ::testing::Invoke; |
31 using ::testing::InSequence; | 36 using ::testing::InSequence; |
32 using ::testing::Return; | 37 using ::testing::Return; |
33 | 38 |
34 namespace { | 39 namespace { |
35 | 40 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 128 } |
124 | 129 |
125 void DoCallOnMessage(extensions::CastChannelAPI* api, | 130 void DoCallOnMessage(extensions::CastChannelAPI* api, |
126 MockCastSocket* cast_socket, | 131 MockCastSocket* cast_socket, |
127 const std::string& message) { | 132 const std::string& message) { |
128 MessageInfo message_info; | 133 MessageInfo message_info; |
129 FillMessageInfo(&message_info, message); | 134 FillMessageInfo(&message_info, message); |
130 api->OnMessage(cast_socket, message_info); | 135 api->OnMessage(cast_socket, message_info); |
131 } | 136 } |
132 | 137 |
| 138 extensions::CastChannelOpenFunction* CreateOpenFunction( |
| 139 scoped_refptr<Extension> extension) { |
| 140 extensions::CastChannelOpenFunction* cast_channel_open_function = |
| 141 new extensions::CastChannelOpenFunction; |
| 142 cast_channel_open_function->set_extension(extension.get()); |
| 143 return cast_channel_open_function; |
| 144 } |
| 145 |
| 146 extensions::CastChannelSendFunction* CreateSendFunction( |
| 147 scoped_refptr<Extension> extension) { |
| 148 extensions::CastChannelSendFunction* cast_channel_send_function = |
| 149 new extensions::CastChannelSendFunction; |
| 150 cast_channel_send_function->set_extension(extension.get()); |
| 151 return cast_channel_send_function; |
| 152 } |
| 153 |
133 MockCastSocket* mock_cast_socket_; | 154 MockCastSocket* mock_cast_socket_; |
134 net::CapturingNetLog capturing_net_log_; | 155 net::CapturingNetLog capturing_net_log_; |
135 }; | 156 }; |
136 | 157 |
137 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest | 158 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest |
138 // always return true without actually running the test. Remove when fixed. | 159 // always return true without actually running the test. Remove when fixed. |
139 #if defined(OS_WIN) && !defined(NDEBUG) | 160 #if defined(OS_WIN) && !defined(NDEBUG) |
140 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose | 161 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose |
141 #else | 162 #else |
142 #define MAYBE_TestOpenSendClose TestOpenSendClose | 163 #define MAYBE_TestOpenSendClose TestOpenSendClose |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 .WillRepeatedly(Return(cast_channel::READY_STATE_CLOSED)); | 273 .WillRepeatedly(Return(cast_channel::READY_STATE_CLOSED)); |
253 EXPECT_CALL(*mock_cast_socket_, Close(_)); | 274 EXPECT_CALL(*mock_cast_socket_, Close(_)); |
254 | 275 |
255 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 276 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
256 "test_open_error.html")); | 277 "test_open_error.html")); |
257 | 278 |
258 ResultCatcher catcher; | 279 ResultCatcher catcher; |
259 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 280 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
260 } | 281 } |
261 | 282 |
| 283 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { |
| 284 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 285 scoped_refptr<extensions::CastChannelOpenFunction> cast_channel_open_function; |
| 286 |
| 287 // Invalid URL |
| 288 // TODO(mfoltz): Remove this test case when fixing crbug.com/331905 |
| 289 cast_channel_open_function = CreateOpenFunction(empty_extension); |
| 290 std::string error(utils::RunFunctionAndReturnError( |
| 291 cast_channel_open_function.get(), "[\"blargh\"]", browser())); |
| 292 EXPECT_EQ(error, "Invalid connect_info (invalid Cast URL blargh)"); |
| 293 |
| 294 // Wrong type |
| 295 // TODO(mfoltz): Remove this test case when fixing crbug.com/331905 |
| 296 cast_channel_open_function = CreateOpenFunction(empty_extension); |
| 297 error = utils::RunFunctionAndReturnError( |
| 298 cast_channel_open_function.get(), |
| 299 "[123]", browser()); |
| 300 EXPECT_EQ(error, "Invalid connect_info (unknown type)"); |
| 301 |
| 302 // Invalid IP address |
| 303 cast_channel_open_function = CreateOpenFunction(empty_extension); |
| 304 error = utils::RunFunctionAndReturnError( |
| 305 cast_channel_open_function.get(), |
| 306 "[{\"ipAddress\": \"invalid_ip\", \"port\": 8009, \"auth\": \"ssl\"}]", |
| 307 browser()); |
| 308 EXPECT_EQ(error, "Invalid connect_info (invalid IP address)"); |
| 309 |
| 310 // Invalid port |
| 311 cast_channel_open_function = CreateOpenFunction(empty_extension); |
| 312 error = utils::RunFunctionAndReturnError( |
| 313 cast_channel_open_function.get(), |
| 314 "[{\"ipAddress\": \"127.0.0.1\", \"port\": -200, \"auth\": \"ssl\"}]", |
| 315 browser()); |
| 316 EXPECT_EQ(error, "Invalid connect_info (invalid port)"); |
| 317 |
| 318 // Auth not set |
| 319 cast_channel_open_function = CreateOpenFunction(empty_extension); |
| 320 error = utils::RunFunctionAndReturnError( |
| 321 cast_channel_open_function.get(), |
| 322 "[{\"ipAddress\": \"127.0.0.1\", \"port\": 8009}]", |
| 323 browser()); |
| 324 EXPECT_EQ(error, "connect_info.auth is required"); |
| 325 } |
| 326 |
| 327 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestSendInvalidMessageInfo) { |
| 328 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 329 scoped_refptr<extensions::CastChannelSendFunction> cast_channel_send_function; |
| 330 |
| 331 // Numbers are not supported |
| 332 cast_channel_send_function = CreateSendFunction(empty_extension); |
| 333 std::string error(utils::RunFunctionAndReturnError( |
| 334 cast_channel_send_function.get(), |
| 335 "[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " |
| 336 "\"connectInfo\": " |
| 337 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
| 338 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " |
| 339 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
| 340 "\"destinationId\": \"dest\", \"data\": 1235}]", |
| 341 browser())); |
| 342 EXPECT_EQ(error, "Invalid type of message_info.data"); |
| 343 |
| 344 // Missing namespace_ |
| 345 cast_channel_send_function = CreateSendFunction(empty_extension); |
| 346 error = utils::RunFunctionAndReturnError( |
| 347 cast_channel_send_function.get(), |
| 348 "[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " |
| 349 "\"connectInfo\": " |
| 350 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
| 351 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " |
| 352 "{\"namespace_\": \"\", \"sourceId\": \"src\", " |
| 353 "\"destinationId\": \"dest\", \"data\": \"data\"}]", |
| 354 browser()); |
| 355 EXPECT_EQ(error, "message_info.namespace_ is required"); |
| 356 |
| 357 // Missing source_id |
| 358 cast_channel_send_function = CreateSendFunction(empty_extension); |
| 359 error = utils::RunFunctionAndReturnError( |
| 360 cast_channel_send_function.get(), |
| 361 "[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " |
| 362 "\"connectInfo\": " |
| 363 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
| 364 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " |
| 365 "{\"namespace_\": \"foo\", \"sourceId\": \"\", " |
| 366 "\"destinationId\": \"dest\", \"data\": \"data\"}]", |
| 367 browser()); |
| 368 EXPECT_EQ(error, "message_info.source_id is required"); |
| 369 |
| 370 // Missing destination_id |
| 371 cast_channel_send_function = CreateSendFunction(empty_extension); |
| 372 error = utils::RunFunctionAndReturnError( |
| 373 cast_channel_send_function.get(), |
| 374 "[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " |
| 375 "\"connectInfo\": " |
| 376 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
| 377 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " |
| 378 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
| 379 "\"destinationId\": \"\", \"data\": \"data\"}]", |
| 380 browser()); |
| 381 EXPECT_EQ(error, "message_info.destination_id is required"); |
| 382 } |
OLD | NEW |