Files
ChaosCrypt/chaoscrypt/tests/test_tui.py
T
dvs 2529edac1d v2.1.6: Restructure as a distributable library
Restructure ChaosCrypt from a single-file script into a complete,
installable Python package with three interfaces: a typed Python API, an
argument-driven CLI, and a bilingual interactive TUI.

The algorithm itself is unchanged. Ciphertexts and key files remain fully
interoperable with the original implementation in both directions, which is
enforced by dedicated tests rather than merely documented.

Added
- Library API: ChaosCrypt facade plus ChaosCryptCore for the raw algorithm
- Unified error hierarchy: 16 named types, all ChaosCryptError subclasses,
  rendering as ChaosCrypt_ERR_<Detail>_<Code>
- CLI: keygen, encrypt, decrypt, file, info, chpwd, tui, with --json output,
  stdin/stdout piping, and three password sources per secret
- Bilingual TUI: English default, runtime switch to Chinese, getpass password
  entry with a non-TTY fallback that keeps it scriptable
- Binary and file support: encrypt_bytes/decrypt_bytes and
  encrypt_file/decrypt_file
- Key management: change_key_password rewraps without altering key material;
  export_key_text/load_from_text move a key without touching disk
- Documentation: SECURITY.md with the full design and threat analysis, plus
  docs/ covering the API, CLI, architecture and build process
- Test suite: 92 tests across round-trips, binary safety, key management,
  error types, CLI, TUI, i18n and legacy interoperability
- Packaging: pyproject.toml, setup.py compatibility shim, MANIFEST.in,
  py.typed, examples, MIT licence

Fixed
- Conflicting --pwd option between the -p and -P groups prevented the CLI
  from starting at all
- Wrong key file password reported as file corruption (E002); structural
  validation now distinguishes it from a damaged file (E003)
- export_key_text produced key material without loading it into the instance
- Non-hexadecimal input was accepted as ciphertext and silently decrypted
  into garbage, making corruption indistinguishable from success; malformed
  input now raises E010 before any processing
- TUI failed under piped input because getpass requires a TTY
- TUI kept reading the default key file name after generating a new one

Changed
- Source tree reorganised: the package lives at chaoscrypt/, with tests,
  examples and legacy alongside it; documentation and build artefacts at the
  repository root
- Code comments unified in English; public docstrings document parameters,
  return values and raised exceptions
- English is now the default UI language
- ChaosCryptChat removed from the repository
- Attribution updated to Dvs (DvsXT); project URLs point to Gitea
2026-09-27 17:03:05 +08:00

140 lines
5.0 KiB
Python

