Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/client/session/assignment_source.h" | 5 #include "blimp/client/session/assignment_source.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "blimp/client/app/blimp_client_switches.h" | 18 #include "blimp/client/app/blimp_client_switches.h" |
| 19 #include "blimp/common/get_client_token.h" | |
| 19 #include "blimp/common/protocol_version.h" | 20 #include "blimp/common/protocol_version.h" |
| 21 #include "blimp/common/switches.h" | |
| 20 #include "components/safe_json/testing_json_parser.h" | 22 #include "components/safe_json/testing_json_parser.h" |
| 21 #include "net/base/test_data_directory.h" | 23 #include "net/base/test_data_directory.h" |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 26 | 28 |
| 27 using testing::_; | 29 using testing::_; |
| 28 using testing::DoAll; | 30 using testing::DoAll; |
| 29 using testing::InSequence; | 31 using testing::InSequence; |
| 30 using testing::NotNull; | 32 using testing::NotNull; |
| 31 using testing::Return; | 33 using testing::Return; |
| 32 using testing::SetArgPointee; | 34 using testing::SetArgPointee; |
| 33 | 35 |
| 34 namespace blimp { | 36 namespace blimp { |
| 35 namespace client { | 37 namespace client { |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 const uint8_t kTestIpAddress[] = {127, 0, 0, 1}; | 40 const uint8_t kTestIpAddress[] = {127, 0, 0, 1}; |
| 39 const uint16_t kTestPort = 8086; | 41 const uint16_t kTestPort = 8086; |
| 40 const char kTestIpAddressString[] = "127.0.0.1"; | 42 const char kTestIpAddressString[] = "127.0.0.1"; |
| 41 const char kTcpTransportName[] = "tcp"; | 43 const char kTcpTransportName[] = "tcp"; |
| 42 const char kSslTransportName[] = "ssl"; | 44 const char kSslTransportName[] = "ssl"; |
| 43 const char kCertRelativePath[] = | 45 const char kCertRelativePath[] = |
| 44 "blimp/client/session/test_selfsigned_cert.pem"; | 46 "blimp/client/session/test_selfsigned_cert.pem"; |
| 45 const char kTestClientToken[] = "secrett0ken"; | 47 const char kTestClientToken[] = "secrett0ken"; |
| 46 const char kTestAuthToken[] = "UserAuthT0kenz"; | 48 const char kTestAuthToken[] = "UserAuthT0kenz"; |
| 47 const char kAssignerUrl[] = "http://www.assigner.test/"; | 49 const char kAssignerUrl[] = "http://www.assigner.test/"; |
| 50 const char kTestClientTokenPath[] = "blimp/test/data/test_client_token"; | |
| 48 | 51 |
| 49 MATCHER_P(AssignmentEquals, assignment, "") { | 52 MATCHER_P(AssignmentEquals, assignment, "") { |
| 50 return arg.transport_protocol == assignment.transport_protocol && | 53 return arg.transport_protocol == assignment.transport_protocol && |
| 51 arg.engine_endpoint == assignment.engine_endpoint && | 54 arg.engine_endpoint == assignment.engine_endpoint && |
| 52 arg.client_token == assignment.client_token && | 55 arg.client_token == assignment.client_token && |
| 53 ((!assignment.cert && !arg.cert) || | 56 ((!assignment.cert && !arg.cert) || |
| 54 (arg.cert && assignment.cert && | 57 (arg.cert && assignment.cert && |
| 55 arg.cert->Equals(assignment.cert.get()))); | 58 arg.cert->Equals(assignment.cert.get()))); |
| 56 } | 59 } |
| 57 | 60 |
| 58 // Converts |value| to a JSON string. | 61 // Converts |value| to a JSON string. |
| 59 std::string ValueToString(const base::Value& value) { | 62 std::string ValueToString(const base::Value& value) { |
| 60 std::string json; | 63 std::string json; |
| 61 base::JSONWriter::Write(value, &json); | 64 base::JSONWriter::Write(value, &json); |
| 62 return json; | 65 return json; |
| 63 } | 66 } |
| 64 | 67 |
| 65 class AssignmentSourceTest : public testing::Test { | 68 class AssignmentSourceTest : public testing::Test { |
| 66 public: | 69 public: |
| 67 AssignmentSourceTest() | 70 AssignmentSourceTest() |
| 68 : source_(GURL(kAssignerUrl), | 71 : source_(GURL(kAssignerUrl), |
| 69 message_loop_.task_runner(), | 72 message_loop_.task_runner(), |
| 70 message_loop_.task_runner()) {} | 73 message_loop_.task_runner()) {} |
| 71 | 74 |
| 72 void SetUp() override { | 75 void SetUp() override { |
| 73 base::FilePath src_root; | 76 base::FilePath src_root; |
| 74 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 77 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
| 75 ASSERT_FALSE(src_root.empty()); | 78 ASSERT_FALSE(src_root.empty()); |
| 76 cert_path_ = src_root.Append(kCertRelativePath); | 79 cert_path_ = src_root.Append(kCertRelativePath); |
| 80 client_token_path_ = src_root.Append(kTestClientTokenPath); | |
| 77 ASSERT_TRUE(base::ReadFileToString(cert_path_, &cert_pem_)); | 81 ASSERT_TRUE(base::ReadFileToString(cert_path_, &cert_pem_)); |
| 78 net::CertificateList cert_list = | 82 net::CertificateList cert_list = |
| 79 net::X509Certificate::CreateCertificateListFromBytes( | 83 net::X509Certificate::CreateCertificateListFromBytes( |
| 80 cert_pem_.data(), cert_pem_.size(), | 84 cert_pem_.data(), cert_pem_.size(), |
| 81 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); | 85 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 82 ASSERT_FALSE(cert_list.empty()); | 86 ASSERT_FALSE(cert_list.empty()); |
| 83 cert_ = std::move(cert_list[0]); | 87 cert_ = std::move(cert_list[0]); |
| 84 ASSERT_TRUE(cert_); | 88 ASSERT_TRUE(cert_); |
| 85 } | 89 } |
| 86 | 90 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 // MessageLoop is required by TestingJsonParser's self-deletion logic. | 160 // MessageLoop is required by TestingJsonParser's self-deletion logic. |
| 157 // TODO(bauerb): Replace this with a TestSimpleTaskRunner once | 161 // TODO(bauerb): Replace this with a TestSimpleTaskRunner once |
| 158 // TestingJsonParser no longer requires having a MessageLoop. | 162 // TestingJsonParser no longer requires having a MessageLoop. |
| 159 base::MessageLoop message_loop_; | 163 base::MessageLoop message_loop_; |
| 160 | 164 |
| 161 net::TestURLFetcherFactory factory_; | 165 net::TestURLFetcherFactory factory_; |
| 162 | 166 |
| 163 // Path to the PEM-encoded certificate chain. | 167 // Path to the PEM-encoded certificate chain. |
| 164 base::FilePath cert_path_; | 168 base::FilePath cert_path_; |
| 165 | 169 |
| 170 // Path to the client token; | |
| 171 base::FilePath client_token_path_; | |
| 172 | |
| 166 // Payload of PEM certificate chain at |cert_path_|. | 173 // Payload of PEM certificate chain at |cert_path_|. |
| 167 std::string cert_pem_; | 174 std::string cert_pem_; |
| 168 | 175 |
| 169 // X509 certificate decoded from |cert_path_|. | 176 // X509 certificate decoded from |cert_path_|. |
| 170 scoped_refptr<net::X509Certificate> cert_; | 177 scoped_refptr<net::X509Certificate> cert_; |
| 171 | 178 |
| 172 AssignmentSource source_; | 179 AssignmentSource source_; |
| 173 | 180 |
| 174 // Allows safe_json to parse JSON in-process, instead of depending on a | 181 // Allows safe_json to parse JSON in-process, instead of depending on a |
| 175 // utility proces. | 182 // utility proces. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 192 dict->SetString("host", kTestIpAddressString); | 199 dict->SetString("host", kTestIpAddressString); |
| 193 dict->SetInteger("port", kTestPort); | 200 dict->SetInteger("port", kTestPort); |
| 194 dict->SetString("certificate", cert_pem_); | 201 dict->SetString("certificate", cert_pem_); |
| 195 return dict; | 202 return dict; |
| 196 } | 203 } |
| 197 | 204 |
| 198 TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { | 205 TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { |
| 199 Assignment assignment; | 206 Assignment assignment; |
| 200 assignment.transport_protocol = Assignment::TransportProtocol::TCP; | 207 assignment.transport_protocol = Assignment::TransportProtocol::TCP; |
| 201 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort); | 208 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort); |
| 202 assignment.client_token = kDummyClientToken; | |
| 203 assignment.cert = scoped_refptr<net::X509Certificate>(nullptr); | 209 assignment.cert = scoped_refptr<net::X509Certificate>(nullptr); |
| 204 | 210 |
| 205 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 211 auto cmd_line = base::CommandLine::ForCurrentProcess(); |
| 206 switches::kEngineIP, kTestIpAddressString); | 212 |
|
Kevin M
2016/05/12 17:44:44
Nit: remove this newline, so that all command-line
CJ
2016/05/12 18:09:30
Done.
| |
| 207 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 213 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString); |
| 208 switches::kEnginePort, std::to_string(kTestPort)); | 214 cmd_line->AppendSwitchASCII(switches::kEnginePort, |
| 209 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 215 std::to_string(kTestPort)); |
| 210 switches::kEngineTransport, kTcpTransportName); | 216 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kTcpTransportName); |
| 217 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value()); | |
| 218 | |
| 219 assignment.client_token = GetClientToken(*cmd_line); | |
| 220 | |
| 221 CHECK_EQ("MyVoiceIsMyPassport", assignment.client_token); | |
| 211 | 222 |
| 212 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 223 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 213 AssignmentEquals(assignment))) | 224 AssignmentEquals(assignment))) |
| 214 .Times(1); | 225 .Times(1); |
| 215 | 226 |
| 216 GetAlternateAssignment(); | 227 GetAlternateAssignment(); |
| 217 } | 228 } |
| 218 | 229 |
| 219 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { | 230 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { |
| 220 Assignment assignment; | 231 Assignment assignment; |
| 221 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | 232 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 222 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort); | 233 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort); |
| 223 assignment.client_token = kDummyClientToken; | |
| 224 assignment.cert = cert_; | 234 assignment.cert = cert_; |
| 225 | 235 |
| 226 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 236 auto cmd_line = base::CommandLine::ForCurrentProcess(); |
| 227 switches::kEngineIP, kTestIpAddressString); | 237 |
| 228 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 238 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString); |
| 229 switches::kEnginePort, std::to_string(kTestPort)); | 239 cmd_line->AppendSwitchASCII(switches::kEnginePort, |
| 230 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 240 std::to_string(kTestPort)); |
| 231 switches::kEngineTransport, kSslTransportName); | 241 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kSslTransportName); |
| 232 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 242 cmd_line->AppendSwitchASCII(switches::kEngineCertPath, cert_path_.value()); |
| 233 switches::kEngineCertPath, cert_path_.value()); | 243 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value()); |
| 244 | |
| 245 assignment.client_token = GetClientToken(*cmd_line); | |
| 234 | 246 |
| 235 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 247 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 236 AssignmentEquals(assignment))) | 248 AssignmentEquals(assignment))) |
| 237 .Times(1); | 249 .Times(1); |
| 238 | 250 |
| 239 GetAlternateAssignment(); | 251 GetAlternateAssignment(); |
| 240 } | 252 } |
| 241 | 253 |
| 242 TEST_F(AssignmentSourceTest, TestSuccess) { | 254 TEST_F(AssignmentSourceTest, TestSuccess) { |
| 243 Assignment assignment = BuildSslAssignment(); | 255 Assignment assignment = BuildSslAssignment(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 EXPECT_CALL(*this, AssignmentResponse( | 390 EXPECT_CALL(*this, AssignmentResponse( |
| 379 AssignmentSource::Result::RESULT_INVALID_CERT, _)); | 391 AssignmentSource::Result::RESULT_INVALID_CERT, _)); |
| 380 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, | 392 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, |
| 381 ValueToString(*response), | 393 ValueToString(*response), |
| 382 kTestAuthToken, kEngineVersion); | 394 kTestAuthToken, kEngineVersion); |
| 383 } | 395 } |
| 384 | 396 |
| 385 } // namespace | 397 } // namespace |
| 386 } // namespace client | 398 } // namespace client |
| 387 } // namespace blimp | 399 } // namespace blimp |
| OLD | NEW |