def test_zipalign_valid_path(self): zipalign = Zipalign() assert os.path.isfile(zipalign.zipalign_path) with pytest.raises(subprocess.CalledProcessError) as e: subprocess.check_output(zipalign.zipalign_path, stderr=subprocess.STDOUT) assert "usage: zipalign" in e.value.output.decode().lower()
def test_align_error_generic(self, tmp_demo_apk_v10_rebuild_path: str, monkeypatch): def mock(*args, **kwargs): raise Exception monkeypatch.setattr("subprocess.check_output", mock) with pytest.raises(Exception): Zipalign().align(tmp_demo_apk_v10_rebuild_path)
def test_align_error_invalid_file(self, tmp_working_directory_path: str): invalid_file_path = os.path.join(tmp_working_directory_path, "invalid.apk") with open(invalid_file_path, "w") as invalid_file: invalid_file.write("This is not an apk file\n") with pytest.raises(subprocess.CalledProcessError): Zipalign().align(invalid_file_path)
def check_external_tool_dependencies(): """ Make sure all the external needed tools are available and ready to be used. """ # APKTOOL_PATH, JARSIGNER_PATH and ZIPALIGN_PATH environment variables can be # used to specify the location of the external tools (make sure they have the # execute permission). If there is a problem with any of the executables below, # an exception will be thrown by the corresponding constructor. Apktool() Jarsigner() Zipalign()
def align_obfuscated_apk(self) -> None: # This method must be called AFTER the obfuscated apk has been signed. # The obfuscated apk will be aligned with zipalign. zipalign: Zipalign = Zipalign() try: zipalign.align(self.obfuscated_apk_path) except Exception as e: self.logger.error("Error during apk alignment: {0}".format(e)) raise
def test_align_error_invalid_apk_path(self): with pytest.raises(FileNotFoundError): Zipalign().align("invalid.apk.path")
def test_align_valid_apk(self, tmp_demo_apk_v10_rebuild_path: str): output = Zipalign().align(tmp_demo_apk_v10_rebuild_path) assert "verification succes" in output.lower()
def test_zipalign_wrong_path(self, monkeypatch): monkeypatch.setenv("ZIPALIGN_PATH", "invalid.zipalign.path") with pytest.raises(RuntimeError): Zipalign()