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/strings/string_number_conversions.h" |
15 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "blimp/client/app/blimp_client_switches.h" | 19 #include "blimp/client/app/blimp_client_switches.h" |
19 #include "blimp/common/get_client_token.h" | 20 #include "blimp/common/get_client_token.h" |
20 #include "blimp/common/protocol_version.h" | 21 #include "blimp/common/protocol_version.h" |
21 #include "blimp/common/switches.h" | 22 #include "blimp/common/switches.h" |
22 #include "components/safe_json/testing_json_parser.h" | 23 #include "components/safe_json/testing_json_parser.h" |
23 #include "net/base/test_data_directory.h" | 24 #include "net/base/test_data_directory.h" |
24 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 EXPECT_EQ(nullptr, factory_.GetFetcherByID(0)); | 100 EXPECT_EQ(nullptr, factory_.GetFetcherByID(0)); |
100 base::RunLoop().RunUntilIdle(); | 101 base::RunLoop().RunUntilIdle(); |
101 } | 102 } |
102 | 103 |
103 // See net/base/net_errors.h for possible status errors. | 104 // See net/base/net_errors.h for possible status errors. |
104 void GetNetworkAssignmentAndWaitForResponse( | 105 void GetNetworkAssignmentAndWaitForResponse( |
105 net::HttpStatusCode response_code, | 106 net::HttpStatusCode response_code, |
106 int status, | 107 int status, |
107 const std::string& response, | 108 const std::string& response, |
108 const std::string& client_auth_token, | 109 const std::string& client_auth_token, |
109 const std::string& protocol_version) { | 110 const int protocol_version) { |
110 source_.GetAssignment(client_auth_token, | 111 source_.GetAssignment(client_auth_token, |
111 base::Bind(&AssignmentSourceTest::AssignmentResponse, | 112 base::Bind(&AssignmentSourceTest::AssignmentResponse, |
112 base::Unretained(this))); | 113 base::Unretained(this))); |
113 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
114 | 115 |
115 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 116 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
116 | 117 |
117 EXPECT_NE(nullptr, fetcher); | 118 EXPECT_NE(nullptr, fetcher); |
118 EXPECT_EQ(kAssignerUrl, fetcher->GetOriginalURL().spec()); | 119 EXPECT_EQ(kAssignerUrl, fetcher->GetOriginalURL().spec()); |
119 | 120 |
120 // Check that the request has a valid protocol_version. | 121 // Check that the request has a valid protocol_version. |
121 std::unique_ptr<base::Value> json = | 122 std::unique_ptr<base::Value> json = |
122 base::JSONReader::Read(fetcher->upload_data()); | 123 base::JSONReader::Read(fetcher->upload_data()); |
123 EXPECT_NE(nullptr, json.get()); | 124 EXPECT_NE(nullptr, json.get()); |
124 | 125 |
125 const base::DictionaryValue* dict; | 126 const base::DictionaryValue* dict; |
126 EXPECT_TRUE(json->GetAsDictionary(&dict)); | 127 EXPECT_TRUE(json->GetAsDictionary(&dict)); |
127 | 128 |
128 std::string uploaded_protocol_version; | 129 std::string uploaded_protocol_version; |
129 EXPECT_TRUE( | 130 EXPECT_TRUE( |
130 dict->GetString("protocol_version", &uploaded_protocol_version)); | 131 dict->GetString("protocol_version", &uploaded_protocol_version)); |
131 EXPECT_EQ(protocol_version, uploaded_protocol_version); | 132 std::string expected_protocol_version = base::IntToString(protocol_version); |
| 133 EXPECT_EQ(expected_protocol_version, uploaded_protocol_version); |
132 | 134 |
133 // Check that the request has a valid authentication header. | 135 // Check that the request has a valid authentication header. |
134 net::HttpRequestHeaders headers; | 136 net::HttpRequestHeaders headers; |
135 fetcher->GetExtraRequestHeaders(&headers); | 137 fetcher->GetExtraRequestHeaders(&headers); |
136 | 138 |
137 std::string authorization; | 139 std::string authorization; |
138 EXPECT_TRUE(headers.GetHeader("Authorization", &authorization)); | 140 EXPECT_TRUE(headers.GetHeader("Authorization", &authorization)); |
139 EXPECT_EQ("Bearer " + client_auth_token, authorization); | 141 EXPECT_EQ("Bearer " + client_auth_token, authorization); |
140 | 142 |
141 // Send the fake response back. | 143 // Send the fake response back. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 254 |
253 TEST_F(AssignmentSourceTest, TestSuccess) { | 255 TEST_F(AssignmentSourceTest, TestSuccess) { |
254 Assignment assignment = BuildSslAssignment(); | 256 Assignment assignment = BuildSslAssignment(); |
255 | 257 |
256 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 258 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
257 AssignmentEquals(assignment))) | 259 AssignmentEquals(assignment))) |
258 .Times(1); | 260 .Times(1); |
259 | 261 |
260 GetNetworkAssignmentAndWaitForResponse( | 262 GetNetworkAssignmentAndWaitForResponse( |
261 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), | 263 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), |
262 kTestAuthToken, kEngineVersion); | 264 kTestAuthToken, kProtocolVersion); |
263 } | 265 } |
264 | 266 |
265 TEST_F(AssignmentSourceTest, TestValidAfterError) { | 267 TEST_F(AssignmentSourceTest, TestValidAfterError) { |
266 InSequence sequence; | 268 InSequence sequence; |
267 Assignment assignment = BuildSslAssignment(); | 269 Assignment assignment = BuildSslAssignment(); |
268 | 270 |
269 EXPECT_CALL(*this, AssignmentResponse( | 271 EXPECT_CALL(*this, AssignmentResponse( |
270 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)) | 272 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)) |
271 .Times(1) | 273 .Times(1) |
272 .RetiresOnSaturation(); | 274 .RetiresOnSaturation(); |
273 | 275 |
274 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, | 276 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, |
275 AssignmentEquals(assignment))) | 277 AssignmentEquals(assignment))) |
276 .Times(1) | 278 .Times(1) |
277 .RetiresOnSaturation(); | 279 .RetiresOnSaturation(); |
278 | 280 |
279 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, | 281 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, |
280 net::Error::ERR_INSUFFICIENT_RESOURCES, | 282 net::Error::ERR_INSUFFICIENT_RESOURCES, |
281 "", kTestAuthToken, kEngineVersion); | 283 "", kTestAuthToken, kProtocolVersion); |
282 | 284 |
283 GetNetworkAssignmentAndWaitForResponse( | 285 GetNetworkAssignmentAndWaitForResponse( |
284 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), | 286 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), |
285 kTestAuthToken, kEngineVersion); | 287 kTestAuthToken, kProtocolVersion); |
286 } | 288 } |
287 | 289 |
288 TEST_F(AssignmentSourceTest, TestNetworkFailure) { | 290 TEST_F(AssignmentSourceTest, TestNetworkFailure) { |
289 EXPECT_CALL(*this, AssignmentResponse( | 291 EXPECT_CALL(*this, AssignmentResponse( |
290 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)); | 292 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)); |
291 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, | 293 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, |
292 net::Error::ERR_INSUFFICIENT_RESOURCES, | 294 net::Error::ERR_INSUFFICIENT_RESOURCES, |
293 "", kTestAuthToken, kEngineVersion); | 295 "", kTestAuthToken, kProtocolVersion); |
294 } | 296 } |
295 | 297 |
296 TEST_F(AssignmentSourceTest, TestBadRequest) { | 298 TEST_F(AssignmentSourceTest, TestBadRequest) { |
297 EXPECT_CALL(*this, AssignmentResponse( | 299 EXPECT_CALL(*this, AssignmentResponse( |
298 AssignmentSource::Result::RESULT_BAD_REQUEST, _)); | 300 AssignmentSource::Result::RESULT_BAD_REQUEST, _)); |
299 GetNetworkAssignmentAndWaitForResponse(net::HTTP_BAD_REQUEST, net::Error::OK, | 301 GetNetworkAssignmentAndWaitForResponse(net::HTTP_BAD_REQUEST, net::Error::OK, |
300 "", kTestAuthToken, kEngineVersion); | 302 "", kTestAuthToken, kProtocolVersion); |
301 } | 303 } |
302 | 304 |
303 TEST_F(AssignmentSourceTest, TestUnauthorized) { | 305 TEST_F(AssignmentSourceTest, TestUnauthorized) { |
304 EXPECT_CALL(*this, | 306 EXPECT_CALL(*this, |
305 AssignmentResponse( | 307 AssignmentResponse( |
306 AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, _)); | 308 AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, _)); |
307 GetNetworkAssignmentAndWaitForResponse(net::HTTP_UNAUTHORIZED, net::Error::OK, | 309 GetNetworkAssignmentAndWaitForResponse(net::HTTP_UNAUTHORIZED, net::Error::OK, |
308 "", kTestAuthToken, kEngineVersion); | 310 "", kTestAuthToken, kProtocolVersion); |
309 } | 311 } |
310 | 312 |
311 TEST_F(AssignmentSourceTest, TestForbidden) { | 313 TEST_F(AssignmentSourceTest, TestForbidden) { |
312 EXPECT_CALL(*this, AssignmentResponse( | 314 EXPECT_CALL(*this, AssignmentResponse( |
313 AssignmentSource::Result::RESULT_USER_INVALID, _)); | 315 AssignmentSource::Result::RESULT_USER_INVALID, _)); |
314 GetNetworkAssignmentAndWaitForResponse(net::HTTP_FORBIDDEN, net::Error::OK, | 316 GetNetworkAssignmentAndWaitForResponse(net::HTTP_FORBIDDEN, net::Error::OK, |
315 "", kTestAuthToken, kEngineVersion); | 317 "", kTestAuthToken, kProtocolVersion); |
316 } | 318 } |
317 | 319 |
318 TEST_F(AssignmentSourceTest, TestTooManyRequests) { | 320 TEST_F(AssignmentSourceTest, TestTooManyRequests) { |
319 EXPECT_CALL(*this, AssignmentResponse( | 321 EXPECT_CALL(*this, AssignmentResponse( |
320 AssignmentSource::Result::RESULT_OUT_OF_VMS, _)); | 322 AssignmentSource::Result::RESULT_OUT_OF_VMS, _)); |
321 GetNetworkAssignmentAndWaitForResponse(static_cast<net::HttpStatusCode>(429), | 323 GetNetworkAssignmentAndWaitForResponse(static_cast<net::HttpStatusCode>(429), |
322 net::Error::OK, "", kTestAuthToken, | 324 net::Error::OK, "", kTestAuthToken, |
323 kEngineVersion); | 325 kProtocolVersion); |
324 } | 326 } |
325 | 327 |
326 TEST_F(AssignmentSourceTest, TestInternalServerError) { | 328 TEST_F(AssignmentSourceTest, TestInternalServerError) { |
327 EXPECT_CALL(*this, AssignmentResponse( | 329 EXPECT_CALL(*this, AssignmentResponse( |
328 AssignmentSource::Result::RESULT_SERVER_ERROR, _)); | 330 AssignmentSource::Result::RESULT_SERVER_ERROR, _)); |
329 GetNetworkAssignmentAndWaitForResponse(net::HTTP_INTERNAL_SERVER_ERROR, | 331 GetNetworkAssignmentAndWaitForResponse(net::HTTP_INTERNAL_SERVER_ERROR, |
330 net::Error::OK, "", kTestAuthToken, | 332 net::Error::OK, "", kTestAuthToken, |
331 kEngineVersion); | 333 kProtocolVersion); |
332 } | 334 } |
333 | 335 |
334 TEST_F(AssignmentSourceTest, TestUnexpectedNetCodeFallback) { | 336 TEST_F(AssignmentSourceTest, TestUnexpectedNetCodeFallback) { |
335 EXPECT_CALL(*this, AssignmentResponse( | 337 EXPECT_CALL(*this, AssignmentResponse( |
336 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 338 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
337 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED, | 339 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED, |
338 net::Error::OK, "", kTestAuthToken, | 340 net::Error::OK, "", kTestAuthToken, |
339 kEngineVersion); | 341 kProtocolVersion); |
340 } | 342 } |
341 | 343 |
342 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { | 344 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { |
343 Assignment assignment = BuildSslAssignment(); | 345 Assignment assignment = BuildSslAssignment(); |
344 | 346 |
345 // Remove half the response. | 347 // Remove half the response. |
346 std::string response = ValueToString(*BuildAssignerResponse()); | 348 std::string response = ValueToString(*BuildAssignerResponse()); |
347 response = response.substr(response.size() / 2); | 349 response = response.substr(response.size() / 2); |
348 | 350 |
349 EXPECT_CALL(*this, AssignmentResponse( | 351 EXPECT_CALL(*this, AssignmentResponse( |
350 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 352 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
351 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, | 353 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, |
352 kTestAuthToken, kEngineVersion); | 354 kTestAuthToken, kProtocolVersion); |
353 } | 355 } |
354 | 356 |
355 TEST_F(AssignmentSourceTest, TestMissingResponsePort) { | 357 TEST_F(AssignmentSourceTest, TestMissingResponsePort) { |
356 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); | 358 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); |
357 response->Remove("port", nullptr); | 359 response->Remove("port", nullptr); |
358 EXPECT_CALL(*this, AssignmentResponse( | 360 EXPECT_CALL(*this, AssignmentResponse( |
359 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 361 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
360 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, | 362 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, |
361 ValueToString(*response), | 363 ValueToString(*response), |
362 kTestAuthToken, kEngineVersion); | 364 kTestAuthToken, kProtocolVersion); |
363 } | 365 } |
364 | 366 |
365 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { | 367 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { |
366 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); | 368 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); |
367 response->SetString("host", "happywhales.test"); | 369 response->SetString("host", "happywhales.test"); |
368 | 370 |
369 EXPECT_CALL(*this, AssignmentResponse( | 371 EXPECT_CALL(*this, AssignmentResponse( |
370 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 372 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
371 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, | 373 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, |
372 ValueToString(*response), | 374 ValueToString(*response), |
373 kTestAuthToken, kEngineVersion); | 375 kTestAuthToken, kProtocolVersion); |
374 } | 376 } |
375 | 377 |
376 TEST_F(AssignmentSourceTest, TestMissingCert) { | 378 TEST_F(AssignmentSourceTest, TestMissingCert) { |
377 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); | 379 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); |
378 response->Remove("certificate", nullptr); | 380 response->Remove("certificate", nullptr); |
379 EXPECT_CALL(*this, AssignmentResponse( | 381 EXPECT_CALL(*this, AssignmentResponse( |
380 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); | 382 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); |
381 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, | 383 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, |
382 ValueToString(*response), | 384 ValueToString(*response), |
383 kTestAuthToken, kEngineVersion); | 385 kTestAuthToken, kProtocolVersion); |
384 } | 386 } |
385 | 387 |
386 TEST_F(AssignmentSourceTest, TestInvalidCert) { | 388 TEST_F(AssignmentSourceTest, TestInvalidCert) { |
387 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); | 389 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); |
388 response->SetString("certificate", "h4x0rz!"); | 390 response->SetString("certificate", "h4x0rz!"); |
389 EXPECT_CALL(*this, AssignmentResponse( | 391 EXPECT_CALL(*this, AssignmentResponse( |
390 AssignmentSource::Result::RESULT_INVALID_CERT, _)); | 392 AssignmentSource::Result::RESULT_INVALID_CERT, _)); |
391 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, | 393 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, |
392 ValueToString(*response), | 394 ValueToString(*response), |
393 kTestAuthToken, kEngineVersion); | 395 kTestAuthToken, kProtocolVersion); |
394 } | 396 } |
395 | 397 |
396 } // namespace | 398 } // namespace |
397 } // namespace client | 399 } // namespace client |
398 } // namespace blimp | 400 } // namespace blimp |
OLD | NEW |