Chromium Code Reviews| Index: blimp/client/session/assignment_source_unittest.cc |
| diff --git a/blimp/client/session/assignment_source_unittest.cc b/blimp/client/session/assignment_source_unittest.cc |
| index d893ae3d3a79b4e342b233957b7215cd212e7e16..3a84f5174333f185902fff30e4ed29314b0632f3 100644 |
| --- a/blimp/client/session/assignment_source_unittest.cc |
| +++ b/blimp/client/session/assignment_source_unittest.cc |
| @@ -5,56 +5,64 @@ |
| #include "blimp/client/session/assignment_source.h" |
| #include "base/command_line.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| #include "base/json/json_reader.h" |
| #include "base/json/json_writer.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| #include "base/test/test_simple_task_runner.h" |
| #include "base/thread_task_runner_handle.h" |
| #include "base/values.h" |
| #include "blimp/client/app/blimp_client_switches.h" |
| #include "blimp/common/protocol_version.h" |
| +#include "components/safe_json/testing_json_parser.h" |
| +#include "net/base/test_data_directory.h" |
| #include "net/url_request/test_url_fetcher_factory.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| using testing::_; |
| +using testing::DoAll; |
| using testing::InSequence; |
| +using testing::NotNull; |
| +using testing::Return; |
| +using testing::SetArgPointee; |
| namespace blimp { |
| namespace client { |
| namespace { |
| +const uint8_t kTestIPAddress[] = {127, 0, 0, 1}; |
| +const uint16_t kTestPort = 8086; |
| +const char kTestIP[] = "127.0.0.1"; |
| +const char kTCP[] = "tcp"; |
| +const char kQUIC[] = "quic"; |
| +const char kSSL[] = "ssl"; |
| +const char kCertRelativePath[] = |
| + "blimp/client/session/test_selfsigned_cert.pem"; |
| + |
| MATCHER_P(AssignmentEquals, assignment, "") { |
| return arg.transport_protocol == assignment.transport_protocol && |
| arg.ip_endpoint == assignment.ip_endpoint && |
| arg.client_token == assignment.client_token && |
| - arg.certificate == assignment.certificate && |
| - arg.certificate_fingerprint == assignment.certificate_fingerprint; |
| -} |
| - |
| -net::IPEndPoint BuildIPEndPoint(const std::string& ip, int port) { |
| - net::IPAddress ip_address; |
| - EXPECT_TRUE(ip_address.AssignFromIPLiteral(ip)); |
| - |
| - return net::IPEndPoint(ip_address, port); |
| -} |
| - |
| -Assignment BuildValidAssignment() { |
| - Assignment assignment; |
| - assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| - assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); |
| - assignment.client_token = "SecretT0kenz"; |
| - assignment.certificate_fingerprint = "WhaleWhaleWhale"; |
| - assignment.certificate = "whaaaaaaaaaaaaale"; |
| - return assignment; |
| + ((!assignment.cert && !arg.cert) || |
| + (arg.cert && assignment.cert && |
| + arg.cert->Equals(assignment.cert.get()))); |
| } |
| -std::string BuildResponseFromAssignment(const Assignment& assignment) { |
| +// Builds simulated JSON response from the Assigner service. |
| +// |assignment|: The Assignment to convert. |
| +// |expected_cert_str|: The PEM encoded certificate to include in the response. |
| +std::string BuildResponseFromAssignment(const Assignment& assignment, |
| + const std::string& expected_cert_str) { |
| base::DictionaryValue dict; |
| dict.SetString("clientToken", assignment.client_token); |
| dict.SetString("host", assignment.ip_endpoint.address().ToString()); |
| dict.SetInteger("port", assignment.ip_endpoint.port()); |
| - dict.SetString("certificateFingerprint", assignment.certificate_fingerprint); |
| - dict.SetString("certificate", assignment.certificate); |
| + dict.SetString("certificate", expected_cert_str); |
| std::string json; |
| base::JSONWriter::Write(dict, &json); |
| @@ -63,10 +71,17 @@ std::string BuildResponseFromAssignment(const Assignment& assignment) { |
| class AssignmentSourceTest : public testing::Test { |
| public: |
| - AssignmentSourceTest() |
| - : task_runner_(new base::TestSimpleTaskRunner), |
| - task_runner_handle_(task_runner_), |
| - source_(task_runner_, task_runner_) {} |
| + AssignmentSourceTest() : source_(message_loop_.task_runner()) {} |
| + |
| + void SetUp() override { |
| + base::FilePath src_root; |
| + PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
| + cert_path_ = src_root.Append(kCertRelativePath); |
| + ASSERT_TRUE(base::ReadFileToString(cert_path_, &cert_pem_)); |
| + cert_ = std::move(net::X509Certificate::CreateCertificateListFromBytes( |
| + cert_pem_.data(), cert_pem_.size(), |
| + net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE)[0]); |
| + } |
| // This expects the AssignmentSource::GetAssignment to return a custom |
| // endpoint without having to hit the network. This will typically be used |
| @@ -77,7 +92,7 @@ class AssignmentSourceTest : public testing::Test { |
| base::Bind(&AssignmentSourceTest::AssignmentResponse, |
| base::Unretained(this))); |
| EXPECT_EQ(nullptr, factory_.GetFetcherByID(0)); |
| - task_runner_->RunUntilIdle(); |
| + base::RunLoop().RunUntilIdle(); |
| } |
| // See net/base/net_errors.h for possible status errors. |
| @@ -90,11 +105,10 @@ class AssignmentSourceTest : public testing::Test { |
| source_.GetAssignment(client_auth_token, |
| base::Bind(&AssignmentSourceTest::AssignmentResponse, |
| base::Unretained(this))); |
| + base::RunLoop().RunUntilIdle(); |
| net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| - task_runner_->RunUntilIdle(); |
| - |
| EXPECT_NE(nullptr, fetcher); |
| EXPECT_EQ(kDefaultAssignerURL, fetcher->GetOriginalURL().spec()); |
| @@ -125,30 +139,59 @@ class AssignmentSourceTest : public testing::Test { |
| fetcher->SetResponseString(response); |
| fetcher->delegate()->OnURLFetchComplete(fetcher); |
| - task_runner_->RunUntilIdle(); |
| + base::RunLoop().RunUntilIdle(); |
| } |
| MOCK_METHOD2(AssignmentResponse, |
| void(AssignmentSource::Result, const Assignment&)); |
| protected: |
| + Assignment BuildValidAssignment(); |
| + |
| // Used to drive all AssignmentSource tasks. |
| - scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| - base::ThreadTaskRunnerHandle task_runner_handle_; |
| + // MessageLoop is required by TestingJsonParser's self-deletion logic. |
|
Bernhard Bauer
2016/02/26 16:26:30
Ooh... actually, I should probably modify TestingJ
Kevin M
2016/02/26 19:57:23
Done.
|
| + base::MessageLoop message_loop_; |
| net::TestURLFetcherFactory factory_; |
| + base::ScopedTempDir temp_dir_; |
| + |
| + // Path to the PEM-encoded certificate chain. |
| + base::FilePath cert_path_; |
| + |
| + // Payload of PEM certificate chain at |cert_path_|. |
| + std::string cert_pem_; |
| + |
| + // X509 certificate decoded from |cert_path_|. |
| + scoped_refptr<net::X509Certificate> cert_; |
| + |
| AssignmentSource source_; |
| + |
| + safe_json::TestingJsonParser::ScopedFactoryOverride json_parsing_factory_; |
| }; |
| +Assignment AssignmentSourceTest::BuildValidAssignment() { |
| + Assignment assignment; |
| + assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| + assignment.ip_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| + assignment.client_token = "SecretT0kenz"; |
| + assignment.cert = cert_; |
| + return assignment; |
| +} |
| + |
| TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { |
| Assignment assignment; |
| assignment.transport_protocol = Assignment::TransportProtocol::TCP; |
| - assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); |
| + assignment.ip_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| assignment.client_token = kDummyClientToken; |
| + assignment.cert = scoped_refptr<net::X509Certificate>(nullptr); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kEngineIP, |
| + kTestIP); |
| base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| - switches::kBlimpletEndpoint, "tcp:100.150.200.250:500"); |
| + switches::kEnginePort, std::to_string(kTestPort)); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEngineTransport, kTCP); |
| EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| AssignmentEquals(assignment))) |
| @@ -160,11 +203,18 @@ TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { |
| TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { |
| Assignment assignment; |
| assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| - assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); |
| + assignment.ip_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| assignment.client_token = kDummyClientToken; |
| + assignment.cert = cert_; |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kEngineIP, |
| + kTestIP); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEnginePort, std::to_string(kTestPort)); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEngineTransport, kSSL); |
| base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| - switches::kBlimpletEndpoint, "ssl:100.150.200.250:500"); |
| + switches::kEngineCertPath, cert_path_.value()); |
| EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| AssignmentEquals(assignment))) |
| @@ -176,11 +226,18 @@ TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { |
| TEST_F(AssignmentSourceTest, TestQUICAlternateEndpointSuccess) { |
| Assignment assignment; |
| assignment.transport_protocol = Assignment::TransportProtocol::QUIC; |
| - assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); |
| + assignment.ip_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| assignment.client_token = kDummyClientToken; |
| + assignment.cert = cert_; |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kEngineIP, |
| + kTestIP); |
| base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| - switches::kBlimpletEndpoint, "quic:100.150.200.250:500"); |
| + switches::kEnginePort, std::to_string(kTestPort)); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEngineTransport, kQUIC); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEngineCertPath, cert_path_.value()); |
| EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| AssignmentEquals(assignment))) |
| @@ -197,8 +254,9 @@ TEST_F(AssignmentSourceTest, TestSuccess) { |
| .Times(1); |
| GetNetworkAssignmentAndWaitForResponse( |
| - net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), |
| - "UserAuthT0kenz", kEngineVersion); |
| + net::HTTP_OK, net::Error::OK, |
| + BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| + kEngineVersion); |
| } |
| TEST_F(AssignmentSourceTest, TestSecondRequestInterruptsFirst) { |
| @@ -221,8 +279,9 @@ TEST_F(AssignmentSourceTest, TestSecondRequestInterruptsFirst) { |
| .RetiresOnSaturation(); |
| GetNetworkAssignmentAndWaitForResponse( |
| - net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), |
| - "UserAuthT0kenz", kEngineVersion); |
| + net::HTTP_OK, net::Error::OK, |
| + BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| + kEngineVersion); |
| } |
| TEST_F(AssignmentSourceTest, TestValidAfterError) { |
| @@ -244,8 +303,9 @@ TEST_F(AssignmentSourceTest, TestValidAfterError) { |
| "", "UserAuthT0kenz", kEngineVersion); |
| GetNetworkAssignmentAndWaitForResponse( |
| - net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), |
| - "UserAuthT0kenz", kEngineVersion); |
| + net::HTTP_OK, net::Error::OK, |
| + BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| + kEngineVersion); |
| } |
| TEST_F(AssignmentSourceTest, TestNetworkFailure) { |
| @@ -306,7 +366,7 @@ TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { |
| Assignment assignment = BuildValidAssignment(); |
| // Remove half the response. |
| - std::string response = BuildResponseFromAssignment(assignment); |
| + std::string response = BuildResponseFromAssignment(assignment, cert_pem_); |
| response = response.substr(response.size() / 2); |
| EXPECT_CALL(*this, AssignmentResponse( |
| @@ -320,7 +380,6 @@ TEST_F(AssignmentSourceTest, TestMissingResponsePort) { |
| base::DictionaryValue dict; |
| dict.SetString("clientToken", "SecretT0kenz"); |
| dict.SetString("host", "happywhales"); |
| - dict.SetString("certificateFingerprint", "WhaleWhaleWhale"); |
| dict.SetString("certificate", "whaaaaaaaaaaaaale"); |
| std::string response; |
| @@ -338,7 +397,6 @@ TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { |
| dict.SetString("clientToken", "SecretT0kenz"); |
| dict.SetString("host", "happywhales"); |
| dict.SetInteger("port", 500); |
| - dict.SetString("certificateFingerprint", "WhaleWhaleWhale"); |
| dict.SetString("certificate", "whaaaaaaaaaaaaale"); |
| std::string response; |
| @@ -350,6 +408,37 @@ TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { |
| "UserAuthT0kenz", kEngineVersion); |
| } |
| +TEST_F(AssignmentSourceTest, TestMissingCert) { |
| + base::DictionaryValue dict; |
| + dict.SetString("clientToken", "SecretT0kenz"); |
| + dict.SetString("host", "127.0.0.1"); |
| + dict.SetInteger("port", 500); |
| + |
| + std::string response; |
| + base::JSONWriter::Write(dict, &response); |
| + |
| + EXPECT_CALL(*this, AssignmentResponse( |
| + AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
| + GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
| + "UserAuthT0kenz", kEngineVersion); |
| +} |
| + |
| +TEST_F(AssignmentSourceTest, TestInvalidCert) { |
| + base::DictionaryValue dict; |
| + dict.SetString("clientToken", "SecretT0kenz"); |
| + dict.SetString("host", "127.0.0.1"); |
| + dict.SetInteger("port", 500); |
| + dict.SetString("certificate", "h4x0r c3r7"); |
| + |
| + std::string response; |
| + base::JSONWriter::Write(dict, &response); |
| + |
| + EXPECT_CALL(*this, AssignmentResponse( |
| + AssignmentSource::Result::RESULT_INVALID_CERT, _)); |
| + GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
| + "UserAuthT0kenz", kEngineVersion); |
| +} |
| + |
| } // namespace |
| } // namespace client |
| } // namespace blimp |