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