"""
Interactive TUI tests.
A key sequence is piped into the UI and the rendered output is inspected.
Outside a TTY the UI falls back from ``getpass`` to ``input``, which is what
makes it scriptable.
"""
import os
import re
import subprocess
import sys
import pytest
KEY_PWD = "tui-key-password"
USER_PWD = "tui-user-password"
def _run_tui(seq: str, cwd: str, timeout: int = 90):
env = dict(os.environ, PYTHONIOENCODING="utf-8", NO_COLOR="1")
return subprocess.run(
[sys.executable, "-m", "chaoscrypt"],
input=seq, capture_output=True, text=True,
encoding="utf-8", env=env, cwd=cwd, timeout=timeout,
)
@pytest.fixture
def tui_dir(tmp_path):
"""Create a TUI key file in an isolated directory."""
seq = f"5\nt.key\n{KEY_PWD}\n{KEY_PWD}\n\nq\n"
r = _run_tui(seq, str(tmp_path))
assert (tmp_path / "t.key").exists(), r.stdout
return str(tmp_path)
class TestTUI:
def test_launches_in_english_by_default(self, tmp_path):
r = _run_tui("q\n", str(tmp_path))
assert r.returncode == 0
assert "ChaosCrypt" in r.stdout
assert "Select an action" in r.stdout
assert "Encrypt text" in r.stdout
def test_shows_scope_notice(self, tmp_path):
r = _run_tui("q\n", str(tmp_path))
assert "unaudited" in r.stdout
assert "not for production" in r.stdout.lower()
def test_keygen_creates_file(self, tmp_path):
r = _run_tui(f"5\nmy.key\n{KEY_PWD}\n{KEY_PWD}\n\nq\n", str(tmp_path))
assert (tmp_path / "my.key").exists(), r.stdout
assert "Key file written to" in r.stdout
def test_keygen_password_mismatch(self, tmp_path):
r = _run_tui("5\nm.key\npwd-one\npwd-two\n\nq\n", str(tmp_path))
assert "E007" in r.stdout
assert not (tmp_path / "m.key").exists()
def test_encrypt_then_decrypt_in_new_session(self, tmp_path):
seq = (f"5\nt.key\n{KEY_PWD}\n{KEY_PWD}\n\n"
f"1\nsecret payload\n{USER_PWD}\n\nq\n")
r1 = _run_tui(seq, str(tmp_path))
m = re.search(r"Ciphertext:\s*\n([0-9a-f]{16,})", r1.stdout)
assert m, r1.stdout
ct = m.group(1).strip()
seq2 = f"6\nt.key\n{KEY_PWD}\n\n2\n{ct}\n{USER_PWD}\n\nq\n"
r2 = _run_tui(seq2, str(tmp_path))
assert "secret payload" in r2.stdout, r2.stdout
def test_load_remembers_key_file(self, tui_dir):
seq = f"6\nt.key\n{KEY_PWD}\n\n1\nremembers\n{USER_PWD}\n\nq\n"
r = _run_tui(seq, tui_dir)
assert re.search(r"Ciphertext:\s*\n([0-9a-f]{16,})", r.stdout), r.stdout
def test_wrong_user_password_shows_error(self, tui_dir):
seq = f"6\nt.key\n{KEY_PWD}\n\n2\n{'a' * 64}\nWRONG-{USER_PWD}\n\nq\n"
r = _run_tui(seq, tui_dir)
assert "ChaosCrypt_ERR_" in r.stdout
def test_wrong_key_password_shows_e003(self, tui_dir):
r = _run_tui("6\nt.key\nWRONG-KEY-PWD\n\nq\n", tui_dir)
assert "_E003" in r.stdout
def test_language_switch_to_chinese(self, tmp_path):
r = _run_tui("9\n2\n\nq\n", str(tmp_path))
assert "混沌混淆加密" in r.stdout
assert "请选择操作" in r.stdout
def test_language_switch_back_to_english(self, tmp_path):
r = _run_tui("9\n2\n\n9\n1\n\nq\n", str(tmp_path))
assert "Select an action" in r.stdout
def test_chinese_mode_encrypt_works(self, tui_dir):
seq = (f"6\nt.key\n{KEY_PWD}\n\n9\n2\n\n"
f"1\n中文内容\n{USER_PWD}\n\nq\n")
r = _run_tui(seq, tui_dir)
assert re.search(r"密文:\s*\n([0-9a-f]{16,})", r.stdout), r.stdout
def test_about_page_has_design_notes(self, tmp_path):
r = _run_tui("a\n\nq\n", str(tmp_path))
assert "10^5621" in r.stdout
assert "MIT" in r.stdout
assert "Shor" in r.stdout
def test_key_info_shows_sizes(self, tui_dir):
r = _run_tui(f"6\nt.key\n{KEY_PWD}\n\n7\n\nq\n", tui_dir)
assert "4096" in r.stdout
assert "512" in r.stdout
def test_invalid_option_reprompts(self, tmp_path):
r = _run_tui("zzz\n\nq\n", str(tmp_path))
assert "Unrecognised option" in r.stdout
def test_change_key_password_in_session(self, tui_dir):
seq = f"8\nt.key\n{KEY_PWD}\nnewkeypass\nnewkeypass\n\nq\n"
r = _run_tui(seq, tui_dir)
assert "updated" in r.stdout
r2 = _run_tui("6\nt.key\nnewkeypass\n\nq\n", tui_dir)
assert "Key file loaded" in r2.stdout
def test_file_encrypt_decrypt_via_tui(self, tui_dir):
src = os.path.join(tui_dir, "payload.txt")
with open(src, "w", encoding="utf-8") as f:
f.write("TUI file payload")
seq = (f"6\nt.key\n{KEY_PWD}\n\n"
f"3\n{src}\n{src}.cc\n{USER_PWD}\n\nq\n")
r = _run_tui(seq, tui_dir)
assert os.path.exists(src + ".cc"), r.stdout
seq2 = (f"6\nt.key\n{KEY_PWD}\n\n"
f"4\n{src}.cc\n{src}.out\n{USER_PWD}\n\nq\n")
r2 = _run_tui(seq2, tui_dir)
assert os.path.exists(src + ".out"), r2.stdout
assert open(src + ".out", encoding="utf-8").read() == "TUI file payload"