Marco Ricci commited on 2025-12-25 16:25:15
Zeige 1 geänderte Dateien mit 41 Einfügungen und 43 Löschungen.
| ... | ... |
@@ -21,11 +21,10 @@ import packaging.version |
| 21 | 21 |
import pytest |
| 22 | 22 |
from typing_extensions import NamedTuple |
| 23 | 23 |
|
| 24 |
-import tests.data |
|
| 25 |
-import tests.data.callables |
|
| 26 |
-import tests.machinery |
|
| 27 | 24 |
from derivepassphrase import _types, ssh_agent |
| 28 | 25 |
from derivepassphrase.ssh_agent import socketprovider |
| 26 |
+from tests import data, machinery |
|
| 27 |
+from tests.data import callables |
|
| 29 | 28 |
|
| 30 | 29 |
if TYPE_CHECKING: |
| 31 | 30 |
from collections.abc import Iterator, Sequence |
| ... | ... |
@@ -398,7 +397,7 @@ class SpawnHandler(NamedTuple): |
| 398 | 397 |
key: str |
| 399 | 398 |
executable: str | None |
| 400 | 399 |
spawn_func: SSHAgentSpawnFunc | SSHAgentInterfaceFunc |
| 401 |
- agent_type: tests.data.KnownSSHAgent |
|
| 400 |
+ agent_type: data.KnownSSHAgent |
|
| 402 | 401 |
agent_name: str | None |
| 403 | 402 |
|
| 404 | 403 |
|
| ... | ... |
@@ -407,42 +406,42 @@ spawn_handlers: dict[str, SpawnHandler] = {
|
| 407 | 406 |
"unix-pageant", |
| 408 | 407 |
None, |
| 409 | 408 |
spawn_pageant_on_posix, |
| 410 |
- tests.data.KnownSSHAgent.UNIXPageant, |
|
| 409 |
+ data.KnownSSHAgent.UNIXPageant, |
|
| 411 | 410 |
"Pageant (UNIX)", |
| 412 | 411 |
), |
| 413 | 412 |
"openssh": SpawnHandler( |
| 414 | 413 |
"openssh", |
| 415 | 414 |
None, |
| 416 | 415 |
spawn_openssh_agent_on_posix, |
| 417 |
- tests.data.KnownSSHAgent.OpenSSHAgent, |
|
| 416 |
+ data.KnownSSHAgent.OpenSSHAgent, |
|
| 418 | 417 |
"ssh-agent (OpenSSH)", |
| 419 | 418 |
), |
| 420 | 419 |
"pageant": SpawnHandler( |
| 421 | 420 |
"pageant", |
| 422 | 421 |
None, |
| 423 | 422 |
interface_pageant_on_the_annoying_os, |
| 424 |
- tests.data.KnownSSHAgent.Pageant, |
|
| 423 |
+ data.KnownSSHAgent.Pageant, |
|
| 425 | 424 |
"Pageant", |
| 426 | 425 |
), |
| 427 | 426 |
"openssh-on-windows": SpawnHandler( |
| 428 | 427 |
"openssh-on-windows", |
| 429 | 428 |
None, |
| 430 | 429 |
interface_openssh_agent_on_the_annoying_os, |
| 431 |
- tests.data.KnownSSHAgent.OpenSSHAgentOnWindows, |
|
| 430 |
+ data.KnownSSHAgent.OpenSSHAgentOnWindows, |
|
| 432 | 431 |
"ssh-agent (OpenSSH on Windows)", |
| 433 | 432 |
), |
| 434 | 433 |
"stub_agent_with_address": SpawnHandler( |
| 435 | 434 |
"stub_agent_with_address", |
| 436 | 435 |
None, |
| 437 | 436 |
spawn_noop, |
| 438 |
- tests.data.KnownSSHAgent.StubbedSSHAgent, |
|
| 437 |
+ data.KnownSSHAgent.StubbedSSHAgent, |
|
| 439 | 438 |
"stub_agent_with_address (derivepassphrase test suite)", |
| 440 | 439 |
), |
| 441 | 440 |
"stub_agent_with_address_and_deterministic_dsa": SpawnHandler( |
| 442 | 441 |
"stub_agent_with_address_and_deterministic_dsa", |
| 443 | 442 |
None, |
| 444 | 443 |
spawn_noop, |
| 445 |
- tests.data.KnownSSHAgent.StubbedSSHAgent, |
|
| 444 |
+ data.KnownSSHAgent.StubbedSSHAgent, |
|
| 446 | 445 |
"stub_agent_with_address_and_deterministic_dsa " |
| 447 | 446 |
"(derivepassphrase test suite)", |
| 448 | 447 |
), |
| ... | ... |
@@ -450,7 +449,7 @@ spawn_handlers: dict[str, SpawnHandler] = {
|
| 450 | 449 |
"(system-agent)", |
| 451 | 450 |
None, |
| 452 | 451 |
spawn_noop, |
| 453 |
- tests.data.KnownSSHAgent.UNKNOWN, |
|
| 452 |
+ data.KnownSSHAgent.UNKNOWN, |
|
| 454 | 453 |
None, |
| 455 | 454 |
), |
| 456 | 455 |
} |
| ... | ... |
@@ -460,7 +459,7 @@ The standard registry of agent spawning functions. |
| 460 | 459 |
|
| 461 | 460 |
|
| 462 | 461 |
def is_agent_permitted( |
| 463 |
- agent_type: tests.data.KnownSSHAgent, |
|
| 462 |
+ agent_type: data.KnownSSHAgent, |
|
| 464 | 463 |
) -> bool: # pragma: no cover [external] |
| 465 | 464 |
"""May the given SSH agent be spawned by the test harness? |
| 466 | 465 |
|
| ... | ... |
@@ -478,11 +477,11 @@ def is_agent_permitted( |
| 478 | 477 |
""" |
| 479 | 478 |
if not os.environ.get("PERMITTED_SSH_AGENTS"):
|
| 480 | 479 |
return True |
| 481 |
- permitted_agents = {tests.data.KnownSSHAgent.StubbedSSHAgent}
|
|
| 480 |
+ permitted_agents = {data.KnownSSHAgent.StubbedSSHAgent}
|
|
| 482 | 481 |
permitted_agents.update({
|
| 483 |
- tests.data.KnownSSHAgent(x) |
|
| 482 |
+ data.KnownSSHAgent(x) |
|
| 484 | 483 |
for x in os.environ["PERMITTED_SSH_AGENTS"].split(",")
|
| 485 |
- if x in tests.data.KnownSSHAgent.__members__ |
|
| 484 |
+ if x in data.KnownSSHAgent.__members__ |
|
| 486 | 485 |
}) |
| 487 | 486 |
return agent_type in permitted_agents |
| 488 | 487 |
|
| ... | ... |
@@ -551,9 +550,9 @@ class CannotSpawnError(RuntimeError): |
| 551 | 550 |
def spawn_named_agent( # noqa: C901 |
| 552 | 551 |
executable: str | None, |
| 553 | 552 |
spawn_func: SSHAgentSpawnFunc | SSHAgentInterfaceFunc, |
| 554 |
- agent_type: tests.data.KnownSSHAgent, |
|
| 553 |
+ agent_type: data.KnownSSHAgent, |
|
| 555 | 554 |
agent_name: str, |
| 556 |
-) -> Iterator[tests.data.SpawnedSSHAgentInfo]: # pragma: no cover [external] |
|
| 555 |
+) -> Iterator[data.SpawnedSSHAgentInfo]: # pragma: no cover [external] |
|
| 557 | 556 |
"""Spawn the named SSH agent and check that it is operational. |
| 558 | 557 |
|
| 559 | 558 |
Using the correct agent-specific spawn function from the |
| ... | ... |
@@ -563,9 +562,8 @@ def spawn_named_agent( # noqa: C901 |
| 563 | 562 |
tear down the communication channel and terminate the SSH agent. |
| 564 | 563 |
|
| 565 | 564 |
The SSH agent's instructions for setting up the communication |
| 566 |
- channel are parsed with |
|
| 567 |
- [`tests.data.callables.parse_sh_export_line`][]. See the caveats |
|
| 568 |
- there. |
|
| 565 |
+ channel are parsed with [`callables.parse_sh_export_line`][]. See |
|
| 566 |
+ the caveats there. |
|
| 569 | 567 |
|
| 570 | 568 |
Args: |
| 571 | 569 |
executable: |
| ... | ... |
@@ -612,7 +610,7 @@ def spawn_named_agent( # noqa: C901 |
| 612 | 610 |
ssh_auth_sock = agent_env.pop("SSH_AUTH_SOCK", None)
|
| 613 | 611 |
proc_or_socket = spawn_func(executable=executable, env=agent_env) |
| 614 | 612 |
with exit_stack: |
| 615 |
- if agent_type == tests.data.KnownSSHAgent.StubbedSSHAgent: |
|
| 613 |
+ if agent_type == data.KnownSSHAgent.StubbedSSHAgent: |
|
| 616 | 614 |
ssh_auth_sock = None |
| 617 | 615 |
elif spawn_func is spawn_noop: |
| 618 | 616 |
ssh_auth_sock = os.environ.get("SSH_AUTH_SOCK")
|
| ... | ... |
@@ -628,7 +626,7 @@ def spawn_named_agent( # noqa: C901 |
| 628 | 626 |
assert proc.stdout is not None |
| 629 | 627 |
ssh_auth_sock_line = proc.stdout.readline() |
| 630 | 628 |
try: |
| 631 |
- ssh_auth_sock = tests.data.callables.parse_sh_export_line( |
|
| 629 |
+ ssh_auth_sock = callables.parse_sh_export_line( |
|
| 632 | 630 |
ssh_auth_sock_line, env_name="SSH_AUTH_SOCK" |
| 633 | 631 |
) |
| 634 | 632 |
except ValueError: # pragma: no cover [external] |
| ... | ... |
@@ -642,11 +640,11 @@ def spawn_named_agent( # noqa: C901 |
| 642 | 640 |
err_msg = f"Cannot parse agent output: {pid_line!r}"
|
| 643 | 641 |
raise CannotSpawnError(err_msg) |
| 644 | 642 |
monkeypatch = exit_stack.enter_context(pytest.MonkeyPatch.context()) |
| 645 |
- if agent_type == tests.data.KnownSSHAgent.StubbedSSHAgent: |
|
| 643 |
+ if agent_type == data.KnownSSHAgent.StubbedSSHAgent: |
|
| 646 | 644 |
assert ssh_auth_sock is None |
| 647 | 645 |
monkeypatch.setenv( |
| 648 | 646 |
"SSH_AUTH_SOCK", |
| 649 |
- tests.machinery.StubbedSSHAgentSocketWithAddress.ADDRESS, |
|
| 647 |
+ machinery.StubbedSSHAgentSocketWithAddress.ADDRESS, |
|
| 650 | 648 |
) |
| 651 | 649 |
monkeypatch.setattr( |
| 652 | 650 |
ssh_agent.SSHAgentClient, |
| ... | ... |
@@ -657,9 +655,9 @@ def spawn_named_agent( # noqa: C901 |
| 657 | 655 |
) |
| 658 | 656 |
client = exit_stack.enter_context( |
| 659 | 657 |
ssh_agent.SSHAgentClient.ensure_agent_subcontext( |
| 660 |
- tests.machinery.StubbedSSHAgentSocketWithAddressAndDeterministicDSA() |
|
| 658 |
+ machinery.StubbedSSHAgentSocketWithAddressAndDeterministicDSA() |
|
| 661 | 659 |
if "stub_agent_with_address_and_deterministic_dsa" in agent_name |
| 662 |
- else tests.machinery.StubbedSSHAgentSocketWithAddress() |
|
| 660 |
+ else machinery.StubbedSSHAgentSocketWithAddress() |
|
| 663 | 661 |
) |
| 664 | 662 |
) |
| 665 | 663 |
elif ( |
| ... | ... |
@@ -687,7 +685,7 @@ def spawn_named_agent( # noqa: C901 |
| 687 | 685 |
# agent is not one of our test agents, and if the check fails, |
| 688 | 686 |
# skip this agent. |
| 689 | 687 |
if ( |
| 690 |
- agent_type != tests.data.KnownSSHAgent.StubbedSSHAgent |
|
| 688 |
+ agent_type != data.KnownSSHAgent.StubbedSSHAgent |
|
| 691 | 689 |
): # pragma: no cover [external] |
| 692 | 690 |
try: |
| 693 | 691 |
client.list_keys() # sanity test |
| ... | ... |
@@ -698,7 +696,7 @@ def spawn_named_agent( # noqa: C901 |
| 698 | 696 |
) as exc: # pragma: no cover [failsafe] |
| 699 | 697 |
msg = f'agent failed the "list keys" sanity test: {exc!r}'
|
| 700 | 698 |
raise CannotSpawnError(msg) from exc |
| 701 |
- yield tests.data.SpawnedSSHAgentInfo( |
|
| 699 |
+ yield data.SpawnedSSHAgentInfo( |
|
| 702 | 700 |
agent_type, client, spawn_func is not spawn_noop |
| 703 | 701 |
) |
| 704 | 702 |
assert os.environ.get("SSH_AUTH_SOCK", None) == startup_ssh_auth_sock, (
|
| ... | ... |
@@ -708,7 +706,7 @@ def spawn_named_agent( # noqa: C901 |
| 708 | 706 |
|
| 709 | 707 |
@pytest.fixture |
| 710 | 708 |
def running_ssh_agent( # pragma: no cover [external] |
| 711 |
-) -> Iterator[tests.data.RunningSSHAgentInfo]: |
|
| 709 |
+) -> Iterator[data.RunningSSHAgentInfo]: |
|
| 712 | 710 |
"""Ensure a running SSH agent, if possible, as a pytest fixture. |
| 713 | 711 |
|
| 714 | 712 |
Check for a running SSH agent, or spawn a new one if possible. We |
| ... | ... |
@@ -731,10 +729,10 @@ def running_ssh_agent( # pragma: no cover [external] |
| 731 | 729 |
""" |
| 732 | 730 |
|
| 733 | 731 |
def prepare_environment( |
| 734 |
- agent_type: tests.data.KnownSSHAgent, |
|
| 735 |
- ) -> Iterator[tests.data.RunningSSHAgentInfo]: |
|
| 732 |
+ agent_type: data.KnownSSHAgent, |
|
| 733 |
+ ) -> Iterator[data.RunningSSHAgentInfo]: |
|
| 736 | 734 |
with pytest.MonkeyPatch.context() as monkeypatch: |
| 737 |
- if agent_type == tests.data.KnownSSHAgent.StubbedSSHAgent: |
|
| 735 |
+ if agent_type == data.KnownSSHAgent.StubbedSSHAgent: |
|
| 738 | 736 |
monkeypatch.setattr( |
| 739 | 737 |
ssh_agent.SSHAgentClient, |
| 740 | 738 |
"SOCKET_PROVIDERS", |
| ... | ... |
@@ -742,14 +740,14 @@ def running_ssh_agent( # pragma: no cover [external] |
| 742 | 740 |
) |
| 743 | 741 |
monkeypatch.setenv( |
| 744 | 742 |
"SSH_AUTH_SOCK", |
| 745 |
- tests.machinery.StubbedSSHAgentSocketWithAddress.ADDRESS, |
|
| 743 |
+ machinery.StubbedSSHAgentSocketWithAddress.ADDRESS, |
|
| 746 | 744 |
) |
| 747 |
- yield tests.data.RunningSSHAgentInfo( |
|
| 748 |
- tests.machinery.StubbedSSHAgentSocketWithAddressAndDeterministicDSA, |
|
| 749 |
- tests.data.KnownSSHAgent.StubbedSSHAgent, |
|
| 745 |
+ yield data.RunningSSHAgentInfo( |
|
| 746 |
+ machinery.StubbedSSHAgentSocketWithAddressAndDeterministicDSA, |
|
| 747 |
+ data.KnownSSHAgent.StubbedSSHAgent, |
|
| 750 | 748 |
) |
| 751 | 749 |
else: |
| 752 |
- yield tests.data.RunningSSHAgentInfo( |
|
| 750 |
+ yield data.RunningSSHAgentInfo( |
|
| 753 | 751 |
os.environ["SSH_AUTH_SOCK"], |
| 754 | 752 |
agent_type, |
| 755 | 753 |
) |
| ... | ... |
@@ -786,7 +784,7 @@ def running_ssh_agent( # pragma: no cover [external] |
| 786 | 784 |
@pytest.fixture(params=spawn_handlers_params) |
| 787 | 785 |
def spawn_ssh_agent( |
| 788 | 786 |
request: pytest.FixtureRequest, |
| 789 |
-) -> Iterator[tests.data.SpawnedSSHAgentInfo]: # pragma: no cover [external] |
|
| 787 |
+) -> Iterator[data.SpawnedSSHAgentInfo]: # pragma: no cover [external] |
|
| 790 | 788 |
"""Spawn an isolated SSH agent, if possible, as a pytest fixture. |
| 791 | 789 |
|
| 792 | 790 |
Spawn a new SSH agent isolated from other SSH use by other |
| ... | ... |
@@ -839,7 +837,7 @@ def spawn_ssh_agent( |
| 839 | 837 |
|
| 840 | 838 |
@pytest.fixture |
| 841 | 839 |
def ssh_agent_client_with_test_keys_loaded( # noqa: C901 |
| 842 |
- spawn_ssh_agent: tests.data.SpawnedSSHAgentInfo, |
|
| 840 |
+ spawn_ssh_agent: data.SpawnedSSHAgentInfo, |
|
| 843 | 841 |
) -> Iterator[ssh_agent.SSHAgentClient]: |
| 844 | 842 |
"""Provide an SSH agent with loaded test keys, as a pytest fixture. |
| 845 | 843 |
|
| ... | ... |
@@ -904,7 +902,7 @@ def ssh_agent_client_with_test_keys_loaded( # noqa: C901 |
| 904 | 902 |
return (return_code, bytes(payload) + lifetime_constraint) |
| 905 | 903 |
|
| 906 | 904 |
try: |
| 907 |
- for key_type, key_struct in tests.data.ALL_KEYS.items(): |
|
| 905 |
+ for key_type, key_struct in data.ALL_KEYS.items(): |
|
| 908 | 906 |
private_key_data = key_struct.private_key_blob |
| 909 | 907 |
if private_key_data is None: # pragma: no cover [failsafe] |
| 910 | 908 |
continue |
| ... | ... |
@@ -935,12 +933,12 @@ def ssh_agent_client_with_test_keys_loaded( # noqa: C901 |
| 935 | 933 |
pair.key for pair in client.list_keys() |
| 936 | 934 |
}) |
| 937 | 935 |
if ( |
| 938 |
- agent_type == tests.data.KnownSSHAgent.UNIXPageant |
|
| 936 |
+ agent_type == data.KnownSSHAgent.UNIXPageant |
|
| 939 | 937 |
and key_struct.public_key_data in current_loaded_keys |
| 940 | 938 |
): |
| 941 | 939 |
pass |
| 942 | 940 |
elif ( |
| 943 |
- agent_type == tests.data.KnownSSHAgent.UNIXPageant |
|
| 941 |
+ agent_type == data.KnownSSHAgent.UNIXPageant |
|
| 944 | 942 |
and not isolated |
| 945 | 943 |
): |
| 946 | 944 |
request_code, payload = prepare_payload( |
| ... | ... |
@@ -984,7 +982,7 @@ def ssh_agent_client_with_test_keys_loaded( # noqa: C901 |
| 984 | 982 |
) |
| 985 | 983 |
yield client |
| 986 | 984 |
finally: |
| 987 |
- for key_type, key_struct in tests.data.ALL_KEYS.items(): |
|
| 985 |
+ for key_type, key_struct in data.ALL_KEYS.items(): |
|
| 988 | 986 |
if not isolated and ( |
| 989 | 987 |
key_type in successfully_loaded_keys |
| 990 | 988 |
): # pragma: no cover [external] |
| 991 | 989 |