| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h" | 8 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_function_test_utils.h" | 10 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_test_message_listener.h" | 12 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/extensions/application_launch.h" | |
| 15 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "extensions/browser/api/dns/host_resolver_wrapper.h" | 17 #include "extensions/browser/api/dns/host_resolver_wrapper.h" |
| 19 #include "extensions/browser/api/socket/socket_api.h" | 18 #include "extensions/browser/api/socket/socket_api.h" |
| 20 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 21 #include "net/test/spawned_test_server/spawned_test_server.h" | 20 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 22 | 21 |
| 22 #if !defined(DISABLE_NACL) |
| 23 #include "base/command_line.h" |
| 24 #include "chrome/browser/ui/extensions/application_launch.h" |
| 25 #include "ppapi/shared_impl/ppapi_switches.h" |
| 26 #endif |
| 27 |
| 23 using extensions::Extension; | 28 using extensions::Extension; |
| 24 | 29 |
| 25 namespace utils = extension_function_test_utils; | 30 namespace utils = extension_function_test_utils; |
| 26 | 31 |
| 27 namespace { | 32 namespace { |
| 28 | 33 |
| 29 const std::string kHostname = "127.0.0.1"; | 34 const std::string kHostname = "127.0.0.1"; |
| 30 const int kPort = 8888; | 35 const int kPort = 8888; |
| 31 | 36 |
| 32 class SocketApiTest : public ExtensionApiTest { | 37 class SocketApiTest : public ExtensionApiTest { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 61 // move these Pepper API tests there. | 66 // move these Pepper API tests there. |
| 62 class SocketPpapiTest : public SocketApiTest { | 67 class SocketPpapiTest : public SocketApiTest { |
| 63 public: | 68 public: |
| 64 SocketPpapiTest() { | 69 SocketPpapiTest() { |
| 65 } | 70 } |
| 66 virtual ~SocketPpapiTest() { | 71 virtual ~SocketPpapiTest() { |
| 67 } | 72 } |
| 68 | 73 |
| 69 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 74 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 70 SocketApiTest::SetUpCommandLine(command_line); | 75 SocketApiTest::SetUpCommandLine(command_line); |
| 71 // TODO(yzshen): It is better to use switches::kEnablePepperTesting. | 76 command_line->AppendSwitch(switches::kEnablePepperTesting); |
| 72 // However, that requires adding a new DEPS entry. Considering that we are | |
| 73 // going to move the Pepper API tests to a new place, use a string literal | |
| 74 // for now. | |
| 75 command_line->AppendSwitch("enable-pepper-testing"); | |
| 76 } | 77 } |
| 77 | 78 |
| 78 virtual void SetUpOnMainThread() OVERRIDE { | 79 virtual void SetUpOnMainThread() OVERRIDE { |
| 79 SocketApiTest::SetUpOnMainThread(); | 80 SocketApiTest::SetUpOnMainThread(); |
| 80 | 81 |
| 81 PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir_); | 82 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir_)); |
| 82 app_dir_ = app_dir_.AppendASCII("ppapi/tests/extensions/socket/newlib"); | 83 app_dir_ = app_dir_.AppendASCII("ppapi") |
| 84 .AppendASCII("tests") |
| 85 .AppendASCII("extensions") |
| 86 .AppendASCII("socket") |
| 87 .AppendASCII("newlib"); |
| 83 } | 88 } |
| 84 | 89 |
| 85 protected: | 90 protected: |
| 86 void LaunchTestingApp() { | 91 void LaunchTestingApp() { |
| 87 const Extension* extension = LoadExtension(app_dir_); | 92 const Extension* extension = LoadExtension(app_dir_); |
| 88 ASSERT_TRUE(extension); | 93 ASSERT_TRUE(extension); |
| 89 | 94 |
| 90 AppLaunchParams params(browser()->profile(), | 95 AppLaunchParams params(browser()->profile(), |
| 91 extension, | 96 extension, |
| 92 extensions::LAUNCH_CONTAINER_NONE, | 97 extensions::LAUNCH_CONTAINER_NONE, |
| 93 NEW_WINDOW); | 98 NEW_WINDOW); |
| 94 params.command_line = *CommandLine::ForCurrentProcess(); | 99 params.command_line = *CommandLine::ForCurrentProcess(); |
| 95 OpenApplication(params); | 100 OpenApplication(params); |
| 96 } | 101 } |
| 97 | 102 |
| 98 private: | 103 private: |
| 99 base::FilePath app_dir_; | 104 base::FilePath app_dir_; |
| 100 }; | 105 }; |
| 101 #endif | 106 #endif // !defined(DISABLE_NACL) |
| 102 | 107 |
| 103 } // namespace | 108 } // namespace |
| 104 | 109 |
| 105 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPCreateGood) { | 110 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPCreateGood) { |
| 106 scoped_refptr<extensions::SocketCreateFunction> socket_create_function( | 111 scoped_refptr<extensions::SocketCreateFunction> socket_create_function( |
| 107 new extensions::SocketCreateFunction()); | 112 new extensions::SocketCreateFunction()); |
| 108 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | 113 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 109 | 114 |
| 110 socket_create_function->set_extension(empty_extension.get()); | 115 socket_create_function->set_extension(empty_extension.get()); |
| 111 socket_create_function->set_has_callback(true); | 116 socket_create_function->set_has_callback(true); |
| 112 | 117 |
| 113 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 118 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
| 114 socket_create_function.get(), "[\"udp\"]", browser(), utils::NONE)); | 119 socket_create_function.get(), "[\"udp\"]", browser(), utils::NONE)); |
| 115 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 120 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
| 116 base::DictionaryValue *value = | 121 base::DictionaryValue *value = |
| 117 static_cast<base::DictionaryValue*>(result.get()); | 122 static_cast<base::DictionaryValue*>(result.get()); |
| 118 int socketId = -1; | 123 int socket_id = -1; |
| 119 EXPECT_TRUE(value->GetInteger("socketId", &socketId)); | 124 EXPECT_TRUE(value->GetInteger("socketId", &socket_id)); |
| 120 EXPECT_TRUE(socketId > 0); | 125 EXPECT_GT(socket_id, 0); |
| 121 } | 126 } |
| 122 | 127 |
| 123 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPCreateGood) { | 128 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPCreateGood) { |
| 124 scoped_refptr<extensions::SocketCreateFunction> socket_create_function( | 129 scoped_refptr<extensions::SocketCreateFunction> socket_create_function( |
| 125 new extensions::SocketCreateFunction()); | 130 new extensions::SocketCreateFunction()); |
| 126 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | 131 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 127 | 132 |
| 128 socket_create_function->set_extension(empty_extension.get()); | 133 socket_create_function->set_extension(empty_extension.get()); |
| 129 socket_create_function->set_has_callback(true); | 134 socket_create_function->set_has_callback(true); |
| 130 | 135 |
| 131 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 136 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
| 132 socket_create_function.get(), "[\"tcp\"]", browser(), utils::NONE)); | 137 socket_create_function.get(), "[\"tcp\"]", browser(), utils::NONE)); |
| 133 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 138 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
| 134 base::DictionaryValue *value = | 139 base::DictionaryValue *value = |
| 135 static_cast<base::DictionaryValue*>(result.get()); | 140 static_cast<base::DictionaryValue*>(result.get()); |
| 136 int socketId = -1; | 141 int socket_id = -1; |
| 137 EXPECT_TRUE(value->GetInteger("socketId", &socketId)); | 142 EXPECT_TRUE(value->GetInteger("socketId", &socket_id)); |
| 138 ASSERT_TRUE(socketId > 0); | 143 ASSERT_GT(socket_id, 0); |
| 139 } | 144 } |
| 140 | 145 |
| 141 IN_PROC_BROWSER_TEST_F(SocketApiTest, GetNetworkList) { | 146 IN_PROC_BROWSER_TEST_F(SocketApiTest, GetNetworkList) { |
| 142 scoped_refptr<extensions::SocketGetNetworkListFunction> socket_function( | 147 scoped_refptr<extensions::SocketGetNetworkListFunction> socket_function( |
| 143 new extensions::SocketGetNetworkListFunction()); | 148 new extensions::SocketGetNetworkListFunction()); |
| 144 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | 149 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 145 | 150 |
| 146 socket_function->set_extension(empty_extension.get()); | 151 socket_function->set_extension(empty_extension.get()); |
| 147 socket_function->set_has_callback(true); | 152 socket_function->set_has_callback(true); |
| 148 | 153 |
| 149 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | 154 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( |
| 150 socket_function.get(), "[]", browser(), utils::NONE)); | 155 socket_function.get(), "[]", browser(), utils::NONE)); |
| 151 ASSERT_EQ(base::Value::TYPE_LIST, result->GetType()); | 156 ASSERT_EQ(base::Value::TYPE_LIST, result->GetType()); |
| 152 | 157 |
| 153 // If we're invoking socket tests, all we can confirm is that we have at | 158 // If we're invoking socket tests, all we can confirm is that we have at |
| 154 // least one address, but not what it is. | 159 // least one address, but not what it is. |
| 155 base::ListValue *value = static_cast<base::ListValue*>(result.get()); | 160 base::ListValue *value = static_cast<base::ListValue*>(result.get()); |
| 156 ASSERT_TRUE(value->GetSize() > 0); | 161 ASSERT_GT(value->GetSize(), 0U); |
| 157 } | 162 } |
| 158 | 163 |
| 159 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) { | 164 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) { |
| 160 scoped_ptr<net::SpawnedTestServer> test_server( | 165 scoped_ptr<net::SpawnedTestServer> test_server( |
| 161 new net::SpawnedTestServer( | 166 new net::SpawnedTestServer( |
| 162 net::SpawnedTestServer::TYPE_UDP_ECHO, | 167 net::SpawnedTestServer::TYPE_UDP_ECHO, |
| 163 net::SpawnedTestServer::kLocalhost, | 168 net::SpawnedTestServer::kLocalhost, |
| 164 base::FilePath(FILE_PATH_LITERAL("net/data")))); | 169 base::FilePath(FILE_PATH_LITERAL("net/data")))); |
| 165 EXPECT_TRUE(test_server->Start()); | 170 EXPECT_TRUE(test_server->Start()); |
| 166 | 171 |
| 167 net::HostPortPair host_port_pair = test_server->host_port_pair(); | 172 net::HostPortPair host_port_pair = test_server->host_port_pair(); |
| 168 int port = host_port_pair.port(); | 173 int port = host_port_pair.port(); |
| 169 ASSERT_TRUE(port > 0); | 174 ASSERT_GT(port, 0); |
| 170 | 175 |
| 171 // Test that sendTo() is properly resolving hostnames. | 176 // Test that sendTo() is properly resolving hostnames. |
| 172 host_port_pair.set_host("LOCALhost"); | 177 host_port_pair.set_host("LOCALhost"); |
| 173 | 178 |
| 174 ResultCatcher catcher; | 179 ResultCatcher catcher; |
| 175 catcher.RestrictToProfile(browser()->profile()); | 180 catcher.RestrictToProfile(browser()->profile()); |
| 176 | 181 |
| 177 ExtensionTestMessageListener listener("info_please", true); | 182 ExtensionTestMessageListener listener("info_please", true); |
| 178 | 183 |
| 179 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); | 184 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| 180 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 185 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 181 listener.Reply( | 186 listener.Reply( |
| 182 base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port)); | 187 base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port)); |
| 183 | 188 |
| 184 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 189 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 185 } | 190 } |
| 186 | 191 |
| 187 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) { | 192 IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) { |
| 188 scoped_ptr<net::SpawnedTestServer> test_server( | 193 scoped_ptr<net::SpawnedTestServer> test_server( |
| 189 new net::SpawnedTestServer( | 194 new net::SpawnedTestServer( |
| 190 net::SpawnedTestServer::TYPE_TCP_ECHO, | 195 net::SpawnedTestServer::TYPE_TCP_ECHO, |
| 191 net::SpawnedTestServer::kLocalhost, | 196 net::SpawnedTestServer::kLocalhost, |
| 192 base::FilePath(FILE_PATH_LITERAL("net/data")))); | 197 base::FilePath(FILE_PATH_LITERAL("net/data")))); |
| 193 EXPECT_TRUE(test_server->Start()); | 198 EXPECT_TRUE(test_server->Start()); |
| 194 | 199 |
| 195 net::HostPortPair host_port_pair = test_server->host_port_pair(); | 200 net::HostPortPair host_port_pair = test_server->host_port_pair(); |
| 196 int port = host_port_pair.port(); | 201 int port = host_port_pair.port(); |
| 197 ASSERT_TRUE(port > 0); | 202 ASSERT_GT(port, 0); |
| 198 | 203 |
| 199 // Test that connect() is properly resolving hostnames. | 204 // Test that connect() is properly resolving hostnames. |
| 200 host_port_pair.set_host("lOcAlHoSt"); | 205 host_port_pair.set_host("lOcAlHoSt"); |
| 201 | 206 |
| 202 ResultCatcher catcher; | 207 ResultCatcher catcher; |
| 203 catcher.RestrictToProfile(browser()->profile()); | 208 catcher.RestrictToProfile(browser()->profile()); |
| 204 | 209 |
| 205 ExtensionTestMessageListener listener("info_please", true); | 210 ExtensionTestMessageListener listener("info_please", true); |
| 206 | 211 |
| 207 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); | 212 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("socket/api"))); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 IN_PROC_BROWSER_TEST_F(SocketPpapiTest, MAYBE_UDP) { | 266 IN_PROC_BROWSER_TEST_F(SocketPpapiTest, MAYBE_UDP) { |
| 262 scoped_ptr<net::SpawnedTestServer> test_server( | 267 scoped_ptr<net::SpawnedTestServer> test_server( |
| 263 new net::SpawnedTestServer( | 268 new net::SpawnedTestServer( |
| 264 net::SpawnedTestServer::TYPE_UDP_ECHO, | 269 net::SpawnedTestServer::TYPE_UDP_ECHO, |
| 265 net::SpawnedTestServer::kLocalhost, | 270 net::SpawnedTestServer::kLocalhost, |
| 266 base::FilePath(FILE_PATH_LITERAL("net/data")))); | 271 base::FilePath(FILE_PATH_LITERAL("net/data")))); |
| 267 EXPECT_TRUE(test_server->Start()); | 272 EXPECT_TRUE(test_server->Start()); |
| 268 | 273 |
| 269 net::HostPortPair host_port_pair = test_server->host_port_pair(); | 274 net::HostPortPair host_port_pair = test_server->host_port_pair(); |
| 270 int port = host_port_pair.port(); | 275 int port = host_port_pair.port(); |
| 271 ASSERT_TRUE(port > 0); | 276 ASSERT_GT(port, 0); |
| 272 | 277 |
| 273 // Test that sendTo() is properly resolving hostnames. | 278 // Test that sendTo() is properly resolving hostnames. |
| 274 host_port_pair.set_host("LOCALhost"); | 279 host_port_pair.set_host("LOCALhost"); |
| 275 | 280 |
| 276 ResultCatcher catcher; | 281 ResultCatcher catcher; |
| 277 catcher.RestrictToProfile(browser()->profile()); | 282 catcher.RestrictToProfile(browser()->profile()); |
| 278 | 283 |
| 279 ExtensionTestMessageListener listener("info_please", true); | 284 ExtensionTestMessageListener listener("info_please", true); |
| 280 | 285 |
| 281 LaunchTestingApp(); | 286 LaunchTestingApp(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 296 IN_PROC_BROWSER_TEST_F(SocketPpapiTest, MAYBE_TCP) { | 301 IN_PROC_BROWSER_TEST_F(SocketPpapiTest, MAYBE_TCP) { |
| 297 scoped_ptr<net::SpawnedTestServer> test_server( | 302 scoped_ptr<net::SpawnedTestServer> test_server( |
| 298 new net::SpawnedTestServer( | 303 new net::SpawnedTestServer( |
| 299 net::SpawnedTestServer::TYPE_TCP_ECHO, | 304 net::SpawnedTestServer::TYPE_TCP_ECHO, |
| 300 net::SpawnedTestServer::kLocalhost, | 305 net::SpawnedTestServer::kLocalhost, |
| 301 base::FilePath(FILE_PATH_LITERAL("net/data")))); | 306 base::FilePath(FILE_PATH_LITERAL("net/data")))); |
| 302 EXPECT_TRUE(test_server->Start()); | 307 EXPECT_TRUE(test_server->Start()); |
| 303 | 308 |
| 304 net::HostPortPair host_port_pair = test_server->host_port_pair(); | 309 net::HostPortPair host_port_pair = test_server->host_port_pair(); |
| 305 int port = host_port_pair.port(); | 310 int port = host_port_pair.port(); |
| 306 ASSERT_TRUE(port > 0); | 311 ASSERT_GT(port, 0); |
| 307 | 312 |
| 308 // Test that connect() is properly resolving hostnames. | 313 // Test that connect() is properly resolving hostnames. |
| 309 host_port_pair.set_host("lOcAlHoSt"); | 314 host_port_pair.set_host("lOcAlHoSt"); |
| 310 | 315 |
| 311 ResultCatcher catcher; | 316 ResultCatcher catcher; |
| 312 catcher.RestrictToProfile(browser()->profile()); | 317 catcher.RestrictToProfile(browser()->profile()); |
| 313 | 318 |
| 314 ExtensionTestMessageListener listener("info_please", true); | 319 ExtensionTestMessageListener listener("info_please", true); |
| 315 | 320 |
| 316 LaunchTestingApp(); | 321 LaunchTestingApp(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 | 357 |
| 353 LaunchTestingApp(); | 358 LaunchTestingApp(); |
| 354 | 359 |
| 355 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 360 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 356 listener.Reply( | 361 listener.Reply( |
| 357 base::StringPrintf("multicast:%s:%d", kHostname.c_str(), kPort)); | 362 base::StringPrintf("multicast:%s:%d", kHostname.c_str(), kPort)); |
| 358 | 363 |
| 359 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 364 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 360 } | 365 } |
| 361 #endif | 366 #endif |
| OLD | NEW |