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

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

Issue 1417363004: Verify certificate of Privet v3 device (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/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/spawned_test_server/spawned_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::SpawnedTestServer;
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
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 const base::FilePath::CharType kDocRoot[] =
1089 FILE_PATH_LITERAL("chrome/test/data");
1090
1091 class PrivetHttpWithServerTest : public ::testing::Test,
1092 public PrivetURLFetcher::Delegate {
1093 protected:
1094 PrivetHttpWithServerTest()
1095 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {}
1096 void OnError(PrivetURLFetcher* fetcher,
1097 PrivetURLFetcher::ErrorType error) override {
1098 done_ = true;
1099 success_ = false;
1100 error_ = error;
1101
1102 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
1103 }
1104
1105 void OnParsedJson(PrivetURLFetcher* fetcher,
1106 const base::DictionaryValue& value,
1107 bool has_error) override {
1108 NOTREACHED();
1109 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
1110 }
1111
1112 bool OnRawData(PrivetURLFetcher* fetcher,
1113 bool response_is_file,
1114 const std::string& data_string,
1115 const base::FilePath& data_file) override {
1116 done_ = true;
1117 success_ = true;
1118
1119 base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
1120 return true;
1121 }
1122
1123 void CreateClient() {
1124 client_.reset(new PrivetHTTPClientImpl(
1125 "test", https_server_->host_port_pair(),
1126 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1127 }
1128
1129 void Run() {
1130 success_ = false;
1131 done_ = false;
1132
1133 base::RunLoop run_loop;
1134 quit_ = run_loop.QuitClosure();
1135
1136 auto fetcher = client_->CreateURLFetcher(
1137 https_server_->GetURL("files/simple.html"), net::URLFetcher::GET, this);
1138
1139 fetcher->SetMaxRetries(1);
1140 fetcher->V3Mode();
1141 fetcher->Start();
1142
1143 run_loop.Run();
1144
1145 EXPECT_TRUE(done_);
1146 }
1147
1148 bool success_ = false;
1149 bool done_ = false;
1150 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType();
1151 content::TestBrowserThreadBundle thread_bundle_;
1152
1153 scoped_ptr<SpawnedTestServer> https_server_;
1154 scoped_ptr<PrivetHTTPClientImpl> client_;
1155
1156 base::Closure quit_;
1157 };
1158
1159 TEST_F(PrivetHttpWithServerTest, Http) {
1160 https_server_.reset(new SpawnedTestServer(net::BaseTestServer::TYPE_HTTP,
1161 SpawnedTestServer::kLocalhost,
1162 base::FilePath(kDocRoot)));
1163 ASSERT_TRUE(https_server_->Start());
1164
1165 CreateClient();
1166 Run();
1167 EXPECT_TRUE(success_);
1168
1169 CreateClient();
1170 net::SHA256HashValue fingerprint = {};
1171 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint);
1172
1173 Run();
1174 EXPECT_FALSE(success_);
1175 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
1176 }
1177
1178 TEST_F(PrivetHttpWithServerTest, Https) {
1179 https_server_.reset(new SpawnedTestServer(
1180 SpawnedTestServer::TYPE_HTTPS,
1181 SpawnedTestServer::SSLOptions(SpawnedTestServer::SSLOptions::CERT_OK),
1182 base::FilePath(kDocRoot)));
1183 ASSERT_TRUE(https_server_->Start());
1184
1185 CreateClient();
1186 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_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
1196 #endif // OS_MACOSX
1197
1198 CreateClient();
1199 net::SHA256HashValue fingerprint =
1200 net::X509Certificate::CalculateFingerprint256(
1201 https_server_->GetCertificate()->os_cert_handle());
1202 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint);
1203
1204 Run();
1205 EXPECT_TRUE(success_);
1206
1207 CreateClient();
1208 fingerprint = {};
1209 client_->SwitchToHttps(https_server_->host_port_pair().port(), fingerprint);
1210
1211 Run();
1212 EXPECT_FALSE(success_);
1213 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_);
1214 }
1215
1080 } // namespace 1216 } // namespace
1081 1217
1082 } // namespace local_discovery 1218 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privet_http_impl.cc ('k') | chrome/browser/local_discovery/privet_url_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698