def test_for_archive_create_by_root(self): """Test extracting archives created by root.""" response = requests.get( "https://registry.npmjs.org/ajax-worker/-/ajax-worker-1.2.3.tgz") with tempfile.NamedTemporaryFile(suffix=".tgz") as package, tempfile.TemporaryDirectory() \ as extracted: package.write(response.content) Archive.extract(package.name, extracted) with pytest.raises(PermissionError): shutil.rmtree(extracted) Archive.fix_permissions(extracted + "/package")
def test_empty_archive(self, archive_name): """Test extracting an empty archive. Nothing should fail and the destination directory should exist afterwards. """ archive_path = Path(__file__).resolve().parent / Path( 'data/archives/' + archive_name) with tempfile.TemporaryDirectory() as temp_dir: dest_dir = Path(temp_dir) / Path('extracted_jar') assert not dest_dir.exists() Archive.extract(str(archive_path), str(dest_dir)) assert dest_dir.exists()
def test_for_archive_create_by_root(self): """Test extracting archives created by root.""" response = requests.get( "https://registry.npmjs.org/ajax-worker/-/ajax-worker-1.2.3.tgz") with tempfile.NamedTemporaryFile(suffix=".tgz") as package, tempfile.TemporaryDirectory() \ as extracted: dest_dir = Path(extracted) / Path('dest_dir') package.write(response.content) Archive.extract(package.name, str(dest_dir)) assert Path(str(dest_dir)).exists() shutil.rmtree(str(dest_dir)) assert not Path(str(dest_dir)).exists()
def get_extracted_source_jar(self): """Get extracted package source jar file. :return: path to extracted source jar file """ if not os.path.isdir(self._extracted_source_jar_dir): source_jar_path = self.get_source_jar() try: Archive.extract(source_jar_path, self._extracted_source_jar_dir) except Exception: # remove in case of failure so if one catches the exception, # the extraction code is correctly called again shutil.rmtree(self._extracted_source_jar_dir, ignore_errors=True) raise return self._extracted_source_jar_dir
def get_extracted_source_tarball(self): """Get path to the extracted package tarball. :return: path to the extracted package tarball """ self._construct_source_tarball_names() if not os.path.isdir(self._extracted_tarball_dir): source_tarball_path = self.get_source_tarball() os.makedirs(self._extracted_tarball_dir) try: Archive.extract(source_tarball_path, self._extracted_tarball_dir) except Exception: # remove in case of failure so if one catches the exception, # the extraction code is correctly called again shutil.rmtree(self._extracted_tarball_dir, ignore_errors=True) raise return self._extracted_tarball_dir
def test_bad_permissions(self, archive_name): """Test working with an archive which contains files with bad permissions. Bad permissions = 000. All extracted files need to be readable so they can be processed by various tools later. """ archive_path = Path(__file__).resolve().parent / Path( 'data/archives/' + archive_name) with tempfile.TemporaryDirectory() as temp_dir: dest_dir = Path(temp_dir) / Path('dest_dir') Archive.extract(str(archive_path), str(dest_dir)) assert dest_dir.exists() file_path = dest_dir / Path('cant_touch_me') # is the file readable? assert file_path.stat().st_mode & stat.S_IRUSR shutil.rmtree(str(dest_dir), ignore_errors=True) assert not dest_dir.exists()