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 STATUS_VALID = 0 |
| 19 STATUS_INVALID_ORIGIN = 1 |
| 20 STATUS_IN_PUBLIC_SUFFIX_LIST = 2 |
| 21 STATUS_ERROR = 3 |
| 22 STATUS_IP_ADDRESS = 4 |
| 23 |
| 24 TestOrigins = { |
| 25 'https//': STATUS_INVALID_ORIGIN, |
| 26 'https://example.com:xx': STATUS_INVALID_ORIGIN, |
| 27 'https://google.com': STATUS_VALID, |
| 28 'google.com': STATUS_VALID, |
| 29 'http://10.0.0.1': STATUS_IP_ADDRESS, |
| 30 '10.0.0.1': STATUS_IP_ADDRESS, |
| 31 'https://com': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 32 'https://com:443': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 33 'com': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 34 'co.uk': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 35 'github.io': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 36 'githubusercontent.com': STATUS_IN_PUBLIC_SUFFIX_LIST, |
| 37 'https://adsf': STATUS_VALID, |
| 38 'adsf': STATUS_VALID, |
| 39 } |
| 40 |
| 41 # Default utility path, relative to script_dir. |
| 42 # - Assumes utility is compiled to standard output directory, e.g. |
| 43 # <chromium dir>/src/out/Default |
| 44 DEFAULT_UTILITY_PATH = '../../../out/Default/validate_subdomain_origin' |
| 45 |
| 46 def main(): |
| 47 default_utility_path = os.path.join(script_dir, DEFAULT_UTILITY_PATH) |
| 48 |
| 49 parser = argparse.ArgumentParser( |
| 50 description="Test the validate_subdomain_origin utility") |
| 51 parser.add_argument("--utility-path", |
| 52 help="Path to the compiled utility", |
| 53 default=default_utility_path) |
| 54 |
| 55 args = parser.parse_args() |
| 56 |
| 57 utility_path = os.path.expanduser(args.utility_path) |
| 58 if not os.path.exists(utility_path): |
| 59 print "ERROR" |
| 60 print "Utility not found at: %s" % utility_path |
| 61 print |
| 62 sys.exit(1) |
| 63 |
| 64 print "Using compiled utility found at: %s" % utility_path |
| 65 print |
| 66 |
| 67 failed_tests = 0 |
| 68 |
| 69 # Test handling of number of arguments |
| 70 no_args_rc = subprocess.call(utility_path) |
| 71 if no_args_rc != STATUS_ERROR: |
| 72 failed_tests += 1 |
| 73 print "Test failed for no arguments: expected %d, actual %d" % ( |
| 74 STATUS_ERROR, no_args_rc) |
| 75 |
| 76 too_many_args_rc = subprocess.call([utility_path, "first", "second"]) |
| 77 if too_many_args_rc != STATUS_ERROR: |
| 78 failed_tests += 1 |
| 79 print "Test failed for 2 arguments: expected %d, actual %d" % ( |
| 80 STATUS_ERROR, too_many_args_rc) |
| 81 |
| 82 # Test validation of various origins, and formats |
| 83 for origin, expected_result in TestOrigins.items(): |
| 84 rc = subprocess.call([utility_path, origin]) |
| 85 if rc != expected_result: |
| 86 failed_tests += 1 |
| 87 print "Test failed for '%s': expected %d, actual %d" % ( |
| 88 origin, expected_result, rc) |
| 89 print |
| 90 continue |
| 91 |
| 92 if failed_tests > 0: |
| 93 print "Failed %d tests" % failed_tests |
| 94 print |
| 95 sys.exit(1) |
| 96 |
| 97 print "All tests passed" |
| 98 |
| 99 if __name__ == "__main__": |
| 100 main() |
OLD | NEW |