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.
8 from metrics import chrome_proxy
9 from metrics import network_unittest
10 from metrics import test_page_measurement_results
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',
18 'Content-Type': 'text/html',
19 'Content-Length': str(len(network_unittest.HTML_BODY)),
21 body=network_unittest.HTML_BODY)
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',
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 +
34 body=network_unittest.HTML_BODY))
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',
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,
46 body=base64.b64encode(network_unittest.IMAGE_BODY),
47 base64_encoded_body=True,
48 served_from_cache=True))
50 # An image fetched directly.
51 EVENT_IMAGE_DIRECT = (
52 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
53 url='http://test.image',
55 'Content-Type': 'image/jpeg',
56 'Content-Encoding': 'gzip',
58 body=base64.b64encode(network_unittest.IMAGE_BODY),
59 base64_encoded_body=True))
61 # A safe-browsing malware response.
62 EVENT_MALWARE_PROXY = (
63 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
64 url='http://test.malware',
67 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER,
68 'Location': 'http://test.malware',
73 class ChromeProxyMetricTest(unittest.TestCase):
77 def _StubGetProxyInfo(self, info):
78 def stub(unused_tab, unused_url=''): # pylint: disable=W0613
79 return ChromeProxyMetricTest._test_proxy_info
80 chrome_proxy.GetProxyInfoFromNetworkInternals = stub
81 ChromeProxyMetricTest._test_proxy_info = info
83 def testChromeProxyResponse(self):
84 # An https non-proxy response.
85 resp = chrome_proxy.ChromeProxyResponse(
86 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
87 url='https://test.url',
89 'Content-Type': 'text/html',
90 'Content-Length': str(len(network_unittest.HTML_BODY)),
91 'Via': 'some other via',
93 body=network_unittest.HTML_BODY))
94 self.assertFalse(resp.ShouldHaveChromeProxyViaHeader())
95 self.assertFalse(resp.HasChromeProxyViaHeader())
96 self.assertTrue(resp.IsValidByViaHeader())
98 # A proxied JPEG image response
99 resp = chrome_proxy.ChromeProxyResponse(
100 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
101 url='http://test.image',
103 'Content-Type': 'image/jpeg',
104 'Content-Encoding': 'gzip',
105 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER,
106 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
108 body=base64.b64encode(network_unittest.IMAGE_BODY),
109 base64_encoded_body=True))
110 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader())
111 self.assertTrue(resp.HasChromeProxyViaHeader())
112 self.assertTrue(resp.IsValidByViaHeader())
114 def testChromeProxyMetricForDataSaving(self):
115 metric = chrome_proxy.ChromeProxyMetric()
118 EVENT_HTML_PROXY_DEPRECATED_VIA,
119 EVENT_IMAGE_PROXY_CACHED,
121 metric.SetEvents(events)
123 self.assertTrue(len(events), len(list(metric.IterResponses(None))))
124 results = test_page_measurement_results.TestPageMeasurementResults(self)
126 metric.AddResultsForDataSaving(None, results)
127 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2)
128 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1)
129 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2)
131 def testChromeProxyMetricForHeaderValidation(self):
132 metric = chrome_proxy.ChromeProxyMetric()
135 EVENT_HTML_PROXY_DEPRECATED_VIA,
136 EVENT_IMAGE_PROXY_CACHED,
139 results = test_page_measurement_results.TestPageMeasurementResults(self)
141 missing_via_exception = False
143 metric.AddResultsForHeaderValidation(None, results)
144 except chrome_proxy.ChromeProxyMetricException:
145 missing_via_exception = True
146 # Only the HTTP image response does not have a valid Via header.
147 self.assertTrue(missing_via_exception)
149 # Two events with valid Via headers.
151 EVENT_HTML_PROXY_DEPRECATED_VIA,
152 EVENT_IMAGE_PROXY_CACHED])
153 metric.AddResultsForHeaderValidation(None, results)
154 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2)
156 def testChromeProxyMetricForBypass(self):
157 metric = chrome_proxy.ChromeProxyMetric()
160 EVENT_HTML_PROXY_DEPRECATED_VIA,
161 EVENT_IMAGE_PROXY_CACHED,
163 results = test_page_measurement_results.TestPageMeasurementResults(self)
165 bypass_exception = False
167 metric.AddResultsForBypass(None, results)
168 except chrome_proxy.ChromeProxyMetricException:
169 bypass_exception = True
170 # Two of the first three events have Via headers.
171 self.assertTrue(bypass_exception)
173 # Use directly fetched image only. It is treated as bypassed.
174 metric.SetEvents([EVENT_IMAGE_DIRECT])
175 metric.AddResultsForBypass(None, results)
176 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1)
178 def testChromeProxyMetricForHTTPFallback(self):
179 metric = chrome_proxy.ChromeProxyMetric()
182 EVENT_HTML_PROXY_DEPRECATED_VIA])
183 results = test_page_measurement_results.TestPageMeasurementResults(self)
185 fallback_exception = False
187 info['enabled'] = False
188 self._StubGetProxyInfo(info)
190 metric.AddResultsForBypass(None, results)
191 except chrome_proxy.ChromeProxyMetricException:
192 fallback_exception = True
193 self.assertTrue(fallback_exception)
195 fallback_exception = False
196 info['enabled'] = True
198 'something.else.com:80',
199 chrome_proxy.PROXY_SETTING_DIRECT
201 self._StubGetProxyInfo(info)
203 metric.AddResultsForBypass(None, results)
204 except chrome_proxy.ChromeProxyMetricException:
205 fallback_exception = True
206 self.assertTrue(fallback_exception)
208 info['enabled'] = True
210 chrome_proxy.PROXY_SETTING_HTTP,
211 chrome_proxy.PROXY_SETTING_DIRECT
213 self._StubGetProxyInfo(info)
214 metric.AddResultsForHTTPFallback(None, results)
216 def testChromeProxyMetricForSafebrowsing(self):
217 metric = chrome_proxy.ChromeProxyMetric()
218 metric.SetEvents([EVENT_MALWARE_PROXY])
219 results = test_page_measurement_results.TestPageMeasurementResults(self)
221 metric.AddResultsForSafebrowsing(None, results)
222 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)
224 # Clear results and metrics to test no response for safebrowsing
225 results = test_page_measurement_results.TestPageMeasurementResults(self)
227 metric.AddResultsForSafebrowsing(None, results)
228 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)