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
3 // found in the LICENSE file.
4 
5 #include "base/memory/ref_counted.h"
6 #include "base/strings/stringprintf.h"
7 #include "build/build_config.h"
8 #include "extensions/browser/api/sockets_udp/sockets_udp_api.h"
9 #include "extensions/browser/api/sockets_udp/test_udp_echo_server.h"
10 #include "extensions/browser/api_test_utils.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_builder.h"
13 #include "extensions/shell/test/shell_apitest.h"
14 #include "extensions/shell/test/shell_test.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 
19 namespace extensions {
20 
21 const char kHostname[] = "www.foo.com";
22 const int kPort = 8888;
23 
24 class SocketsUdpApiTest : public ShellApiTest {
25  public:
SetUpOnMainThread()26   void SetUpOnMainThread() override {
27     ShellApiTest::SetUpOnMainThread();
28     host_resolver()->AddRule(kHostname, "127.0.0.1");
29   }
30 };
31 
IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest,SocketsUdpCreateGood)32 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, SocketsUdpCreateGood) {
33   scoped_refptr<api::SocketsUdpCreateFunction> socket_create_function(
34       new api::SocketsUdpCreateFunction());
35   scoped_refptr<const Extension> empty_extension =
36       ExtensionBuilder("Test").Build();
37 
38   socket_create_function->set_extension(empty_extension.get());
39   socket_create_function->set_has_callback(true);
40 
41   std::unique_ptr<base::Value> result(
42       api_test_utils::RunFunctionAndReturnSingleResult(
43           socket_create_function.get(), "[]", browser_context()));
44 
45   base::DictionaryValue* value = NULL;
46   ASSERT_TRUE(result->GetAsDictionary(&value));
47   int socketId = -1;
48   EXPECT_TRUE(value->GetInteger("socketId", &socketId));
49   ASSERT_TRUE(socketId > 0);
50 }
51 
52 // Disable SocketsUdpExtension on Mac due to time out.
53 // See https://crbug.com/844402.
54 // Disable on Linux for flakiness. See https://crbug.com/875920.
55 #if defined(OS_MAC) || defined(OS_LINUX) || defined(OS_CHROMEOS)
56 #define MAYBE_SocketsUdpExtension DISABLED_SocketsUdpExtension
57 #else
58 #define MAYBE_SocketsUdpExtension SocketsUdpExtension
59 #endif
60 
IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest,MAYBE_SocketsUdpExtension)61 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, MAYBE_SocketsUdpExtension) {
62   TestUdpEchoServer udp_echo_server;
63   net::HostPortPair host_port_pair;
64   ASSERT_TRUE(udp_echo_server.Start(&host_port_pair));
65 
66   int port = host_port_pair.port();
67   ASSERT_TRUE(port > 0);
68 
69   // Test that sendTo() is properly resolving hostnames.
70   host_port_pair.set_host(kHostname);
71 
72   ResultCatcher catcher;
73   catcher.RestrictToBrowserContext(browser_context());
74 
75   ExtensionTestMessageListener listener("info_please", true);
76 
77   ASSERT_TRUE(LoadApp("sockets_udp/api"));
78   EXPECT_TRUE(listener.WaitUntilSatisfied());
79   listener.Reply(
80       base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port));
81 
82   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
83 }
84 
85 // See crbug.com/321451.
IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest,DISABLED_SocketsUdpMulticast)86 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, DISABLED_SocketsUdpMulticast) {
87   ResultCatcher catcher;
88   catcher.RestrictToBrowserContext(browser_context());
89   ExtensionTestMessageListener listener("info_please", true);
90   ASSERT_TRUE(LoadApp("sockets_udp/api"));
91   EXPECT_TRUE(listener.WaitUntilSatisfied());
92   listener.Reply(base::StringPrintf("multicast:%s:%d", kHostname, kPort));
93 
94   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
95 }
96 
97 }  // namespace extensions
98