diff --git a/test/travis.sh b/test/travis.sh index 019c49c5257b..3ba71f82cabf 100755 --- a/test/travis.sh +++ b/test/travis.sh @@ -39,8 +39,8 @@ set -e if [ "$TEST" == "rules" ]; then echo >&2 "Performing comprehensive coverage test." docker run --rm -ti -v $(pwd):/opt httpse python utils/ruleset_filenames_validate.py - docker run --rm -ti -v $(pwd):/opt httpse bash -c "utils/remove-obsolete-references.sh" - docker run --rm -ti -v $(pwd):/opt httpse bash -c "utils/validate.sh" + docker run --rm -ti -v $(pwd):/opt httpse python utils/validate.py + docker run --rm -ti -v $(pwd):/opt httpse python utils/trivial-validate.py --quiet docker run --rm -ti -v $(pwd):/opt httpse bash -c "test/rules.sh" docker run --rm -ti -v $(pwd):/opt node bash -c "cd /opt && node utils/normalize-securecookie.js" [ `git diff --name-only $RULESETFOLDER | wc -l` -eq 0 ] diff --git a/utils/trivial-validate.py b/utils/trivial-validate.py index 87863896a721..1fe9bcbc957c 100755 --- a/utils/trivial-validate.py +++ b/utils/trivial-validate.py @@ -150,6 +150,8 @@ def nomes_all(where=sys.argv[1:]): xpath_from = etree.XPath("/ruleset/rule/@from") xpath_to = etree.XPath("/ruleset/rule/@to") +print("Complicated validation & assertions of rulesets using utils/trivial-validate.py begins...") + host_counter = Counter() for filename in filenames: xml_parser = etree.XMLParser(remove_blank_text=True) @@ -200,4 +202,10 @@ def nomes_all(where=sys.argv[1:]): failure = 1 fail("%s failed: %s" % (host, is_valid_target_host.__doc__)) +if failure != 0: + message = "Complicated validation & assertions of rulesets using utils/trivial-validate.py failed." +else: + message = "Complicated validation & assertions of rulesets using utils/trivial-validate.py succeeded." + +print(message) sys.exit(failure) diff --git a/utils/validate.py b/utils/validate.py new file mode 100644 index 000000000000..4cd5da1560cd --- /dev/null +++ b/utils/validate.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python2.7 + +import argparse +import glob +import os +import unicodedata + +from lxml import etree + +def normalize(f): + """ + OSX and Linux filesystems encode composite characters differently in + filenames. We should normalize to NFC: https://unicode.org/reports/tr15/ + """ + f = unicodedata.normalize("NFC", unicode(f, "utf-8")).encode("utf-8") + return f + +# commandline arguments parsing (nobody use it, though) +parser = argparse.ArgumentParser(description="Validate rulesets against relaxng.xml") +parser.add_argument("--source_dir", default="src/chrome/content/rules") + +args = parser.parse_args() + +# XML ruleset files +files = map(normalize, glob.glob(os.path.join(args.source_dir, "*.xml"))) + +# read the schema file +relaxng_doc = etree.parse('utils/relaxng.xml') +relaxng = etree.RelaxNG(relaxng_doc) + +exit_code = 0 + +print("Validation of rulesets against utils/relaxng.xml begins...") + +for filename in sorted(files): + tree = etree.parse(filename) + + if not relaxng.validate(tree): + exit_code = 1 + e = relaxng.error_log.last_error + print("%s %s:%s:%s: %s" % (e.level_name, e.filename, e.line, e.column, e.message)) + + + +if exit_code == 0: + message = "Validation of rulesets against utils/relaxng.xml succeeded." +else: + message = "\nTwo very common reasons for this are the following:\n" \ + " - missing caret (^) in 'from' attribute: it should be \"^http:\" and not \"http:\"\n" \ + " - missing trailing slashes in 'from' or 'to' when specifying full hostnames: \n" \ + " it should be \"https://eff.org/\" and not \"https://eff.org\"\n\n" \ + "Validation of rulesets against utils/relaxng.xml failed." + +print(message) +exit(exit_code) diff --git a/utils/validate.sh b/utils/validate.sh deleted file mode 100755 index ddad0b70710f..000000000000 --- a/utils/validate.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -# Validates rulesets, using both trivial-validate.py (slower, can make more -# complicated assertions) and xmllint with our custom RELAX NG grammar that -# specifies the format of ruleset XML (faster, more limited). -set -o errexit -o pipefail -cd $(dirname $0) - -# =============== BEGIN VALIDATION ================ -# Unless we're in a hurry, validate the ruleset library & locales - -die() { - echo >&2 "ERROR:" "$@" - exit 1 -} - -if python2.7 trivial-validate.py --quiet >&2 -then - echo Validation of included rulesets completed. >&2 - echo >&2 -else - die "Validation of rulesets failed." -fi - -# Check for xmllint. -type xmllint >/dev/null || die "xmllint not available" - -GRAMMAR="relaxng.xml" -# xmllint spams stderr with " validates, even with the --noout flag, -# so we capture only the results that do not contain 'validates' -validate_grammar() { - find ../src/chrome/content/rules -name "*.xml" | \ - xargs xmllint --noout --relaxng $GRAMMAR -} -grammar_errors=$(validate_grammar 2>&1 | grep -v "validates" || true) -if [ -z "$grammar_errors" ] -then - echo Validation of rulesets against $GRAMMAR succeeded. >&2 -else - echo >&2 "$grammar_errors" - # One very common error is to mess up rule attributes, so we check for - # this explicitly. - if [[ $grammar_errors == *"Element rule failed to validate attributes"* ]] - then - echo "Two very common reasons for this are the following:" - echo "- Missing caret (^) in 'from' attribute: it should be \"^http:\" and not \"http:\"." - echo "- Missing trailing slashes in 'from' or 'to' when specifying full hostnames: it should be \"https://eff.org/\" and not \"https://eff.org\"." - fi - die "Validation of rulesets against $GRAMMAR failed." -fi