| 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/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" | 5 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event
_creator.h" | 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event
_creator.h" |
| 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event
_storage_delegate_test_utils.h" | 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event
_storage_delegate_test_utils.h" |
| 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs_test_utils.h" | 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs_test_utils.h" |
| 18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 19 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 namespace data_reduction_proxy { | 23 namespace data_reduction_proxy { |
| 23 | 24 |
| 24 class DataReductionProxyHeadersTest : public testing::Test { | 25 class DataReductionProxyHeadersTest : public testing::Test { |
| 25 protected: | 26 protected: |
| 26 void SetUp() override { | 27 void SetUp() override { |
| 27 storage_delegate_.reset(new TestDataReductionProxyEventStorageDelegate()); | 28 storage_delegate_.reset(new TestDataReductionProxyEventStorageDelegate()); |
| 28 event_creator_.reset( | 29 event_creator_.reset( |
| 29 new DataReductionProxyEventCreator(storage_delegate_.get())); | 30 new DataReductionProxyEventCreator(storage_delegate_.get())); |
| 30 } | 31 } |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 false, | 491 false, |
| 491 }, | 492 }, |
| 492 }; | 493 }; |
| 493 for (size_t i = 0; i < arraysize(tests); ++i) { | 494 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 494 std::string headers(tests[i].headers); | 495 std::string headers(tests[i].headers); |
| 495 HeadersToRaw(&headers); | 496 HeadersToRaw(&headers); |
| 496 scoped_refptr<net::HttpResponseHeaders> parsed( | 497 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 497 new net::HttpResponseHeaders(headers)); | 498 new net::HttpResponseHeaders(headers)); |
| 498 | 499 |
| 499 DataReductionProxyInfo data_reduction_proxy_info; | 500 DataReductionProxyInfo data_reduction_proxy_info; |
| 500 EXPECT_EQ( | 501 EXPECT_EQ(tests[i].expected_result, |
| 501 tests[i].expected_result, | 502 ParseHeadersForBypassInfo(*parsed, &data_reduction_proxy_info)); |
| 502 ParseHeadersForBypassInfo(parsed.get(), &data_reduction_proxy_info)); | |
| 503 EXPECT_EQ(tests[i].expected_retry_delay, | 503 EXPECT_EQ(tests[i].expected_retry_delay, |
| 504 data_reduction_proxy_info.bypass_duration.InSeconds()); | 504 data_reduction_proxy_info.bypass_duration.InSeconds()); |
| 505 EXPECT_EQ(tests[i].expected_bypass_all, | 505 EXPECT_EQ(tests[i].expected_bypass_all, |
| 506 data_reduction_proxy_info.bypass_all); | 506 data_reduction_proxy_info.bypass_all); |
| 507 EXPECT_EQ(tests[i].expected_mark_proxies_as_bad, | 507 EXPECT_EQ(tests[i].expected_mark_proxies_as_bad, |
| 508 data_reduction_proxy_info.mark_proxies_as_bad); | 508 data_reduction_proxy_info.mark_proxies_as_bad); |
| 509 } | 509 } |
| 510 } | 510 } |
| 511 | 511 |
| 512 TEST_F(DataReductionProxyHeadersTest, ParseHeadersAndSetProxyInfo) { | 512 TEST_F(DataReductionProxyHeadersTest, ParseHeadersAndSetProxyInfo) { |
| 513 std::string headers = | 513 std::string headers = |
| 514 "HTTP/1.1 200 OK\n" | 514 "HTTP/1.1 200 OK\n" |
| 515 "connection: keep-alive\n" | 515 "connection: keep-alive\n" |
| 516 "Chrome-Proxy: bypass=0\n" | 516 "Chrome-Proxy: bypass=0\n" |
| 517 "Content-Length: 999\n"; | 517 "Content-Length: 999\n"; |
| 518 HeadersToRaw(&headers); | 518 HeadersToRaw(&headers); |
| 519 scoped_refptr<net::HttpResponseHeaders> parsed( | 519 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 520 new net::HttpResponseHeaders(headers)); | 520 new net::HttpResponseHeaders(headers)); |
| 521 | 521 |
| 522 DataReductionProxyInfo data_reduction_proxy_info; | 522 DataReductionProxyInfo data_reduction_proxy_info; |
| 523 EXPECT_TRUE( | 523 EXPECT_TRUE(ParseHeadersForBypassInfo(*parsed, &data_reduction_proxy_info)); |
| 524 ParseHeadersForBypassInfo(parsed.get(), &data_reduction_proxy_info)); | |
| 525 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); | 524 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); |
| 526 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); | 525 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); |
| 527 EXPECT_FALSE(data_reduction_proxy_info.bypass_all); | 526 EXPECT_FALSE(data_reduction_proxy_info.bypass_all); |
| 528 } | 527 } |
| 529 | 528 |
| 530 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { | 529 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { |
| 531 const struct { | 530 const struct { |
| 532 const char* headers; | 531 const char* headers; |
| 533 bool expected_result; | 532 bool expected_result; |
| 534 bool expected_has_intermediary; | 533 bool expected_has_intermediary; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 }; | 604 }; |
| 606 for (size_t i = 0; i < arraysize(tests); ++i) { | 605 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 607 std::string headers(tests[i].headers); | 606 std::string headers(tests[i].headers); |
| 608 HeadersToRaw(&headers); | 607 HeadersToRaw(&headers); |
| 609 scoped_refptr<net::HttpResponseHeaders> parsed( | 608 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 610 new net::HttpResponseHeaders(headers)); | 609 new net::HttpResponseHeaders(headers)); |
| 611 | 610 |
| 612 bool has_chrome_proxy_via_header, has_intermediary; | 611 bool has_chrome_proxy_via_header, has_intermediary; |
| 613 if (tests[i].ignore_intermediary) { | 612 if (tests[i].ignore_intermediary) { |
| 614 has_chrome_proxy_via_header = | 613 has_chrome_proxy_via_header = |
| 615 HasDataReductionProxyViaHeader(parsed.get(), NULL); | 614 HasDataReductionProxyViaHeader(*parsed, NULL); |
| 616 } | 615 } |
| 617 else { | 616 else { |
| 618 has_chrome_proxy_via_header = | 617 has_chrome_proxy_via_header = |
| 619 HasDataReductionProxyViaHeader(parsed.get(), &has_intermediary); | 618 HasDataReductionProxyViaHeader(*parsed, &has_intermediary); |
| 620 } | 619 } |
| 621 EXPECT_EQ(tests[i].expected_result, has_chrome_proxy_via_header); | 620 EXPECT_EQ(tests[i].expected_result, has_chrome_proxy_via_header); |
| 622 if (has_chrome_proxy_via_header && !tests[i].ignore_intermediary) { | 621 if (has_chrome_proxy_via_header && !tests[i].ignore_intermediary) { |
| 623 EXPECT_EQ(tests[i].expected_has_intermediary, has_intermediary); | 622 EXPECT_EQ(tests[i].expected_has_intermediary, has_intermediary); |
| 624 } | 623 } |
| 625 } | 624 } |
| 626 } | 625 } |
| 627 | 626 |
| 628 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { | 627 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { |
| 629 const struct { | 628 const struct { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 scoped_refptr<net::HttpResponseHeaders> parsed( | 760 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 762 new net::HttpResponseHeaders(headers)); | 761 new net::HttpResponseHeaders(headers)); |
| 763 DataReductionProxyInfo chrome_proxy_info; | 762 DataReductionProxyInfo chrome_proxy_info; |
| 764 | 763 |
| 765 base::FieldTrialList trial_list(nullptr); | 764 base::FieldTrialList trial_list(nullptr); |
| 766 base::FieldTrialList::CreateFieldTrial( | 765 base::FieldTrialList::CreateFieldTrial( |
| 767 "DataReductionProxyServerExperiments", | 766 "DataReductionProxyServerExperiments", |
| 768 tests[i].in_tamper_detection_experiment ? "TamperDetection_Enabled" | 767 tests[i].in_tamper_detection_experiment ? "TamperDetection_Enabled" |
| 769 : "TamperDetection_Disabled"); | 768 : "TamperDetection_Disabled"); |
| 770 | 769 |
| 771 EXPECT_EQ(tests[i].expected_result, GetDataReductionProxyBypassType( | 770 EXPECT_EQ(tests[i].expected_result, |
| 772 parsed.get(), &chrome_proxy_info)); | 771 GetDataReductionProxyBypassType(std::vector<GURL>(), *parsed, |
| 772 &chrome_proxy_info)); |
| 773 } | 773 } |
| 774 } | 774 } |
| 775 | 775 |
| 776 TEST_F(DataReductionProxyHeadersTest, |
| 777 GetDataReductionProxyBypassEventTypeURLRedirectCycle) { |
| 778 const struct { |
| 779 const char* headers; |
| 780 std::vector<GURL> url_chain; |
| 781 DataReductionProxyBypassType expected_result; |
| 782 } tests[] = { |
| 783 { |
| 784 "HTTP/1.1 200 OK\n" |
| 785 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 786 std::vector<GURL>{GURL("http://google.com/1"), |
| 787 GURL("http://google.com/2"), |
| 788 GURL("http://google.com/1")}, |
| 789 BYPASS_EVENT_TYPE_URL_REDIRECT_CYCLE, |
| 790 }, |
| 791 { |
| 792 "HTTP/1.1 200 OK\n" |
| 793 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 794 std::vector<GURL>{ |
| 795 GURL("http://google.com/1"), GURL("http://google.com/2"), |
| 796 GURL("http://google.com/1"), GURL("http://google.com/2")}, |
| 797 BYPASS_EVENT_TYPE_URL_REDIRECT_CYCLE, |
| 798 }, |
| 799 { |
| 800 "HTTP/1.1 200 OK\n" |
| 801 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 802 std::vector<GURL>{GURL("http://google.com/1")}, BYPASS_EVENT_TYPE_MAX, |
| 803 }, |
| 804 { |
| 805 "HTTP/1.1 200 OK\n" |
| 806 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 807 std::vector<GURL>{GURL("http://google.com/1"), |
| 808 GURL("http://google.com/2")}, |
| 809 BYPASS_EVENT_TYPE_MAX, |
| 810 }, |
| 811 { |
| 812 "HTTP/1.1 200 OK\n" |
| 813 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 814 std::vector<GURL>{GURL("http://google.com/1"), |
| 815 GURL("http://google.com/2"), |
| 816 GURL("http://google.com/3")}, |
| 817 BYPASS_EVENT_TYPE_MAX, |
| 818 }, |
| 819 { |
| 820 "HTTP/1.1 200 OK\n" |
| 821 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 822 std::vector<GURL>{ |
| 823 GURL("http://google.com/1"), GURL("http://google.com/2"), |
| 824 GURL("http://google.com/3"), GURL("http://google.com/1")}, |
| 825 BYPASS_EVENT_TYPE_URL_REDIRECT_CYCLE, |
| 826 }, |
| 827 { |
| 828 "HTTP/1.1 200 OK\n" |
| 829 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 830 std::vector<GURL>{ |
| 831 GURL("http://google.com/1"), GURL("http://google.com/2"), |
| 832 GURL("http://google.com/1"), GURL("http://google.com/3")}, |
| 833 BYPASS_EVENT_TYPE_MAX, |
| 834 }, |
| 835 { |
| 836 "HTTP/1.1 200 OK\n" |
| 837 "Via: 1.1 Chrome-Compression-Proxy\n", |
| 838 std::vector<GURL>(), BYPASS_EVENT_TYPE_MAX, |
| 839 }}; |
| 840 |
| 841 for (const auto& test : tests) { |
| 842 std::string headers(test.headers); |
| 843 HeadersToRaw(&headers); |
| 844 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 845 new net::HttpResponseHeaders(headers)); |
| 846 DataReductionProxyInfo chrome_proxy_info; |
| 847 |
| 848 EXPECT_EQ(test.expected_result, |
| 849 GetDataReductionProxyBypassType(test.url_chain, *parsed, |
| 850 &chrome_proxy_info)); |
| 851 } |
| 852 } |
| 853 |
| 776 TEST_F(DataReductionProxyHeadersTest, | 854 TEST_F(DataReductionProxyHeadersTest, |
| 777 GetDataReductionProxyActionFingerprintChromeProxy) { | 855 GetDataReductionProxyActionFingerprintChromeProxy) { |
| 778 const struct { | 856 const struct { |
| 779 std::string label; | 857 std::string label; |
| 780 const char* headers; | 858 const char* headers; |
| 781 bool expected_fingerprint_exist; | 859 bool expected_fingerprint_exist; |
| 782 std::string expected_fingerprint; | 860 std::string expected_fingerprint; |
| 783 } tests[] = { | 861 } tests[] = { |
| 784 { "fingerprint doesn't exist", | 862 { "fingerprint doesn't exist", |
| 785 "HTTP/1.1 200 OK\n" | 863 "HTTP/1.1 200 OK\n" |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 std::string output_values_string; | 1088 std::string output_values_string; |
| 1011 for (size_t j = 0; j < output_values.size(); ++j) | 1089 for (size_t j = 0; j < output_values.size(); ++j) |
| 1012 output_values_string += output_values[j] + ","; | 1090 output_values_string += output_values[j] + ","; |
| 1013 | 1091 |
| 1014 EXPECT_EQ(test[i].expected_output_values_string, output_values_string) | 1092 EXPECT_EQ(test[i].expected_output_values_string, output_values_string) |
| 1015 << test[i].label; | 1093 << test[i].label; |
| 1016 } | 1094 } |
| 1017 } | 1095 } |
| 1018 | 1096 |
| 1019 } // namespace data_reduction_proxy | 1097 } // namespace data_reduction_proxy |
| OLD | NEW |