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

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..4adeb5187d1eb96b615bf382869d1a5a6921f8a2 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,13 +27,16 @@
#include "chrome/browser/local_discovery/pwg_raster_converter.h"
#endif // ENABLE_PRINT_PREVIEW
-using testing::StrictMock;
-using testing::NiceMock;
-
namespace local_discovery {
namespace {
+using testing::StrictMock;
+using testing::NiceMock;
+
+using content::BrowserThread;
+using net::EmbeddedTestServer;
+
const char kSampleInfoResponse[] = "{"
" \"version\": \"1.0\","
" \"name\": \"Common printer\","
@@ -1077,6 +1085,126 @@ 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.Append(FILE_PATH_LITERAL("chrome/test/data")));
+ }
+
+ 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_);
+}
+
+TEST_F(PrivetHttpWithServerTest, HttpsServer) {
+ CreateServer(EmbeddedTestServer::TYPE_HTTPS);
+
+ CreateClient();
+ Run();
+ EXPECT_FALSE(success_);
+ EXPECT_EQ(PrivetURLFetcher::UNKNOWN_ERROR, error_);
+
+ 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

Powered by Google App Engine
This is Rietveld 408576698