Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alighazi288 committed Jan 6, 2025
1 parent 6463ad6 commit b907173
Showing 1 changed file with 18 additions and 58 deletions.
76 changes: 18 additions & 58 deletions src/borg/testsuite/helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from ..helpers import safe_unlink
from ..helpers import text_to_json, binary_to_json
from ..helpers import classify_ec, max_ec
from ..helpers.passphrase import Passphrase, PasswordRetriesExceeded, PassphraseWrong
from ..helpers.passphrase import Passphrase, PasswordRetriesExceeded
from ..platform import is_cygwin, is_win32, is_darwin
from . import FakeInputs, are_hardlinks_supported
from . import rejected_dotdot_paths
Expand Down Expand Up @@ -1408,85 +1408,45 @@ def test_passphrase_new_retries(self, monkeypatch):
def test_passphrase_repr(self):
assert "secret" not in repr(Passphrase("secret"))

def test_passphrase_wrong_debug_enabled(self, capsys, monkeypatch):
def test_passphrase_wrong_debug(self, capsys, monkeypatch):
passphrase = "wrong_passphrase"
monkeypatch.setenv("BORG_DEBUG_PASSPHRASE", "YES")
monkeypatch.setenv("BORG_PASSPHRASE", "wrong_passphrase")
monkeypatch.setenv("BORG_PASSCOMMAND", "echo wrong_passphrase")
monkeypatch.setenv("BORG_PASSPHRASE_FD", "123")
monkeypatch.setenv("BORG_PASSPHRASE", "env_passphrase")
monkeypatch.setenv("BORG_PASSCOMMAND", "command")
monkeypatch.setenv("BORG_PASSPHRASE_FD", "fd_value")

with pytest.raises(PassphraseWrong) as exc_info:
Passphrase.display_debug_info("wrong_passphrase")
raise PassphraseWrong()
Passphrase.display_debug_info(passphrase)

out, err = capsys.readouterr()
assert "Incorrect passphrase!" in err
assert 'Passphrase used (between double-quotes): "wrong_passphrase"' in err
assert "77726f6e675f70617373706872617365" in err
assert "BORG_PASSPHRASE" in err
assert "BORG_PASSCOMMAND" in err
assert "BORG_PASSPHRASE_FD" in err

assert str(exc_info.value) == (
"passphrase supplied in BORG_PASSPHRASE, by BORG_PASSCOMMAND or via BORG_PASSPHRASE_FD is incorrect."
)
assert passphrase in err
assert bin_to_hex(passphrase.encode("utf-8")) in err
assert 'BORG_PASSPHRASE = "env_passphrase"' in err
assert 'BORG_PASSCOMMAND = "command"' in err
assert 'BORG_PASSPHRASE_FD = "fd_value"' in err

def test_passphrase_wrong_debug_disabled(self, capsys, monkeypatch):
monkeypatch.delenv("BORG_DEBUG_PASSPHRASE", raising=False)

with pytest.raises(PassphraseWrong):
Passphrase.display_debug_info("wrong_passphrase")
raise PassphraseWrong()

Passphrase.display_debug_info(passphrase)
out, err = capsys.readouterr()

assert "Incorrect passphrase!" not in err
assert 'Passphrase used (between double-quotes): "wrong_passphrase"' not in err
assert passphrase not in err

def test_verification(self, capsys, monkeypatch):
passphrase = "test_passphrase"
hex_value = passphrase.encode("utf-8").hex()

monkeypatch.setenv("BORG_DISPLAY_PASSPHRASE", "no")
Passphrase.verification(passphrase)
out, err = capsys.readouterr()
assert "Your passphrase (between double-quotes)" not in err
assert passphrase not in err

monkeypatch.setenv("BORG_DISPLAY_PASSPHRASE", "yes")
Passphrase.verification(passphrase)
out, err = capsys.readouterr()
assert 'Your passphrase (between double-quotes): "test_passphrase"' in err
assert "UTF-8 encoding in hex" in err

# Case 3: Non-ASCII passphrase
passphrase = "tëst_päṩṩ"
hex_value = passphrase.encode("utf-8").hex()
Passphrase.verification(passphrase)
out, err = capsys.readouterr()
assert passphrase in err
assert hex_value in err

def test_display_debug_info(self, capsys, monkeypatch):
passphrase = "debug_test"
monkeypatch.setenv("BORG_DEBUG_PASSPHRASE", "YES")
monkeypatch.setenv("BORG_PASSPHRASE", "debug_env_passphrase")
monkeypatch.setenv("BORG_PASSCOMMAND", "command")
monkeypatch.setenv("BORG_PASSPHRASE_FD", "fd_value")

Passphrase.display_debug_info(passphrase)

out, err = capsys.readouterr()
assert "Incorrect passphrase!" in err
assert 'Passphrase used (between double-quotes): "debug_test"' in err
assert "64656275675f74657374" in err # UTF-8 hex encoding of 'debug_test'
assert 'BORG_PASSPHRASE = "debug_env_passphrase"' in err
assert 'BORG_PASSCOMMAND = "command"' in err
assert 'BORG_PASSPHRASE_FD = "fd_value"' in err

monkeypatch.delenv("BORG_DEBUG_PASSPHRASE", raising=False)

Passphrase.display_debug_info(passphrase)
out, err = capsys.readouterr()

assert "Incorrect passphrase!" not in err
assert 'Passphrase used (between double-quotes): "debug_test"' not in err


@pytest.mark.parametrize(
"ec_range,ec_class",
Expand Down

0 comments on commit b907173

Please sign in to comment.