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" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/run_loop.h" | |
| 10 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/values.h" | 18 #include "base/values.h" |
| 13 #include "blimp/client/app/blimp_client_switches.h" | 19 #include "blimp/client/app/blimp_client_switches.h" |
| 14 #include "blimp/common/protocol_version.h" | 20 #include "blimp/common/protocol_version.h" |
| 21 #include "components/safe_json/testing_json_parser.h" | |
| 22 #include "net/base/test_data_directory.h" | |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 23 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 26 |
| 19 using testing::_; | 27 using testing::_; |
| 28 using testing::DoAll; | |
| 20 using testing::InSequence; | 29 using testing::InSequence; |
| 30 using testing::NotNull; | |
| 31 using testing::Return; | |
| 32 using testing::SetArgPointee; | |
| 21 | 33 |
| 22 namespace blimp { | 34 namespace blimp { |
| 23 namespace client { | 35 namespace client { |
| 24 namespace { | 36 namespace { |
| 25 | 37 |
| 38 const uint8_t kTestIPAddress[] = {127, 0, 0, 1}; | |
| 39 const uint16_t kTestPort = 8086; | |
| 40 const char kTestIP[] = "127.0.0.1"; | |
|
Wez
2016/03/01 00:23:56
nit: Give this and the two below more helpful name
Kevin M
2016/03/01 18:23:17
Done.
Kevin M
2016/03/01 18:23:17
Done.
| |
| 41 const char kTCP[] = "tcp"; | |
| 42 const char kSSL[] = "ssl"; | |
| 43 const char kCertRelativePath[] = | |
| 44 "blimp/client/session/test_selfsigned_cert.pem"; | |
| 45 | |
| 26 MATCHER_P(AssignmentEquals, assignment, "") { | 46 MATCHER_P(AssignmentEquals, assignment, "") { |
| 27 return arg.transport_protocol == assignment.transport_protocol && | 47 return arg.transport_protocol == assignment.transport_protocol && |
| 28 arg.ip_endpoint == assignment.ip_endpoint && | 48 arg.engine_endpoint == assignment.engine_endpoint && |
| 29 arg.client_token == assignment.client_token && | 49 arg.client_token == assignment.client_token && |
| 30 arg.certificate == assignment.certificate && | 50 ((!assignment.cert && !arg.cert) || |
| 31 arg.certificate_fingerprint == assignment.certificate_fingerprint; | 51 (arg.cert && assignment.cert && |
| 52 arg.cert->Equals(assignment.cert.get()))); | |
| 32 } | 53 } |
| 33 | 54 |
| 34 net::IPEndPoint BuildIPEndPoint(const std::string& ip, int port) { | 55 // Builds simulated JSON response from the Assigner service. |
| 35 net::IPAddress ip_address; | 56 // |assignment|: The Assignment to convert. |
| 36 EXPECT_TRUE(ip_address.AssignFromIPLiteral(ip)); | 57 // |expected_cert_str|: The PEM encoded certificate to include in the response. |
| 37 | 58 std::string BuildResponseFromAssignment(const Assignment& assignment, |
| 38 return net::IPEndPoint(ip_address, port); | 59 const std::string& expected_cert_str) { |
| 39 } | |
| 40 | |
| 41 Assignment BuildValidAssignment() { | |
| 42 Assignment assignment; | |
| 43 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | |
| 44 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); | |
| 45 assignment.client_token = "SecretT0kenz"; | |
| 46 assignment.certificate_fingerprint = "WhaleWhaleWhale"; | |
| 47 assignment.certificate = "whaaaaaaaaaaaaale"; | |
| 48 return assignment; | |
| 49 } | |
| 50 | |
| 51 std::string BuildResponseFromAssignment(const Assignment& assignment) { | |
| 52 base::DictionaryValue dict; | 60 base::DictionaryValue dict; |
| 53 dict.SetString("clientToken", assignment.client_token); | 61 dict.SetString("clientToken", assignment.client_token); |
| 54 dict.SetString("host", assignment.ip_endpoint.address().ToString()); | 62 dict.SetString("host", assignment.engine_endpoint.address().ToString()); |
| 55 dict.SetInteger("port", assignment.ip_endpoint.port()); | 63 dict.SetInteger("port", assignment.engine_endpoint.port()); |
| 56 dict.SetString("certificateFingerprint", assignment.certificate_fingerprint); | 64 dict.SetString("certificate", expected_cert_str); |
| 57 dict.SetString("certificate", assignment.certificate); | |
| 58 | 65 |
| 59 std::string json; | 66 std::string json; |
| 60 base::JSONWriter::Write(dict, &json); | 67 base::JSONWriter::Write(dict, &json); |
| 61 return json; | 68 return json; |
| 62 } | 69 } |
| 63 | 70 |
| 64 class AssignmentSourceTest : public testing::Test { | 71 class AssignmentSourceTest : public testing::Test { |
| 65 public: | 72 public: |
| 66 AssignmentSourceTest() | 73 AssignmentSourceTest() |
| 67 : task_runner_(new base::TestSimpleTaskRunner), | 74 : source_(message_loop_.task_runner(), message_loop_.task_runner()) {} |
| 68 task_runner_handle_(task_runner_), | 75 |
| 69 source_(task_runner_, task_runner_) {} | 76 void SetUp() override { |
| 77 base::FilePath src_root; | |
| 78 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | |
| 79 cert_path_ = src_root.Append(kCertRelativePath); | |
| 80 ASSERT_TRUE(base::ReadFileToString(cert_path_, &cert_pem_)); | |
| 81 cert_ = std::move(net::X509Certificate::CreateCertificateListFromBytes( | |
| 82 cert_pem_.data(), cert_pem_.size(), | |
| 83 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE)[0]); | |
| 84 } | |
| 70 | 85 |
| 71 // This expects the AssignmentSource::GetAssignment to return a custom | 86 // This expects the AssignmentSource::GetAssignment to return a custom |
| 72 // endpoint without having to hit the network. This will typically be used | 87 // endpoint without having to hit the network. This will typically be used |
| 73 // for testing that specifying an assignment via the command line works as | 88 // for testing that specifying an assignment via the command line works as |
| 74 // expected. | 89 // expected. |
| 75 void GetAlternateAssignment() { | 90 void GetAlternateAssignment() { |
| 76 source_.GetAssignment("", | 91 source_.GetAssignment("", |
| 77 base::Bind(&AssignmentSourceTest::AssignmentResponse, | 92 base::Bind(&AssignmentSourceTest::AssignmentResponse, |
| 78 base::Unretained(this))); | 93 base::Unretained(this))); |
| 79 EXPECT_EQ(nullptr, factory_.GetFetcherByID(0)); | 94 EXPECT_EQ(nullptr, factory_.GetFetcherByID(0)); |
| 80 task_runner_->RunUntilIdle(); | 95 base::RunLoop().RunUntilIdle(); |
| 81 } | 96 } |
| 82 | 97 |
| 83 // See net/base/net_errors.h for possible status errors. | 98 // See net/base/net_errors.h for possible status errors. |
| 84 void GetNetworkAssignmentAndWaitForResponse( | 99 void GetNetworkAssignmentAndWaitForResponse( |
| 85 net::HttpStatusCode response_code, | 100 net::HttpStatusCode response_code, |
| 86 int status, | 101 int status, |
| 87 const std::string& response, | 102 const std::string& response, |
| 88 const std::string& client_auth_token, | 103 const std::string& client_auth_token, |
| 89 const std::string& protocol_version) { | 104 const std::string& protocol_version) { |
| 90 source_.GetAssignment(client_auth_token, | 105 source_.GetAssignment(client_auth_token, |
| 91 base::Bind(&AssignmentSourceTest::AssignmentResponse, | 106 base::Bind(&AssignmentSourceTest::AssignmentResponse, |
| 92 base::Unretained(this))); | 107 base::Unretained(this))); |
| 108 base::RunLoop().RunUntilIdle(); | |
| 93 | 109 |
| 94 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 110 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 95 | 111 |
| 96 task_runner_->RunUntilIdle(); | |
| 97 | |
| 98 EXPECT_NE(nullptr, fetcher); | 112 EXPECT_NE(nullptr, fetcher); |
| 99 EXPECT_EQ(kDefaultAssignerURL, fetcher->GetOriginalURL().spec()); | 113 EXPECT_EQ(kDefaultAssignerURL, fetcher->GetOriginalURL().spec()); |
| 100 | 114 |
| 101 // Check that the request has a valid protocol_version. | 115 // Check that the request has a valid protocol_version. |
| 102 scoped_ptr<base::Value> json = | 116 scoped_ptr<base::Value> json = |
| 103 base::JSONReader::Read(fetcher->upload_data()); | 117 base::JSONReader::Read(fetcher->upload_data()); |
| 104 EXPECT_NE(nullptr, json.get()); | 118 EXPECT_NE(nullptr, json.get()); |
| 105 | 119 |
| 106 const base::DictionaryValue* dict; | 120 const base::DictionaryValue* dict; |
| 107 EXPECT_TRUE(json->GetAsDictionary(&dict)); | 121 EXPECT_TRUE(json->GetAsDictionary(&dict)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 118 std::string authorization; | 132 std::string authorization; |
| 119 EXPECT_TRUE(headers.GetHeader("Authorization", &authorization)); | 133 EXPECT_TRUE(headers.GetHeader("Authorization", &authorization)); |
| 120 EXPECT_EQ("Bearer " + client_auth_token, authorization); | 134 EXPECT_EQ("Bearer " + client_auth_token, authorization); |
| 121 | 135 |
| 122 // Send the fake response back. | 136 // Send the fake response back. |
| 123 fetcher->set_response_code(response_code); | 137 fetcher->set_response_code(response_code); |
| 124 fetcher->set_status(net::URLRequestStatus::FromError(status)); | 138 fetcher->set_status(net::URLRequestStatus::FromError(status)); |
| 125 fetcher->SetResponseString(response); | 139 fetcher->SetResponseString(response); |
| 126 fetcher->delegate()->OnURLFetchComplete(fetcher); | 140 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 127 | 141 |
| 128 task_runner_->RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
| 129 } | 143 } |
| 130 | 144 |
| 131 MOCK_METHOD2(AssignmentResponse, | 145 MOCK_METHOD2(AssignmentResponse, |
| 132 void(AssignmentSource::Result, const Assignment&)); | 146 void(AssignmentSource::Result, const Assignment&)); |
| 133 | 147 |
| 134 protected: | 148 protected: |
| 149 Assignment BuildValidAssignment(); | |
| 150 | |
| 135 // Used to drive all AssignmentSource tasks. | 151 // Used to drive all AssignmentSource tasks. |
| 136 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 152 // MessageLoop is required by TestingJsonParser's self-deletion logic. |
| 137 base::ThreadTaskRunnerHandle task_runner_handle_; | 153 // TODO(bauerb): Replace this with a TestSimpleTaskRunner once |
| 154 // TestingJsonParser no longer requires having a MessageLoop. | |
| 155 base::MessageLoop message_loop_; | |
| 138 | 156 |
| 139 net::TestURLFetcherFactory factory_; | 157 net::TestURLFetcherFactory factory_; |
| 140 | 158 |
| 159 base::ScopedTempDir temp_dir_; | |
| 160 | |
| 161 // Path to the PEM-encoded certificate chain. | |
| 162 base::FilePath cert_path_; | |
| 163 | |
| 164 // Payload of PEM certificate chain at |cert_path_|. | |
| 165 std::string cert_pem_; | |
| 166 | |
| 167 // X509 certificate decoded from |cert_path_|. | |
| 168 scoped_refptr<net::X509Certificate> cert_; | |
| 169 | |
| 141 AssignmentSource source_; | 170 AssignmentSource source_; |
| 171 | |
| 172 safe_json::TestingJsonParser::ScopedFactoryOverride json_parsing_factory_; | |
| 142 }; | 173 }; |
| 143 | 174 |
| 175 Assignment AssignmentSourceTest::BuildValidAssignment() { | |
| 176 Assignment assignment; | |
| 177 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | |
| 178 assignment.engine_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); | |
| 179 assignment.client_token = "SecretT0kenz"; | |
| 180 assignment.cert = cert_; | |
| 181 return assignment; | |
| 182 } | |
| 183 | |
| 144 TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { | 184 TEST_F(AssignmentSourceTest, TestTCPAlternateEndpointSuccess) { |
| 145 Assignment assignment; | 185 Assignment assignment; |
| 146 assignment.transport_protocol = Assignment::TransportProtocol::TCP; | 186 assignment.transport_protocol = Assignment::TransportProtocol::TCP; |
| 147 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); | 187 assignment.engine_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| 148 assignment.client_token = kDummyClientToken; | 188 assignment.client_token = kDummyClientToken; |
| 189 assignment.cert = scoped_refptr<net::X509Certificate>(nullptr); | |
| 149 | 190 |
| 191 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kEngineIP, | |
| 192 kTestIP); | |
| 150 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 193 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 151 switches::kBlimpletEndpoint, "tcp:100.150.200.250:500"); | 194 switches::kEnginePort, std::to_string(kTestPort)); |
| 195 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 196 switches::kEngineTransport, kTCP); | |
| 152 | 197 |
| 153 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 198 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 154 AssignmentEquals(assignment))) | 199 AssignmentEquals(assignment))) |
| 155 .Times(1); | 200 .Times(1); |
| 156 | 201 |
| 157 GetAlternateAssignment(); | 202 GetAlternateAssignment(); |
| 158 } | 203 } |
| 159 | 204 |
| 160 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { | 205 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { |
| 161 Assignment assignment; | 206 Assignment assignment; |
| 162 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | 207 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 163 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); | 208 assignment.engine_endpoint = net::IPEndPoint(kTestIPAddress, kTestPort); |
| 164 assignment.client_token = kDummyClientToken; | 209 assignment.client_token = kDummyClientToken; |
| 210 assignment.cert = cert_; | |
| 165 | 211 |
| 212 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kEngineIP, | |
| 213 kTestIP); | |
| 166 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 214 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 167 switches::kBlimpletEndpoint, "ssl:100.150.200.250:500"); | 215 switches::kEnginePort, std::to_string(kTestPort)); |
| 216 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 217 switches::kEngineTransport, kSSL); | |
| 218 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 219 switches::kEngineCertPath, cert_path_.value()); | |
| 168 | 220 |
| 169 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 221 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 170 AssignmentEquals(assignment))) | 222 AssignmentEquals(assignment))) |
| 171 .Times(1); | |
| 172 | |
| 173 GetAlternateAssignment(); | |
| 174 } | |
| 175 | |
| 176 TEST_F(AssignmentSourceTest, TestQUICAlternateEndpointSuccess) { | |
| 177 Assignment assignment; | |
| 178 assignment.transport_protocol = Assignment::TransportProtocol::QUIC; | |
| 179 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500); | |
| 180 assignment.client_token = kDummyClientToken; | |
| 181 | |
| 182 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 183 switches::kBlimpletEndpoint, "quic:100.150.200.250:500"); | |
| 184 | |
| 185 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | |
| 186 AssignmentEquals(assignment))) | |
| 187 .Times(1); | 223 .Times(1); |
| 188 | 224 |
| 189 GetAlternateAssignment(); | 225 GetAlternateAssignment(); |
| 190 } | 226 } |
| 191 | 227 |
| 192 TEST_F(AssignmentSourceTest, TestSuccess) { | 228 TEST_F(AssignmentSourceTest, TestSuccess) { |
| 193 Assignment assignment = BuildValidAssignment(); | 229 Assignment assignment = BuildValidAssignment(); |
| 194 | 230 |
| 195 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 231 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 196 AssignmentEquals(assignment))) | 232 AssignmentEquals(assignment))) |
| 197 .Times(1); | 233 .Times(1); |
| 198 | 234 |
| 199 GetNetworkAssignmentAndWaitForResponse( | 235 GetNetworkAssignmentAndWaitForResponse( |
| 200 net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), | 236 net::HTTP_OK, net::Error::OK, |
| 201 "UserAuthT0kenz", kEngineVersion); | 237 BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| 238 kEngineVersion); | |
| 202 } | 239 } |
| 203 | 240 |
| 204 TEST_F(AssignmentSourceTest, TestSecondRequestInterruptsFirst) { | 241 TEST_F(AssignmentSourceTest, TestSecondRequestInterruptsFirst) { |
| 205 InSequence sequence; | 242 InSequence sequence; |
| 206 Assignment assignment = BuildValidAssignment(); | 243 Assignment assignment = BuildValidAssignment(); |
| 207 | 244 |
| 208 source_.GetAssignment("", | 245 source_.GetAssignment("", |
| 209 base::Bind(&AssignmentSourceTest::AssignmentResponse, | 246 base::Bind(&AssignmentSourceTest::AssignmentResponse, |
| 210 base::Unretained(this))); | 247 base::Unretained(this))); |
| 211 | 248 |
| 212 EXPECT_CALL(*this, AssignmentResponse( | 249 EXPECT_CALL(*this, AssignmentResponse( |
| 213 AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, | 250 AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, |
| 214 AssignmentEquals(Assignment()))) | 251 AssignmentEquals(Assignment()))) |
| 215 .Times(1) | 252 .Times(1) |
| 216 .RetiresOnSaturation(); | 253 .RetiresOnSaturation(); |
| 217 | 254 |
| 218 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 255 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 219 AssignmentEquals(assignment))) | 256 AssignmentEquals(assignment))) |
| 220 .Times(1) | 257 .Times(1) |
| 221 .RetiresOnSaturation(); | 258 .RetiresOnSaturation(); |
| 222 | 259 |
| 223 GetNetworkAssignmentAndWaitForResponse( | 260 GetNetworkAssignmentAndWaitForResponse( |
| 224 net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), | 261 net::HTTP_OK, net::Error::OK, |
| 225 "UserAuthT0kenz", kEngineVersion); | 262 BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| 263 kEngineVersion); | |
| 226 } | 264 } |
| 227 | 265 |
| 228 TEST_F(AssignmentSourceTest, TestValidAfterError) { | 266 TEST_F(AssignmentSourceTest, TestValidAfterError) { |
| 229 InSequence sequence; | 267 InSequence sequence; |
| 230 Assignment assignment = BuildValidAssignment(); | 268 Assignment assignment = BuildValidAssignment(); |
| 231 | 269 |
| 232 EXPECT_CALL(*this, AssignmentResponse( | 270 EXPECT_CALL(*this, AssignmentResponse( |
| 233 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)) | 271 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)) |
| 234 .Times(1) | 272 .Times(1) |
| 235 .RetiresOnSaturation(); | 273 .RetiresOnSaturation(); |
| 236 | 274 |
| 237 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 275 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
| 238 AssignmentEquals(assignment))) | 276 AssignmentEquals(assignment))) |
| 239 .Times(1) | 277 .Times(1) |
| 240 .RetiresOnSaturation(); | 278 .RetiresOnSaturation(); |
| 241 | 279 |
| 242 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, | 280 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, |
| 243 net::Error::ERR_INSUFFICIENT_RESOURCES, | 281 net::Error::ERR_INSUFFICIENT_RESOURCES, |
| 244 "", "UserAuthT0kenz", kEngineVersion); | 282 "", "UserAuthT0kenz", kEngineVersion); |
| 245 | 283 |
| 246 GetNetworkAssignmentAndWaitForResponse( | 284 GetNetworkAssignmentAndWaitForResponse( |
| 247 net::HTTP_OK, net::Error::OK, BuildResponseFromAssignment(assignment), | 285 net::HTTP_OK, net::Error::OK, |
| 248 "UserAuthT0kenz", kEngineVersion); | 286 BuildResponseFromAssignment(assignment, cert_pem_), "UserAuthT0kenz", |
| 287 kEngineVersion); | |
| 249 } | 288 } |
| 250 | 289 |
| 251 TEST_F(AssignmentSourceTest, TestNetworkFailure) { | 290 TEST_F(AssignmentSourceTest, TestNetworkFailure) { |
| 252 EXPECT_CALL(*this, AssignmentResponse( | 291 EXPECT_CALL(*this, AssignmentResponse( |
| 253 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)); | 292 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)); |
| 254 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, | 293 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, |
| 255 net::Error::ERR_INSUFFICIENT_RESOURCES, | 294 net::Error::ERR_INSUFFICIENT_RESOURCES, |
| 256 "", "UserAuthT0kenz", kEngineVersion); | 295 "", "UserAuthT0kenz", kEngineVersion); |
| 257 } | 296 } |
| 258 | 297 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 338 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
| 300 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED, | 339 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED, |
| 301 net::Error::OK, "", "UserAuthT0kenz", | 340 net::Error::OK, "", "UserAuthT0kenz", |
| 302 kEngineVersion); | 341 kEngineVersion); |
| 303 } | 342 } |
| 304 | 343 |
| 305 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { | 344 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { |
| 306 Assignment assignment = BuildValidAssignment(); | 345 Assignment assignment = BuildValidAssignment(); |
| 307 | 346 |
| 308 // Remove half the response. | 347 // Remove half the response. |
| 309 std::string response = BuildResponseFromAssignment(assignment); | 348 std::string response = BuildResponseFromAssignment(assignment, cert_pem_); |
| 310 response = response.substr(response.size() / 2); | 349 response = response.substr(response.size() / 2); |
| 311 | 350 |
| 312 EXPECT_CALL(*this, AssignmentResponse( | 351 EXPECT_CALL(*this, AssignmentResponse( |
| 313 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 352 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
| 314 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | 353 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
| 315 "UserAuthT0kenz", kEngineVersion); | 354 "UserAuthT0kenz", kEngineVersion); |
| 316 } | 355 } |
| 317 | 356 |
| 318 TEST_F(AssignmentSourceTest, TestMissingResponsePort) { | 357 TEST_F(AssignmentSourceTest, TestMissingResponsePort) { |
| 319 // Purposely do not add the 'port' field to the response. | 358 // Purposely do not add the 'port' field to the response. |
| 320 base::DictionaryValue dict; | 359 base::DictionaryValue dict; |
| 321 dict.SetString("clientToken", "SecretT0kenz"); | 360 dict.SetString("clientToken", "SecretT0kenz"); |
| 322 dict.SetString("host", "happywhales"); | 361 dict.SetString("host", "happywhales"); |
| 323 dict.SetString("certificateFingerprint", "WhaleWhaleWhale"); | |
| 324 dict.SetString("certificate", "whaaaaaaaaaaaaale"); | 362 dict.SetString("certificate", "whaaaaaaaaaaaaale"); |
| 325 | 363 |
| 326 std::string response; | 364 std::string response; |
| 327 base::JSONWriter::Write(dict, &response); | 365 base::JSONWriter::Write(dict, &response); |
| 328 | 366 |
| 329 EXPECT_CALL(*this, AssignmentResponse( | 367 EXPECT_CALL(*this, AssignmentResponse( |
| 330 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 368 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
| 331 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | 369 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
| 332 "UserAuthT0kenz", kEngineVersion); | 370 "UserAuthT0kenz", kEngineVersion); |
| 333 } | 371 } |
| 334 | 372 |
| 335 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { | 373 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { |
| 336 // Purposely add an invalid IP field to the response. | 374 // Purposely add an invalid IP field to the response. |
| 337 base::DictionaryValue dict; | 375 base::DictionaryValue dict; |
| 338 dict.SetString("clientToken", "SecretT0kenz"); | 376 dict.SetString("clientToken", "SecretT0kenz"); |
| 339 dict.SetString("host", "happywhales"); | 377 dict.SetString("host", "happywhales"); |
| 340 dict.SetInteger("port", 500); | 378 dict.SetInteger("port", 500); |
| 341 dict.SetString("certificateFingerprint", "WhaleWhaleWhale"); | |
| 342 dict.SetString("certificate", "whaaaaaaaaaaaaale"); | 379 dict.SetString("certificate", "whaaaaaaaaaaaaale"); |
| 343 | 380 |
| 344 std::string response; | 381 std::string response; |
| 345 base::JSONWriter::Write(dict, &response); | 382 base::JSONWriter::Write(dict, &response); |
| 346 | 383 |
| 347 EXPECT_CALL(*this, AssignmentResponse( | 384 EXPECT_CALL(*this, AssignmentResponse( |
| 348 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 385 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
| 349 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | 386 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
| 350 "UserAuthT0kenz", kEngineVersion); | 387 "UserAuthT0kenz", kEngineVersion); |
| 351 } | 388 } |
| 352 | 389 |
| 390 TEST_F(AssignmentSourceTest, TestMissingCert) { | |
| 391 base::DictionaryValue dict; | |
| 392 dict.SetString("clientToken", "SecretT0kenz"); | |
| 393 dict.SetString("host", "127.0.0.1"); | |
| 394 dict.SetInteger("port", 500); | |
| 395 | |
| 396 std::string response; | |
| 397 base::JSONWriter::Write(dict, &response); | |
| 398 | |
| 399 EXPECT_CALL(*this, AssignmentResponse( | |
| 400 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | |
| 401 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | |
| 402 "UserAuthT0kenz", kEngineVersion); | |
| 403 } | |
| 404 | |
| 405 TEST_F(AssignmentSourceTest, TestInvalidCert) { | |
| 406 base::DictionaryValue dict; | |
| 407 dict.SetString("clientToken", "SecretT0kenz"); | |
| 408 dict.SetString("host", "127.0.0.1"); | |
| 409 dict.SetInteger("port", 500); | |
| 410 dict.SetString("certificate", "h4x0r c3r7"); | |
| 411 | |
| 412 std::string response; | |
| 413 base::JSONWriter::Write(dict, &response); | |
| 414 | |
| 415 EXPECT_CALL(*this, AssignmentResponse( | |
| 416 AssignmentSource::Result::RESULT_INVALID_CERT, _)); | |
| 417 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | |
| 418 "UserAuthT0kenz", kEngineVersion); | |
| 419 } | |
| 420 | |
| 353 } // namespace | 421 } // namespace |
| 354 } // namespace client | 422 } // namespace client |
| 355 } // namespace blimp | 423 } // namespace blimp |
| OLD | NEW |