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): ...@@ -28,7 +28,10 @@ def expand_pattern(expansion_pattern, test_expansion_schema):
return expansion 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" assert isinstance(artifact_order, list), "artifact_order should be a list"
if artifact_index >= len(artifact_order): if artifact_index >= len(artifact_order):
...@@ -39,10 +42,8 @@ def permute_expansion(expansion, artifact_order, selection = {}, artifact_index ...@@ -39,10 +42,8 @@ def permute_expansion(expansion, artifact_order, selection = {}, artifact_index
for artifact_value in expansion[artifact_key]: for artifact_value in expansion[artifact_key]:
selection[artifact_key] = artifact_value selection[artifact_key] = artifact_value
for next_selection in permute_expansion(expansion, for next_selection in permute_expansion(expansion, artifact_order,
artifact_order, selection, artifact_index + 1):
selection,
artifact_index + 1):
yield next_selection yield next_selection
...@@ -66,14 +67,16 @@ def generate_selection(config, selection, spec, test_html_template_basename): ...@@ -66,14 +67,16 @@ def generate_selection(config, selection, spec, test_html_template_basename):
''' % (config.test_case_name, test_parameters) ''' % (config.test_case_name, test_parameters)
selection['spec_name'] = spec['name'] 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_description'] = spec['description']
selection['spec_specification_url'] = spec['specification_url'] selection['spec_specification_url'] = spec['specification_url']
selection['helper_js'] = config.helper_js selection['helper_js'] = config.helper_js
selection['sanity_checker_js'] = config.sanity_checker_js selection['sanity_checker_js'] = config.sanity_checker_js
selection['spec_json_js'] = config.spec_json_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_headers_filename = test_filename + ".headers"
test_directory = os.path.dirname(test_filename) test_directory = os.path.dirname(test_filename)
...@@ -90,7 +93,8 @@ def generate_selection(config, selection, spec, test_html_template_basename): ...@@ -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. # Adjust the template for the test invoking JS. Indent it to look nice.
selection['generated_disclaimer'] = generated_disclaimer.rstrip() 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'] = \
selection['test_description'].rstrip().replace("\n", "\n" + " " * 33) selection['test_description'].rstrip().replace("\n", "\n" + " " * 33)
...@@ -123,8 +127,10 @@ def generate_test_source_files(config, spec_json, target): ...@@ -123,8 +127,10 @@ def generate_test_source_files(config, spec_json, target):
specification = spec_json['specification'] specification = spec_json['specification']
spec_json_js_template = util.get_template('spec_json.js.template') spec_json_js_template = util.get_template('spec_json.js.template')
generated_spec_json_filename = os.path.join(config.spec_directory, "spec_json.js") generated_spec_json_filename = os.path.join(config.spec_directory,
util.write_file(generated_spec_json_filename, "spec_json.js")
util.write_file(
generated_spec_json_filename,
spec_json_js_template % {'spec_json': json.dumps(spec_json)}) spec_json_js_template % {'spec_json': json.dumps(spec_json)})
# Choose a debug/release template depending on the target. # Choose a debug/release template depending on the target.
...@@ -149,13 +155,17 @@ def generate_test_source_files(config, spec_json, target): ...@@ -149,13 +155,17 @@ def generate_test_source_files(config, spec_json, target):
output_dict = {} output_dict = {}
for expansion_pattern in spec['test_expansion']: 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): for selection in permute_expansion(expansion, artifact_order):
selection_path = config.selection_pattern % selection selection_path = config.selection_pattern % selection
if not selection_path in exclusion_dict: if not selection_path in exclusion_dict:
if selection_path in output_dict: if selection_path in output_dict:
if expansion_pattern['expansion'] != 'override': 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) sys.exit(1)
output_dict[selection_path] = copy.deepcopy(selection) output_dict[selection_path] = copy.deepcopy(selection)
else: else:
...@@ -163,19 +173,25 @@ def generate_test_source_files(config, spec_json, target): ...@@ -163,19 +173,25 @@ def generate_test_source_files(config, spec_json, target):
for selection_path in output_dict: for selection_path in output_dict:
selection = output_dict[selection_path] selection = output_dict[selection_path]
generate_selection(config, generate_selection(config, selection, spec, html_template)
selection,
spec,
html_template)
def main(config): def main(config):
parser = argparse.ArgumentParser(description='Test suite generator utility') parser = argparse.ArgumentParser(
parser.add_argument('-t', '--target', type = str, description='Test suite generator utility')
choices = ("release", "debug"), default = "release", parser.add_argument(
help = 'Sets the appropriate template for generating tests') '-t',
parser.add_argument('-s', '--spec', type = str, default = None, '--target',
help = 'Specify a file used for describing and generating the tests') 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. # TODO(kristijanburnik): Add option for the spec_json file.
args = parser.parse_args() args = parser.parse_args()
......
...@@ -3,10 +3,10 @@ from __future__ import print_function ...@@ -3,10 +3,10 @@ from __future__ import print_function
import os, sys, json, re import os, sys, json, re
script_directory = os.path.dirname(os.path.abspath(__file__)) script_directory = os.path.dirname(os.path.abspath(__file__))
template_directory = os.path.abspath(os.path.join(script_directory, template_directory = os.path.abspath(
'template')) os.path.join(script_directory, 'template'))
test_root_directory = os.path.abspath(os.path.join(script_directory, test_root_directory = os.path.abspath(
'..', '..', '..')) os.path.join(script_directory, '..', '..', '..'))
def get_template(basename): def get_template(basename):
......
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
import os import os
import sys 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 import generate
...@@ -39,7 +43,8 @@ expectation: %(expectation)s ...@@ -39,7 +43,8 @@ expectation: %(expectation)s
self.test_case_name = 'MixedContentTestCase' self.test_case_name = 'MixedContentTestCase'
script_directory = os.path.dirname(os.path.abspath(__file__)) script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(os.path.join(script_directory, '..', '..')) self.spec_directory = os.path.abspath(
os.path.join(script_directory, '..', '..'))
def handleDelivery(self, selection, spec): def handleDelivery(self, selection, spec):
opt_in_method = selection['opt_in_method'] opt_in_method = selection['opt_in_method']
......
...@@ -5,6 +5,7 @@ from __future__ import print_function ...@@ -5,6 +5,7 @@ from __future__ import print_function
import json, sys import json, sys
from common_paths import * from common_paths import *
def assert_non_empty_string(obj, field): def assert_non_empty_string(obj, field):
assert field in obj, 'Missing field "%s"' % field assert field in obj, 'Missing field "%s"' % field
assert isinstance(obj[field], basestring), \ assert isinstance(obj[field], basestring), \
...@@ -71,13 +72,14 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema): ...@@ -71,13 +72,14 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema):
assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key, assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key,
sub_schema) sub_schema)
def validate(spec_json, details): def validate(spec_json, details):
""" Validates the json specification for generating tests. """ """ Validates the json specification for generating tests. """
details['object'] = spec_json details['object'] = spec_json
assert_contains_only_fields(spec_json, ["specification", assert_contains_only_fields(
"test_expansion_schema", spec_json,
"excluded_tests"]) ["specification", "test_expansion_schema", "excluded_tests"])
assert_non_empty_list(spec_json, "specification") assert_non_empty_list(spec_json, "specification")
assert_non_empty_dict(spec_json, "test_expansion_schema") assert_non_empty_dict(spec_json, "test_expansion_schema")
assert_non_empty_list(spec_json, "excluded_tests") assert_non_empty_list(spec_json, "excluded_tests")
...@@ -93,11 +95,10 @@ def validate(spec_json, details): ...@@ -93,11 +95,10 @@ def validate(spec_json, details):
details['object'] = spec details['object'] = spec
# Validate required fields for a single spec. # Validate required fields for a single spec.
assert_contains_only_fields(spec, ['name', assert_contains_only_fields(spec, [
'title', 'name', 'title', 'description', 'specification_url',
'description', 'test_expansion'
'specification_url', ])
'test_expansion'])
assert_non_empty_string(spec, 'name') assert_non_empty_string(spec, 'name')
assert_non_empty_string(spec, 'title') assert_non_empty_string(spec, 'title')
assert_non_empty_string(spec, 'description') assert_non_empty_string(spec, 'description')
...@@ -123,14 +124,10 @@ def validate(spec_json, details): ...@@ -123,14 +124,10 @@ def validate(spec_json, details):
# Validate the test_expansion schema members. # Validate the test_expansion schema members.
details['object'] = test_expansion_schema details['object'] = test_expansion_schema
assert_contains_only_fields(test_expansion_schema, ['expansion', assert_contains_only_fields(test_expansion_schema, [
'source_scheme', 'expansion', 'source_scheme', 'opt_in_method', 'context_nesting',
'opt_in_method', 'redirection', 'subresource', 'origin', 'expectation'
'context_nesting', ])
'redirection',
'subresource',
'origin',
'expectation'])
# Validate excluded tests. # Validate excluded tests.
details['object'] = excluded_tests details['object'] = excluded_tests
for excluded_test_expansion in excluded_tests: for excluded_test_expansion in excluded_tests:
...@@ -139,9 +136,7 @@ def validate(spec_json, details): ...@@ -139,9 +136,7 @@ def validate(spec_json, details):
details['object'] = excluded_test_expansion details['object'] = excluded_test_expansion
for artifact in test_expansion_schema: for artifact in test_expansion_schema:
details['test_expansion_field'] = artifact details['test_expansion_field'] = artifact
assert_valid_artifact( assert_valid_artifact(excluded_test_expansion, artifact,
excluded_test_expansion,
artifact,
test_expansion_schema[artifact]) test_expansion_schema[artifact])
del details['test_expansion_field'] del details['test_expansion_field']
...@@ -159,7 +154,7 @@ def assert_valid_spec_json(spec_json): ...@@ -159,7 +154,7 @@ def assert_valid_spec_json(spec_json):
def main(): def main():
spec_json = load_spec_json(); spec_json = load_spec_json()
assert_valid_spec_json(spec_json) assert_valid_spec_json(spec_json)
print("Spec JSON is valid.") print("Spec JSON is valid.")
......
...@@ -3,9 +3,14 @@ ...@@ -3,9 +3,14 @@
import os import os
import sys 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 import generate
class ReferrerPolicyConfig(object): class ReferrerPolicyConfig(object):
def __init__(self): def __init__(self):
self.selection_pattern = '%(delivery_method)s/' + \ self.selection_pattern = '%(delivery_method)s/' + \
...@@ -34,7 +39,8 @@ the target request is %(origin)s.''' ...@@ -34,7 +39,8 @@ the target request is %(origin)s.'''
self.test_case_name = 'ReferrerPolicyTestCase' self.test_case_name = 'ReferrerPolicyTestCase'
script_directory = os.path.dirname(os.path.abspath(__file__)) script_directory = os.path.dirname(os.path.abspath(__file__))
self.spec_directory = os.path.abspath(os.path.join(script_directory, '..', '..')) self.spec_directory = os.path.abspath(
os.path.join(script_directory, '..', '..'))
def handleDelivery(self, selection, spec): def handleDelivery(self, selection, spec):
delivery_method = selection['delivery_method'] delivery_method = selection['delivery_method']
......
...@@ -5,6 +5,7 @@ from __future__ import print_function ...@@ -5,6 +5,7 @@ from __future__ import print_function
import json, sys import json, sys
from common_paths import * from common_paths import *
def assert_non_empty_string(obj, field): def assert_non_empty_string(obj, field):
assert field in obj, 'Missing field "%s"' % field assert field in obj, 'Missing field "%s"' % field
assert isinstance(obj[field], basestring), \ assert isinstance(obj[field], basestring), \
...@@ -71,14 +72,15 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema): ...@@ -71,14 +72,15 @@ def assert_valid_artifact(exp_pattern, artifact_key, schema):
assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key, assert_valid_artifact(exp_pattern[artifact_key], sub_artifact_key,
sub_schema) sub_schema)
def validate(spec_json, details): def validate(spec_json, details):
""" Validates the json specification for generating tests. """ """ Validates the json specification for generating tests. """
details['object'] = spec_json details['object'] = spec_json
assert_contains_only_fields(spec_json, ["specification", assert_contains_only_fields(spec_json, [
"referrer_policy_schema", "specification", "referrer_policy_schema", "test_expansion_schema",
"test_expansion_schema", "excluded_tests"
"excluded_tests"]) ])
assert_non_empty_list(spec_json, "specification") assert_non_empty_list(spec_json, "specification")
assert_non_empty_list(spec_json, "referrer_policy_schema") assert_non_empty_list(spec_json, "referrer_policy_schema")
assert_non_empty_dict(spec_json, "test_expansion_schema") assert_non_empty_dict(spec_json, "test_expansion_schema")
...@@ -96,12 +98,10 @@ def validate(spec_json, details): ...@@ -96,12 +98,10 @@ def validate(spec_json, details):
details['object'] = spec details['object'] = spec
# Validate required fields for a single spec. # Validate required fields for a single spec.
assert_contains_only_fields(spec, ['name', assert_contains_only_fields(spec, [
'title', 'name', 'title', 'description', 'referrer_policy',
'description', 'specification_url', 'test_expansion'
'referrer_policy', ])
'specification_url',
'test_expansion'])
assert_non_empty_string(spec, 'name') assert_non_empty_string(spec, 'name')
assert_non_empty_string(spec, 'title') assert_non_empty_string(spec, 'title')
assert_non_empty_string(spec, 'description') assert_non_empty_string(spec, 'description')
...@@ -128,14 +128,10 @@ def validate(spec_json, details): ...@@ -128,14 +128,10 @@ def validate(spec_json, details):
# Validate the test_expansion schema members. # Validate the test_expansion schema members.
details['object'] = test_expansion_schema details['object'] = test_expansion_schema
assert_contains_only_fields(test_expansion_schema, ['expansion', assert_contains_only_fields(test_expansion_schema, [
'delivery_method', 'expansion', 'delivery_method', 'redirection', 'origin',
'redirection', 'source_protocol', 'target_protocol', 'subresource', 'referrer_url'
'origin', ])
'source_protocol',
'target_protocol',
'subresource',
'referrer_url'])
# Validate excluded tests. # Validate excluded tests.
details['object'] = excluded_tests details['object'] = excluded_tests
for excluded_test_expansion in excluded_tests: for excluded_test_expansion in excluded_tests:
...@@ -144,9 +140,7 @@ def validate(spec_json, details): ...@@ -144,9 +140,7 @@ def validate(spec_json, details):
details['object'] = excluded_test_expansion details['object'] = excluded_test_expansion
for artifact in test_expansion_schema: for artifact in test_expansion_schema:
details['test_expansion_field'] = artifact details['test_expansion_field'] = artifact
assert_valid_artifact( assert_valid_artifact(excluded_test_expansion, artifact,
excluded_test_expansion,
artifact,
test_expansion_schema[artifact]) test_expansion_schema[artifact])
del details['test_expansion_field'] del details['test_expansion_field']
...@@ -164,7 +158,7 @@ def assert_valid_spec_json(spec_json): ...@@ -164,7 +158,7 @@ def assert_valid_spec_json(spec_json):
def main(): def main():
spec_json = load_spec_json(); spec_json = load_spec_json()
assert_valid_spec_json(spec_json) assert_valid_spec_json(spec_json)
print("Spec JSON is valid.") 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