Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Tests for the validate_subdomain_origin utility | |
| 7 | |
| 8 usage: test_validate.py [-h] [--utility-path UTILITY_PATH] | |
| 9 | |
| 10 """ | |
| 11 import argparse | |
| 12 import os | |
| 13 import subprocess | |
| 14 import sys | |
| 15 | |
| 16 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
| 17 | |
| 18 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
| |
| 19 'http': 1, | |
| 20 'https': 1, | |
| 21 'https//': 1, | |
| 22 'https://example.com:xx': 1, | |
| 23 'https://google.com': 0, | |
| 24 'google.com': 0, | |
| 25 'http://10.0.0.1': 0, | |
| 26 '10.0.0.1': 0, | |
| 27 'https://com': 2, | |
| 28 'https://com:443': 2, | |
| 29 'com': 2, | |
| 30 'co.uk': 2, | |
| 31 'github.io': 2, | |
| 32 'githubusercontent.com': 2, | |
| 33 'https://adsf': 0, | |
| 34 'adsf': 0, | |
| 35 } | |
| 36 | |
| 37 # Default utility path, relative to script_dir. | |
| 38 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'
| |
| 39 | |
| 40 def main(): | |
| 41 default_utility_path = os.path.join(script_dir, DEFAULT_UTILITY_PATH) | |
| 42 | |
| 43 parser = argparse.ArgumentParser( | |
| 44 description="Test the validate_subdomain_origin utility") | |
| 45 parser.add_argument("--utility-path", | |
| 46 help="Path to the compiled utility", | |
| 47 default=default_utility_path) | |
| 48 | |
| 49 args = parser.parse_args() | |
| 50 | |
| 51 if not os.path.exists(args.utility_path): | |
| 52 print "ERROR" | |
| 53 print "Utility not found at: %s" % args.utility_path | |
| 54 print | |
| 55 sys.exit(1) | |
| 56 | |
| 57 failed_tests = 0 | |
| 58 | |
| 59 # Test handling of number of arguments | |
| 60 no_args_rc = subprocess.call(args.utility_path) | |
| 61 if no_args_rc != 3: | |
| 62 failed_tests += 1 | |
| 63 print "Test failed for no arguments: expected %d, actual %d" % ( | |
| 64 3, no_args_rc) | |
| 65 | |
| 66 too_many_args_rc = subprocess.call([args.utility_path, "first", "second"]) | |
| 67 if too_many_args_rc != 3: | |
| 68 failed_tests += 1 | |
| 69 print "Test failed for 2 arguments: expected %d, actual %d" % ( | |
| 70 3, too_many_args_rc) | |
| 71 | |
| 72 # Test validation of various origins, and formats | |
| 73 for origin, expected_result in TestOrigins.items(): | |
| 74 rc = subprocess.call([args.utility_path, origin]) | |
| 75 if rc != expected_result: | |
| 76 failed_tests += 1 | |
| 77 print "Test failed for '%s': expected %d, actual %d" % ( | |
| 78 origin, expected_result, rc) | |
| 79 print | |
| 80 continue | |
| 81 | |
| 82 if failed_tests > 0: | |
| 83 print "Failed %d tests" % failed_tests | |
| 84 print | |
| 85 sys.exit(1) | |
| 86 | |
| 87 print "All tests passed" | |
| 88 | |
| 89 if __name__ == "__main__": | |
| 90 main() | |
| OLD | NEW |