OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import base64 |
| 6 import unittest |
| 7 |
| 8 from metrics import chrome_proxy |
| 9 from metrics import network_unittest |
| 10 from metrics import test_page_measurement_results |
| 11 |
| 12 |
| 13 # Timeline events used in tests. |
| 14 # An HTML not via proxy. |
| 15 EVENT_HTML_PROXY = network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 16 url='http://test.html1', |
| 17 response_headers={ |
| 18 'Content-Type': 'text/html', |
| 19 'Content-Length': str(len(network_unittest.HTML_BODY)), |
| 20 }, |
| 21 body=network_unittest.HTML_BODY) |
| 22 |
| 23 # An HTML via proxy with the deprecated Via header. |
| 24 EVENT_HTML_PROXY_DEPRECATED_VIA = ( |
| 25 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 26 url='http://test.html2', |
| 27 response_headers={ |
| 28 'Content-Type': 'text/html', |
| 29 'Content-Encoding': 'gzip', |
| 30 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)), |
| 31 'Via': (chrome_proxy.CHROME_PROXY_VIA_HEADER_DEPRECATED + |
| 32 ',other-via'), |
| 33 }, |
| 34 body=network_unittest.HTML_BODY)) |
| 35 |
| 36 # An image via proxy with Via header and it is cached. |
| 37 EVENT_IMAGE_PROXY_CACHED = ( |
| 38 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 39 url='http://test.image', |
| 40 response_headers={ |
| 41 'Content-Type': 'image/jpeg', |
| 42 'Content-Encoding': 'gzip', |
| 43 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), |
| 44 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| 45 }, |
| 46 body=base64.b64encode(network_unittest.IMAGE_BODY), |
| 47 base64_encoded_body=True, |
| 48 served_from_cache=True)) |
| 49 |
| 50 # An image fetched directly. |
| 51 EVENT_IMAGE_DIRECT = ( |
| 52 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 53 url='http://test.image', |
| 54 response_headers={ |
| 55 'Content-Type': 'image/jpeg', |
| 56 'Content-Encoding': 'gzip', |
| 57 }, |
| 58 body=base64.b64encode(network_unittest.IMAGE_BODY), |
| 59 base64_encoded_body=True)) |
| 60 |
| 61 # A safe-browsing malware response. |
| 62 EVENT_MALWARE_PROXY = ( |
| 63 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 64 url='http://test.malware', |
| 65 response_headers={ |
| 66 'X-Malware-Url': '1', |
| 67 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| 68 'Location': 'http://test.malware', |
| 69 }, |
| 70 status=307)) |
| 71 |
| 72 |
| 73 class ChromeProxyMetricTest(unittest.TestCase): |
| 74 def testChromeProxyResponse(self): |
| 75 # An https non-proxy response. |
| 76 resp = chrome_proxy.ChromeProxyResponse( |
| 77 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 78 url='https://test.url', |
| 79 response_headers={ |
| 80 'Content-Type': 'text/html', |
| 81 'Content-Length': str(len(network_unittest.HTML_BODY)), |
| 82 'Via': 'some other via', |
| 83 }, |
| 84 body=network_unittest.HTML_BODY)) |
| 85 self.assertFalse(resp.ShouldHaveChromeProxyViaHeader()) |
| 86 self.assertFalse(resp.HasChromeProxyViaHeader()) |
| 87 self.assertTrue(resp.IsValidByViaHeader()) |
| 88 |
| 89 # A proxied JPEG image response |
| 90 resp = chrome_proxy.ChromeProxyResponse( |
| 91 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 92 url='http://test.image', |
| 93 response_headers={ |
| 94 'Content-Type': 'image/jpeg', |
| 95 'Content-Encoding': 'gzip', |
| 96 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| 97 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), |
| 98 }, |
| 99 body=base64.b64encode(network_unittest.IMAGE_BODY), |
| 100 base64_encoded_body=True)) |
| 101 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader()) |
| 102 self.assertTrue(resp.HasChromeProxyViaHeader()) |
| 103 self.assertTrue(resp.IsValidByViaHeader()) |
| 104 |
| 105 def testChromeProxyMetricForDataSaving(self): |
| 106 metric = chrome_proxy.ChromeProxyMetric() |
| 107 events = [ |
| 108 EVENT_HTML_PROXY, |
| 109 EVENT_HTML_PROXY_DEPRECATED_VIA, |
| 110 EVENT_IMAGE_PROXY_CACHED, |
| 111 EVENT_IMAGE_DIRECT] |
| 112 metric.SetEvents(events) |
| 113 |
| 114 self.assertTrue(len(events), len(list(metric.IterResponses(None)))) |
| 115 results = test_page_measurement_results.TestPageMeasurementResults(self) |
| 116 |
| 117 metric.AddResultsForDataSaving(None, results) |
| 118 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) |
| 119 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) |
| 120 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) |
| 121 |
| 122 def testChromeProxyMetricForHeaderValidation(self): |
| 123 metric = chrome_proxy.ChromeProxyMetric() |
| 124 metric.SetEvents([ |
| 125 EVENT_HTML_PROXY, |
| 126 EVENT_HTML_PROXY_DEPRECATED_VIA, |
| 127 EVENT_IMAGE_PROXY_CACHED, |
| 128 EVENT_IMAGE_DIRECT]) |
| 129 |
| 130 results = test_page_measurement_results.TestPageMeasurementResults(self) |
| 131 |
| 132 missing_via_exception = False |
| 133 try: |
| 134 metric.AddResultsForHeaderValidation(None, results) |
| 135 except chrome_proxy.ChromeProxyMetricException: |
| 136 missing_via_exception = True |
| 137 # Only the HTTP image response does not have a valid Via header. |
| 138 self.assertTrue(missing_via_exception) |
| 139 |
| 140 # Two events with valid Via headers. |
| 141 metric.SetEvents([ |
| 142 EVENT_HTML_PROXY_DEPRECATED_VIA, |
| 143 EVENT_IMAGE_PROXY_CACHED]) |
| 144 metric.AddResultsForHeaderValidation(None, results) |
| 145 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) |
| 146 |
| 147 def testChromeProxyMetricForBypass(self): |
| 148 metric = chrome_proxy.ChromeProxyMetric() |
| 149 metric.SetEvents([ |
| 150 EVENT_HTML_PROXY, |
| 151 EVENT_HTML_PROXY_DEPRECATED_VIA, |
| 152 EVENT_IMAGE_PROXY_CACHED, |
| 153 EVENT_IMAGE_DIRECT]) |
| 154 results = test_page_measurement_results.TestPageMeasurementResults(self) |
| 155 |
| 156 bypass_exception = False |
| 157 try: |
| 158 metric.AddResultsForBypass(None, results) |
| 159 except chrome_proxy.ChromeProxyMetricException: |
| 160 bypass_exception = True |
| 161 # Two of the first three events have Via headers. |
| 162 self.assertTrue(bypass_exception) |
| 163 |
| 164 # Use directly fetched image only. It is treated as bypassed. |
| 165 metric.SetEvents([EVENT_IMAGE_DIRECT]) |
| 166 metric.AddResultsForBypass(None, results) |
| 167 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) |
| 168 |
| 169 def testChromeProxyMetricForSafebrowsing(self): |
| 170 metric = chrome_proxy.ChromeProxyMetric() |
| 171 metric.SetEvents([EVENT_MALWARE_PROXY]) |
| 172 results = test_page_measurement_results.TestPageMeasurementResults(self) |
| 173 |
| 174 metric.AddResultsForSafebrowsing(None, results) |
| 175 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) |
| 176 |
| 177 # Clear results and metrics to test no response for safebrowsing |
| 178 results = test_page_measurement_results.TestPageMeasurementResults(self) |
| 179 metric.SetEvents([]) |
| 180 metric.AddResultsForSafebrowsing(None, results) |
| 181 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) |
OLD | NEW |