Skip to content

Commit

Permalink
CLI: Catch LZMAError when wrong password is given
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Aug 30, 2020
1 parent ac08e3e commit 20135bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions py7zr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import _lzma
import argparse
import getpass
import lzma
import os
import pathlib
import platform
Expand Down Expand Up @@ -284,6 +286,9 @@ def run_extract(self, args: argparse.Namespace) -> int:
except py7zr.exceptions.PasswordRequired:
print('The archive is encrypted, but password is not given. ABORT.')
return 1
except lzma.LZMAError or _lzma.LZMAError:
print('The archive is corrupted, or password is wrong if given. ABORT.')
return 1

cb = None # Optional[ExtractCallback]
if verbose:
Expand All @@ -303,6 +308,9 @@ def run_extract(self, args: argparse.Namespace) -> int:
except py7zr.exceptions.PasswordRequired:
print('The archive is encrypted, but password is not given. ABORT.')
return 1
except lzma.LZMAError or _lzma.LZMAError:
print('The archive is corrupted, or password is wrong if given. ABORT.')
return 1
else:
return 0

Expand Down
16 changes: 16 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,22 @@ def _getpasswd():
check_output(expected, tmp_path)


@pytest.mark.cli
def test_cli_encrypted_wrong_password(monkeypatch, tmp_path, capsys):

def _getpasswd():
return 'badsecret'

monkeypatch.setattr(getpass, "getpass", _getpasswd)

expected = 'The archive is corrupted, or password is wrong if given. ABORT.\n'

arcfile = os.path.join(testdata_path, "encrypted_1.7z")
cli = py7zr.cli.Cli()
cli.run(["x", "--password", arcfile, str(tmp_path.resolve())])
out, err = capsys.readouterr()
assert out == expected

@pytest.mark.basic
def test_digests():
arcfile = os.path.join(testdata_path, "test_2.7z")
Expand Down

0 comments on commit 20135bd

Please sign in to comment.