| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/autofill/core/browser/autofill_download_manager.h" | 5 #include "components/autofill/core/browser/autofill_download_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
| 16 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "components/autofill/core/browser/autofill_field.h" | 18 #include "components/autofill/core/browser/autofill_field.h" |
| 19 #include "components/autofill/core/browser/autofill_metrics.h" | 19 #include "components/autofill/core/browser/autofill_metrics.h" |
| 20 #include "components/autofill/core/browser/autofill_type.h" | 20 #include "components/autofill/core/browser/autofill_type.h" |
| 21 #include "components/autofill/core/browser/form_structure.h" | 21 #include "components/autofill/core/browser/form_structure.h" |
| 22 #include "components/autofill/core/browser/test_autofill_driver.h" | 22 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 23 #include "components/autofill/core/common/form_data.h" | 23 #include "components/autofill/core/common/form_data.h" |
| 24 #include "net/http/http_request_headers.h" | |
| 25 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 26 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
| 27 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/zlib/google/compression_utils.h" | |
| 32 | 30 |
| 33 using base::ASCIIToUTF16; | 31 using base::ASCIIToUTF16; |
| 34 | 32 |
| 35 namespace autofill { | 33 namespace autofill { |
| 36 | 34 |
| 37 namespace { | 35 namespace { |
| 38 | 36 |
| 39 // Call |fetcher->OnURLFetchComplete()| as the URLFetcher would when | 37 // Call |fetcher->OnURLFetchComplete()| as the URLFetcher would when |
| 40 // a response is received. Params allow caller to set fake status. | 38 // a response is received. Params allow caller to set fake status. |
| 41 void FakeOnURLFetchComplete(net::TestURLFetcher* fetcher, | 39 void FakeOnURLFetchComplete(net::TestURLFetcher* fetcher, |
| 42 int response_code, | 40 int response_code, |
| 43 const std::string& response_body) { | 41 const std::string& response_body) { |
| 44 fetcher->set_url(GURL()); | 42 fetcher->set_url(GURL()); |
| 45 fetcher->set_status(net::URLRequestStatus()); | 43 fetcher->set_status(net::URLRequestStatus()); |
| 46 fetcher->set_response_code(response_code); | 44 fetcher->set_response_code(response_code); |
| 47 fetcher->SetResponseString(response_body); | 45 fetcher->SetResponseString(response_body); |
| 48 | 46 |
| 49 fetcher->delegate()->OnURLFetchComplete(fetcher); | 47 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 50 } | 48 } |
| 51 | 49 |
| 52 // Compresses |data| and returns the result. | |
| 53 std::string Compress(const std::string& data) { | |
| 54 std::string compressed_data; | |
| 55 EXPECT_TRUE(compression::GzipCompress(data, &compressed_data)); | |
| 56 return compressed_data; | |
| 57 } | |
| 58 | |
| 59 } // namespace | 50 } // namespace |
| 60 | 51 |
| 61 // This tests AutofillDownloadManager. AutofillDownloadManagerTest implements | 52 // This tests AutofillDownloadManager. AutofillDownloadManagerTest implements |
| 62 // AutofillDownloadManager::Observer and creates an instance of | 53 // AutofillDownloadManager::Observer and creates an instance of |
| 63 // AutofillDownloadManager. Then it records responses to different initiated | 54 // AutofillDownloadManager. Then it records responses to different initiated |
| 64 // requests, which are verified later. To mock network requests | 55 // requests, which are verified later. To mock network requests |
| 65 // TestURLFetcherFactory is used, which creates URLFetchers that do not | 56 // TestURLFetcherFactory is used, which creates URLFetchers that do not |
| 66 // go over the wire, but allow calling back HTTP responses directly. | 57 // go over the wire, but allow calling back HTTP responses directly. |
| 67 // The responses in test are out of order and verify: successful query request, | 58 // The responses in test are out of order and verify: successful query request, |
| 68 // successful upload request, failed upload request. | 59 // successful upload request, failed upload request. |
| (...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 // No responses yet | 633 // No responses yet |
| 643 EXPECT_EQ(0U, responses_.size()); | 634 EXPECT_EQ(0U, responses_.size()); |
| 644 | 635 |
| 645 fetcher = factory.GetFetcherByID(3); | 636 fetcher = factory.GetFetcherByID(3); |
| 646 ASSERT_TRUE(fetcher); | 637 ASSERT_TRUE(fetcher); |
| 647 FakeOnURLFetchComplete(fetcher, 200, std::string(responses[0])); | 638 FakeOnURLFetchComplete(fetcher, 200, std::string(responses[0])); |
| 648 ASSERT_EQ(1U, responses_.size()); | 639 ASSERT_EQ(1U, responses_.size()); |
| 649 EXPECT_EQ(responses[0], responses_.front().response); | 640 EXPECT_EQ(responses[0], responses_.front().response); |
| 650 } | 641 } |
| 651 | 642 |
| 652 TEST_F(AutofillDownloadManagerTest, QueryRequestIsGzipped) { | |
| 653 // Expected query (uncompressed for visual verification). | |
| 654 AutofillQueryContents query; | |
| 655 query.set_client_version("6.1.1715.1442/en (GGLL)"); | |
| 656 AutofillQueryContents::Form* query_form = query.add_form(); | |
| 657 query_form->set_signature(14546501144368603154U); | |
| 658 | |
| 659 query_form->add_field()->set_signature(239111655U); | |
| 660 query_form->add_field()->set_signature(3763331450U); | |
| 661 query_form->add_field()->set_signature(3494530716U); | |
| 662 | |
| 663 std::string expected_query_string; | |
| 664 ASSERT_TRUE(query.SerializeToString(&expected_query_string)); | |
| 665 | |
| 666 // Create and register factory. | |
| 667 net::TestURLFetcherFactory factory; | |
| 668 | |
| 669 FormData form; | |
| 670 | |
| 671 FormFieldData field; | |
| 672 field.form_control_type = "text"; | |
| 673 | |
| 674 field.label = ASCIIToUTF16("username"); | |
| 675 field.name = ASCIIToUTF16("username"); | |
| 676 form.fields.push_back(field); | |
| 677 | |
| 678 field.label = ASCIIToUTF16("First Name"); | |
| 679 field.name = ASCIIToUTF16("firstname"); | |
| 680 form.fields.push_back(field); | |
| 681 | |
| 682 field.label = ASCIIToUTF16("Last Name"); | |
| 683 field.name = ASCIIToUTF16("lastname"); | |
| 684 form.fields.push_back(field); | |
| 685 | |
| 686 FormStructure* form_structure = new FormStructure(form); | |
| 687 ScopedVector<FormStructure> form_structures; | |
| 688 form_structures.push_back(form_structure); | |
| 689 | |
| 690 base::HistogramTester histogram; | |
| 691 // Request with id 0. | |
| 692 EXPECT_TRUE(download_manager_.StartQueryRequest(form_structures.get())); | |
| 693 histogram.ExpectUniqueSample("Autofill.ServerQueryResponse", | |
| 694 AutofillMetrics::QUERY_SENT, 1); | |
| 695 | |
| 696 // Request payload is gzipped. | |
| 697 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 698 ASSERT_TRUE(fetcher); | |
| 699 EXPECT_EQ(Compress(expected_query_string), fetcher->upload_data()); | |
| 700 | |
| 701 // Proper content-encoding header is defined. | |
| 702 net::HttpRequestHeaders headers; | |
| 703 fetcher->GetExtraRequestHeaders(&headers); | |
| 704 std::string header; | |
| 705 EXPECT_TRUE(headers.GetHeader("content-encoding", &header)); | |
| 706 EXPECT_EQ("gzip", header); | |
| 707 | |
| 708 // TODO(http://crbug.com/580102) The >100% compression ratio is a known | |
| 709 // problem. | |
| 710 // Expect that the compression is logged. | |
| 711 // NOTE: To get the expected value, run tests with --vmodule=autofill*=1 and | |
| 712 // watch for the VLOG which indicates compression. | |
| 713 histogram.ExpectUniqueSample("Autofill.PayloadCompressionRatio.Query", 133, | |
| 714 1); | |
| 715 } | |
| 716 | |
| 717 TEST_F(AutofillDownloadManagerTest, UploadRequestIsGzipped) { | |
| 718 // Expected upload (uncompressed for visual verification). | |
| 719 AutofillUploadContents upload; | |
| 720 upload.set_submission(true); | |
| 721 upload.set_client_version("6.1.1715.1442/en (GGLL)"); | |
| 722 upload.set_form_signature(14546501144368603154U); | |
| 723 upload.set_autofill_used(true); | |
| 724 upload.set_data_present(""); | |
| 725 | |
| 726 std::string expected_upload_string; | |
| 727 ASSERT_TRUE(upload.SerializeToString(&expected_upload_string)); | |
| 728 | |
| 729 // Create and register factory. | |
| 730 net::TestURLFetcherFactory factory; | |
| 731 | |
| 732 FormData form; | |
| 733 | |
| 734 FormFieldData field; | |
| 735 field.form_control_type = "text"; | |
| 736 | |
| 737 field.label = ASCIIToUTF16("username"); | |
| 738 field.name = ASCIIToUTF16("username"); | |
| 739 form.fields.push_back(field); | |
| 740 | |
| 741 field.label = ASCIIToUTF16("First Name"); | |
| 742 field.name = ASCIIToUTF16("firstname"); | |
| 743 form.fields.push_back(field); | |
| 744 | |
| 745 field.label = ASCIIToUTF16("Last Name"); | |
| 746 field.name = ASCIIToUTF16("lastname"); | |
| 747 form.fields.push_back(field); | |
| 748 | |
| 749 FormStructure* form_structure = new FormStructure(form); | |
| 750 ScopedVector<FormStructure> form_structures; | |
| 751 form_structures.push_back(form_structure); | |
| 752 | |
| 753 base::HistogramTester histogram; | |
| 754 // Request with id 0. | |
| 755 EXPECT_TRUE(download_manager_.StartUploadRequest( | |
| 756 *(form_structures[0]), true, ServerFieldTypeSet(), std::string(), true)); | |
| 757 | |
| 758 // Request payload is gzipped. | |
| 759 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 760 ASSERT_TRUE(fetcher); | |
| 761 EXPECT_EQ(Compress(expected_upload_string), fetcher->upload_data()); | |
| 762 | |
| 763 // Proper content-encoding header is defined. | |
| 764 net::HttpRequestHeaders headers; | |
| 765 fetcher->GetExtraRequestHeaders(&headers); | |
| 766 std::string header; | |
| 767 EXPECT_TRUE(headers.GetHeader("content-encoding", &header)); | |
| 768 EXPECT_EQ("gzip", header); | |
| 769 | |
| 770 // TODO(http://crbug.com/580102) The >100% compression ratio is a known | |
| 771 // problem. | |
| 772 // Expect that the compression is logged. | |
| 773 // NOTE: To get the expected value, run tests with --vmodule=autofill*=1 and | |
| 774 // watch for the VLOG which indicates compression. | |
| 775 histogram.ExpectUniqueSample("Autofill.PayloadCompressionRatio.Upload", 150, | |
| 776 1); | |
| 777 } | |
| 778 | |
| 779 } // namespace autofill | 643 } // namespace autofill |
| OLD | NEW |