Skip to content

Commit

Permalink
Don't wonk out on big add-ons (bug 776017)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbasta committed Oct 9, 2012
1 parent d59b6c9 commit 6635895
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
40 changes: 40 additions & 0 deletions 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 from tests.js_helper import TestCase




Expand Down Expand Up @@ -71,3 +78,36 @@ def test_nested_functions(self):
""") """)
self.assert_var_eq("foo", "second") 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)
18 changes: 17 additions & 1 deletion validator/testcases/content.py
Expand Up @@ -201,6 +201,17 @@ def test_packed_scripts(err, xpi_package):
if not scripts: if not scripts:
return 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",

This comment has been minimized.

Copy link
@cvan

cvan Oct 9, 2012

Contributor

consider lowercase

This comment has been minimized.

Copy link
@mattbasta

mattbasta Oct 9, 2012

Author Owner

See bug 778499

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 # Get the chrome manifest in case there's information about pollution
# exemptions. # exemptions.
chrome = err.get_resource("chrome.manifest_nopush") chrome = err.get_resource("chrome.manifest_nopush")
Expand All @@ -221,6 +232,12 @@ def test_packed_scripts(err, xpi_package):
for script in script_bundle["scripts"]: for script in script_bundle["scripts"]:
file_data = unicodehelper.decode(package.read(script)) 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: if marked_scripts:
reversed_script = chrome.reverse_lookup(script_bundle["state"], reversed_script = chrome.reverse_lookup(script_bundle["state"],
script) script)
Expand All @@ -232,7 +249,6 @@ def test_packed_scripts(err, xpi_package):
else: else:
# Run the standard script tests on the scripts. # Run the standard script tests on the scripts.
testendpoint_js.test_js_file(err, script, file_data) 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"])): for i in range(len(script_bundle["state"])):
err.pop_state() err.pop_state()
Expand Down

0 comments on commit 6635895

Please sign in to comment.