Commit 0ae651fc authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT/common/security-features] Format some scripts using yapf

Purely mechanical.

Bug: 906850
Change-Id: I4a9ef0246359160abd4857d23ccbed9dd5503813
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1715755
Auto-Submit: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680742}
parent 4f8538cc
......@@ -28,7 +28,10 @@ def expand_pattern(expansion_pattern, test_expansion_schema):
return expansion
def permute_expansion(expansion, artifact_order, selection = {}, artifact_index = 0):
def permute_expansion(expansion,
artifact_order,
selection={},
artifact_index=0):
assert isinstance(artifact_order, list), "artifact_order should be a list"
if artifact_index >= len(artifact_order):
......@@ -39,18 +42,16 @@ def permute_expansion(expansion, artifact_order, selection = {}, artifact_index
for artifact_value in expansion[artifact_key]:
selection[artifact_key] = artifact_value
for next_selection in permute_expansion(expansion,
artifact_order,
selection,
artifact_index + 1):
for next_selection in permute_expansion(expansion, artifact_order,
selection, artifact_index + 1):
yield next_selection
def generate_selection(config, selection, spec, test_html_template_basename):
# TODO: Refactor out this referrer-policy-specific part.
if 'referrer_policy' in spec:
# Oddball: it can be None, so in JS it's null.
selection['referrer_policy'] = spec['referrer_policy']
# Oddball: it can be None, so in JS it's null.
selection['referrer_policy'] = spec['referrer_policy']
test_parameters = json.dumps(selection, indent=2, separators=(',', ':'))
# Adjust the template for the test invoking JS. Indent it to look nice.
......@@ -66,14 +67,16 @@ def generate_selection(config, selection, spec, test_html_template_basename):
''' % (config.test_case_name, test_parameters)
selection['spec_name'] = spec['name']
selection['test_page_title'] = config.test_page_title_template % spec['title']
selection[
'test_page_title'] = config.test_page_title_template % spec['title']
selection['spec_description'] = spec['description']
selection['spec_specification_url'] = spec['specification_url']
selection['helper_js'] = config.helper_js
selection['sanity_checker_js'] = config.sanity_checker_js
selection['spec_json_js'] = config.spec_json_js
test_filename = os.path.join(config.spec_directory, config.test_file_path_pattern % selection)
test_filename = os.path.join(config.spec_directory,
config.test_file_path_pattern % selection)
test_headers_filename = test_filename + ".headers"
test_directory = os.path.dirname(test_filename)
......@@ -90,7 +93,8 @@ def generate_selection(config, selection, spec, test_html_template_basename):
# Adjust the template for the test invoking JS. Indent it to look nice.
selection['generated_disclaimer'] = generated_disclaimer.rstrip()
selection['test_description'] = config.test_description_template % selection
selection[
'test_description'] = config.test_description_template % selection
selection['test_description'] = \
selection['test_description'].rstrip().replace("\n", "\n" + " " * 33)
......@@ -123,9 +127,11 @@ def generate_test_source_files(config, spec_json, target):
specification = spec_json['specification']
spec_json_js_template = util.get_template('spec_json.js.template')
generated_spec_json_filename = os.path.join(config.spec_directory, "spec_json.js")
util.write_file(generated_spec_json_filename,
spec_json_js_template % {'spec_json': json.dumps(spec_json)})
generated_spec_json_filename = os.path.join(config.spec_directory,
"spec_json.js")
util.write_file(
generated_spec_json_filename,
spec_json_js_template % {'spec_json': json.dumps(spec_json)})
# Choose a debug/release template depending on the target.
html_template = "test.%s.html.template" % target
......@@ -149,13 +155,17 @@ def generate_test_source_files(config, spec_json, target):
output_dict = {}
for expansion_pattern in spec['test_expansion']:
expansion = expand_pattern(expansion_pattern, test_expansion_schema)
expansion = expand_pattern(expansion_pattern,
test_expansion_schema)
for selection in permute_expansion(expansion, artifact_order):
selection_path = config.selection_pattern % selection
if not selection_path in exclusion_dict:
if selection_path in output_dict:
if expansion_pattern['expansion'] != 'override':
print("Error: %s's expansion is default but overrides %s" % (selection['name'], output_dict[selection_path]['name']))
print(
"Error: %s's expansion is default but overrides %s"
% (selection['name'],
output_dict[selection_path]['name']))
sys.exit(1)
output_dict[selection_path] = copy.deepcopy(selection)
else:
......@@ -163,24 +173,30 @@ def generate_test_source_files(config, spec_json, target):
for selection_path in output_dict:
selection = output_dict[selection_path]
generate_selection(config,
selection,
spec,
html_template)
generate_selection(config, selection, spec, html_template)
def main(config):
parser = argparse.ArgumentParser(description='Test suite generator utility')
parser.add_argument('-t', '--target', type = str,
choices = ("release", "debug"), default = "release",
help = 'Sets the appropriate template for generating tests')
parser.add_argument('-s', '--spec', type = str, default = None,
help = 'Specify a file used for describing and generating the tests')
parser = argparse.ArgumentParser(
description='Test suite generator utility')
parser.add_argument(
'-t',
'--target',
type=str,
choices=("release", "debug"),
default="release",
help='Sets the appropriate template for generating tests')
parser.add_argument(
'-s',
'--spec',
type=str,
default=None,
help='Specify a file used for describing and generating the tests')
# TODO(kristijanburnik): Add option for the spec_json file.
args = parser.parse_args()
if args.spec:
config.spec_directory = args.spec
config.spec_directory = args.spec
spec_filename = os.path.join(config.spec_directory, "spec.src.json")
spec_json = util.load_spec_json(spec_filename)
......
......@@ -3,10 +3,10 @@ from __future__ import print_function
import os, sys, json, re
script_directory = os.path.dirname(os.path.abspath(__file__))
template_directory = os.path.abspath(os.path.join(script_directory,
'template'))
test_root_directory = os.path.abspath(os.path.join(script_directory,
'..', '..', '..'))
template_directory = os.path.abspath(
os.path.join(script_directory, 'template'))
test_root_directory = os.path.abspath(
os.path.join(script_directory, '..', '..', '..'))
def get_template(basename):
......@@ -20,22 +20,22 @@ def write_file(filename, contents):
def read_nth_line(fp, line_number):
fp.seek(0)
for i, line in enumerate(fp):
if (i + 1) == line_number:
return line
fp.seek(0)
for i, line in enumerate(fp):
if (i + 1) == line_number:
return line
def load_spec_json(path_to_spec):
re_error_location = re.compile('line ([0-9]+) column ([0-9]+)')
with open(path_to_spec, "r") as f:
try:
return json.load(f)
return json.load(f)
except ValueError as ex:
print(ex.message)
match = re_error_location.search(ex.message)
if match:
line_number, column = int(match.group(1)), int(match.group(2))
print(read_nth_line(f, line_number).rstrip())
print(" " * (column - 1) + "^")
sys.exit(1)
print(ex.message)
match = re_error_location.search(ex.message)
if match:
line_number, column = int(match.group(1)), int(match.group(2))
print(read_nth_line(f, line_number).rstrip())
print(" " * (column - 1) + "^")
sys.exit(1)
......@@ -3,23 +3,27 @@
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', 'common', 'security-features', 'tools'))
sys.path.insert(
0,
os.path.join(
os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', 'common',
'security-features', 'tools'))
import generate
class MixedContentConfig(object):
def __init__(self):
self.selection_pattern = '%(subresource)s/' + \
'%(opt_in_method)s/' + \
'%(origin)s/' + \
'%(context_nesting)s/' + \
'%(redirection)s/'
self.test_file_path_pattern = self.selection_pattern + \
'%(spec_name)s/' + \
'%(name)s.%(source_scheme)s.html'
self.test_description_template = '''opt_in_method: %(opt_in_method)s
def __init__(self):
self.selection_pattern = '%(subresource)s/' + \
'%(opt_in_method)s/' + \
'%(origin)s/' + \
'%(context_nesting)s/' + \
'%(redirection)s/'
self.test_file_path_pattern = self.selection_pattern + \
'%(spec_name)s/' + \
'%(name)s.%(source_scheme)s.html'
self.test_description_template = '''opt_in_method: %(opt_in_method)s
origin: %(origin)s
source_scheme: %(source_scheme)s
context_nesting: %(context_nesting)s
......@@ -28,37 +32,38 @@ subresource: %(subresource)s
expectation: %(expectation)s
'''
self.test_page_title_template = 'Mixed-Content: %s'
self.test_page_title_template = 'Mixed-Content: %s'
self.helper_js = '/mixed-content/generic/mixed-content-test-case.js?pipe=sub'
self.helper_js = '/mixed-content/generic/mixed-content-test-case.js?pipe=sub'
# For debug target only.
self.sanity_checker_js = '/mixed-content/generic/sanity-checker.js'
self.spec_json_js = '/mixed-content/spec_json.js'
# For debug target only.
self.sanity_checker_js = '/mixed-content/generic/sanity-checker.js'
self.spec_json_js = '/mixed-content/spec_json.js'
self.test_case_name = 'MixedContentTestCase'
self.test_case_name = 'MixedContentTestCase'
script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(os.path.join(script_directory, '..', '..'))
script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(
os.path.join(script_directory, '..', '..'))
def handleDelivery(self, selection, spec):
opt_in_method = selection['opt_in_method']
def handleDelivery(self, selection, spec):
opt_in_method = selection['opt_in_method']
meta = ''
headers = []
meta = ''
headers = []
# TODO(kristijanburnik): Implement the opt-in-method here.
if opt_in_method == 'meta-csp':
meta = '<meta http-equiv="Content-Security-Policy" ' + \
'content="block-all-mixed-content">'
elif opt_in_method == 'http-csp':
headers.append("Content-Security-Policy: block-all-mixed-content")
elif opt_in_method == 'no-opt-in':
pass
else:
raise ValueError("Invalid opt_in_method %s" % opt_in_method)
# TODO(kristijanburnik): Implement the opt-in-method here.
if opt_in_method == 'meta-csp':
meta = '<meta http-equiv="Content-Security-Policy" ' + \
'content="block-all-mixed-content">'
elif opt_in_method == 'http-csp':
headers.append("Content-Security-Policy: block-all-mixed-content")
elif opt_in_method == 'no-opt-in':
pass
else:
raise ValueError("Invalid opt_in_method %s" % opt_in_method)
return {"meta": meta, "headers": headers}
return {"meta": meta, "headers": headers}
if __name__ == '__main__':
......
......@@ -5,6 +5,7 @@ from __future__ import print_function
import json, sys
from common_paths import *
def assert_non_empty_string(obj, field):
assert field in obj, 'Missing field "%s"' % field
assert isinstance(obj[field], basestring), \
......@@ -31,8 +32,8 @@ def assert_contains(obj, field):
def assert_value_from(obj, field, items):
assert obj[field] in items, \
'Field "%s" must be from: %s' % (field, str(items))
assert obj[field] in items, \
'Field "%s" must be from: %s' % (field, str(items))
def assert_atom_or_list_items_from(obj, field, items):
......@@ -71,13 +72,14 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema):
assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key,
sub_schema)
def validate(spec_json, details):
""" Validates the json specification for generating tests. """
details['object'] = spec_json
assert_contains_only_fields(spec_json, ["specification",
"test_expansion_schema",
"excluded_tests"])
assert_contains_only_fields(
spec_json,
["specification", "test_expansion_schema", "excluded_tests"])
assert_non_empty_list(spec_json, "specification")
assert_non_empty_dict(spec_json, "test_expansion_schema")
assert_non_empty_list(spec_json, "excluded_tests")
......@@ -93,11 +95,10 @@ def validate(spec_json, details):
details['object'] = spec
# Validate required fields for a single spec.
assert_contains_only_fields(spec, ['name',
'title',
'description',
'specification_url',
'test_expansion'])
assert_contains_only_fields(spec, [
'name', 'title', 'description', 'specification_url',
'test_expansion'
])
assert_non_empty_string(spec, 'name')
assert_non_empty_string(spec, 'title')
assert_non_empty_string(spec, 'description')
......@@ -123,14 +124,10 @@ def validate(spec_json, details):
# Validate the test_expansion schema members.
details['object'] = test_expansion_schema
assert_contains_only_fields(test_expansion_schema, ['expansion',
'source_scheme',
'opt_in_method',
'context_nesting',
'redirection',
'subresource',
'origin',
'expectation'])
assert_contains_only_fields(test_expansion_schema, [
'expansion', 'source_scheme', 'opt_in_method', 'context_nesting',
'redirection', 'subresource', 'origin', 'expectation'
])
# Validate excluded tests.
details['object'] = excluded_tests
for excluded_test_expansion in excluded_tests:
......@@ -139,10 +136,8 @@ def validate(spec_json, details):
details['object'] = excluded_test_expansion
for artifact in test_expansion_schema:
details['test_expansion_field'] = artifact
assert_valid_artifact(
excluded_test_expansion,
artifact,
test_expansion_schema[artifact])
assert_valid_artifact(excluded_test_expansion, artifact,
test_expansion_schema[artifact])
del details['test_expansion_field']
del details['object']
......@@ -159,7 +154,7 @@ def assert_valid_spec_json(spec_json):
def main():
spec_json = load_spec_json();
spec_json = load_spec_json()
assert_valid_spec_json(spec_json)
print("Spec JSON is valid.")
......
......@@ -3,65 +3,71 @@
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', 'common', 'security-features', 'tools'))
sys.path.insert(
0,
os.path.join(
os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', 'common',
'security-features', 'tools'))
import generate
class ReferrerPolicyConfig(object):
def __init__(self):
self.selection_pattern = '%(delivery_method)s/' + \
'%(origin)s/' + \
'%(source_protocol)s-%(target_protocol)s/' + \
'%(subresource)s/' + \
'%(redirection)s/'
def __init__(self):
self.selection_pattern = '%(delivery_method)s/' + \
'%(origin)s/' + \
'%(source_protocol)s-%(target_protocol)s/' + \
'%(subresource)s/' + \
'%(redirection)s/'
self.test_file_path_pattern = '%(spec_name)s/' + self.selection_pattern + \
'%(name)s.%(source_protocol)s.html'
self.test_file_path_pattern = '%(spec_name)s/' + self.selection_pattern + \
'%(name)s.%(source_protocol)s.html'
self.test_description_template = '''The referrer URL is %(referrer_url)s when a
self.test_description_template = '''The referrer URL is %(referrer_url)s when a
document served over %(source_protocol)s requires an %(target_protocol)s
sub-resource via %(subresource)s using the %(delivery_method)s
delivery method with %(redirection)s and when
the target request is %(origin)s.'''
self.test_page_title_template = 'Referrer-Policy: %s'
self.test_page_title_template = 'Referrer-Policy: %s'
self.helper_js = '/referrer-policy/generic/referrer-policy-test-case.sub.js'
self.helper_js = '/referrer-policy/generic/referrer-policy-test-case.sub.js'
# For debug target only.
self.sanity_checker_js = '/referrer-policy/generic/sanity-checker.js'
self.spec_json_js = '/referrer-policy/spec_json.js'
# For debug target only.
self.sanity_checker_js = '/referrer-policy/generic/sanity-checker.js'
self.spec_json_js = '/referrer-policy/spec_json.js'
self.test_case_name = 'ReferrerPolicyTestCase'
self.test_case_name = 'ReferrerPolicyTestCase'
script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(os.path.join(script_directory, '..', '..'))
script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(
os.path.join(script_directory, '..', '..'))
def handleDelivery(self, selection, spec):
delivery_method = selection['delivery_method']
delivery_value = spec['referrer_policy']
def handleDelivery(self, selection, spec):
delivery_method = selection['delivery_method']
delivery_value = spec['referrer_policy']
meta = ''
headers = []
if delivery_value != None:
if delivery_method == 'meta-referrer':
meta = \
'<meta name="referrer" content="%s">' % delivery_value
elif delivery_method == 'http-rp':
meta = \
"<!-- No meta: Referrer policy delivered via HTTP headers. -->"
headers.append('Referrer-Policy: ' + '%s' % delivery_value)
# TODO(kristijanburnik): Limit to WPT origins.
headers.append('Access-Control-Allow-Origin: *')
elif delivery_method == 'attr-referrer':
# attr-referrer is supported by the JS test wrapper.
pass
elif delivery_method == 'rel-noreferrer':
# rel=noreferrer is supported by the JS test wrapper.
pass
else:
raise ValueError('Not implemented delivery_method: ' \
+ delivery_method)
return {"meta": meta, "headers": headers}
meta = ''
headers = []
if delivery_value != None:
if delivery_method == 'meta-referrer':
meta = \
'<meta name="referrer" content="%s">' % delivery_value
elif delivery_method == 'http-rp':
meta = \
"<!-- No meta: Referrer policy delivered via HTTP headers. -->"
headers.append('Referrer-Policy: ' + '%s' % delivery_value)
# TODO(kristijanburnik): Limit to WPT origins.
headers.append('Access-Control-Allow-Origin: *')
elif delivery_method == 'attr-referrer':
# attr-referrer is supported by the JS test wrapper.
pass
elif delivery_method == 'rel-noreferrer':
# rel=noreferrer is supported by the JS test wrapper.
pass
else:
raise ValueError('Not implemented delivery_method: ' \
+ delivery_method)
return {"meta": meta, "headers": headers}
if __name__ == '__main__':
......
......@@ -5,6 +5,7 @@ from __future__ import print_function
import json, sys
from common_paths import *
def assert_non_empty_string(obj, field):
assert field in obj, 'Missing field "%s"' % field
assert isinstance(obj[field], basestring), \
......@@ -31,8 +32,8 @@ def assert_contains(obj, field):
def assert_value_from(obj, field, items):
assert obj[field] in items, \
'Field "%s" must be from: %s' % (field, str(items))
assert obj[field] in items, \
'Field "%s" must be from: %s' % (field, str(items))
def assert_atom_or_list_items_from(obj, field, items):
......@@ -71,14 +72,15 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema):
assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key,
sub_schema)
def validate(spec_json, details):
""" Validates the json specification for generating tests. """
details['object'] = spec_json
assert_contains_only_fields(spec_json, ["specification",
"referrer_policy_schema",
"test_expansion_schema",
"excluded_tests"])
assert_contains_only_fields(spec_json, [
"specification", "referrer_policy_schema", "test_expansion_schema",
"excluded_tests"
])
assert_non_empty_list(spec_json, "specification")
assert_non_empty_list(spec_json, "referrer_policy_schema")
assert_non_empty_dict(spec_json, "test_expansion_schema")
......@@ -96,12 +98,10 @@ def validate(spec_json, details):
details['object'] = spec
# Validate required fields for a single spec.
assert_contains_only_fields(spec, ['name',
'title',
'description',
'referrer_policy',
'specification_url',
'test_expansion'])
assert_contains_only_fields(spec, [
'name', 'title', 'description', 'referrer_policy',
'specification_url', 'test_expansion'
])
assert_non_empty_string(spec, 'name')
assert_non_empty_string(spec, 'title')
assert_non_empty_string(spec, 'description')
......@@ -128,14 +128,10 @@ def validate(spec_json, details):
# Validate the test_expansion schema members.
details['object'] = test_expansion_schema
assert_contains_only_fields(test_expansion_schema, ['expansion',
'delivery_method',
'redirection',
'origin',
'source_protocol',
'target_protocol',
'subresource',
'referrer_url'])
assert_contains_only_fields(test_expansion_schema, [
'expansion', 'delivery_method', 'redirection', 'origin',
'source_protocol', 'target_protocol', 'subresource', 'referrer_url'
])
# Validate excluded tests.
details['object'] = excluded_tests
for excluded_test_expansion in excluded_tests:
......@@ -144,10 +140,8 @@ def validate(spec_json, details):
details['object'] = excluded_test_expansion
for artifact in test_expansion_schema:
details['test_expansion_field'] = artifact
assert_valid_artifact(
excluded_test_expansion,
artifact,
test_expansion_schema[artifact])
assert_valid_artifact(excluded_test_expansion, artifact,
test_expansion_schema[artifact])
del details['test_expansion_field']
del details['object']
......@@ -164,7 +158,7 @@ def assert_valid_spec_json(spec_json):
def main():
spec_json = load_spec_json();
spec_json = load_spec_json()
assert_valid_spec_json(spec_json)
print("Spec JSON is valid.")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment