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