Browse files

Don't wonk out on big add-ons (bug 776017)

  • Loading branch information...
1 parent d59b6c9 commit 6635895bc2d1011b4e34d6a10593334f5cb988c5 @mattbasta committed Oct 9, 2012
Showing with 57 additions and 1 deletion.
  1. +40 −0 tests/js/test_traversal.py
  2. +17 −1 validator/testcases/content.py
View
40 tests/js/test_traversal.py
@@ -1,3 +1,10 @@
+from itertools import imap, repeat
+
+from mock import patch
+
+from validator.testcases import content
+
+from tests.helper import MockXPI
from tests.js_helper import TestCase
@@ -71,3 +78,36 @@ def test_nested_functions(self):
""")
self.assert_var_eq("foo", "second")
+
+class TestTooMuchJS(TestCase):
+ """
+ If you have an add-on and you trigger this code, then you're probably doing
+ it wrong. This tests that any add-on with more than 1000 JS files in any
+ given XPI or JAR do not perform exhaustive validation.
+ """
+
+ @patch('validator.testcases.content.run_regex_tests', lambda *a, **kw: None)
+ @patch('validator.testcases.scripting.test_js_file', lambda *a, **kw: None)
+ def run_mocked_scripts(self, count):
+ self.setup_err()
+
+ scripts = dict(zip(imap(lambda i: "s%d.js" % i, xrange(count)),
+ repeat("tests/resources/content/regex_error.js")))
+
+ x = MockXPI(scripts)
+
+ self.err.save_resource(
+ "scripts",
+ [{"scripts": scripts.keys(),
+ "package": x,
+ "state": []}])
+
+ content.test_packed_scripts(self.err, x)
+
+ def test_few_js(self):
+ self.run_mocked_scripts(900)
+ self.assert_silent()
+
+ def test_many_js(self):
+ self.run_mocked_scripts(1001)
+ self.assert_failed(with_warnings=True)
View
18 validator/testcases/content.py
@@ -201,6 +201,17 @@ def test_packed_scripts(err, xpi_package):
if not scripts:
return
+ total_scripts = sum(len(bundle["scripts"]) for bundle in scripts)
+ exhaustive = True
+ if total_scripts > 1000:
+ err.warning(
+ err_id=("testcases_content", "packed_js", "too_much_js"),
+ warning="TOO MUCH JS FOR EXHAUSTIVE VALIDATION",
@cvan
cvan added a line comment Oct 9, 2012

consider lowercase

@mattbasta
Owner
mattbasta added a line comment Oct 9, 2012

See bug 778499

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ description="There are too many JS files for the validator to "
+ "process sequentially. An editor must manually "
+ "review the JS in this add-on.")
+ exhaustive = False
+
# Get the chrome manifest in case there's information about pollution
# exemptions.
chrome = err.get_resource("chrome.manifest_nopush")
@@ -221,6 +232,12 @@ def test_packed_scripts(err, xpi_package):
for script in script_bundle["scripts"]:
file_data = unicodehelper.decode(package.read(script))
+ run_regex_tests(file_data, err, script, is_js=True)
+ # If we're not running an exhaustive set of tests, skip the full JS
+ # parse and traversal.
+ if not exhaustive:
+ continue
+
if marked_scripts:
reversed_script = chrome.reverse_lookup(script_bundle["state"],
script)
@@ -232,7 +249,6 @@ def test_packed_scripts(err, xpi_package):
else:
# Run the standard script tests on the scripts.
testendpoint_js.test_js_file(err, script, file_data)
- run_regex_tests(file_data, err, script, is_js=True)
for i in range(len(script_bundle["state"])):
err.pop_state()

0 comments on commit 6635895

Please sign in to comment.