Skip to content

Commit

Permalink
CLI: switch error message according to password given or not
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 20135bd commit 9817573
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 9 additions & 3 deletions py7zr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# 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
Expand All @@ -29,6 +28,7 @@
from lzma import CHECK_CRC64, CHECK_SHA256, is_check_supported
from typing import Any, List, Optional

import _lzma # type: ignore
import texttable # type: ignore

import py7zr
Expand Down Expand Up @@ -287,7 +287,10 @@ def run_extract(self, args: argparse.Namespace) -> int:
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.')
if password is None:
print('The archive is corrupted. ABORT.')
else:
print('The archive is corrupted, or password is wrong. ABORT.')
return 1

cb = None # Optional[ExtractCallback]
Expand All @@ -309,7 +312,10 @@ def run_extract(self, args: argparse.Namespace) -> int:
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.')
if password is None:
print('The archive is corrupted. ABORT.')
else:
print('The archive is corrupted, or password is wrong. ABORT.')
return 1
else:
return 0
Expand Down
20 changes: 19 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,32 @@ def _getpasswd():

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

expected = 'The archive is corrupted, or password is wrong if given. ABORT.\n'
expected = 'The archive is corrupted, or password is wrong. 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.cli
def test_cli_encrypted_zero_length_password(monkeypatch, tmp_path, capsys):

def _getpasswd():
return ''

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

expected = 'The archive is corrupted, or password is wrong. 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 9817573

Please sign in to comment.