Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / socket / socket_apitest.cc
blobeb52807312825ca5e164e1cb8b1ccd2b074f94ba
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
13 #include "extensions/browser/api/dns/mock_host_resolver_creator.h"
14 #include "extensions/browser/api/socket/socket_api.h"
15 #include "extensions/test/extension_test_message_listener.h"
16 #include "extensions/test/result_catcher.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/spawned_test_server/spawned_test_server.h"
20 using extensions::Extension;
21 using extensions::ResultCatcher;
23 namespace {
25 const std::string kHostname = "127.0.0.1";
26 const int kPort = 8888;
28 class SocketApiTest : public ExtensionApiTest {
29 public:
30 SocketApiTest() : resolver_event_(true, false),
31 resolver_creator_(
32 new extensions::MockHostResolverCreator()) {
35 void SetUpOnMainThread() override {
36 extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
37 resolver_creator_->CreateMockHostResolver());
40 void TearDownOnMainThread() override {
41 extensions::HostResolverWrapper::GetInstance()->
42 SetHostResolverForTesting(NULL);
43 resolver_creator_->DeleteMockHostResolver();
46 private:
47 base::WaitableEvent resolver_event_;
49 // The MockHostResolver asserts that it's used on the same thread on which
50 // it's created, which is actually a stronger rule than its real counterpart.
51 // But that's fine; it's good practice.
52 scoped_refptr<extensions::MockHostResolverCreator> resolver_creator_;
55 } // namespace
57 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) {
58 scoped_ptr<net::SpawnedTestServer> test_server(
59 new net::SpawnedTestServer(
60 net::SpawnedTestServer::TYPE_UDP_ECHO,
61 net::SpawnedTestServer::kLocalhost,
62 base::FilePath(FILE_PATH_LITERAL("net/data"))));
63 EXPECT_TRUE(test_server->Start());
65 net::HostPortPair host_port_pair = test_server->host_port_pair();
66 int port = host_port_pair.port();
67 ASSERT_GT(port, 0);
69 // Test that sendTo() is properly resolving hostnames.
70 host_port_pair.set_host("LOCALhost");
72 ResultCatcher catcher;
73 catcher.RestrictToBrowserContext(browser()->profile());
75 ExtensionTestMessageListener listener("info_please", true);
77 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api")));
78 EXPECT_TRUE(listener.WaitUntilSatisfied());
79 listener.Reply(
80 base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port));
82 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
85 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) {
86 scoped_ptr<net::SpawnedTestServer> test_server(
87 new net::SpawnedTestServer(
88 net::SpawnedTestServer::TYPE_TCP_ECHO,
89 net::SpawnedTestServer::kLocalhost,
90 base::FilePath(FILE_PATH_LITERAL("net/data"))));
91 EXPECT_TRUE(test_server->Start());
93 net::HostPortPair host_port_pair = test_server->host_port_pair();
94 int port = host_port_pair.port();
95 ASSERT_GT(port, 0);
97 // Test that connect() is properly resolving hostnames.
98 host_port_pair.set_host("lOcAlHoSt");
100 ResultCatcher catcher;
101 catcher.RestrictToBrowserContext(browser()->profile());
103 ExtensionTestMessageListener listener("info_please", true);
105 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api")));
106 EXPECT_TRUE(listener.WaitUntilSatisfied());
107 listener.Reply(
108 base::StringPrintf("tcp:%s:%d", host_port_pair.host().c_str(), port));
110 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
113 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPServerExtension) {
114 ResultCatcher catcher;
115 catcher.RestrictToBrowserContext(browser()->profile());
116 ExtensionTestMessageListener listener("info_please", true);
117 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api")));
118 EXPECT_TRUE(listener.WaitUntilSatisfied());
119 listener.Reply(
120 base::StringPrintf("tcp_server:%s:%d", kHostname.c_str(), kPort));
122 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
125 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPServerUnbindOnUnload) {
126 ResultCatcher catcher;
127 const Extension* extension =
128 LoadExtension(test_data_dir_.AppendASCII("socket/unload"));
129 ASSERT_TRUE(extension);
130 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
132 UnloadExtension(extension->id());
134 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/unload")))
135 << message_;
136 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
139 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketMulticast) {
140 ResultCatcher catcher;
141 catcher.RestrictToBrowserContext(browser()->profile());
142 ExtensionTestMessageListener listener("info_please", true);
143 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api")));
144 EXPECT_TRUE(listener.WaitUntilSatisfied());
145 listener.Reply(
146 base::StringPrintf("multicast:%s:%d", kHostname.c_str(), kPort));
148 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();