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/path_service.h"
7 #include "base/stringprintf.h"
8 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
9 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h"
10 #include "chrome/browser/extensions/api/socket/socket_api.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_function_test_utils.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_test_message_listener.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/extensions/application_launch.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/test/test_server.h"
24 using extensions::Extension
;
26 namespace utils
= extension_function_test_utils
;
30 const std::string kHostname
= "127.0.0.1";
31 const int kPort
= 8888;
33 class SocketApiTest
: public ExtensionApiTest
{
35 SocketApiTest() : resolver_event_(true, false),
37 new extensions::MockHostResolverCreator()) {
40 // We need this while the socket.{listen,accept} methods require the
41 // enable-experimental-extension-apis flag. After that we should remove it,
42 // as well as the "experimental" permission in the test apps' manifests.
43 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
44 ExtensionApiTest::SetUpCommandLine(command_line
);
45 command_line
->AppendSwitch(switches::kEnableExperimentalExtensionApis
);
48 virtual void SetUpOnMainThread() OVERRIDE
{
49 extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
50 resolver_creator_
->CreateMockHostResolver());
53 virtual void CleanUpOnMainThread() OVERRIDE
{
54 extensions::HostResolverWrapper::GetInstance()->
55 SetHostResolverForTesting(NULL
);
56 resolver_creator_
->DeleteMockHostResolver();
60 base::WaitableEvent resolver_event_
;
62 // The MockHostResolver asserts that it's used on the same thread on which
63 // it's created, which is actually a stronger rule than its real counterpart.
64 // But that's fine; it's good practice.
65 scoped_refptr
<extensions::MockHostResolverCreator
> resolver_creator_
;
68 #if !defined(DISABLE_NACL)
69 // TODO(yzshen): Build testing framework for all extensions APIs in Pepper. And
70 // move these Pepper API tests there.
71 class SocketPpapiTest
: public SocketApiTest
{
75 virtual ~SocketPpapiTest() {
78 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
79 SocketApiTest::SetUpCommandLine(command_line
);
80 // TODO(yzshen): It is better to use switches::kEnablePepperTesting.
81 // However, that requires adding a new DEPS entry. Considering that we are
82 // going to move the Pepper API tests to a new place, use a string literal
84 command_line
->AppendSwitch("enable-pepper-testing");
86 PathService::Get(chrome::DIR_GEN_TEST_DATA
, &app_dir_
);
87 app_dir_
= app_dir_
.AppendASCII(
88 "chrome/test/data/extensions/api_test/socket/ppapi/newlib");
92 void LaunchTestingApp() {
93 const Extension
* extension
= LoadExtension(app_dir_
);
94 ASSERT_TRUE(extension
);
96 chrome::AppLaunchParams
params(browser()->profile(), extension
,
97 extension_misc::LAUNCH_NONE
,
99 params
.command_line
= CommandLine::ForCurrentProcess();
100 chrome::OpenApplication(params
);
104 base::FilePath app_dir_
;
110 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketUDPCreateGood
) {
111 scoped_refptr
<extensions::SocketCreateFunction
> socket_create_function(
112 new extensions::SocketCreateFunction());
113 scoped_refptr
<Extension
> empty_extension(utils::CreateEmptyExtension());
115 socket_create_function
->set_extension(empty_extension
.get());
116 socket_create_function
->set_has_callback(true);
118 scoped_ptr
<base::Value
> result(utils::RunFunctionAndReturnSingleResult(
119 socket_create_function
,
121 browser(), utils::NONE
));
122 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, result
->GetType());
123 DictionaryValue
*value
= static_cast<DictionaryValue
*>(result
.get());
125 EXPECT_TRUE(value
->GetInteger("socketId", &socketId
));
126 EXPECT_TRUE(socketId
> 0);
129 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketTCPCreateGood
) {
130 scoped_refptr
<extensions::SocketCreateFunction
> socket_create_function(
131 new extensions::SocketCreateFunction());
132 scoped_refptr
<Extension
> empty_extension(utils::CreateEmptyExtension());
134 socket_create_function
->set_extension(empty_extension
.get());
135 socket_create_function
->set_has_callback(true);
137 scoped_ptr
<base::Value
> result(utils::RunFunctionAndReturnSingleResult(
138 socket_create_function
,
140 browser(), utils::NONE
));
141 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, result
->GetType());
142 DictionaryValue
*value
= static_cast<DictionaryValue
*>(result
.get());
144 EXPECT_TRUE(value
->GetInteger("socketId", &socketId
));
145 ASSERT_TRUE(socketId
> 0);
148 IN_PROC_BROWSER_TEST_F(SocketApiTest
, GetNetworkList
) {
149 scoped_refptr
<extensions::SocketGetNetworkListFunction
> socket_function(
150 new extensions::SocketGetNetworkListFunction());
151 scoped_refptr
<Extension
> empty_extension(utils::CreateEmptyExtension());
153 socket_function
->set_extension(empty_extension
.get());
154 socket_function
->set_has_callback(true);
156 scoped_ptr
<base::Value
> result(utils::RunFunctionAndReturnSingleResult(
157 socket_function
, "[]", browser(), utils::NONE
));
158 ASSERT_EQ(base::Value::TYPE_LIST
, result
->GetType());
160 // If we're invoking socket tests, all we can confirm is that we have at
161 // least one address, but not what it is.
162 ListValue
*value
= static_cast<ListValue
*>(result
.get());
163 ASSERT_TRUE(value
->GetSize() > 0);
166 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketUDPExtension
) {
167 scoped_ptr
<net::TestServer
> test_server(
168 new net::TestServer(net::TestServer::TYPE_UDP_ECHO
,
169 net::TestServer::kLocalhost
,
170 base::FilePath(FILE_PATH_LITERAL("net/data"))));
171 EXPECT_TRUE(test_server
->Start());
173 net::HostPortPair host_port_pair
= test_server
->host_port_pair();
174 int port
= host_port_pair
.port();
175 ASSERT_TRUE(port
> 0);
177 // Test that sendTo() is properly resolving hostnames.
178 host_port_pair
.set_host("LOCALhost");
180 ResultCatcher catcher
;
181 catcher
.RestrictToProfile(browser()->profile());
183 ExtensionTestMessageListener
listener("info_please", true);
185 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("socket/api")));
186 EXPECT_TRUE(listener
.WaitUntilSatisfied());
188 base::StringPrintf("udp:%s:%d", host_port_pair
.host().c_str(), port
));
190 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
193 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketTCPExtension
) {
194 scoped_ptr
<net::TestServer
> test_server(
195 new net::TestServer(net::TestServer::TYPE_TCP_ECHO
,
196 net::TestServer::kLocalhost
,
197 base::FilePath(FILE_PATH_LITERAL("net/data"))));
198 EXPECT_TRUE(test_server
->Start());
200 net::HostPortPair host_port_pair
= test_server
->host_port_pair();
201 int port
= host_port_pair
.port();
202 ASSERT_TRUE(port
> 0);
204 // Test that connect() is properly resolving hostnames.
205 host_port_pair
.set_host("lOcAlHoSt");
207 ResultCatcher catcher
;
208 catcher
.RestrictToProfile(browser()->profile());
210 ExtensionTestMessageListener
listener("info_please", true);
212 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("socket/api")));
213 EXPECT_TRUE(listener
.WaitUntilSatisfied());
215 base::StringPrintf("tcp:%s:%d", host_port_pair
.host().c_str(), port
));
217 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
220 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketTCPServerExtension
) {
221 ResultCatcher catcher
;
222 catcher
.RestrictToProfile(browser()->profile());
223 ExtensionTestMessageListener
listener("info_please", true);
224 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("socket/api")));
225 EXPECT_TRUE(listener
.WaitUntilSatisfied());
227 base::StringPrintf("tcp_server:%s:%d", kHostname
.c_str(), kPort
));
229 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
232 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketTCPServerUnbindOnUnload
) {
233 ResultCatcher catcher
;
234 const Extension
* extension
=
235 LoadExtension(test_data_dir_
.AppendASCII("socket/unload"));
236 ASSERT_TRUE(extension
);
237 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
239 UnloadExtension(extension
->id());
241 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("socket/unload")))
243 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
246 IN_PROC_BROWSER_TEST_F(SocketApiTest
, SocketMulticast
) {
247 ResultCatcher catcher
;
248 catcher
.RestrictToProfile(browser()->profile());
249 ExtensionTestMessageListener
listener("info_please", true);
250 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("socket/api")));
251 EXPECT_TRUE(listener
.WaitUntilSatisfied());
253 base::StringPrintf("multicast:%s:%d", kHostname
.c_str(), kPort
));
255 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
258 #if !defined(DISABLE_NACL)
259 IN_PROC_BROWSER_TEST_F(SocketPpapiTest
, UDP
) {
260 scoped_ptr
<net::TestServer
> test_server(
261 new net::TestServer(net::TestServer::TYPE_UDP_ECHO
,
262 net::TestServer::kLocalhost
,
263 base::FilePath(FILE_PATH_LITERAL("net/data"))));
264 EXPECT_TRUE(test_server
->Start());
266 net::HostPortPair host_port_pair
= test_server
->host_port_pair();
267 int port
= host_port_pair
.port();
268 ASSERT_TRUE(port
> 0);
270 // Test that sendTo() is properly resolving hostnames.
271 host_port_pair
.set_host("LOCALhost");
273 ResultCatcher catcher
;
274 catcher
.RestrictToProfile(browser()->profile());
276 ExtensionTestMessageListener
listener("info_please", true);
280 EXPECT_TRUE(listener
.WaitUntilSatisfied());
282 base::StringPrintf("udp:%s:%d", host_port_pair
.host().c_str(), port
));
284 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
287 IN_PROC_BROWSER_TEST_F(SocketPpapiTest
, TCP
) {
288 scoped_ptr
<net::TestServer
> test_server(
289 new net::TestServer(net::TestServer::TYPE_TCP_ECHO
,
290 net::TestServer::kLocalhost
,
291 base::FilePath(FILE_PATH_LITERAL("net/data"))));
292 EXPECT_TRUE(test_server
->Start());
294 net::HostPortPair host_port_pair
= test_server
->host_port_pair();
295 int port
= host_port_pair
.port();
296 ASSERT_TRUE(port
> 0);
298 // Test that connect() is properly resolving hostnames.
299 host_port_pair
.set_host("lOcAlHoSt");
301 ResultCatcher catcher
;
302 catcher
.RestrictToProfile(browser()->profile());
304 ExtensionTestMessageListener
listener("info_please", true);
308 EXPECT_TRUE(listener
.WaitUntilSatisfied());
310 base::StringPrintf("tcp:%s:%d", host_port_pair
.host().c_str(), port
));
312 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
315 IN_PROC_BROWSER_TEST_F(SocketPpapiTest
, TCPServer
) {
316 ResultCatcher catcher
;
317 catcher
.RestrictToProfile(browser()->profile());
318 ExtensionTestMessageListener
listener("info_please", true);
322 EXPECT_TRUE(listener
.WaitUntilSatisfied());
324 base::StringPrintf("tcp_server:%s:%d", kHostname
.c_str(), kPort
));
326 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();