OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/extensions/api/gcd_private/privet_v3_context_getter.h" |
14 #include "chrome/browser/local_discovery/privet_http_impl.h" | 15 #include "chrome/browser/local_discovery/privet_http_impl.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
20 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
21 #include "net/url_request/test_url_fetcher_factory.h" | 22 #include "net/url_request/test_url_fetcher_factory.h" |
22 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
23 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 | 1036 |
1036 class PrivetHttpWithServerTest : public ::testing::Test, | 1037 class PrivetHttpWithServerTest : public ::testing::Test, |
1037 public PrivetURLFetcher::Delegate { | 1038 public PrivetURLFetcher::Delegate { |
1038 protected: | 1039 protected: |
1039 PrivetHttpWithServerTest() | 1040 PrivetHttpWithServerTest() |
1040 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} | 1041 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} |
1041 | 1042 |
1042 void SetUp() override { | 1043 void SetUp() override { |
1043 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 1044 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
1044 switches::kEnablePrivetV3); | 1045 switches::kEnablePrivetV3); |
1045 context_getter_ = new net::TestURLRequestContextGetter( | |
1046 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
1047 } | 1046 } |
1048 | 1047 |
1049 void OnError(PrivetURLFetcher* fetcher, | 1048 void OnError(PrivetURLFetcher* fetcher, |
1050 PrivetURLFetcher::ErrorType error) override { | 1049 PrivetURLFetcher::ErrorType error) override { |
1051 done_ = true; | 1050 done_ = true; |
1052 success_ = false; | 1051 success_ = false; |
1053 error_ = error; | 1052 error_ = error; |
1054 | 1053 |
1055 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); | 1054 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); |
1056 } | 1055 } |
(...skipping 20 matching lines...) Expand all Loading... |
1077 server_.reset(new EmbeddedTestServer(type)); | 1076 server_.reset(new EmbeddedTestServer(type)); |
1078 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); | 1077 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); |
1079 | 1078 |
1080 base::FilePath test_data_dir; | 1079 base::FilePath test_data_dir; |
1081 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | 1080 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); |
1082 server_->ServeFilesFromDirectory( | 1081 server_->ServeFilesFromDirectory( |
1083 test_data_dir.Append(FILE_PATH_LITERAL("chrome/test/data"))); | 1082 test_data_dir.Append(FILE_PATH_LITERAL("chrome/test/data"))); |
1084 } | 1083 } |
1085 | 1084 |
1086 void CreateClient() { | 1085 void CreateClient() { |
| 1086 context_getter_ = new extensions::PrivetV3ContextGetter( |
| 1087 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
1087 client_.reset(new PrivetHTTPClientImpl("test", server_->host_port_pair(), | 1088 client_.reset(new PrivetHTTPClientImpl("test", server_->host_port_pair(), |
1088 context_getter_)); | 1089 context_getter_)); |
1089 } | 1090 } |
1090 | 1091 |
1091 void Run() { | 1092 void Run() { |
1092 success_ = false; | 1093 success_ = false; |
1093 done_ = false; | 1094 done_ = false; |
1094 | 1095 |
1095 base::RunLoop run_loop; | 1096 base::RunLoop run_loop; |
1096 quit_ = run_loop.QuitClosure(); | 1097 quit_ = run_loop.QuitClosure(); |
1097 | 1098 |
1098 scoped_ptr<PrivetURLFetcher> fetcher = client_->CreateURLFetcher( | 1099 scoped_ptr<PrivetURLFetcher> fetcher = client_->CreateURLFetcher( |
1099 server_->GetURL("/simple.html"), net::URLFetcher::GET, this); | 1100 server_->GetURL("/simple.html"), net::URLFetcher::GET, this); |
1100 | 1101 |
1101 fetcher->SetMaxRetries(1); | 1102 fetcher->SetMaxRetries(1); |
1102 fetcher->V3Mode(); | 1103 fetcher->V3Mode(); |
1103 fetcher->Start(); | 1104 fetcher->Start(); |
1104 | 1105 |
1105 run_loop.Run(); | 1106 run_loop.Run(); |
1106 | 1107 |
1107 EXPECT_TRUE(done_); | 1108 EXPECT_TRUE(done_); |
1108 } | 1109 } |
1109 | 1110 |
1110 bool success_ = false; | 1111 bool success_ = false; |
1111 bool done_ = false; | 1112 bool done_ = false; |
1112 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType(); | 1113 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType(); |
1113 content::TestBrowserThreadBundle thread_bundle_; | 1114 content::TestBrowserThreadBundle thread_bundle_; |
1114 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; | 1115 scoped_refptr<extensions::PrivetV3ContextGetter> context_getter_; |
1115 scoped_ptr<EmbeddedTestServer> server_; | 1116 scoped_ptr<EmbeddedTestServer> server_; |
1116 scoped_ptr<PrivetHTTPClientImpl> client_; | 1117 scoped_ptr<PrivetHTTPClientImpl> client_; |
1117 | 1118 |
1118 base::Closure quit_; | 1119 base::Closure quit_; |
1119 }; | 1120 }; |
1120 | 1121 |
1121 TEST_F(PrivetHttpWithServerTest, HttpServer) { | 1122 TEST_F(PrivetHttpWithServerTest, HttpServer) { |
1122 CreateServer(EmbeddedTestServer::TYPE_HTTP); | 1123 CreateServer(EmbeddedTestServer::TYPE_HTTP); |
1123 | 1124 |
1124 CreateClient(); | 1125 CreateClient(); |
1125 Run(); | 1126 Run(); |
1126 EXPECT_TRUE(success_); | 1127 EXPECT_TRUE(success_); |
1127 | 1128 |
1128 CreateClient(); | 1129 CreateClient(); |
1129 net::SHA256HashValue fingerprint = {}; | 1130 client_->SwitchToHttps(server_->host_port_pair().port()); |
1130 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); | |
1131 Run(); | 1131 Run(); |
1132 EXPECT_FALSE(success_); | 1132 EXPECT_FALSE(success_); |
1133 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); | 1133 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); |
1134 } | 1134 } |
1135 | 1135 |
1136 TEST_F(PrivetHttpWithServerTest, HttpsServer) { | 1136 TEST_F(PrivetHttpWithServerTest, HttpsServer) { |
1137 CreateServer(EmbeddedTestServer::TYPE_HTTPS); | 1137 CreateServer(EmbeddedTestServer::TYPE_HTTPS); |
1138 | 1138 |
1139 CreateClient(); | 1139 CreateClient(); |
1140 Run(); | 1140 Run(); |
1141 EXPECT_FALSE(success_); | 1141 EXPECT_FALSE(success_); |
1142 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); | 1142 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); |
1143 | 1143 |
1144 CreateClient(); | 1144 CreateClient(); |
1145 net::SHA256HashValue fingerprint = | 1145 net::SHA256HashValue fingerprint = |
1146 net::X509Certificate::CalculateFingerprint256( | 1146 net::X509Certificate::CalculateFingerprint256( |
1147 server_->GetCertificate()->os_cert_handle()); | 1147 server_->GetCertificate()->os_cert_handle()); |
1148 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); | 1148 { |
| 1149 base::RunLoop run_loop; |
| 1150 context_getter_->AddPairedHost(client_->GetHost(), fingerprint, |
| 1151 run_loop.QuitClosure()); |
| 1152 run_loop.Run(); |
| 1153 } |
| 1154 client_->SwitchToHttps(server_->host_port_pair().port()); |
1149 Run(); | 1155 Run(); |
1150 EXPECT_TRUE(success_); | 1156 EXPECT_TRUE(success_); |
1151 | 1157 |
1152 CreateClient(); | 1158 CreateClient(); |
1153 fingerprint = {}; | 1159 fingerprint = {}; |
1154 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); | 1160 client_->SwitchToHttps(server_->host_port_pair().port()); |
1155 Run(); | 1161 Run(); |
1156 EXPECT_FALSE(success_); | 1162 EXPECT_FALSE(success_); |
1157 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_); | 1163 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_); |
1158 } | 1164 } |
1159 | 1165 |
1160 } // namespace | 1166 } // namespace |
1161 | 1167 |
1162 } // namespace local_discovery | 1168 } // namespace local_discovery |
OLD | NEW |