def test_compress_unpacked(self): input_path = self.FILES["unpacked"]["path"] output_path = gf.tmp_directory() cont = Container(output_path, ContainerFormat.UNPACKED) cont.compress(input_path) self.assertFalse(os.path.isfile(output_path)) copy = Container(output_path, ContainerFormat.UNPACKED) self.assertEqual(copy.entries, self.EXPECTED_ENTRIES) gf.delete_directory(output_path)
def test_decompress(self): for key in self.FILES: output_path = gf.tmp_directory() f = self.FILES[key] cont = Container(f["path"]) cont.decompress(output_path) copy = Container(output_path, ContainerFormat.UNPACKED) self.assertEqual(copy.entries, self.EXPECTED_ENTRIES) gf.delete_directory(output_path)
def test_decompress(self): output_path = tempfile.mkdtemp() for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) cont.decompress(output_path) copy = Container(output_path, ContainerFormat.UNPACKED) self.assertEqual(copy.entries(), self.EXPECTED_ENTRIES) delete_directory(output_path)
def test_compress_file(self): input_path = self.FILES["unpacked"]["path"] for key in self.FILES: fmt = self.FILES[key]["format"] if fmt != ContainerFormat.UNPACKED: handler, output_path = gf.tmp_file(suffix="." + fmt) cont = Container(output_path, fmt) cont.compress(input_path) self.assertTrue(os.path.isfile(output_path)) copy = Container(output_path, fmt) self.assertEqual(copy.entries, self.EXPECTED_ENTRIES) gf.delete_file(handler, output_path)
def test_wizard_analyze_valid(self): f = self.FILES[0] analyzer = AnalyzeContainer( Container(gf.absolute_path(f["path"], __file__))) job = analyzer.analyze(config_string=self.CONFIG_STRING) self.assertIsNotNone(job) self.assertEqual(len(job), f["length"])
def test_is_entry_safe_true(self): cont = Container(self.FILES["unpacked"]["path"]) for entry in [ "foo", "foo/bar", "foo/../bar", "foo/../bar/baz", "foo/../bar/../baz", "./foo", "./foo/bar", "foo/./bar" ]: self.assertTrue(cont.is_entry_safe(entry))
def test_find_entry_existing_not_exact(self): entry = "p001.xhtml" for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) self.assertFalse(cont.find_entry(entry, exact=True)) self.assertTrue(cont.find_entry(entry, exact=False))
def test_read_entry_missing(self): entry = "config_not_existing.txt" for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) result = cont.read_entry(entry) self.assertIsNone(result)
def test_find_entry_missing(self): entry = "config_not_existing.txt" for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) self.assertFalse(cont.find_entry(entry, exact=True)) self.assertFalse(cont.find_entry(entry, exact=False))
def test_constructor(self): for key in self.FILES: f = self.FILES[key] file_path = f["path"] container_format = f["format"] cont = Container(file_path, container_format) self.assertEqual(cont.file_path, file_path) self.assertEqual(cont.container_format, container_format)
def test_is_entry_safe_false(self): cont = Container(self.FILES["unpacked"]["path"]) for entry in [ "../foo", "/foo", "foo/../../../../../../../../../../../../bar", "foo/../../../../../bar/../../../../../../baz" ]: self.assertFalse(cont.is_entry_safe(entry))
def test_read_entry_existing(self): entry = "config.txt" for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) result = cont.read_entry(entry) self.assertIsNotNone(result) self.assertEqual(len(result), f["config_size"])
def check_container(self, container_path, container_format=None, config_string=None): """ Check whether the given container is well-formed. :param string container_path: the path of the container to be checked :param container_format: the format of the container :type container_format: :class:`~aeneas.container.ContainerFormat` :param string config_string: the configuration string generated by the wizard :rtype: :class:`~aeneas.validator.ValidatorResult` """ self.log([u"Checking container '%s'", container_path]) self.result = ValidatorResult() if self._are_safety_checks_disabled(u"check_container"): return self.result if not (gf.file_exists(container_path) or gf.directory_exists(container_path)): self._failed(u"Container '%s' not found." % container_path) return self.result container = Container(container_path, container_format) try: self.log(u"Checking container has config file") if config_string is not None: self.log(u"Container with config string from wizard") self.check_config_txt(config_string, is_config_string=True) elif container.has_config_xml: self.log(u"Container has XML config file") contents = container.read_entry(container.entry_config_xml) if contents is None: self._failed(u"Unable to read the contents of XML config file.") return self.result self.check_config_xml(contents) elif container.has_config_txt: self.log(u"Container has TXT config file") contents = container.read_entry(container.entry_config_txt) if contents is None: self._failed(u"Unable to read the contents of TXT config file.") return self.result self.check_config_txt(contents, is_config_string=False) else: self._failed(u"Container does not have a TXT or XML configuration file.") self.log(u"Checking we have a valid job in the container") if not self.result.passed: return self.result self.log(u"Analyze the contents of the container") analyzer = AnalyzeContainer(container) if config_string is not None: job = analyzer.analyze(config_string=config_string) else: job = analyzer.analyze() self._check_analyzed_job(job, container) except OSError: self._failed(u"Unable to read the contents of the container.") return self.result
def check_container(self, container_path, container_format=None): """ Check whether the given container is well-formed. :param container_path: the path of the container to be checked :type container_path: string (path) :param container_format: the format of the container :type container_format: string (from ContainerFormat enumeration) :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log(["Checking container file '%s'", container_path]) result = ValidatorResult() # check the container file exists self._log("Checking container file exists") if not os.path.exists(container_path): msg = "Container file '%s' not found." % container_path result.passed = False result.add_error(msg) self._log(msg) return result # check if we have config.xml or config.txt self._log("Checking container file has config file") container = Container(container_path, container_format) if container.has_config_xml: self._log("Container has XML config file") result = self._check_container_with_xml_config( container=container, config_contents=None) elif container.has_config_txt: self._log("Container has TXT config file") result = self._check_container_with_txt_config_string( container=container, config_string=None) else: msg = "Container does not have a TXT or XML configuration file." result.passed = False result.add_error(msg) self._log(msg) # return result self._log(["Checking container: returning %s", result.passed]) return result
def check_container_from_wizard(self, container_path, config_string, container_format=None): """ Check whether the given container and configuration strings from the wizard are well-formed. :param container_path: the path of the container to be checked :type container_path: string (path) :param config_string: the configuration string generated by the wizard :type config_string: string :param container_format: the format of the container :type container_format: string (from ContainerFormat enumeration) :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log("Checking container from wizard") container = Container(container_path, container_format) return self._check_container_with_txt_config_string( container=container, config_string=config_string)
def test_entries_unpacked_relative(self): f = self.FILES["unpacked"] cont = Container(f["path"]) self.assertEqual(cont.entries, self.EXPECTED_ENTRIES)
def test_find_entry_not_existing(self): cont = Container(self.NOT_EXISTING) with self.assertRaises(TypeError): self.assertIsNone(cont.find_entry(self.EXPECTED_ENTRIES[0]))
def test_entries_unpacked_absolute(self): f = self.FILES["unpacked"] cont = Container(os.path.abspath(f["path"])) self.assertEqual(cont.entries, self.EXPECTED_ENTRIES)
def test_entries_empty_file(self): for f in self.EMPTY_FILES: cont = Container(f) with self.assertRaises(OSError): self.assertEqual(len(cont.entries), 0)
def test_is_safe_empty_file(self): for f in self.EMPTY_FILES: cont = Container(f) with self.assertRaises(OSError): self.assertTrue(cont.is_safe)
def test_is_safe_not_existing(self): cont = Container(self.NOT_EXISTING) with self.assertRaises(TypeError): self.assertTrue(cont.is_safe)
def test_find_entry_empty_directory(self): output_path = gf.tmp_directory() cont = Container(output_path) self.assertIsNone(cont.find_entry(self.EXPECTED_ENTRIES[0])) gf.delete_directory(output_path)
def test_entries(self): for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) self.assertEqual(cont.entries, self.EXPECTED_ENTRIES)
def test_is_safe_empty_directory(self): output_path = gf.tmp_directory() cont = Container(output_path) self.assertTrue(cont.is_safe) gf.delete_directory(output_path)
def test_path_none(self): with self.assertRaises(TypeError): cont = Container(file_path=None)
def test_is_safe(self): for key in self.FILES: f = self.FILES[key] cont = Container(f["path"]) self.assertTrue(cont.is_safe)
def test_invalid_container_format(self): with self.assertRaises(ValueError): con = Container(file_path=self.FILES["zip"]["path"], container_format="foo")
def test_find_entry_empty_file(self): for f in self.EMPTY_FILES: cont = Container(f) with self.assertRaises(OSError): self.assertIsNone(cont.find_entry(self.EXPECTED_ENTRIES[0]))
def test_analyze(self): for f in self.FILES: analyzer = AnalyzeContainer(Container(get_abs_path(f["path"]))) job = analyzer.analyze() self.assertEqual(len(job), f["length"])
def test_entries_empty_directory(self): output_path = gf.tmp_directory() cont = Container(output_path) self.assertEqual(len(cont.entries), 0) gf.delete_directory(output_path)