Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/chrome_proxy/integration_tests/chrome_proxy_metrics_unittest.py

Issue 792303006: Remove the net-internals dependency from chrome proxy telemetry tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 import base64 5 import base64
6 import unittest 6 import unittest
7 7
8 from integration_tests import chrome_proxy_metrics as metrics 8 from integration_tests import chrome_proxy_metrics as metrics
9 from integration_tests import network_metrics_unittest as network_unittest 9 from integration_tests import network_metrics_unittest as network_unittest
10 from metrics import test_page_test_results 10 from metrics import test_page_test_results
11 11
12 12
13 # Timeline events used in tests. 13 # Timeline events used in tests.
14 # An HTML not via proxy. 14 # An HTML not via proxy.
15 EVENT_HTML_PROXY = network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( 15 EVENT_HTML_DIRECT = network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
16 url='http://test.html1', 16 url='http://test.html1',
17 response_headers={ 17 response_headers={
18 'Content-Type': 'text/html', 18 'Content-Type': 'text/html',
19 'Content-Length': str(len(network_unittest.HTML_BODY)), 19 'Content-Length': str(len(network_unittest.HTML_BODY)),
20 }, 20 },
21 body=network_unittest.HTML_BODY) 21 body=network_unittest.HTML_BODY)
22 22
23 # An HTML via proxy.
24 EVENT_HTML_PROXY_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': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
32 },
33 body=network_unittest.HTML_BODY,
34 remote_port=443))
35
36 # An HTML via the HTTP fallback proxy.
37 EVENT_HTML_PROXY_VIA_HTTP_FALLBACK = (
38 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
39 url='http://test.html2',
40 response_headers={
41 'Content-Type': 'text/html',
42 'Content-Encoding': 'gzip',
43 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)),
44 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
45 },
46 body=network_unittest.HTML_BODY,
47 remote_port=80))
48
23 # An HTML via proxy with the deprecated Via header. 49 # An HTML via proxy with the deprecated Via header.
24 EVENT_HTML_PROXY_DEPRECATED_VIA = ( 50 EVENT_HTML_PROXY_DEPRECATED_VIA = (
25 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( 51 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
26 url='http://test.html2', 52 url='http://test.html2',
27 response_headers={ 53 response_headers={
28 'Content-Type': 'text/html', 54 'Content-Type': 'text/html',
29 'Content-Encoding': 'gzip', 55 'Content-Encoding': 'gzip',
30 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)), 56 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)),
31 'Via': (metrics.CHROME_PROXY_VIA_HEADER_DEPRECATED + 57 'Via': (metrics.CHROME_PROXY_VIA_HEADER_DEPRECATED +
32 ',other-via'), 58 ',other-via'),
33 }, 59 },
34 body=network_unittest.HTML_BODY)) 60 body=network_unittest.HTML_BODY))
35 61
62 # An image via proxy with Via header.
63 EVENT_IMAGE_PROXY_VIA = (
64 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
65 url='http://test.image',
66 response_headers={
67 'Content-Type': 'image/jpeg',
68 'Content-Encoding': 'gzip',
69 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
70 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
71 },
72 body=base64.b64encode(network_unittest.IMAGE_BODY),
73 base64_encoded_body=True,
74 remote_port=443))
75
76 # An image via the HTTP fallback proxy.
77 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK = (
78 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
79 url='http://test.image',
80 response_headers={
81 'Content-Type': 'image/jpeg',
82 'Content-Encoding': 'gzip',
83 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
84 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
85 },
86 body=base64.b64encode(network_unittest.IMAGE_BODY),
87 base64_encoded_body=True,
88 remote_port=80))
89
36 # An image via proxy with Via header and it is cached. 90 # An image via proxy with Via header and it is cached.
37 EVENT_IMAGE_PROXY_CACHED = ( 91 EVENT_IMAGE_PROXY_CACHED = (
38 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( 92 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
39 url='http://test.image', 93 url='http://test.image',
40 response_headers={ 94 response_headers={
41 'Content-Type': 'image/jpeg', 95 'Content-Type': 'image/jpeg',
42 'Content-Encoding': 'gzip', 96 'Content-Encoding': 'gzip',
43 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), 97 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
44 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, 98 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
45 }, 99 },
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 }, 183 },
130 body=base64.b64encode(network_unittest.IMAGE_BODY), 184 body=base64.b64encode(network_unittest.IMAGE_BODY),
131 base64_encoded_body=True)) 185 base64_encoded_body=True))
132 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader()) 186 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader())
133 self.assertTrue(resp.HasChromeProxyViaHeader()) 187 self.assertTrue(resp.HasChromeProxyViaHeader())
134 self.assertTrue(resp.IsValidByViaHeader()) 188 self.assertTrue(resp.IsValidByViaHeader())
135 189
136 def testChromeProxyMetricForDataSaving(self): 190 def testChromeProxyMetricForDataSaving(self):
137 metric = metrics.ChromeProxyMetric() 191 metric = metrics.ChromeProxyMetric()
138 events = [ 192 events = [
139 EVENT_HTML_PROXY, 193 EVENT_HTML_DIRECT,
140 EVENT_HTML_PROXY_DEPRECATED_VIA, 194 EVENT_HTML_PROXY_DEPRECATED_VIA,
141 EVENT_IMAGE_PROXY_CACHED, 195 EVENT_IMAGE_PROXY_CACHED,
142 EVENT_IMAGE_DIRECT] 196 EVENT_IMAGE_DIRECT]
143 metric.SetEvents(events) 197 metric.SetEvents(events)
144 198
145 self.assertTrue(len(events), len(list(metric.IterResponses(None)))) 199 self.assertTrue(len(events), len(list(metric.IterResponses(None))))
146 results = test_page_test_results.TestPageTestResults(self) 200 results = test_page_test_results.TestPageTestResults(self)
147 201
148 metric.AddResultsForDataSaving(None, results) 202 metric.AddResultsForDataSaving(None, results)
149 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) 203 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2)
150 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) 204 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1)
151 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) 205 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2)
152 206
153 def testChromeProxyMetricForHeaderValidation(self): 207 def testChromeProxyMetricForHeaderValidation(self):
154 metric = metrics.ChromeProxyMetric() 208 metric = metrics.ChromeProxyMetric()
155 metric.SetEvents([ 209 metric.SetEvents([
156 EVENT_HTML_PROXY, 210 EVENT_HTML_DIRECT,
157 EVENT_HTML_PROXY_DEPRECATED_VIA, 211 EVENT_HTML_PROXY_DEPRECATED_VIA,
158 EVENT_IMAGE_PROXY_CACHED, 212 EVENT_IMAGE_PROXY_CACHED,
159 EVENT_IMAGE_DIRECT]) 213 EVENT_IMAGE_DIRECT])
160 214
161 results = test_page_test_results.TestPageTestResults(self) 215 results = test_page_test_results.TestPageTestResults(self)
162 216
163 missing_via_exception = False 217 missing_via_exception = False
164 try: 218 try:
165 metric.AddResultsForHeaderValidation(None, results) 219 metric.AddResultsForHeaderValidation(None, results)
166 except metrics.ChromeProxyMetricException: 220 except metrics.ChromeProxyMetricException:
167 missing_via_exception = True 221 missing_via_exception = True
168 # Only the HTTP image response does not have a valid Via header. 222 # Only the HTTP image response does not have a valid Via header.
169 self.assertTrue(missing_via_exception) 223 self.assertTrue(missing_via_exception)
170 224
171 # Two events with valid Via headers. 225 # Two events with valid Via headers.
172 metric.SetEvents([ 226 metric.SetEvents([
173 EVENT_HTML_PROXY_DEPRECATED_VIA, 227 EVENT_HTML_PROXY_DEPRECATED_VIA,
174 EVENT_IMAGE_PROXY_CACHED]) 228 EVENT_IMAGE_PROXY_CACHED])
175 metric.AddResultsForHeaderValidation(None, results) 229 metric.AddResultsForHeaderValidation(None, results)
176 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) 230 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2)
177 231
178 def testChromeProxyMetricForBypass(self): 232 def testChromeProxyMetricForBypass(self):
179 metric = metrics.ChromeProxyMetric() 233 metric = metrics.ChromeProxyMetric()
180 metric.SetEvents([ 234 metric.SetEvents([
181 EVENT_HTML_PROXY, 235 EVENT_HTML_DIRECT,
182 EVENT_HTML_PROXY_DEPRECATED_VIA, 236 EVENT_HTML_PROXY_DEPRECATED_VIA,
183 EVENT_IMAGE_PROXY_CACHED, 237 EVENT_IMAGE_PROXY_CACHED,
184 EVENT_IMAGE_DIRECT]) 238 EVENT_IMAGE_DIRECT])
185 results = test_page_test_results.TestPageTestResults(self) 239 results = test_page_test_results.TestPageTestResults(self)
186 240
187 bypass_exception = False 241 bypass_exception = False
188 try: 242 try:
189 metric.AddResultsForBypass(None, results) 243 metric.AddResultsForBypass(None, results)
190 except metrics.ChromeProxyMetricException: 244 except metrics.ChromeProxyMetricException:
191 bypass_exception = True 245 bypass_exception = True
192 # Two of the first three events have Via headers. 246 # Two of the first three events have Via headers.
193 self.assertTrue(bypass_exception) 247 self.assertTrue(bypass_exception)
194 248
195 # Use directly fetched image only. It is treated as bypassed. 249 # Use directly fetched image only. It is treated as bypassed.
196 metric.SetEvents([EVENT_IMAGE_DIRECT]) 250 metric.SetEvents([EVENT_IMAGE_DIRECT])
197 metric.AddResultsForBypass(None, results) 251 metric.AddResultsForBypass(None, results)
198 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) 252 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1)
199 253
200 def testChromeProxyMetricForCorsBypass(self): 254 def testChromeProxyMetricForCorsBypass(self):
201 metric = metrics.ChromeProxyMetric() 255 metric = metrics.ChromeProxyMetric()
202 metric.SetEvents([EVENT_HTML_PROXY_DEPRECATED_VIA, 256 metric.SetEvents([EVENT_HTML_PROXY_DEPRECATED_VIA,
203 EVENT_IMAGE_BYPASS, 257 EVENT_IMAGE_BYPASS,
204 EVENT_IMAGE_DIRECT]) 258 EVENT_IMAGE_DIRECT])
205 results = test_page_test_results.TestPageTestResults(self) 259 results = test_page_test_results.TestPageTestResults(self)
206 metric.AddResultsForCorsBypass(None, results) 260 metric.AddResultsForCorsBypass(None, results)
207 results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1) 261 results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1)
208 262
263 def testChromeProxyMetricForBlockOnce(self):
264 metric = metrics.ChromeProxyMetric()
265 metric.SetEvents([EVENT_HTML_DIRECT,
266 EVENT_IMAGE_PROXY_VIA])
267 results = test_page_test_results.TestPageTestResults(self)
268 metric.AddResultsForBlockOnce(None, results)
269 results.AssertHasPageSpecificScalarValue('eligible_responses', 'count', 2)
270 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1)
209 271
210 def testChromeProxyMetricForHTTPFallback(self): 272 metric.SetEvents([EVENT_HTML_DIRECT,
211 metric = metrics.ChromeProxyMetric() 273 EVENT_IMAGE_DIRECT])
212 metric.SetEvents([ 274 exception_occurred = False
213 EVENT_HTML_PROXY,
214 EVENT_HTML_PROXY_DEPRECATED_VIA])
215 results = test_page_test_results.TestPageTestResults(self)
216
217 fallback_exception = False
218 info = {}
219 info['enabled'] = False
220 self._StubGetProxyInfo(info)
221 try: 275 try:
222 metric.AddResultsForBypass(None, results) 276 metric.AddResultsForBlockOnce(None, results)
223 except metrics.ChromeProxyMetricException: 277 except metrics.ChromeProxyMetricException:
224 fallback_exception = True 278 exception_occurred = True
225 self.assertTrue(fallback_exception) 279 # The second response was over direct, but was expected via proxy.
226 280 self.assertTrue(exception_occurred)
227 fallback_exception = False
228 info['enabled'] = True
229 info['proxies'] = [
230 'something.else.com:80',
231 metrics.PROXY_SETTING_DIRECT
232 ]
233 self._StubGetProxyInfo(info)
234 try:
235 metric.AddResultsForBypass(None, results)
236 except metrics.ChromeProxyMetricException:
237 fallback_exception = True
238 self.assertTrue(fallback_exception)
239
240 info['enabled'] = True
241 info['proxies'] = [
242 metrics.PROXY_SETTING_HTTP,
243 metrics.PROXY_SETTING_DIRECT
244 ]
245 self._StubGetProxyInfo(info)
246 metric.AddResultsForHTTPFallback(None, results)
247 281
248 def testChromeProxyMetricForSafebrowsing(self): 282 def testChromeProxyMetricForSafebrowsing(self):
249 metric = metrics.ChromeProxyMetric() 283 metric = metrics.ChromeProxyMetric()
250 metric.SetEvents([EVENT_MALWARE_PROXY]) 284 metric.SetEvents([EVENT_MALWARE_PROXY])
251 results = test_page_test_results.TestPageTestResults(self) 285 results = test_page_test_results.TestPageTestResults(self)
252 286
253 metric.AddResultsForSafebrowsing(None, results) 287 metric.AddResultsForSafebrowsing(None, results)
254 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) 288 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)
255 289
256 # Clear results and metrics to test no response for safebrowsing 290 # Clear results and metrics to test no response for safebrowsing
257 results = test_page_test_results.TestPageTestResults(self) 291 results = test_page_test_results.TestPageTestResults(self)
258 metric.SetEvents([]) 292 metric.SetEvents([])
259 metric.AddResultsForSafebrowsing(None, results) 293 metric.AddResultsForSafebrowsing(None, results)
260 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) 294 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)
295
296 def testChromeProxyMetricForHTTPFallback(self):
297 metric = metrics.ChromeProxyMetric()
298 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK,
299 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK])
300 results = test_page_test_results.TestPageTestResults(self)
301 metric.AddResultsForHTTPFallback(None, results)
302 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 2)
303
304 metric.SetEvents([EVENT_HTML_PROXY_VIA,
305 EVENT_IMAGE_PROXY_VIA])
306 exception_occurred = False
307 try:
308 metric.AddResultsForHTTPFallback(None, results)
309 except metrics.ChromeProxyMetricException:
310 exception_occurred = True
311 # The responses came through the SPDY proxy, but were expected through the
312 # HTTP fallback proxy.
313 self.assertTrue(exception_occurred)
314
315 def testChromeProxyMetricForHTTPToDirectFallback(self):
316 metric = metrics.ChromeProxyMetric()
317 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK,
318 EVENT_HTML_DIRECT,
319 EVENT_IMAGE_DIRECT])
320 results = test_page_test_results.TestPageTestResults(self)
321 metric.AddResultsForHTTPToDirectFallback(None, results)
322 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1)
323 results.AssertHasPageSpecificScalarValue('bypass', 'count', 2)
324
325 metric.SetEvents([EVENT_HTML_PROXY_VIA,
326 EVENT_HTML_DIRECT])
327 exception_occurred = False
328 try:
329 metric.AddResultsForHTTPToDirectFallback(None, results)
330 except metrics.ChromeProxyMetricException:
331 exception_occurred = True
332 # The first response was expected through the HTTP fallback proxy.
333 self.assertTrue(exception_occurred)
334
335 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK,
336 EVENT_HTML_PROXY_VIA_HTTP_FALLBACK,
337 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK])
338 exception_occurred = False
339 try:
340 metric.AddResultsForHTTPToDirectFallback(None, results)
341 except metrics.ChromeProxyMetricException:
342 exception_occurred = True
343 # All but the first response were expected to be over direct.
344 self.assertTrue(exception_occurred)
345
346 metric.SetEvents([EVENT_HTML_DIRECT,
347 EVENT_HTML_DIRECT,
348 EVENT_IMAGE_DIRECT])
349 exception_occurred = False
350 try:
351 metric.AddResultsForHTTPToDirectFallback(None, results)
352 except metrics.ChromeProxyMetricException:
353 exception_occurred = True
354 # The first response was expected through the HTTP fallback proxy.
355 self.assertTrue(exception_occurred)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698