Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / api / sockets_tcp / sockets_tcp_apitest.cc
blobd6db1620c23a8256e26d319180e75b40f42be76d
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.
5 #include "base/memory/ref_counted.h"
6 #include "base/strings/stringprintf.h"
7 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
8 #include "extensions/browser/api/dns/mock_host_resolver_creator.h"
9 #include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h"
10 #include "extensions/browser/api_test_utils.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/test_util.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 #include "net/test/spawned_test_server/spawned_test_server.h"
20 namespace extensions {
22 const std::string kHostname = "127.0.0.1";
24 class SocketsTcpApiTest : public ShellApiTest {
25 public:
26 SocketsTcpApiTest()
27 : resolver_event_(true, false),
28 resolver_creator_(new MockHostResolverCreator()) {}
30 void SetUpOnMainThread() override {
31 ShellApiTest::SetUpOnMainThread();
33 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
34 resolver_creator_->CreateMockHostResolver());
37 void TearDownOnMainThread() override {
38 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL);
39 resolver_creator_->DeleteMockHostResolver();
41 ShellApiTest::TearDownOnMainThread();
44 private:
45 base::WaitableEvent resolver_event_;
47 // The MockHostResolver asserts that it's used on the same thread on which
48 // it's created, which is actually a stronger rule than its real counterpart.
49 // But that's fine; it's good practice.
50 scoped_refptr<MockHostResolverCreator> resolver_creator_;
53 IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketsTcpCreateGood) {
54 scoped_refptr<api::SocketsTcpCreateFunction> socket_create_function(
55 new api::SocketsTcpCreateFunction());
56 scoped_refptr<Extension> empty_extension = test_util::CreateEmptyExtension();
58 socket_create_function->set_extension(empty_extension.get());
59 socket_create_function->set_has_callback(true);
61 scoped_ptr<base::Value> result(
62 api_test_utils::RunFunctionAndReturnSingleResult(
63 socket_create_function.get(), "[]", browser_context()));
65 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType());
66 base::DictionaryValue* value =
67 static_cast<base::DictionaryValue*>(result.get());
68 int socketId = -1;
69 EXPECT_TRUE(value->GetInteger("socketId", &socketId));
70 ASSERT_TRUE(socketId > 0);
73 IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtension) {
74 scoped_ptr<net::SpawnedTestServer> test_server(new net::SpawnedTestServer(
75 net::SpawnedTestServer::TYPE_TCP_ECHO, net::SpawnedTestServer::kLocalhost,
76 base::FilePath(FILE_PATH_LITERAL("net/data"))));
77 EXPECT_TRUE(test_server->Start());
79 net::HostPortPair host_port_pair = test_server->host_port_pair();
80 int port = host_port_pair.port();
81 ASSERT_TRUE(port > 0);
83 // Test that connect() is properly resolving hostnames.
84 host_port_pair.set_host("lOcAlHoSt");
86 ResultCatcher catcher;
87 catcher.RestrictToBrowserContext(browser_context());
89 ExtensionTestMessageListener listener("info_please", true);
91 ASSERT_TRUE(LoadApp("sockets_tcp/api"));
92 EXPECT_TRUE(listener.WaitUntilSatisfied());
93 listener.Reply(
94 base::StringPrintf("tcp:%s:%d", host_port_pair.host().c_str(), port));
96 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
99 IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtensionTLS) {
100 scoped_ptr<net::SpawnedTestServer> test_https_server(
101 new net::SpawnedTestServer(
102 net::SpawnedTestServer::TYPE_HTTPS, net::BaseTestServer::SSLOptions(),
103 base::FilePath(FILE_PATH_LITERAL("net/data"))));
104 EXPECT_TRUE(test_https_server->Start());
106 net::HostPortPair https_host_port_pair = test_https_server->host_port_pair();
107 int https_port = https_host_port_pair.port();
108 ASSERT_GT(https_port, 0);
110 ResultCatcher catcher;
111 catcher.RestrictToBrowserContext(browser_context());
113 ExtensionTestMessageListener listener("info_please", true);
115 ASSERT_TRUE(LoadApp("sockets_tcp/api"));
116 EXPECT_TRUE(listener.WaitUntilSatisfied());
117 listener.Reply(base::StringPrintf(
118 "https:%s:%d", https_host_port_pair.host().c_str(), https_port));
120 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
123 } // namespace extensions