def _do_test(xpi_package, allow_old_sdk=True, compat=False): err = ErrorBundle() if compat: err.save_resource('is_compat_test', True) jetpack.inspect_jetpack(err, xpi_package, allow_old_sdk=allow_old_sdk) return err
def test_structure(structure): err = ErrorBundle() err.supported_versions = {} mock_package = MockXPI(structure) content.test_packed_packages(err, mock_package) print err.print_summary(verbose=True) assert err.failed()
def test_notice(): """Tests notice-related functions of the error bundler.""" # Use the StringIO as an output buffer. bundle = ErrorBundle() bundle.notice((), "") # Load the JSON output as an object. output = json.loads(bundle.render_json()) # Run some basic tests assert len(output["messages"]) == 1 print output has_ = False for message in output["messages"]: print message if message["type"] == "notice": has_ = True assert has_ assert not bundle.failed()
def setup_err(manifest_json=None, err=None): if err is None: err = ErrorBundle() if manifest_json is not None: err.save_resource('has_manifest_json', True) err.save_resource('manifest_json', manifest_json) return err
def setup_err(package_json=None, err=None): if err is None: err = ErrorBundle() if package_json is not None: err.save_resource("has_package_json", True) err.save_resource("package_json", package_json) return err
def test_packed_scripts_no_pollution(): """ Test that packed scripts test for pollution without being overzealous. """ x = MockXPI({'foo/bar.js': 'tests/resources/content/pollution_error.js'}) err = ErrorBundle() err.supported_versions = {} c = ChromeManifest(""" content ns jar:subpackage.jar!/ """, 'chrome.manifest') err.save_resource('chrome.manifest_nopush', c, pushable=False) err.save_resource('scripts', [{ 'scripts': ['foo/bar.js'], 'package': x, 'state': ['subpackage', 'subsubpackage'] }]) err.save_resource('marked_scripts', set(['chrome://otherns/foo/bar.js'])) content.test_packed_scripts(err, x) eq_(err.package_stack, []) assert not err.failed()
def test_overlay_object(): """Test that overlay instructions have all its properties.""" err = ErrorBundle() c = chrome_manifest(""" content namespace /foo/bar overlay namespace /uri/goes/here """) err.save_resource('chrome.manifest', c) c.get_applicable_overlays(err) assert not err.failed() assert not err.notices err = ErrorBundle() c = chrome_manifest(""" content namespace /foo/bar overlay /uri/goes/here """) err.save_resource('chrome.manifest', c) c.get_applicable_overlays(err) assert err.failed() assert not err.notices
def test_langpack(): "Tests a language pack in the content validator." err = ErrorBundle(None, True) err.set_type(PACKAGE_LANGPACK) mock_package = MockXPIManager( {"foo.dtd": "tests/resources/content/junk.xpi"}) content.testendpoint_langpack = \ MockTestEndpoint(("test_unsafe_html", )) result = content.test_packed_packages( err, {"foo.dtd": {"extension": "dtd", "name_lower": "foo.dtd"}}, mock_package) print result assert result == 1 content.testendpoint_langpack.assert_expectation( "test_unsafe_html", 1) content.testendpoint_langpack.assert_expectation( "test_unsafe_html", 0, "subpackage")
def test_proper_linked_manifest_relative(): """ Test that linked manifests are imported relatively when using relative paths. """ err = ErrorBundle() package = MockXPI( { "chrome.manifest": "tests/resources/submain/linkman/subdir.manifest", "dir/level2.manifest": "tests/resources/submain/linkman/foosub.manifest", "dir/foo.manifest": "tests/resources/submain/linkman/base2.manifest", } ) submain.populate_chrome_manifest(err, package) chrome = err.get_resource("chrome.manifest") assert chrome assert not err.failed() or err.notices # From the linked manifest: zaps = list(chrome.get_entries("zap")) assert zaps eq_(zaps[0]["filename"], "dir/foo.manifest") eq_(zaps[0]["context"].data, ["zap baz", ""])
def test_webapp_pass(): """Test that a bland webapp file throws no errors.""" err = ErrorBundle() validator.webapp.detect_webapp(err, json.dumps(_get_json())) print err.print_summary(verbose=True) assert not err.failed()
def test_version_forappversions_accepted(): """ Test that for_appversions targets application versions properly. """ err = ErrorBundle() err.supported_versions = {'firefox': ['1.2.3']} tests = decorator.TEST_TIERS decorator.TEST_TIERS = {} @decorator.register_test(tier=5, versions={'firefox': ['1.0.0', '1.2.3']}) def version_test(err, xpi): print 'Ran test' err.save_resource('executed', True) print decorator.TEST_TIERS validator.submain.test_inner_package(err, MockXPI(), for_appversions={'firefox': ['1.2.3']}) assert err.get_resource('executed') decorator.TEST_TIERS = tests
def _test(script): err = ErrorBundle() err.supported_versions = {} err.save_resource("em:bootstrap", "true") scripting.test_js_file(err, "foo", script) return err
def test_packed_scripts_no_pollution(): """ Test that packed scripts test for pollution without being overzealous. """ x = MockXPI({"foo/bar.js": "tests/resources/content/pollution_error.js"}) err = ErrorBundle() err.supported_versions = {} c = ChromeManifest(""" content ns jar:subpackage.jar!/ """, "chrome.manifest") err.save_resource("chrome.manifest_nopush", c, pushable=False) err.save_resource( "scripts", [{"scripts": ["foo/bar.js"], "package": x, "state": ["subpackage", "subsubpackage"]}]) err.save_resource("marked_scripts", set(["chrome://otherns/foo/bar.js"])) content.test_packed_scripts(err, x) eq_(err.package_stack, []) assert not err.failed()
def test_prepare_package_bad_file(): 'Tests that the prepare_package function fails for unknown files' err = ErrorBundle() submain.prepare_package(err, 'tests/resources/main/foo.bar') assert err.failed()
def test_xpi_subpackage(): "XPIs should never be subpackages; only nested extensions" err = ErrorBundle() err.set_type(PACKAGE_EXTENSION) mock_package = MockXPI( {"chrome/package.xpi": "tests/resources/content/subpackage.jar"}) content.testendpoint_validator = \ MockTestEndpoint(("test_package", )) result = content.test_packed_packages( err, mock_package) print result assert result == 1 content.testendpoint_validator.assert_expectation( "test_package", 1) content.testendpoint_validator.assert_expectation( "test_package", 0, "subpackage")
def test_proper_linked_manifest_relative(): """ Test that linked manifests are imported relatively when using relative paths. """ err = ErrorBundle() package = MockXPI({ 'chrome.manifest': 'tests/resources/submain/linkman/subdir.manifest', 'dir/level2.manifest': 'tests/resources/submain/linkman/foosub.manifest', 'dir/foo.manifest': 'tests/resources/submain/linkman/base2.manifest' }) submain.populate_chrome_manifest(err, package) chrome = err.get_resource('chrome.manifest') assert chrome assert not err.failed() or err.notices # From the linked manifest: zaps = list(chrome.get_triples(subject='zap')) assert zaps assert zaps[0]['filename'] == 'dir/foo.manifest' assert zaps[0]['context'].data == ['zap baz', '']
def test_duplicate_files(): """Test that duplicate files in a package are caught.""" err = ErrorBundle() err.save_resource('has_install_rdf', True) packagelayout.test_layout_all(err, MockDupeXPI()) assert err.failed()
def test_jar_subpackage(): "Tests JAR files that are subpackages." err = ErrorBundle() err.set_type(PACKAGE_EXTENSION) err.supported_versions = {"foo": ["1.2.3"]} mock_package = MockXPI( {"chrome/subpackage.jar": "tests/resources/content/subpackage.jar", "subpackage.jar": "tests/resources/content/subpackage.jar"}) content.testendpoint_validator = \ MockTestEndpoint(("test_inner_package", )) result = content.test_packed_packages( err, mock_package) print result assert result == 2 content.testendpoint_validator.assert_expectation( "test_inner_package", 2) content.testendpoint_validator.assert_expectation( "test_inner_package", 2, "subpackage") assert err.supported_versions == {"foo": ["1.2.3"]}
def test_duplicate_files(): """Test that duplicate files in a package are caught.""" err = ErrorBundle() err.save_resource("has_install_rdf", True) packagelayout.test_layout_all(err, MockDupeXPI()) assert err.failed()
def test_jar_nonsubpackage(): "Tests XPI files that are not subpackages." err = ErrorBundle() err.set_type(PACKAGE_MULTI) err.save_resource("is_multipackage", True) mock_package = MockXPI( {"foo.jar": "tests/resources/content/subpackage.jar", "chrome/bar.jar": "tests/resources/content/subpackage.jar"}) content.testendpoint_validator = MockTestEndpoint(("test_inner_package", "test_package")) result = content.test_packed_packages( err, mock_package) print result assert result == 2 content.testendpoint_validator.assert_expectation( "test_package", 2) content.testendpoint_validator.assert_expectation( "test_package", 0, "subpackage")
def test_prepare_package_missing(): 'Tests that the prepare_package function fails when file is not found' err = ErrorBundle() submain.prepare_package(err, 'foo/bar/asdf/qwerty.xyz') assert err.failed()
def test_prepare_package(test_package): """Tests that the prepare_package does not raise any errors when given a valid add-on.""" err = ErrorBundle() submain.prepare_package(err, 'tests/resources/main/foo.xpi') assert not err.failed()
def _test(script): err = ErrorBundle() err.supported_versions = {} err.save_resource('em:bootstrap', 'true') scripting.test_js_file(err, 'foo', script) return err
def validate( path, format="json", approved_applications=os.path.join(os.path.dirname(__file__), "app_versions.json"), determined=True, spidermonkey=False, listed=True, expectation=PACKAGE_ANY, ): """Perform validation in one easy step! format : The format to output the results in approved_applications : Path to the list of approved application versions determined : Whether the validator should continue after a tier fails spidermonkey : Path to the local spidermonkey installation (Default: False) listed : True if the add-on is destined for AMO, false if not expectation : The type of package that should be expected """ # Load up the target applications apps = json.load(open(approved_applications)) validator.testcases.targetapplication.APPROVED_APPLICATIONS = apps bundle = ErrorBundle(listed=listed, determined=determined) if spidermonkey != False: bundle.save_resource("SPIDERMONKEY", spidermonkey) validator.submain.prepare_package(bundle, path, expectation) # Write the results to the pipe formats = {"json": lambda b: b.render_json()} if format is not None: return formats[format](bundle) else: return bundle
def test_blacklisted_files(): """ Tests the validator's ability to hash each individual file and (based on this information) determine whether the addon passes or fails the validation process. """ package_data = open('tests/resources/libraryblacklist/blocked.xpi') package = XPIManager(package_data, mode='r', name='blocked.xpi') err = ErrorBundle() test_content.test_packed_packages(err, package) print err.print_summary() assert err.notices assert not err.failed() eq_( err.metadata.get('identified_files'), { 'test.js': { 'path': 'This file is a false script to facilitate ' 'testing of library blacklisting.' } })
def test_packed_scripts_pollution(): """Test that packed scripts test for pollution properly.""" x = MockXPI({'foo/bar.js': 'tests/resources/content/pollution_error.js'}) err = ErrorBundle() err.supported_versions = {} c = ChromeManifest(""" content ns jar:subpackage.jar!/ """, 'chrome.manifest') err.save_resource('chrome.manifest_nopush', c, pushable=False) err.save_resource('scripts', [{ 'scripts': ['foo/bar.js'], 'package': x, 'state': ['subpackage.jar', 'subsubpackage'] }]) err.save_resource('marked_scripts', set(['chrome://ns/foo/bar.js'])) content.test_packed_scripts(err, x) assert err.package_stack == [] assert err.failed() assert err.warnings assert not err.errors assert err.warnings[0]['file'] == [ 'subpackage.jar', 'subsubpackage', 'foo/bar.js' ]
def test_webapp_pass(): """Test that a bland webapp file throws no errors.""" err = ErrorBundle() _detect(err, _get_json()) print err.print_summary(verbose=True) assert not err.failed()
def test_packed_scripts_pollution(): """Test that packed scripts test for pollution properly.""" x = MockXPI({"foo/bar.js": "tests/resources/content/pollution_error.js"}) err = ErrorBundle() err.supported_versions = {} c = ChromeManifest(""" content ns jar:subpackage.jar!/ """, "chrome.manifest") err.save_resource("chrome.manifest_nopush", c, pushable=False) err.save_resource( "scripts", [{"scripts": ["foo/bar.js"], "package": x, "state": ["subpackage.jar", "subsubpackage"]}]) err.save_resource("marked_scripts", set(["chrome://ns/foo/bar.js"])) content.test_packed_scripts(err, x) eq_(err.package_stack, []) assert err.failed() assert err.warnings assert not err.errors eq_(err.warnings[0]["file"], ['subpackage.jar', 'subsubpackage', 'foo/bar.js'])
def test_downloads_indicator_unused(): """Test that #downloads-indicator is not used.""" err = ErrorBundle(for_appversions=FX27_DEFINITION) csstester.test_css_file(err, "x.css", "#downloads-indicator { display: none; }", 0) assert err.failed() assert err.compat_summary['errors'] == 1
def test_webapp_bad_dev_keys(): """Test that the developer keys are present.""" err = ErrorBundle() data = _get_json() del data["developer"]["name"] _detect(err, data) assert err.failed()
def test_godlikea(): """Test that packages with a godlikea chrome namespaceget rejected.""" err = ErrorBundle() xpi = MockXPI({"chrome/godlikea.jar": True}) packagelayout.test_godlikea(err, xpi) assert err.failed() assert err.errors
def test_downloads_indicator_icon_allowed(): """Test that #downloads-indicator-icon is allowed.""" err = ErrorBundle(for_appversions=FX27_DEFINITION) csstester.test_css_file(err, "x.css", "#downloads-indicator-icon { display: none; }", 0) assert not err.failed() assert all(val == 0 for val in err.compat_summary.values())
def test_webapp_dev_not_dict(): """Test that the developer property must be a dict""" err = ErrorBundle() data = _get_json() data["developer"]["developer"] = "foo" _detect(err, data) assert err.failed()
def test_downloads_indicator_gecko_16(): """Test that #downloads-indicator is allowed for an old Gecko.""" err = ErrorBundle(for_appversions=FX16_DEFINITION) csstester.test_css_file(err, "x.css", "#downloads-indicator { display: none; }", 0) assert not err.failed() assert all(val == 0 for val in err.compat_summary.values())
def test_js_categories_gecko1(): """Test that JS categories raise problems for space-delimited values.""" c = ChromeManifest('category JavaScript global foo bar', 'chrome.manifest') err = ErrorBundle() err.save_resource('chrome.manifest', c) tc_chromemanifest.test_categories(err) assert err.failed()
def _do_test_raw_webextension(manifest, listed=True): err = ErrorBundle(listed=listed) manifest = ManifestJsonParser(err, manifest) err.save_resource('has_manifest_json', True) err.save_resource('manifest_json', manifest) targetapp.test_targetedapplications(err) return err
def test_content_missing_information(): """Test that incomplete information in a content instruction fails.""" err = ErrorBundle() c = ChromeManifest("content foo", "chrome.manifest") err.save_resource("chrome.manifest", c) tc_chromemanifest.test_content_instructions(err) assert err.failed()
def test_content_missing_information(): """Test that incomplete information in a content instruction fails.""" err = ErrorBundle() c = chrome_manifest('content foo') err.save_resource('chrome.manifest', c) tc_chromemanifest.test_content_instructions(err) assert err.failed()
def test_webapp_bad_dev_url(): """Test that the developer keys are correct.""" err = ErrorBundle() data = _get_json() data["developer"]["url"] = "foo" _detect(err, data) assert err.failed()
def test_js_categories_gecko1(): """Test that JS categories raise problems for space-delimited values.""" c = ChromeManifest("category JavaScript global foo bar", "chrome.manifest") err = ErrorBundle() err.save_resource("chrome.manifest", c) tc_chromemanifest.test_categories(err) assert err.failed()
def test_search_pass(): "Tests the submain test_search function with passing data." err = ErrorBundle() submain.detect_opensearch = lambda err, x, listed: err submain.test_search(err, None, PACKAGE_ANY) assert not err.failed()
def test_search_bad_type(): "Tests the submain test_search function with a bad package type." err = ErrorBundle() submain.detect_opensearch = lambda err, x, listed: err submain.test_search(err, None, PACKAGE_THEME) assert err.failed()
def test_prepare_package_bad_file(): "Tests that the prepare_package function fails for unknown files" err = ErrorBundle(None, True) submain.prepare_package(err, "tests/resources/main/foo.bar") assert err.failed() assert err.reject
def test_prepare_package_missing(): "Tests that the prepare_package function fails when file is not found" err = ErrorBundle(None, True) submain.prepare_package(err, "foo/bar/asdf/qwerty.xyz") assert err.failed() assert err.reject
def test_godlikea(): """Test that packages with a godlikea chrome namespaceget rejected.""" err = ErrorBundle() xpi = MockXPI({'chrome/godlikea.jar': True}) packagelayout.test_godlikea(err, xpi) assert err.failed() assert err.errors
def _do_test(path): script = validator.unicodehelper.decode(open(path, 'rb').read()) print script.encode('ascii', 'replace') err = ErrorBundle(instant=True) err.supported_versions = {} err.handler = OutputHandler(sys.stdout, False) validator.testcases.scripting.test_js_file(err, path, script) return err
def test_search_security_error(): """Test that the DefusedXmlException is handled as a security error.""" err = ErrorBundle() url = "tests/resources/searchprovider/lol.xml" detect_opensearch(err, url, listed=False) assert err.failed(), "Expected failure" assert err.errors[0]['id'] == ('opensearch', 'security_error')
def test_search_failure_undecided(): "Tests the submain test_search function with an unrejected fail case" err = ErrorBundle() submain.detect_opensearch = lambda err, x, listed: err.error( ("foo", ), "Test") submain.test_search(err, None, PACKAGE_ANY) assert err.failed()
def test_type(): """ Test that detected type is being stored properly in the error bundle. """ bundle = ErrorBundle() bundle.detected_type = 5 assert bundle.detected_type == 5
def test_pass(): """Test that standard category subjects pass.""" c = chrome_manifest('category foo bar') err = ErrorBundle() err.save_resource('chrome.manifest', c) tc_chromemanifest.test_categories(err) assert not err.failed()
def test_js_categories_gecko2(): """Test that JS categories raise problems for hyphenated values.""" c = ChromeManifest("category JavaScript-DOM-class foo bar", "chrome.manifest") err = ErrorBundle() err.save_resource("chrome.manifest", c) tc_chromemanifest.test_categories(err) assert err.failed()
def test_pass(): """Test that standard category subjects pass.""" c = ChromeManifest("category foo bar", "chrome.manifest") err = ErrorBundle() err.save_resource("chrome.manifest", c) tc_chromemanifest.test_categories(err) assert not err.failed()