Chromium Code Reviews| 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 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 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 url='http://test.html2', | 39 url='http://test.html2', |
| 40 response_headers={ | 40 response_headers={ |
| 41 'Content-Type': 'text/html', | 41 'Content-Type': 'text/html', |
| 42 'Content-Encoding': 'gzip', | 42 'Content-Encoding': 'gzip', |
| 43 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)), | 43 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)), |
| 44 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, | 44 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, |
| 45 }, | 45 }, |
| 46 body=network_unittest.HTML_BODY, | 46 body=network_unittest.HTML_BODY, |
| 47 remote_port=80)) | 47 remote_port=80)) |
| 48 | 48 |
| 49 # An HTML via proxy with the deprecated Via header. | |
| 50 EVENT_HTML_PROXY_DEPRECATED_VIA = ( | |
| 51 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( | |
| 52 url='http://test.html2', | |
| 53 response_headers={ | |
| 54 'Content-Type': 'text/html', | |
| 55 'Content-Encoding': 'gzip', | |
| 56 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)), | |
| 57 'Via': (metrics.CHROME_PROXY_VIA_HEADER_DEPRECATED + | |
| 58 ',other-via'), | |
| 59 }, | |
| 60 body=network_unittest.HTML_BODY)) | |
| 61 | |
| 62 # An image via proxy with Via header. | 49 # An image via proxy with Via header. |
| 63 EVENT_IMAGE_PROXY_VIA = ( | 50 EVENT_IMAGE_PROXY_VIA = ( |
| 64 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( | 51 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 65 url='http://test.image', | 52 url='http://test.image', |
| 66 response_headers={ | 53 response_headers={ |
| 67 'Content-Type': 'image/jpeg', | 54 'Content-Type': 'image/jpeg', |
| 68 'Content-Encoding': 'gzip', | 55 'Content-Encoding': 'gzip', |
| 69 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), | 56 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), |
| 70 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, | 57 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, |
| 71 }, | 58 }, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 EVENT_MALWARE_PROXY = ( | 103 EVENT_MALWARE_PROXY = ( |
| 117 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( | 104 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 118 url='http://test.malware', | 105 url='http://test.malware', |
| 119 response_headers={ | 106 response_headers={ |
| 120 'X-Malware-Url': '1', | 107 'X-Malware-Url': '1', |
| 121 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, | 108 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, |
| 122 'Location': 'http://test.malware', | 109 'Location': 'http://test.malware', |
| 123 }, | 110 }, |
| 124 status=307)) | 111 status=307)) |
| 125 | 112 |
| 126 # An HTML via proxy with the deprecated Via header. | 113 # An HTML via proxy with the deprecated Via header. |
|
sclittle
2015/02/06 20:44:38
Remove the word "deprecated"
bengr
2015/02/06 20:55:14
Done.
| |
| 127 EVENT_IMAGE_BYPASS = ( | 114 EVENT_IMAGE_BYPASS = ( |
| 128 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( | 115 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| 129 url='http://test.image', | 116 url='http://test.image', |
| 130 response_headers={ | 117 response_headers={ |
| 131 'Chrome-Proxy': 'bypass=1', | 118 'Chrome-Proxy': 'bypass=1', |
| 132 'Content-Type': 'text/html', | 119 'Content-Type': 'text/html', |
| 133 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, | 120 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER, |
| 134 }, | 121 }, |
| 135 status=502)) | 122 status=502)) |
| 136 | 123 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 body=base64.b64encode(network_unittest.IMAGE_BODY), | 171 body=base64.b64encode(network_unittest.IMAGE_BODY), |
| 185 base64_encoded_body=True)) | 172 base64_encoded_body=True)) |
| 186 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader()) | 173 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader()) |
| 187 self.assertTrue(resp.HasChromeProxyViaHeader()) | 174 self.assertTrue(resp.HasChromeProxyViaHeader()) |
| 188 self.assertTrue(resp.IsValidByViaHeader()) | 175 self.assertTrue(resp.IsValidByViaHeader()) |
| 189 | 176 |
| 190 def testChromeProxyMetricForDataSaving(self): | 177 def testChromeProxyMetricForDataSaving(self): |
| 191 metric = metrics.ChromeProxyMetric() | 178 metric = metrics.ChromeProxyMetric() |
| 192 events = [ | 179 events = [ |
| 193 EVENT_HTML_DIRECT, | 180 EVENT_HTML_DIRECT, |
| 194 EVENT_HTML_PROXY_DEPRECATED_VIA, | 181 EVENT_HTML_PROXY_VIA, |
| 195 EVENT_IMAGE_PROXY_CACHED, | 182 EVENT_IMAGE_PROXY_CACHED, |
| 196 EVENT_IMAGE_DIRECT] | 183 EVENT_IMAGE_DIRECT] |
| 197 metric.SetEvents(events) | 184 metric.SetEvents(events) |
| 198 | 185 |
| 199 self.assertTrue(len(events), len(list(metric.IterResponses(None)))) | 186 self.assertTrue(len(events), len(list(metric.IterResponses(None)))) |
| 200 results = test_page_test_results.TestPageTestResults(self) | 187 results = test_page_test_results.TestPageTestResults(self) |
| 201 | 188 |
| 202 metric.AddResultsForDataSaving(None, results) | 189 metric.AddResultsForDataSaving(None, results) |
| 203 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) | 190 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) |
| 204 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) | 191 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) |
| 205 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) | 192 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) |
| 206 | 193 |
| 207 def testChromeProxyMetricForHeaderValidation(self): | 194 def testChromeProxyMetricForHeaderValidation(self): |
| 208 metric = metrics.ChromeProxyMetric() | 195 metric = metrics.ChromeProxyMetric() |
| 209 metric.SetEvents([ | 196 metric.SetEvents([ |
| 210 EVENT_HTML_DIRECT, | 197 EVENT_HTML_DIRECT, |
| 211 EVENT_HTML_PROXY_DEPRECATED_VIA, | 198 EVENT_HTML_PROXY_VIA, |
| 212 EVENT_IMAGE_PROXY_CACHED, | 199 EVENT_IMAGE_PROXY_CACHED, |
| 213 EVENT_IMAGE_DIRECT]) | 200 EVENT_IMAGE_DIRECT]) |
| 214 | 201 |
| 215 results = test_page_test_results.TestPageTestResults(self) | 202 results = test_page_test_results.TestPageTestResults(self) |
| 216 | 203 |
| 217 missing_via_exception = False | 204 missing_via_exception = False |
| 218 try: | 205 try: |
| 219 metric.AddResultsForHeaderValidation(None, results) | 206 metric.AddResultsForHeaderValidation(None, results) |
| 220 except metrics.ChromeProxyMetricException: | 207 except metrics.ChromeProxyMetricException: |
| 221 missing_via_exception = True | 208 missing_via_exception = True |
| 222 # Only the HTTP image response does not have a valid Via header. | 209 # Only the HTTP image response does not have a valid Via header. |
| 223 self.assertTrue(missing_via_exception) | 210 self.assertTrue(missing_via_exception) |
| 224 | 211 |
| 225 # Two events with valid Via headers. | 212 # Two events with valid Via headers. |
| 226 metric.SetEvents([ | 213 metric.SetEvents([ |
| 227 EVENT_HTML_PROXY_DEPRECATED_VIA, | 214 EVENT_HTML_PROXY_VIA, |
| 228 EVENT_IMAGE_PROXY_CACHED]) | 215 EVENT_IMAGE_PROXY_CACHED]) |
| 229 metric.AddResultsForHeaderValidation(None, results) | 216 metric.AddResultsForHeaderValidation(None, results) |
| 230 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) | 217 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) |
| 231 | 218 |
| 232 def testChromeProxyMetricForBypass(self): | 219 def testChromeProxyMetricForBypass(self): |
| 233 metric = metrics.ChromeProxyMetric() | 220 metric = metrics.ChromeProxyMetric() |
| 234 metric.SetEvents([ | 221 metric.SetEvents([ |
| 235 EVENT_HTML_DIRECT, | 222 EVENT_HTML_DIRECT, |
| 236 EVENT_HTML_PROXY_DEPRECATED_VIA, | 223 EVENT_HTML_PROXY_VIA, |
| 237 EVENT_IMAGE_PROXY_CACHED, | 224 EVENT_IMAGE_PROXY_CACHED, |
| 238 EVENT_IMAGE_DIRECT]) | 225 EVENT_IMAGE_DIRECT]) |
| 239 results = test_page_test_results.TestPageTestResults(self) | 226 results = test_page_test_results.TestPageTestResults(self) |
| 240 | 227 |
| 241 bypass_exception = False | 228 bypass_exception = False |
| 242 try: | 229 try: |
| 243 metric.AddResultsForBypass(None, results) | 230 metric.AddResultsForBypass(None, results) |
| 244 except metrics.ChromeProxyMetricException: | 231 except metrics.ChromeProxyMetricException: |
| 245 bypass_exception = True | 232 bypass_exception = True |
| 246 # Two of the first three events have Via headers. | 233 # Two of the first three events have Via headers. |
| 247 self.assertTrue(bypass_exception) | 234 self.assertTrue(bypass_exception) |
| 248 | 235 |
| 249 # Use directly fetched image only. It is treated as bypassed. | 236 # Use directly fetched image only. It is treated as bypassed. |
| 250 metric.SetEvents([EVENT_IMAGE_DIRECT]) | 237 metric.SetEvents([EVENT_IMAGE_DIRECT]) |
| 251 metric.AddResultsForBypass(None, results) | 238 metric.AddResultsForBypass(None, results) |
| 252 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) | 239 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) |
| 253 | 240 |
| 254 def testChromeProxyMetricForCorsBypass(self): | 241 def testChromeProxyMetricForCorsBypass(self): |
| 255 metric = metrics.ChromeProxyMetric() | 242 metric = metrics.ChromeProxyMetric() |
| 256 metric.SetEvents([EVENT_HTML_PROXY_DEPRECATED_VIA, | 243 metric.SetEvents([EVENT_HTML_PROXY_VIA, |
| 257 EVENT_IMAGE_BYPASS, | 244 EVENT_IMAGE_BYPASS, |
| 258 EVENT_IMAGE_DIRECT]) | 245 EVENT_IMAGE_DIRECT]) |
| 259 results = test_page_test_results.TestPageTestResults(self) | 246 results = test_page_test_results.TestPageTestResults(self) |
| 260 metric.AddResultsForCorsBypass(None, results) | 247 metric.AddResultsForCorsBypass(None, results) |
| 261 results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1) | 248 results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1) |
| 262 | 249 |
| 263 def testChromeProxyMetricForBlockOnce(self): | 250 def testChromeProxyMetricForBlockOnce(self): |
| 264 metric = metrics.ChromeProxyMetric() | 251 metric = metrics.ChromeProxyMetric() |
| 265 metric.SetEvents([EVENT_HTML_DIRECT, | 252 metric.SetEvents([EVENT_HTML_DIRECT, |
| 266 EVENT_IMAGE_PROXY_VIA]) | 253 EVENT_IMAGE_PROXY_VIA]) |
| 267 results = test_page_test_results.TestPageTestResults(self) | 254 results = test_page_test_results.TestPageTestResults(self) |
| 268 metric.AddResultsForBlockOnce(None, results) | 255 metric.AddResultsForBlockOnce(None, results) |
| 269 results.AssertHasPageSpecificScalarValue('eligible_responses', 'count', 2) | 256 results.AssertHasPageSpecificScalarValue('eligible_responses', 'count', 2) |
| 270 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) | 257 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) |
| 271 | 258 |
| 272 metric.SetEvents([EVENT_HTML_DIRECT, | 259 metric.SetEvents([EVENT_HTML_DIRECT, |
| 273 EVENT_IMAGE_DIRECT]) | 260 EVENT_IMAGE_DIRECT]) |
| 274 exception_occurred = False | 261 exception_occurred = False |
| 275 try: | 262 try: |
| 276 metric.AddResultsForBlockOnce(None, results) | 263 metric.AddResultsForBlockOnce(None, results) |
| 277 except metrics.ChromeProxyMetricException: | 264 except metrics.ChromeProxyMetricException: |
| 278 exception_occurred = True | 265 exception_occurred = True |
| 279 # The second response was over direct, but was expected via proxy. | 266 # The second response was over direct, but was expected via proxy. |
| 280 self.assertTrue(exception_occurred) | 267 self.assertTrue(exception_occurred) |
| 281 | 268 |
| 282 def testChromeProxyMetricForSafebrowsing(self): | 269 def testChromeProxyMetricForSafebrowsingOn(self): |
| 283 metric = metrics.ChromeProxyMetric() | 270 metric = metrics.ChromeProxyMetric() |
| 284 metric.SetEvents([EVENT_MALWARE_PROXY]) | 271 metric.SetEvents([EVENT_MALWARE_PROXY]) |
| 285 results = test_page_test_results.TestPageTestResults(self) | 272 results = test_page_test_results.TestPageTestResults(self) |
| 286 | 273 |
| 287 metric.AddResultsForSafebrowsing(None, results) | 274 metric.AddResultsForSafebrowsingOn(None, results) |
| 288 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) | 275 results.AssertHasPageSpecificScalarValue( |
| 276 'safebrowsing', 'timeout responses', 1) | |
| 289 | 277 |
| 290 # Clear results and metrics to test no response for safebrowsing | 278 # Clear results and metrics to test no response for safebrowsing |
| 291 results = test_page_test_results.TestPageTestResults(self) | 279 results = test_page_test_results.TestPageTestResults(self) |
| 292 metric.SetEvents([]) | 280 metric.SetEvents([]) |
| 293 metric.AddResultsForSafebrowsing(None, results) | 281 metric.AddResultsForSafebrowsingOn(None, results) |
| 294 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) | 282 results.AssertHasPageSpecificScalarValue( |
| 283 'safebrowsing', 'timeout responses', 1) | |
| 295 | 284 |
| 296 def testChromeProxyMetricForHTTPFallback(self): | 285 def testChromeProxyMetricForHTTPFallback(self): |
| 297 metric = metrics.ChromeProxyMetric() | 286 metric = metrics.ChromeProxyMetric() |
| 298 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, | 287 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, |
| 299 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK]) | 288 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK]) |
| 300 results = test_page_test_results.TestPageTestResults(self) | 289 results = test_page_test_results.TestPageTestResults(self) |
| 301 metric.AddResultsForHTTPFallback(None, results) | 290 metric.AddResultsForHTTPFallback(None, results) |
| 302 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 2) | 291 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 2) |
| 303 | 292 |
| 304 metric.SetEvents([EVENT_HTML_PROXY_VIA, | 293 metric.SetEvents([EVENT_HTML_PROXY_VIA, |
| 305 EVENT_IMAGE_PROXY_VIA]) | 294 EVENT_IMAGE_PROXY_VIA]) |
| 306 exception_occurred = False | 295 exception_occurred = False |
| 307 try: | 296 try: |
| 308 metric.AddResultsForHTTPFallback(None, results) | 297 metric.AddResultsForHTTPFallback(None, results) |
| 309 except metrics.ChromeProxyMetricException: | 298 except metrics.ChromeProxyMetricException: |
| 310 exception_occurred = True | 299 exception_occurred = True |
| 311 # The responses came through the SPDY proxy, but were expected through the | 300 # The responses came through the SPDY proxy, but were expected through the |
| 312 # HTTP fallback proxy. | 301 # HTTP fallback proxy. |
| 313 self.assertTrue(exception_occurred) | 302 self.assertTrue(exception_occurred) |
| 314 | 303 |
| 315 def testChromeProxyMetricForHTTPToDirectFallback(self): | 304 def testChromeProxyMetricForHTTPToDirectFallback(self): |
| 316 metric = metrics.ChromeProxyMetric() | 305 metric = metrics.ChromeProxyMetric() |
| 317 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, | 306 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, |
| 318 EVENT_HTML_DIRECT, | 307 EVENT_HTML_DIRECT, |
| 319 EVENT_IMAGE_DIRECT]) | 308 EVENT_IMAGE_DIRECT]) |
| 320 results = test_page_test_results.TestPageTestResults(self) | 309 results = test_page_test_results.TestPageTestResults(self) |
| 321 metric.AddResultsForHTTPToDirectFallback(None, results) | 310 metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') |
| 322 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1) | 311 results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1) |
| 323 results.AssertHasPageSpecificScalarValue('bypass', 'count', 2) | 312 results.AssertHasPageSpecificScalarValue('bypass', 'count', 2) |
| 324 | 313 |
| 325 metric.SetEvents([EVENT_HTML_PROXY_VIA, | 314 metric.SetEvents([EVENT_HTML_PROXY_VIA, |
| 326 EVENT_HTML_DIRECT]) | 315 EVENT_HTML_DIRECT]) |
| 327 exception_occurred = False | 316 exception_occurred = False |
| 328 try: | 317 try: |
| 329 metric.AddResultsForHTTPToDirectFallback(None, results) | 318 metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') |
| 330 except metrics.ChromeProxyMetricException: | 319 except metrics.ChromeProxyMetricException: |
| 331 exception_occurred = True | 320 exception_occurred = True |
| 332 # The first response was expected through the HTTP fallback proxy. | 321 # The first response was expected through the HTTP fallback proxy. |
| 333 self.assertTrue(exception_occurred) | 322 self.assertTrue(exception_occurred) |
| 334 | 323 |
| 335 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, | 324 metric.SetEvents([EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, |
| 336 EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, | 325 EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, |
| 337 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK]) | 326 EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK]) |
| 338 exception_occurred = False | 327 exception_occurred = False |
| 339 try: | 328 try: |
| 340 metric.AddResultsForHTTPToDirectFallback(None, results) | 329 metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') |
| 341 except metrics.ChromeProxyMetricException: | 330 except metrics.ChromeProxyMetricException: |
| 342 exception_occurred = True | 331 exception_occurred = True |
| 343 # All but the first response were expected to be over direct. | 332 # All but the first response were expected to be over direct. |
| 344 self.assertTrue(exception_occurred) | 333 self.assertTrue(exception_occurred) |
| 345 | 334 |
| 346 metric.SetEvents([EVENT_HTML_DIRECT, | 335 metric.SetEvents([EVENT_HTML_DIRECT, |
| 347 EVENT_HTML_DIRECT, | 336 EVENT_HTML_DIRECT, |
| 348 EVENT_IMAGE_DIRECT]) | 337 EVENT_IMAGE_DIRECT]) |
| 349 exception_occurred = False | 338 exception_occurred = False |
| 350 try: | 339 try: |
| 351 metric.AddResultsForHTTPToDirectFallback(None, results) | 340 metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') |
| 352 except metrics.ChromeProxyMetricException: | 341 except metrics.ChromeProxyMetricException: |
| 353 exception_occurred = True | 342 exception_occurred = True |
| 354 # The first response was expected through the HTTP fallback proxy. | 343 # The first response was expected through the HTTP fallback proxy. |
| 355 self.assertTrue(exception_occurred) | 344 self.assertTrue(exception_occurred) |
| OLD | NEW |