Chromium Code Reviews| Index: tools/origin_trials/validate_subdomain_origin/test_validate.py |
| diff --git a/tools/origin_trials/validate_subdomain_origin/test_validate.py b/tools/origin_trials/validate_subdomain_origin/test_validate.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..cca56adecfad8351a9f31948f69b5139e37faacc |
| --- /dev/null |
| +++ b/tools/origin_trials/validate_subdomain_origin/test_validate.py |
| @@ -0,0 +1,90 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Tests for the validate_subdomain_origin utility |
| + |
| +usage: test_validate.py [-h] [--utility-path UTILITY_PATH] |
| + |
| +""" |
| +import argparse |
| +import os |
| +import subprocess |
| +import sys |
| + |
| +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| + |
| +TestOrigins = { |
|
iclelland
2016/11/02 15:25:47
Can we document what these expected return values
chasej
2016/11/03 19:23:40
Done. Declared constants to document the expected
|
| + 'http': 1, |
| + 'https': 1, |
| + 'https//': 1, |
| + 'https://example.com:xx': 1, |
| + 'https://google.com': 0, |
| + 'google.com': 0, |
| + 'http://10.0.0.1': 0, |
| + '10.0.0.1': 0, |
| + 'https://com': 2, |
| + 'https://com:443': 2, |
| + 'com': 2, |
| + 'co.uk': 2, |
| + 'github.io': 2, |
| + 'githubusercontent.com': 2, |
| + 'https://adsf': 0, |
| + 'adsf': 0, |
| +} |
| + |
| +# Default utility path, relative to script_dir. |
| +DEFAULT_UTILITY_PATH = '../bin/validate_subdomain_origin' |
|
iclelland
2016/11/02 15:25:47
Same question as in generate_token.py (but moreso
chasej
2016/11/03 19:23:40
The assumption is that each script is run from it'
|
| + |
| +def main(): |
| + default_utility_path = os.path.join(script_dir, DEFAULT_UTILITY_PATH) |
| + |
| + parser = argparse.ArgumentParser( |
| + description="Test the validate_subdomain_origin utility") |
| + parser.add_argument("--utility-path", |
| + help="Path to the compiled utility", |
| + default=default_utility_path) |
| + |
| + args = parser.parse_args() |
| + |
| + if not os.path.exists(args.utility_path): |
| + print "ERROR" |
| + print "Utility not found at: %s" % args.utility_path |
| + sys.exit(1) |
| + |
| + failed_tests = 0 |
| + |
| + # Test handling of number of arguments |
| + no_args_rc = subprocess.call(args.utility_path) |
| + if no_args_rc != 3: |
| + failed_tests += 1 |
| + print "Test failed for no arguments: expected %d, actual %d" % ( |
| + 3, no_args_rc) |
| + |
| + too_many_args_rc = subprocess.call([args.utility_path, "first", "second"]) |
| + if too_many_args_rc != 3: |
| + failed_tests += 1 |
| + print "Test failed for 2 arguments: expected %d, actual %d" % ( |
| + 3, too_many_args_rc) |
| + |
| + # Test validation of various origins, and formats |
| + for origin, expected_result in TestOrigins.items(): |
| + rc = subprocess.call([args.utility_path, origin]) |
| + if rc != expected_result: |
| + failed_tests += 1 |
| + print "Test failed for '%s': expected %d, actual %d" % ( |
| + origin, expected_result, rc) |
| + continue |
| + |
| + if failed_tests > 0: |
| + print "Failed %d tests" % failed_tests |
| + sys.exit(1) |
| + |
| + print "All tests passed" |
| + |
| +if __name__ == "__main__": |
| + main() |