| // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "base/memory/ref_counted.h" |
| #include "base/strings/stringprintf.h" |
| #include "chrome/browser/extensions/extension_apitest.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/common/chrome_paths.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "content/public/test/browser_test.h" |
| #include "extensions/browser/api/socket/socket_api.h" |
| #include "extensions/browser/api/sockets_udp/test_udp_echo_server.h" |
| #include "extensions/test/extension_test_message_listener.h" |
| #include "extensions/test/result_catcher.h" |
| #include "net/dns/mock_host_resolver.h" |
| |
| using extensions::Extension; |
| using extensions::ResultCatcher; |
| |
| namespace { |
| |
| const char kHostname[] = "www.foo.com"; |
| const int kPort = 8888; |
| |
| class SocketApiTest : public extensions::ExtensionApiTest { |
| public: |
| void SetUpOnMainThread() override { |
| extensions::ExtensionApiTest::SetUpOnMainThread(); |
| host_resolver()->AddRule(kHostname, "127.0.0.1"); |
| } |
| }; |
| |
| } // namespace |
| |
| IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) { |
| extensions::TestUdpEchoServer udp_echo_server; |
| net::HostPortPair host_port_pair; |
| ASSERT_TRUE(udp_echo_server.Start(&host_port_pair)); |
| |
| int port = host_port_pair.port(); |
| ASSERT_GT(port, 0); |
| |
| // Test that sendTo() is properly resolving hostnames. |
| host_port_pair.set_host(kHostname); |
| |
| ResultCatcher catcher; |
| catcher.RestrictToBrowserContext(browser()->profile()); |
| |
| ExtensionTestMessageListener listener("info_please", true); |
| |
| ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| listener.Reply( |
| base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port)); |
| |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| } |
| |
| IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) { |
| net::EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTP); |
| test_server.AddDefaultHandlers(); |
| EXPECT_TRUE(test_server.Start()); |
| |
| net::HostPortPair host_port_pair = test_server.host_port_pair(); |
| int port = host_port_pair.port(); |
| ASSERT_GT(port, 0); |
| |
| // Test that connect() is properly resolving hostnames. |
| host_port_pair.set_host("lOcAlHoSt"); |
| |
| ResultCatcher catcher; |
| catcher.RestrictToBrowserContext(browser()->profile()); |
| |
| ExtensionTestMessageListener listener("info_please", true); |
| |
| ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| listener.Reply( |
| base::StringPrintf("tcp:%s:%d", host_port_pair.host().c_str(), port)); |
| |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| } |
| |
| IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPServerExtension) { |
| ResultCatcher catcher; |
| catcher.RestrictToBrowserContext(browser()->profile()); |
| ExtensionTestMessageListener listener("info_please", true); |
| ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| listener.Reply(base::StringPrintf("tcp_server:127.0.0.1:%d", kPort)); |
| |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| } |
| |
| IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPServerUnbindOnUnload) { |
| ResultCatcher catcher; |
| const Extension* extension = |
| LoadExtension(test_data_dir_.AppendASCII("socket/unload")); |
| ASSERT_TRUE(extension); |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| |
| UnloadExtension(extension->id()); |
| |
| ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/unload"))) |
| << message_; |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| } |
| |
| IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketMulticast) { |
| ResultCatcher catcher; |
| catcher.RestrictToBrowserContext(browser()->profile()); |
| ExtensionTestMessageListener listener("info_please", true); |
| ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| listener.Reply(base::StringPrintf("multicast:%s:%d", kHostname, kPort)); |
| |
| EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| } |