Chromecast: extracts Linux window creation code to a common place.
[chromium-blink-merge.git] / tools / chrome_proxy / integration_tests / chrome_proxy_measurements.py
blob319ef7dde952174b68b054c65889750b9379e5fb
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.
5 import base64
6 import logging
7 import urlparse
9 from integration_tests import chrome_proxy_metrics as metrics
10 from metrics import loading
11 from telemetry.core import util
12 from telemetry.page import page_test
15 class ChromeProxyLatency(page_test.PageTest):
16 """Chrome proxy latency measurement."""
18 def __init__(self, *args, **kwargs):
19 super(ChromeProxyLatency, self).__init__(*args, **kwargs)
21 def WillNavigateToPage(self, page, tab):
22 tab.ClearCache(force=True)
24 def ValidateAndMeasurePage(self, page, tab, results):
25 # Wait for the load event.
26 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
27 loading.LoadingMetric().AddResults(tab, results)
30 class ChromeProxyDataSaving(page_test.PageTest):
31 """Chrome proxy data daving measurement."""
32 def __init__(self, *args, **kwargs):
33 super(ChromeProxyDataSaving, self).__init__(*args, **kwargs)
34 self._metrics = metrics.ChromeProxyMetric()
36 def WillNavigateToPage(self, page, tab):
37 tab.ClearCache(force=True)
38 self._metrics.Start(page, tab)
40 def ValidateAndMeasurePage(self, page, tab, results):
41 # Wait for the load event.
42 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
43 self._metrics.Stop(page, tab)
44 self._metrics.AddResultsForDataSaving(tab, results)
47 class ChromeProxyValidation(page_test.PageTest):
48 """Base class for all chrome proxy correctness measurements."""
50 def __init__(self, restart_after_each_page=False):
51 super(ChromeProxyValidation, self).__init__(
52 needs_browser_restart_after_each_page=restart_after_each_page)
53 self._metrics = metrics.ChromeProxyMetric()
54 self._page = None
55 # Whether a timeout exception is expected during the test.
56 self._expect_timeout = False
58 def CustomizeBrowserOptions(self, options):
59 # Enable the chrome proxy (data reduction proxy).
60 options.AppendExtraBrowserArgs('--enable-spdy-proxy-auth')
62 def WillNavigateToPage(self, page, tab):
63 tab.ClearCache(force=True)
64 assert self._metrics
65 self._metrics.Start(page, tab)
67 def ValidateAndMeasurePage(self, page, tab, results):
68 self._page = page
69 # Wait for the load event.
70 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
71 assert self._metrics
72 self._metrics.Stop(page, tab)
73 self.AddResults(tab, results)
75 def AddResults(self, tab, results):
76 raise NotImplementedError
78 def StopBrowserAfterPage(self, browser, page): # pylint: disable=W0613
79 if hasattr(page, 'restart_after') and page.restart_after:
80 return True
81 return False
83 def RunNavigateSteps(self, page, tab):
84 # The redirect from safebrowsing causes a timeout. Ignore that.
85 try:
86 super(ChromeProxyValidation, self).RunNavigateSteps(page, tab)
87 except util.TimeoutException, e:
88 if self._expect_timeout:
89 logging.warning('Navigation timeout on page %s',
90 page.name if page.name else page.url)
91 else:
92 raise e
95 class ChromeProxyHeaders(ChromeProxyValidation):
96 """Correctness measurement for response headers."""
98 def __init__(self):
99 super(ChromeProxyHeaders, self).__init__(restart_after_each_page=True)
101 def AddResults(self, tab, results):
102 self._metrics.AddResultsForHeaderValidation(tab, results)
105 class ChromeProxyBypass(ChromeProxyValidation):
106 """Correctness measurement for bypass responses."""
108 def __init__(self):
109 super(ChromeProxyBypass, self).__init__(restart_after_each_page=True)
111 def AddResults(self, tab, results):
112 self._metrics.AddResultsForBypass(tab, results)
115 class ChromeProxyFallback(ChromeProxyValidation):
116 """Correctness measurement for proxy fallback responses."""
118 def __init__(self):
119 super(ChromeProxyFallback, self).__init__(restart_after_each_page=True)
121 def AddResults(self, tab, results):
122 self._metrics.AddResultsForFallback(tab, results)
125 class ChromeProxyCorsBypass(ChromeProxyValidation):
126 """Correctness measurement for bypass responses for CORS requests."""
128 def __init__(self):
129 super(ChromeProxyCorsBypass, self).__init__(restart_after_each_page=True)
131 def ValidateAndMeasurePage(self, page, tab, results):
132 # The test page sets window.xhrRequestCompleted to true when the XHR fetch
133 # finishes.
134 tab.WaitForJavaScriptExpression('window.xhrRequestCompleted', 15000)
135 super(ChromeProxyCorsBypass,
136 self).ValidateAndMeasurePage(page, tab, results)
138 def AddResults(self, tab, results):
139 self._metrics.AddResultsForCorsBypass(tab, results)
142 class ChromeProxyBlockOnce(ChromeProxyValidation):
143 """Correctness measurement for block-once responses."""
145 def __init__(self):
146 super(ChromeProxyBlockOnce, self).__init__(restart_after_each_page=True)
148 def AddResults(self, tab, results):
149 self._metrics.AddResultsForBlockOnce(tab, results)
152 class ChromeProxySafebrowsing(ChromeProxyValidation):
153 """Correctness measurement for safebrowsing."""
155 def __init__(self):
156 super(ChromeProxySafebrowsing, self).__init__()
158 def WillNavigateToPage(self, page, tab):
159 super(ChromeProxySafebrowsing, self).WillNavigateToPage(page, tab)
160 self._expect_timeout = True
162 def AddResults(self, tab, results):
163 self._metrics.AddResultsForSafebrowsing(tab, results)
166 _FAKE_PROXY_AUTH_VALUE = 'aabbccdd3b7579186c1b0620614fdb1f0000ffff'
167 _TEST_SERVER = 'chromeproxy-test.appspot.com'
168 _TEST_SERVER_DEFAULT_URL = 'http://' + _TEST_SERVER + '/default'
171 # We rely on the chromeproxy-test server to facilitate some of the tests.
172 # The test server code is at <TBD location> and runs at _TEST_SERVER
174 # The test server allow request to override response status, headers, and
175 # body through query parameters. See GetResponseOverrideURL.
176 def GetResponseOverrideURL(url=_TEST_SERVER_DEFAULT_URL, respStatus=0,
177 respHeader="", respBody=""):
178 """ Compose the request URL with query parameters to override
179 the chromeproxy-test server response.
182 queries = []
183 if respStatus > 0:
184 queries.append('respStatus=%d' % respStatus)
185 if respHeader:
186 queries.append('respHeader=%s' % base64.b64encode(respHeader))
187 if respBody:
188 queries.append('respBody=%s' % base64.b64encode(respBody))
189 if len(queries) == 0:
190 return url
191 "&".join(queries)
192 # url has query already
193 if urlparse.urlparse(url).query:
194 return url + '&' + "&".join(queries)
195 else:
196 return url + '?' + "&".join(queries)
199 class ChromeProxyHTTPFallbackProbeURL(ChromeProxyValidation):
200 """Correctness measurement for proxy fallback.
202 In this test, the probe URL does not return 'OK'. Chrome is expected
203 to use the fallback proxy.
206 def __init__(self):
207 super(ChromeProxyHTTPFallbackProbeURL, self).__init__()
209 def CustomizeBrowserOptions(self, options):
210 super(ChromeProxyHTTPFallbackProbeURL,
211 self).CustomizeBrowserOptions(options)
212 # Use the test server probe URL which returns the response
213 # body as specified by respBody.
214 probe_url = GetResponseOverrideURL(
215 respBody='not OK')
216 options.AppendExtraBrowserArgs(
217 '--data-reduction-proxy-probe-url=%s' % probe_url)
219 def AddResults(self, tab, results):
220 self._metrics.AddResultsForHTTPFallback(tab, results)
223 class ChromeProxyHTTPFallbackViaHeader(ChromeProxyValidation):
224 """Correctness measurement for proxy fallback.
226 In this test, the configured proxy is the chromeproxy-test server which
227 will send back a response without the expected Via header. Chrome is
228 expected to use the fallback proxy and add the configured proxy to the
229 bad proxy list.
232 def __init__(self):
233 super(ChromeProxyHTTPFallbackViaHeader, self).__init__(
234 restart_after_each_page=True)
236 def CustomizeBrowserOptions(self, options):
237 super(ChromeProxyHTTPFallbackViaHeader,
238 self).CustomizeBrowserOptions(options)
239 options.AppendExtraBrowserArgs('--ignore-certificate-errors')
240 options.AppendExtraBrowserArgs(
241 '--spdy-proxy-auth-origin=http://%s' % _TEST_SERVER)
242 options.AppendExtraBrowserArgs(
243 '--spdy-proxy-auth-value=%s' % _FAKE_PROXY_AUTH_VALUE)
245 def AddResults(self, tab, results):
246 proxies = [
247 _TEST_SERVER + ":80",
248 self._metrics.effective_proxies['fallback'],
249 self._metrics.effective_proxies['direct']]
250 bad_proxies = [_TEST_SERVER + ":80", metrics.PROXY_SETTING_HTTP]
251 self._metrics.AddResultsForHTTPFallback(tab, results, proxies, bad_proxies)
254 class ChromeProxyClientVersion(ChromeProxyValidation):
255 """Correctness measurement for version directives in Chrome-Proxy header.
257 The test verifies that the version information provided in the Chrome-Proxy
258 request header overrides any version, if specified, that is provided in the
259 user agent string.
262 def __init__(self):
263 super(ChromeProxyClientVersion, self).__init__()
265 def CustomizeBrowserOptions(self, options):
266 super(ChromeProxyClientVersion,
267 self).CustomizeBrowserOptions(options)
268 options.AppendExtraBrowserArgs('--user-agent="Chrome/32.0.1700.99"')
270 def AddResults(self, tab, results):
271 self._metrics.AddResultsForClientVersion(tab, results)
274 class ChromeProxyHTTPToDirectFallback(ChromeProxyValidation):
275 """Correctness measurement for HTTP proxy fallback to direct."""
277 def __init__(self):
278 super(ChromeProxyHTTPToDirectFallback, self).__init__(
279 restart_after_each_page=True)
281 def WillNavigateToPage(self, page, tab):
282 super(ChromeProxyHTTPToDirectFallback, self).WillNavigateToPage(page, tab)
283 # In order to have this test run starting from the HTTP fallback proxy,
284 # the startup URL is set such that it will trigger a proxy fallback.
285 # Verify that this is true before beginning the test proper.
286 proxies = [
287 self._metrics.effective_proxies['proxy'],
288 self._metrics.effective_proxies['fallback'],
289 self._metrics.effective_proxies['direct']]
290 bad_proxies = [self._metrics.effective_proxies['proxy']]
291 self._metrics.VerifyProxyInfo(tab, proxies, bad_proxies)
293 def AddResults(self, tab, results):
294 self._metrics.AddResultsForHTTPToDirectFallback(tab, results)
297 class ChromeProxyExplicitBypass(ChromeProxyValidation):
298 """Correctness measurement for explicit proxy bypasses.
300 In this test, the configured proxy is the chromeproxy-test server which
301 will send back a response without the expected Via header. Chrome is
302 expected to use the fallback proxy and add the configured proxy to the
303 bad proxy list.
306 def __init__(self):
307 super(ChromeProxyExplicitBypass, self).__init__(
308 restart_after_each_page=True)
310 def CustomizeBrowserOptions(self, options):
311 super(ChromeProxyExplicitBypass,
312 self).CustomizeBrowserOptions(options)
313 options.AppendExtraBrowserArgs('--ignore-certificate-errors')
314 options.AppendExtraBrowserArgs(
315 '--spdy-proxy-auth-origin=http://%s' % _TEST_SERVER)
316 options.AppendExtraBrowserArgs(
317 '--spdy-proxy-auth-value=%s' % _FAKE_PROXY_AUTH_VALUE)
319 def AddResults(self, tab, results):
320 bad_proxies = [{
321 'proxy': _TEST_SERVER + ':80',
322 'retry_seconds_low': self._page.bypass_seconds_low,
323 'retry_seconds_high': self._page.bypass_seconds_high
325 if self._page.num_bypassed_proxies == 2:
326 bad_proxies.append({
327 'proxy': self._metrics.effective_proxies['fallback'],
328 'retry_seconds_low': self._page.bypass_seconds_low,
329 'retry_seconds_high': self._page.bypass_seconds_high
331 else:
332 # Even if the test page only causes the primary proxy to be bypassed,
333 # Chrome will attempt to fetch the favicon for the test server through
334 # the data reduction proxy, which will cause a "block=0" bypass.
335 bad_proxies.append({'proxy': self._metrics.effective_proxies['fallback']})
337 self._metrics.AddResultsForExplicitBypass(tab, results, bad_proxies)
340 class ChromeProxySmoke(ChromeProxyValidation):
341 """Smoke measurement for basic chrome proxy correctness."""
343 def __init__(self):
344 super(ChromeProxySmoke, self).__init__()
346 def WillNavigateToPage(self, page, tab):
347 super(ChromeProxySmoke, self).WillNavigateToPage(page, tab)
348 if page.name == 'safebrowsing':
349 self._expect_timeout = True
351 def AddResults(self, tab, results):
352 # Map a page name to its AddResults func.
353 page_to_metrics = {
354 'header validation': [self._metrics.AddResultsForHeaderValidation],
355 'compression: image': [
356 self._metrics.AddResultsForHeaderValidation,
357 self._metrics.AddResultsForDataSaving,
359 'compression: javascript': [
360 self._metrics.AddResultsForHeaderValidation,
361 self._metrics.AddResultsForDataSaving,
363 'compression: css': [
364 self._metrics.AddResultsForHeaderValidation,
365 self._metrics.AddResultsForDataSaving,
367 'bypass': [self._metrics.AddResultsForBypass],
368 'safebrowsing': [self._metrics.AddResultsForSafebrowsing],
370 if not self._page.name in page_to_metrics:
371 raise page_test.MeasurementFailure(
372 'Invalid page name (%s) in smoke. Page name must be one of:\n%s' % (
373 self._page.name, page_to_metrics.keys()))
374 for add_result in page_to_metrics[self._page.name]:
375 add_result(tab, results)