Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(618)

Unified Diff: tools/origin_trials/validate_subdomain_origin/test_validate.py

Issue 2456053004: Validate origins when generating subdomain tokens (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
+ print
+ 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)
+ print
+ continue
+
+ if failed_tests > 0:
+ print "Failed %d tests" % failed_tests
+ print
+ sys.exit(1)
+
+ print "All tests passed"
+
+if __name__ == "__main__":
+ main()

Powered by Google App Engine
This is Rietveld 408576698