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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/local_discovery/privet_http_unittest.cc
diff --git a/chrome/browser/local_discovery/privet_http_unittest.cc b/chrome/browser/local_discovery/privet_http_unittest.cc
index 1fd60bece9d7805db59d7ec42b9029f52c3b9853..a0ada99e07ea0f7754b584f90d178a62aed6f6d8 100644
--- a/chrome/browser/local_discovery/privet_http_unittest.cc
+++ b/chrome/browser/local_discovery/privet_http_unittest.cc
@@ -7,12 +7,17 @@
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/local_discovery/privet_http_impl.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
+#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/url_request/test_url_fetcher_factory.h"
+#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_test_util.h"
#include "printing/pwg_raster_settings.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -22,12 +27,18 @@
#include "chrome/browser/local_discovery/pwg_raster_converter.h"
#endif // ENABLE_PRINT_PREVIEW
+namespace local_discovery {
+
+namespace {
+
using testing::StrictMock;
using testing::NiceMock;
-namespace local_discovery {
+using content::BrowserThread;
+using net::EmbeddedTestServer;
-namespace {
+const base::FilePath::CharType kTestDataRoot[] =
+ FILE_PATH_LITERAL("chrome/test/data");
const char kSampleInfoResponse[] = "{"
" \"version\": \"1.0\","
@@ -1077,6 +1088,132 @@ TEST_F(PrivetLocalPrintTest, LocalPrintRetryOnInvalidJobID) {
}
#endif // ENABLE_PRINT_PREVIEW
+class PrivetHttpWithServerTest : public ::testing::Test,
+ public PrivetURLFetcher::Delegate {
+ protected:
+ PrivetHttpWithServerTest()
+ : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {}
+ void OnError(PrivetURLFetcher* fetcher,
+ PrivetURLFetcher::ErrorType error) override {
+ done_ = true;
+ success_ = false;
+ error_ = error;
+
+ base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
+ }
+
+ void OnParsedJson(PrivetURLFetcher* fetcher,
+ const base::DictionaryValue& value,
+ bool has_error) override {
+ NOTREACHED();
+ base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
+ }
+
+ bool OnRawData(PrivetURLFetcher* fetcher,
+ bool response_is_file,
+ const std::string& data_string,
+ const base::FilePath& data_file) override {
+ done_ = true;
+ success_ = true;
+
+ base::MessageLoop::current()->PostTask(FROM_HERE, quit_);
+ return true;
+ }
+
+ void CreateServer(EmbeddedTestServer::Type type) {
+ server_.reset(new EmbeddedTestServer(type));
+ ASSERT_TRUE(server_->InitializeAndWaitUntilReady());
+
+ base::FilePath test_data_dir;
+ ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
+ server_->ServeFilesFromDirectory(test_data_dir.AppendASCII(kTestDataRoot));
+ }
+
+ void CreateClient() {
+ client_.reset(new PrivetHTTPClientImpl(
+ "test", server_->host_port_pair(),
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
+ }
+
+ void Run() {
+ success_ = false;
+ done_ = false;
+
+ base::RunLoop run_loop;
+ quit_ = run_loop.QuitClosure();
+
+ auto fetcher = client_->CreateURLFetcher(server_->GetURL("/simple.html"),
+ net::URLFetcher::GET, this);
+
+ fetcher->SetMaxRetries(1);
+ fetcher->V3Mode();
+ fetcher->Start();
+
+ run_loop.Run();
+
+ EXPECT_TRUE(done_);
+ }
+
+ bool success_ = false;
+ bool done_ = false;
+ PrivetURLFetcher::ErrorType error_ = PrivetURLFetcher::ErrorType();
+ content::TestBrowserThreadBundle thread_bundle_;
+
+ scoped_ptr<EmbeddedTestServer> server_;
+ scoped_ptr<PrivetHTTPClientImpl> client_;
+
+ base::Closure quit_;
+};
+
+TEST_F(PrivetHttpWithServerTest, HttpServer) {
+ CreateServer(EmbeddedTestServer::TYPE_HTTP);
+
+ CreateClient();
+ Run();
+ EXPECT_TRUE(success_);
+
+ CreateClient();
+ net::SHA256HashValue fingerprint = {};
+ client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
+
+ Run();
+ EXPECT_FALSE(success_);
+ 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
+}
+
+TEST_F(PrivetHttpWithServerTest, HttpsServer) {
+ CreateServer(EmbeddedTestServer::TYPE_HTTPS);
+
+ CreateClient();
+ Run();
+#ifdef OS_MACOSX
+ // Seems like an issue of the test server, it's in HTTPS mode and still
+ // accepts HTTP requests.
+ // TODO(vitalybuka): Remove #ifdef when test server is fixed. crbug.com/548888
+ EXPECT_TRUE(success_);
+#else
+ EXPECT_FALSE(success_);
+ EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
+#endif // OS_MACOSX
+
+ CreateClient();
+ net::SHA256HashValue fingerprint =
+ net::X509Certificate::CalculateFingerprint256(
+ server_->GetCertificate()->os_cert_handle());
+ client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
+
+ Run();
+ EXPECT_TRUE(success_);
+
+ CreateClient();
+ fingerprint = {};
+ client_->SwitchToHttps(server_->host_port_pair().port(), fingerprint);
+
+ Run();
+ EXPECT_FALSE(success_);
+ EXPECT_EQ(PrivetURLFetcher::REQUEST_CANCELED, error_);
+}
+
} // namespace
} // namespace local_discovery
« 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