Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/local_discovery/privet_http_unittest.cc

Issue 1415653004: Resolve issue from feedback for 1417363004 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
16 #include "net/base/host_port_pair.h" 17 #include "net/base/host_port_pair.h"
17 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
18 #include "net/test/spawned_test_server/spawned_test_server.h" 19 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
20 #include "net/url_request/url_fetcher.h" 21 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_test_util.h" 22 #include "net/url_request/url_request_test_util.h"
22 #include "printing/pwg_raster_settings.h" 23 #include "printing/pwg_raster_settings.h"
23 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 #if defined(ENABLE_PRINT_PREVIEW) 27 #if defined(ENABLE_PRINT_PREVIEW)
27 #include "chrome/browser/local_discovery/pwg_raster_converter.h" 28 #include "chrome/browser/local_discovery/pwg_raster_converter.h"
28 #endif // ENABLE_PRINT_PREVIEW 29 #endif // ENABLE_PRINT_PREVIEW
29 30
30 namespace local_discovery { 31 namespace local_discovery {
31 32
32 namespace { 33 namespace {
33 34
34 using testing::StrictMock; 35 using testing::StrictMock;
35 using testing::NiceMock; 36 using testing::NiceMock;
36 37
37 using content::BrowserThread; 38 using content::BrowserThread;
38 using net::SpawnedTestServer; 39 using net::EmbeddedTestServer;
39 40
40 const char kSampleInfoResponse[] = "{" 41 const char kSampleInfoResponse[] = "{"
41 " \"version\": \"1.0\"," 42 " \"version\": \"1.0\","
42 " \"name\": \"Common printer\"," 43 " \"name\": \"Common printer\","
43 " \"description\": \"Printer connected through Chrome connector\"," 44 " \"description\": \"Printer connected through Chrome connector\","
44 " \"url\": \"https://www.google.com/cloudprint\"," 45 " \"url\": \"https://www.google.com/cloudprint\","
45 " \"type\": [" 46 " \"type\": ["
46 " \"printer\"" 47 " \"printer\""
47 " ]," 48 " ],"
48 " \"id\": \"\"," 49 " \"id\": \"\","
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 kSampleErrorResponsePrinterBusy)); 1079 kSampleErrorResponsePrinterBusy));
1079 1080
1080 RunFor(base::TimeDelta::FromSeconds(3)); 1081 RunFor(base::TimeDelta::FromSeconds(3));
1081 1082
1082 EXPECT_TRUE(SuccessfulResponseToURL( 1083 EXPECT_TRUE(SuccessfulResponseToURL(
1083 GURL("http://10.0.0.8:6006/privet/printer/createjob"), 1084 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1084 kSampleCreatejobResponse)); 1085 kSampleCreatejobResponse));
1085 } 1086 }
1086 #endif // ENABLE_PRINT_PREVIEW 1087 #endif // ENABLE_PRINT_PREVIEW
1087 1088
1088 const base::FilePath::CharType kDocRoot[] =
1089 FILE_PATH_LITERAL("chrome/test/data");
1090
1091 class PrivetHttpWithServerTest : public ::testing::Test, 1089 class PrivetHttpWithServerTest : public ::testing::Test,
1092 public PrivetURLFetcher::Delegate { 1090 public PrivetURLFetcher::Delegate {
1093 protected: 1091 protected:
1094 PrivetHttpWithServerTest() 1092 PrivetHttpWithServerTest() : network_thread_("network thread") {}
1095 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} 1093
1094 void SetUp() override {
1095 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1096 switches::kEnablePrivetV3);
1097
1098 base::Thread::Options network_thread_options;
1099 network_thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
1100 ASSERT_TRUE(network_thread_.StartWithOptions(network_thread_options));
Ryan Sleevi 2015/10/30 23:17:27 BUG: Don't create threads like this. Use TestBrows
Vitaly Buka (NO REVIEWS) 2015/10/31 03:55:35 Done.
1101
1102 context_getter_ =
1103 new net::TestURLRequestContextGetter(network_thread_.task_runner());
1104 }
1105
1096 void OnError(PrivetURLFetcher* fetcher, 1106 void OnError(PrivetURLFetcher* fetcher,
1097 PrivetURLFetcher::ErrorType error) override { 1107 PrivetURLFetcher::ErrorType error) override {
1098 done_ = true; 1108 done_ = true;
1099 success_ = false; 1109 success_ = false;
1100 error_ = error; 1110 error_ = error;
1101 1111
1102 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); 1112 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
Ryan Sleevi 2015/10/30 23:17:27 At some point, preferably in a separate CL, you sh
Vitaly Buka (NO REVIEWS) 2015/10/31 03:55:35 Acknowledged.
1103 } 1113 }
1104 1114
1105 void OnParsedJson(PrivetURLFetcher* fetcher, 1115 void OnParsedJson(PrivetURLFetcher* fetcher,
1106 const base::DictionaryValue& value, 1116 const base::DictionaryValue& value,
1107 bool has_error) override { 1117 bool has_error) override {
1108 NOTREACHED(); 1118 NOTREACHED();
1109 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); 1119 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
1110 } 1120 }
1111 1121
1112 bool OnRawData(PrivetURLFetcher* fetcher, 1122 bool OnRawData(PrivetURLFetcher* fetcher,
1113 bool response_is_file, 1123 bool response_is_file,
1114 const std::string& data_string, 1124 const std::string& data_string,
1115 const base::FilePath& data_file) override { 1125 const base::FilePath& data_file) override {
1116 done_ = true; 1126 done_ = true;
1117 success_ = true; 1127 success_ = true;
1118 1128
1119 base::MessageLoop::current()->PostTask(FROM_HERE, quit_); 1129 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
1120 return true; 1130 return true;
1121 } 1131 }
1122 1132
1133 void CreateServer(EmbeddedTestServer::Type type) {
1134 server_.reset(new EmbeddedTestServer(type));
1135 ASSERT_TRUE(server_->InitializeAndWaitUntilReady());
1136
1137 base::FilePath test_data_dir;
1138 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
1139 server_->ServeFilesFromDirectory(
1140 test_data_dir.Append(FILE_PATH_LITERAL("chrome/test/data")));
1141 }
1142
1123 void CreateClient() { 1143 void CreateClient() {
1124 client_.reset(new PrivetHTTPClientImpl( 1144 client_.reset(new PrivetHTTPClientImpl("test", server_->host_port_pair(),
1125 "test", https_server_->host_port_pair(), 1145 context_getter_));
1126 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1127 } 1146 }
1128 1147
1129 void Run() { 1148 void Run() {
1130 success_ = false; 1149 success_ = false;
1131 done_ = false; 1150 done_ = false;
1132 1151
1133 base::RunLoop run_loop; 1152 base::RunLoop run_loop;
1134 quit_ = run_loop.QuitClosure(); 1153 quit_ = run_loop.QuitClosure();
1135 1154
1136 auto fetcher = client_->CreateURLFetcher( 1155 auto fetcher = client_->CreateURLFetcher(server_->GetURL("/simple.html"),
Ryan Sleevi 2015/10/30 23:17:27 STYLE: This is not an appropriate use of auto See
Vitaly Buka (NO REVIEWS) 2015/10/31 03:55:36 Done.
1137 https_server_->GetURL("files/simple.html"), net::URLFetcher::GET, this); 1156 net::URLFetcher::GET, this);
1138 1157
1139 fetcher->SetMaxRetries(1); 1158 fetcher->SetMaxRetries(1);
1140 fetcher->V3Mode(); 1159 fetcher->V3Mode();
1141 fetcher->Start(); 1160 fetcher->Start();
1142 1161
1143 run_loop.Run(); 1162 run_loop.Run();
1144 1163
1145 EXPECT_TRUE(done_); 1164 EXPECT_TRUE(done_);
1146 } 1165 }
1147 1166
1148 bool success_ = false; 1167 bool success_ = false;
1149 bool done_ = false; 1168 bool done_ = false;
1150 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType(); 1169 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType();
1151 content::TestBrowserThreadBundle thread_bundle_; 1170 base::Thread network_thread_;
1152 1171 base::MessageLoop message_loop_;
1153 scoped_ptr<SpawnedTestServer> https_server_; 1172 scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
1173 scoped_ptr<EmbeddedTestServer> server_;
1154 scoped_ptr<PrivetHTTPClientImpl> client_; 1174 scoped_ptr<PrivetHTTPClientImpl> client_;
1155 1175
1156 base::Closure quit_; 1176 base::Closure quit_;
1157 }; 1177 };
1158 1178
1159 TEST_F(PrivetHttpWithServerTest, Http) { 1179 TEST_F(PrivetHttpWithServerTest, HttpServer) {
1160 https_server_.reset(new SpawnedTestServer(net::BaseTestServer::TYPE_HTTP, 1180 CreateServer(EmbeddedTestServer::TYPE_HTTP);
1161 SpawnedTestServer::kLocalhost,
1162 base::FilePath(kDocRoot)));
1163 ASSERT_TRUE(https_server_->Start());
1164 1181
1165 CreateClient(); 1182 CreateClient();
1166 Run(); 1183 Run();
1167 EXPECT_TRUE(success_); 1184 EXPECT_TRUE(success_);
1168 1185
1169 CreateClient(); 1186 CreateClient();
1170 net::SHA256HashValue fingerprint = {}; 1187 net::SHA256HashValue fingerprint = {};
1171 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); 1188 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1172
1173 Run(); 1189 Run();
1174 EXPECT_FALSE(success_); 1190 EXPECT_FALSE(success_);
1175 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); 1191 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
1176 } 1192 }
1177 1193
1178 TEST_F(PrivetHttpWithServerTest, Https) { 1194 TEST_F(PrivetHttpWithServerTest, HttpsServer) {
1179 https_server_.reset(new SpawnedTestServer( 1195 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 1196
1185 CreateClient(); 1197 CreateClient();
1186 Run(); 1198 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_); 1199 EXPECT_FALSE(success_);
1195 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_); 1200 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
1196 #endif // OS_MACOSX
1197 1201
1198 CreateClient(); 1202 CreateClient();
1199 net::SHA256HashValue fingerprint = 1203 net::SHA256HashValue fingerprint =
1200 net::X509Certificate::CalculateFingerprint256( 1204 net::X509Certificate::CalculateFingerprint256(
1201 https_server_->GetCertificate()->os_cert_handle()); 1205 server_->GetCertificate()->os_cert_handle());
1202 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); 1206 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1203
1204 Run(); 1207 Run();
1205 EXPECT_TRUE(success_); 1208 EXPECT_TRUE(success_);
1206 1209
1207 CreateClient(); 1210 CreateClient();
1208 fingerprint = {}; 1211 fingerprint = {};
1209 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint); 1212 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1210
1211 Run(); 1213 Run();
1212 EXPECT_FALSE(success_); 1214 EXPECT_FALSE(success_);
1213 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_); 1215 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_);
1214 } 1216 }
1215 1217
1216 } // namespace 1218 } // namespace
1217 1219
1218 } // namespace local_discovery 1220 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698