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