| 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/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/local_discovery/privet_http_impl.h" | 14 #include "chrome/browser/local_discovery/privet_http_impl.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "net/base/host_port_pair.h" | 18 #include "net/base/host_port_pair.h" |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "net/test/spawned_test_server/spawned_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "net/url_request/test_url_fetcher_factory.h" | 21 #include "net/url_request/test_url_fetcher_factory.h" |
| 20 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 22 #include "printing/pwg_raster_settings.h" | 24 #include "printing/pwg_raster_settings.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 27 |
| 26 #if defined(ENABLE_PRINT_PREVIEW) | 28 #if defined(ENABLE_PRINT_PREVIEW) |
| 27 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 29 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
| 28 #endif // ENABLE_PRINT_PREVIEW | 30 #endif // ENABLE_PRINT_PREVIEW |
| 29 | 31 |
| 30 namespace local_discovery { | 32 namespace local_discovery { |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 using testing::StrictMock; | 36 using testing::StrictMock; |
| 35 using testing::NiceMock; | 37 using testing::NiceMock; |
| 36 | 38 |
| 37 using content::BrowserThread; | 39 using content::BrowserThread; |
| 38 using net::SpawnedTestServer; | 40 using net::EmbeddedTestServer; |
| 39 | 41 |
| 40 const char kSampleInfoResponse[] = "{" | 42 const char kSampleInfoResponse[] = "{" |
| 41 " \"version\": \"1.0\"," | 43 " \"version\": \"1.0\"," |
| 42 " \"name\": \"Common printer\"," | 44 " \"name\": \"Common printer\"," |
| 43 " \"description\": \"Printer connected through Chrome connector\"," | 45 " \"description\": \"Printer connected through Chrome connector\"," |
| 44 " \"url\": \"https://www.google.com/cloudprint\"," | 46 " \"url\": \"https://www.google.com/cloudprint\"," |
| 45 " \"type\": [" | 47 " \"type\": [" |
| 46 " \"printer\"" | 48 " \"printer\"" |
| 47 " ]," | 49 " ]," |
| 48 " \"id\": \"\"," | 50 " \"id\": \"\"," |
| (...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 kSampleErrorResponsePrinterBusy)); | 1080 kSampleErrorResponsePrinterBusy)); |
| 1079 | 1081 |
| 1080 RunFor(base::TimeDelta::FromSeconds(3)); | 1082 RunFor(base::TimeDelta::FromSeconds(3)); |
| 1081 | 1083 |
| 1082 EXPECT_TRUE(SuccessfulResponseToURL( | 1084 EXPECT_TRUE(SuccessfulResponseToURL( |
| 1083 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 1085 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
| 1084 kSampleCreatejobResponse)); | 1086 kSampleCreatejobResponse)); |
| 1085 } | 1087 } |
| 1086 #endif // ENABLE_PRINT_PREVIEW | 1088 #endif // ENABLE_PRINT_PREVIEW |
| 1087 | 1089 |
| 1088 const base::FilePath::CharType kDocRoot[] = | |
| 1089 FILE_PATH_LITERAL("chrome/test/data"); | |
| 1090 | |
| 1091 class PrivetHttpWithServerTest : public ::testing::Test, | 1090 class PrivetHttpWithServerTest : public ::testing::Test, |
| 1092 public PrivetURLFetcher::Delegate { | 1091 public PrivetURLFetcher::Delegate { |
| 1093 protected: | 1092 protected: |
| 1094 PrivetHttpWithServerTest() | 1093 PrivetHttpWithServerTest() |
| 1095 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} | 1094 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} |
| 1095 |
| 1096 void SetUp() override { |
| 1097 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 1098 switches::kEnablePrivetV3); |
| 1099 context_getter_ = new net::TestURLRequestContextGetter( |
| 1100 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 1101 } |
| 1102 |
| 1096 void OnError(PrivetURLFetcher* fetcher, | 1103 void OnError(PrivetURLFetcher* fetcher, |
| 1097 PrivetURLFetcher::ErrorType error) override { | 1104 PrivetURLFetcher::ErrorType error) override { |
| 1098 done_ = true; | 1105 done_ = true; |
| 1099 success_ = false; | 1106 success_ = false; |
| 1100 error_ = error; | 1107 error_ = error; |
| 1101 | 1108 |
| 1102 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); | 1109 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); |
| 1103 } | 1110 } |
| 1104 | 1111 |
| 1105 void OnParsedJson(PrivetURLFetcher* fetcher, | 1112 void OnParsedJson(PrivetURLFetcher* fetcher, |
| 1106 const base::DictionaryValue& value, | 1113 const base::DictionaryValue& value, |
| 1107 bool has_error) override { | 1114 bool has_error) override { |
| 1108 NOTREACHED(); | 1115 NOTREACHED(); |
| 1109 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); | 1116 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); |
| 1110 } | 1117 } |
| 1111 | 1118 |
| 1112 bool OnRawData(PrivetURLFetcher* fetcher, | 1119 bool OnRawData(PrivetURLFetcher* fetcher, |
| 1113 bool response_is_file, | 1120 bool response_is_file, |
| 1114 const std::string& data_string, | 1121 const std::string& data_string, |
| 1115 const base::FilePath& data_file) override { | 1122 const base::FilePath& data_file) override { |
| 1116 done_ = true; | 1123 done_ = true; |
| 1117 success_ = true; | 1124 success_ = true; |
| 1118 | 1125 |
| 1119 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); | 1126 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); |
| 1120 return true; | 1127 return true; |
| 1121 } | 1128 } |
| 1122 | 1129 |
| 1130 void CreateServer(EmbeddedTestServer::Type type) { |
| 1131 server_.reset(new EmbeddedTestServer(type)); |
| 1132 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); |
| 1133 |
| 1134 base::FilePath test_data_dir; |
| 1135 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); |
| 1136 server_->ServeFilesFromDirectory( |
| 1137 test_data_dir.Append(FILE_PATH_LITERAL("chrome/test/data"))); |
| 1138 } |
| 1139 |
| 1123 void CreateClient() { | 1140 void CreateClient() { |
| 1124 client_.reset(new PrivetHTTPClientImpl( | 1141 client_.reset(new PrivetHTTPClientImpl("test", server_->host_port_pair(), |
| 1125 "test", https_server_->host_port_pair(), | 1142 context_getter_)); |
| 1126 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
| 1127 } | 1143 } |
| 1128 | 1144 |
| 1129 void Run() { | 1145 void Run() { |
| 1130 success_ = false; | 1146 success_ = false; |
| 1131 done_ = false; | 1147 done_ = false; |
| 1132 | 1148 |
| 1133 base::RunLoop run_loop; | 1149 base::RunLoop run_loop; |
| 1134 quit_ = run_loop.QuitClosure(); | 1150 quit_ = run_loop.QuitClosure(); |
| 1135 | 1151 |
| 1136 auto fetcher = client_->CreateURLFetcher( | 1152 scoped_ptr<PrivetURLFetcher> fetcher = client_->CreateURLFetcher( |
| 1137 https_server_->GetURL("files/simple.html"), net::URLFetcher::GET, this); | 1153 server_->GetURL("/simple.html"), net::URLFetcher::GET, this); |
| 1138 | 1154 |
| 1139 fetcher->SetMaxRetries(1); | 1155 fetcher->SetMaxRetries(1); |
| 1140 fetcher->V3Mode(); | 1156 fetcher->V3Mode(); |
| 1141 fetcher->Start(); | 1157 fetcher->Start(); |
| 1142 | 1158 |
| 1143 run_loop.Run(); | 1159 run_loop.Run(); |
| 1144 | 1160 |
| 1145 EXPECT_TRUE(done_); | 1161 EXPECT_TRUE(done_); |
| 1146 } | 1162 } |
| 1147 | 1163 |
| 1148 bool success_ = false; | 1164 bool success_ = false; |
| 1149 bool done_ = false; | 1165 bool done_ = false; |
| 1150 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType(); | 1166 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType(); |
| 1151 content::TestBrowserThreadBundle thread_bundle_; | 1167 content::TestBrowserThreadBundle thread_bundle_; |
| 1152 | 1168 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| 1153 scoped_ptr<SpawnedTestServer> https_server_; | 1169 scoped_ptr<EmbeddedTestServer> server_; |
| 1154 scoped_ptr<PrivetHTTPClientImpl> client_; | 1170 scoped_ptr<PrivetHTTPClientImpl> client_; |
| 1155 | 1171 |
| 1156 base::Closure quit_; | 1172 base::Closure quit_; |
| 1157 }; | 1173 }; |
| 1158 | 1174 |
| 1159 TEST_F(PrivetHttpWithServerTest, Http) { | 1175 TEST_F(PrivetHttpWithServerTest, HttpServer) { |
| 1160 https_server_.reset(new SpawnedTestServer(net::BaseTestServer::TYPE_HTTP, | 1176 CreateServer(EmbeddedTestServer::TYPE_HTTP); |
| 1161 SpawnedTestServer::kLocalhost, | |
| 1162 base::FilePath(kDocRoot))); | |
| 1163 ASSERT_TRUE(https_server_->Start()); | |
| 1164 | 1177 |
| 1165 CreateClient(); | 1178 CreateClient(); |
| 1166 Run(); | 1179 Run(); |
| 1167 EXPECT_TRUE(success_); | 1180 EXPECT_TRUE(success_); |
| 1168 | 1181 |
| 1169 CreateClient(); | 1182 CreateClient(); |
| 1170 net::SHA256HashValue fingerprint = {}; | 1183 net::SHA256HashValue fingerprint = {}; |
| 1171 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); | 1184 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); |
| 1172 | |
| 1173 Run(); | 1185 Run(); |
| 1174 EXPECT_FALSE(success_); | 1186 EXPECT_FALSE(success_); |
| 1175 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); | 1187 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); |
| 1176 } | 1188 } |
| 1177 | 1189 |
| 1178 TEST_F(PrivetHttpWithServerTest, Https) { | 1190 TEST_F(PrivetHttpWithServerTest, HttpsServer) { |
| 1179 https_server_.reset(new SpawnedTestServer( | 1191 CreateServer(EmbeddedTestServer::TYPE_HTTPS); |
| 1180 SpawnedTestServer::TYPE_HTTPS, | |
| 1181 SpawnedTestServer::SSLOptions(SpawnedTestServer::SSLOptions::CERT_OK), | |
| 1182 base::FilePath(kDocRoot))); | |
| 1183 ASSERT_TRUE(https_server_->Start()); | |
| 1184 | 1192 |
| 1185 CreateClient(); | 1193 CreateClient(); |
| 1186 Run(); | 1194 Run(); |
| 1187 #ifdef OS_MACOSX | |
| 1188 // Seems like an issue of the test server, it's in HTTPS mode and still | |
| 1189 // accepts | |
| 1190 // HTTP requests. | |
| 1191 // TODO(vitalybuka): Remove #ifdef when test server is fixed. | |
| 1192 EXPECT_TRUE(success_); | |
| 1193 #else | |
| 1194 EXPECT_FALSE(success_); | 1195 EXPECT_FALSE(success_); |
| 1195 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); | 1196 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); |
| 1196 #endif // OS_MACOSX | |
| 1197 | 1197 |
| 1198 CreateClient(); | 1198 CreateClient(); |
| 1199 net::SHA256HashValue fingerprint = | 1199 net::SHA256HashValue fingerprint = |
| 1200 net::X509Certificate::CalculateFingerprint256( | 1200 net::X509Certificate::CalculateFingerprint256( |
| 1201 https_server_->GetCertificate()->os_cert_handle()); | 1201 server_->GetCertificate()->os_cert_handle()); |
| 1202 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); | 1202 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); |
| 1203 | |
| 1204 Run(); | 1203 Run(); |
| 1205 EXPECT_TRUE(success_); | 1204 EXPECT_TRUE(success_); |
| 1206 | 1205 |
| 1207 CreateClient(); | 1206 CreateClient(); |
| 1208 fingerprint = {}; | 1207 fingerprint = {}; |
| 1209 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); | 1208 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint); |
| 1210 | |
| 1211 Run(); | 1209 Run(); |
| 1212 EXPECT_FALSE(success_); | 1210 EXPECT_FALSE(success_); |
| 1213 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_); | 1211 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_); |
| 1214 } | 1212 } |
| 1215 | 1213 |
| 1216 } // namespace | 1214 } // namespace |
| 1217 | 1215 |
| 1218 } // namespace local_discovery | 1216 } // namespace local_discovery |
| OLD | NEW |