def test_get_applications(): """Test the get_applications method on the RDFParser.""" parser = RDFParser(None, open('tests/resources/installrdf/pass.rdf')) applications = parser.get_applications() assert applications == [{ 'guid': u'{ec8030f7-c20a-464f-9b0e-13a3a9e97384}', 'min_version': u'3.7a5pre', 'max_version': u'0.3'}]
def test_namespacing(): """Tests that the RDF parser successfully creates namespaces.""" r = RDFParser(None, open("tests/resources/rdf/pass.rdf"), "foo") assert r.namespace == "foo" assert str(r.uri("bar")) == "foo#bar" assert str(r.uri("bar", "abc")) == "abc#bar"
def test_namespacing(): """Tests that the RDF parser successfully creates namespaces.""" r = RDFParser(None, open('tests/resources/rdf/pass.rdf'), 'foo') assert r.namespace == 'foo' assert str(r.uri('bar')) == 'foo#bar' assert str(r.uri('bar', 'abc')) == 'abc#bar'
def test_namespacing(): """Tests that the RDF parser successfully creates namespaces.""" r = RDFParser(_load_rdf("tests/resources/rdf/pass.rdf"), "foo") assert r.namespace == "foo" assert str(r.uri("bar")) == "foo#bar" assert str(r.uri("bar", "abc")) == "abc#bar"
def _do_test_raw(rdf, listed=True, overrides=None): err = ErrorBundle(listed=listed) err.overrides = overrides rdf = RDFParser(err, rdf.strip()) err.save_resource('has_install_rdf', True) err.save_resource('install_rdf', rdf) targetapp.test_targetedapplications(err) return err
def _do_test_raw(rdf, listed=True, overrides=None): err = ErrorBundle(listed=listed) err.overrides = overrides rdf = RDFParser(rdf.strip()) err.save_resource("has_install_rdf", True) err.save_resource("install_rdf", rdf) targetapp.test_targetedapplications(err) print err.print_summary() return err
def test_get_root_subject(): 'Tests the integrity of the get_root_subject() function' r = RDFParser(None, open('tests/resources/rdf/pass.rdf')) type_uri = r.uri('type') emtype = r.get_object(None, type_uri) assert emtype is not None emtype = r.get_object(r.get_root_subject(), type_uri) assert emtype is not None
def test_get_root_subject(): "Tests the integrity of the get_root_subject() function" r = RDFParser(_load_rdf("tests/resources/rdf/pass.rdf")) type_uri = r.uri("type") emtype = r.get_object(None, type_uri) assert emtype is not None emtype = r.get_object(r.get_root_subject(), type_uri) assert emtype is not None
def test_get_object(): """"Tests the integrity of the get_object() and get_objects() functions.""" r = RDFParser(_load_rdf("tests/resources/rdf/pass.rdf")) test_uri = r.uri("test") emtest = r.get_object(None, test_uri) assert emtest is not None emtests = r.get_objects(None, test_uri) assert len(emtests) == 3 assert emtests[0] == emtest
def test_get_object(): """Tests the integrity of the get_object() and get_objects() functions.""" r = RDFParser(None, open('tests/resources/rdf/pass.rdf')) test_uri = r.uri('test') emtest = r.get_object(None, test_uri) assert emtest is not None emtests = r.get_objects(None, test_uri) assert len(emtests) == 3 assert emtests[0] == emtest
def _do_test(path, test, failure=True, require_install=False, set_type=0, listed=False, xpi_mode='r'): package_data = open(path, 'rb') package = XPIManager(package_data, mode=xpi_mode, name=path) err = ErrorBundle() if listed: err.save_resource('listed', True) # Populate in the dependencies. if set_type: err.detected_type = set_type # Conduit test requires type if require_install: err.save_resource('has_install_rdf', True) rdf_data = package.read('install.rdf') install_rdf = RDFParser(err, rdf_data) err.save_resource('install_rdf', install_rdf) populate_chrome_manifest(err, package) test(err, package) print err.print_summary(verbose=True) if failure: assert err.failed() else: assert not err.failed() return err
def _run_test_raw(data, failure=True, detected_type=0, listed=True, overrides=None, compat=False): 'Runs a test on an install.rdf snippet' data = data.strip() err = ErrorBundle() err.detected_type = detected_type err.save_resource('listed', listed) err.overrides = overrides if compat: err.save_resource('is_compat_test', True) err.save_resource('has_install_rdf', True) err.save_resource('install_rdf', RDFParser(err, data)) installrdf.test_install_rdf_params(err) print err.print_summary(verbose=True) if failure: # pragma: no cover assert err.failed() or err.notices else: assert not err.failed() and not err.notices return err
def test_billion_laughs_fail(): """Test that the parsing fails for XML will a billion laughs attack.""" xml = """<?xml version="1.0"?> <!DOCTYPE lolz [ <!ENTITY lol "lol"> <!ELEMENT Description (#PCDATA)> <!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"> <!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;"> <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;"> <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;"> <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;"> <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;"> <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;"> <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;"> <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;"> ]> <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"> <Description about="urn:mozilla:install-manifest"> <em:id>&lol9;@basta.mozilla.com</em:id> <em:version>1.2.3.4</em:version> <em:name>foo bar</em:name> <em:targetApplication> <Description> <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> <em:minVersion>3.7a5pre</em:minVersion> <em:maxVersion>0.3</em:maxVersion> </Description> </em:targetApplication> <em:optionsType>2</em:optionsType> </Description> </RDF> """ RDFParser(ErrorBundle(), xml)
def _load_install_rdf(err, package, expectation): try: install_rdf = RDFParser(err, package.read("install.rdf")) except (RDFException, DefusedXmlException) as ex: if isinstance(ex, DefusedXmlException): url = "https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors" reason = "Malicious XML was detected, see {0}.".format(url) line = 0 else: reason = ("Try validating your RDF with the W3 validator: " "http://www.w3.org/RDF/Validator/.") line = ex.line() err.error(err_id=("main", "test_package", "parse_error"), error="Could not parse `install.rdf`.", description=[ "The RDF parser was unable to parse the " "install.rdf file included with this add-on.", reason ], filename="install.rdf", line=line) return else: if install_rdf.rdf is None: err.error(err_id=("main", "test_package", "cannot_parse_installrdf"), error="Cannot read `install.rdf`", description="The install.rdf file could not be parsed.", filename="install.rdf") return else: err.save_resource("has_install_rdf", True, pushable=True) err.save_resource("install_rdf", install_rdf, pushable=True) # Load up the results of the type detection results = detect_type(err, install_rdf, package) if results is None: err.error( err_id=("main", "test_package", "undeterminable_type"), error="Unable to determine add-on type", description="The type detection algorithm could not determine " "the type of the add-on.") return else: err.detected_type = results # Compare the results of the low-level type detection to # that of the expectation and the assumption. if not expectation in (PACKAGE_ANY, results): err.warning(err_id=("main", "test_package", "extension_type_mismatch"), warning="Extension Type Mismatch", description=[ "We detected that the add-on's type does not match " "the expected type.", 'Type "%s" expected, found "%s"' % (types[expectation], types[results]) ])
def _load_install_rdf(err, package, expectation): try: install_rdf = RDFParser(err, package.read('install.rdf')) except (RDFException, DefusedXmlException) as ex: if isinstance(ex, DefusedXmlException): url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors' reason = 'Malicious XML was detected, see {0}.'.format(url) line = 0 else: reason = ('Try validating your RDF with the W3 validator: ' 'http://www.w3.org/RDF/Validator/.') line = ex.line() err.error(err_id=('main', 'test_package', 'parse_error'), error='Could not parse `install.rdf`.', description=('The RDF parser was unable to parse the ' 'install.rdf file included with this add-on.', reason), filename='install.rdf', line=line) return else: if install_rdf.rdf is None: err.error(err_id=('main', 'test_package', 'cannot_parse_installrdf'), error='Cannot read `install.rdf`', description='The install.rdf file could not be parsed.', filename='install.rdf') return else: err.save_resource('has_install_rdf', True, pushable=True) err.save_resource('install_rdf', install_rdf, pushable=True) # Load up the results of the type detection results = detect_type(err, install_rdf, package) if results is None: err.error( err_id=('main', 'test_package', 'undeterminable_type'), error='Unable to determine add-on type', description='The type detection algorithm could not determine ' 'the type of the add-on.') return else: err.detected_type = results # Compare the results of the low-level type detection to # that of the expectation and the assumption. if expectation not in (PACKAGE_ANY, results): err.warning( err_id=('main', 'test_package', 'extension_type_mismatch'), warning='Extension Type Mismatch', description=("We detected that the add-on's type does not match " 'the expected type.', 'Type "%s" expected, found "%s"' % (types[expectation], types[results])))
def test_manifest_ordering(): err = ErrorBundle(listed=True) manifest = ManifestJsonParser(err, INVALID_MANIFEST_JSON) err.save_resource('has_manifest_json', True) err.save_resource('manifest_json', manifest) rdf = RDFParser(err, VALID_INSTALL_RDF.strip()) err.save_resource('has_install_rdf', True) err.save_resource('install_rdf', rdf) targetapp.test_targetedapplications(err) assert not err.failed()
def test_has_rdf(install_rdf): "Tests that tests won't be run if there's no install.rdf" err = ErrorBundle() assert installrdf.test_install_rdf_params(err, None) is None err.detected_type = 0 err.save_resource('install_rdf', RDFParser(err, '<rdf></rdf>')) err.save_resource('has_install_rdf', True) installrdf.test_install_rdf_params(err, None) assert install_rdf.called
def _test_type(file_, expectation, failure=False): 'Tests a file against the expectations' err = ErrorBundle(None, True) package = XPIManager(open(file_), mode='r', name=file_) contents = package.package_contents() # We need to have an install.rdf. assert 'install.rdf' in contents # Load up the install.rdf into an RDFParser install_file = package.read('install.rdf') install_rdf = RDFParser(err, install_file) results = typedetection.detect_type(err, install_rdf, package) assert results == expectation if not failure: assert not err.failed() else: assert err.failed() return err
def testopen(): """Tests that the RDF parser is capable of loading an RDF file successfully.""" r = RDFParser(None, open('tests/resources/rdf/pass.rdf')) assert r.rdf
def test_load_rdf_stringio(): """Tests that the RDF parser is capable of loading an RDF file from a StringIO object successfully.""" r = RDFParser(None, StringIO(open('tests/resources/rdf/pass.rdf').read())) assert r.rdf
def test_load_bad(): """Tests that the RDF parser throws an error for invalid, damaged, or corrupt RDF files.""" r = RDFParser(None, open('tests/resources/rdf/fail.rdf')) assert not r.rdf