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