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

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/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
40 const base::FilePath::CharType kTestDataRoot[] =
41 FILE_PATH_LITERAL("chrome/test/data");
42
32 const char kSampleInfoResponse[] = "{" 43 const char kSampleInfoResponse[] = "{"
33 " \"version\": \"1.0\"," 44 " \"version\": \"1.0\","
34 " \"name\": \"Common printer\"," 45 " \"name\": \"Common printer\","
35 " \"description\": \"Printer connected through Chrome connector\"," 46 " \"description\": \"Printer connected through Chrome connector\","
36 " \"url\": \"https://www.google.com/cloudprint\"," 47 " \"url\": \"https://www.google.com/cloudprint\","
37 " \"type\": [" 48 " \"type\": ["
38 " \"printer\"" 49 " \"printer\""
39 " ]," 50 " ],"
40 " \"id\": \"\"," 51 " \"id\": \"\","
41 " \"device_state\": \"idle\"," 52 " \"device_state\": \"idle\","
(...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 kSampleErrorResponsePrinterBusy)); 1081 kSampleErrorResponsePrinterBusy));
1071 1082
1072 RunFor(base::TimeDelta::FromSeconds(3)); 1083 RunFor(base::TimeDelta::FromSeconds(3));
1073 1084
1074 EXPECT_TRUE(SuccessfulResponseToURL( 1085 EXPECT_TRUE(SuccessfulResponseToURL(
1075 GURL("http://10.0.0.8:6006/privet/printer/createjob"), 1086 GURL("http://10.0.0.8:6006/privet/printer/createjob"),
1076 kSampleCreatejobResponse)); 1087 kSampleCreatejobResponse));
1077 } 1088 }
1078 #endif // ENABLE_PRINT_PREVIEW 1089 #endif // ENABLE_PRINT_PREVIEW
1079 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 CreateServer(EmbeddedTestServer::Type type) {
1124 server_.reset(new EmbeddedTestServer(type));
1125 ASSERT_TRUE(server_->InitializeAndWaitUntilReady());
1126
1127 base::FilePath test_data_dir;
1128 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
1129 server_->ServeFilesFromDirectory(test_data_dir.AppendASCII(kTestDataRoot));
1130 }
1131
1132 void CreateClient() {
1133 client_.reset(new PrivetHTTPClientImpl(
1134 "test", server_->host_port_pair(),
1135 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1136 }
1137
1138 void Run() {
1139 success_ = false;
1140 done_ = false;
1141
1142 base::RunLoop run_loop;
1143 quit_ = run_loop.QuitClosure();
1144
1145 auto fetcher = client_->CreateURLFetcher(server_->GetURL("/simple.html"),
1146 net::URLFetcher::GET, this);
1147
1148 fetcher->SetMaxRetries(1);
1149 fetcher->V3Mode();
1150 fetcher->Start();
1151
1152 run_loop.Run();
1153
1154 EXPECT_TRUE(done_);
1155 }
1156
1157 bool success_ = false;
1158 bool done_ = false;
1159 PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType();
1160 content::TestBrowserThreadBundle thread_bundle_;
1161
1162 scoped_ptr<EmbeddedTestServer> server_;
1163 scoped_ptr<PrivetHTTPClientImpl> client_;
1164
1165 base::Closure quit_;
1166 };
1167
1168 TEST_F(PrivetHttpWithServerTest, HttpServer) {
1169 CreateServer(EmbeddedTestServer::TYPE_HTTP);
1170
1171 CreateClient();
1172 Run();
1173 EXPECT_TRUE(success_);
1174
1175 CreateClient();
1176 net::SHA256HashValue fingerprint = {};
1177 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1178
1179 Run();
1180 EXPECT_FALSE(success_);
1181 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
Vitaly Buka (NO REVIEWS) 2015/10/29 05:27:46 here request fails, as expected, but takes too lon
Vitaly Buka (NO REVIEWS) 2015/10/29 06:24:35 New bug for that: https://code.google.com/p/chromi
1182 }
1183
1184 TEST_F(PrivetHttpWithServerTest, HttpsServer) {
1185 CreateServer(EmbeddedTestServer::TYPE_HTTPS);
1186
1187 CreateClient();
1188 Run();
1189 #ifdef OS_MACOSX
1190 // Seems like an issue of the test server, it's in HTTPS mode and still
1191 // accepts HTTP requests.
1192 // TODO(vitalybuka): Remove #ifdef when test server is fixed. crbug.com/548888
1193 EXPECT_TRUE(success_);
1194 #else
1195 EXPECT_FALSE(success_);
1196 EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
1197 #endif // OS_MACOSX
1198
1199 CreateClient();
1200 net::SHA256HashValue fingerprint =
1201 net::X509Certificate::CalculateFingerprint256(
1202 server_->GetCertificate()->os_cert_handle());
1203 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1204
1205 Run();
1206 EXPECT_TRUE(success_);
1207
1208 CreateClient();
1209 fingerprint = {};
1210 client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
1211
1212 Run();
1213 EXPECT_FALSE(success_);
1214 EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_);
1215 }
1216
1080 } // namespace 1217 } // namespace
1081 1218
1082 } // namespace local_discovery 1219 } // 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