tests.test_sudo module

Unit tests for the privilege-escalation machinery.

These exercise meta_package_manager.sudo.prime_sudo() and its keepalive with a mocked subprocess.run (no test ever launches a real sudo), the escalation policy inventories across the manager pool, and the _StallWatchdog end to end through tests.fake_manager.FakeManager (whose CLI is the Python interpreter, so its subprocesses are real but harmless). The sudo --non-interactive command wrapping of build_cli(), the authentication-failure hint of run() and the CLI wiring of prime_sudo stay in tests.test_execution.

tests.test_sudo.INSTALL_RUN = (Operations.install,)

Operations of the run the fakes below are primed for: install, the one operation _escalating_manager marks privileged.

tests.test_sudo.test_default_sudo_matches_system_managers()[source]

Exactly the system package managers escalate by default.

That holds for classes, their subclasses and bundled definitions alike. User-level managers do not escalate, the dual-scope language managers (npm, pip, gem, cpan) keep their privileged markers dormant, and the polkit-native daemon clients (flatpak, fwupd, pkcon) never mark an operation at all. rpm-ostree talks to a polkit-checked daemon too, but its policy asks for administrator authentication on each change and on the update check, so it escalates like the system managers.

choco is the lone Windows entry, and belongs on the same grounds as the rest: Chocolatey installs into a ProgramData tree it locks to administrators on purpose, so each of its four mutating operations fails unprivileged. scoop stays out, being built around a user-owned prefix.

tests.test_sudo.test_internal_sudo_matches_internal_escalators()[source]

The markers name exactly the managers whose CLI runs sudo mid-run.

None of them escalates through mpm: an internal escalator is never wrapped.

tests.test_sudo.test_resets_sudo_timestamp_matches_the_tools_dropping_the_cache()[source]

The markers name exactly the managers that drop cached credentials.

Their CLI drops the cached credentials before its own sudo. Each one is an internal escalator: the marker qualifies that escalation and is read nowhere else.

tests.test_sudo.test_internal_sudo_operations_are_narrowed_by_cask_alone()[source]

An internal escalator may run its sudo in every mutating operation.

cask is the exception: brew update and brew cleanup never escalate.

tests.test_sudo.test_hidden_prompt_risk_follows_the_operations_a_tool_escalates_in(manager_id, operation, at_risk)[source]

On a terminal, a cold-cache call may prompt only where its tool escalates.

fink and the AUR helpers escalate to refresh and to clean up, like every internal escalator by default. cask does not, and no read does.

tests.test_sudo.only_escalator(escalator_id, *, selected=None)[source]

Pretend the host carries exactly escalator_id, or none at all.

selected drives the process-wide choice the CLI would have recorded, so a test can force an escalator the way --sudo-command does.

The binary is also pinned as genuine, so resolve_escalator never spends a subprocess.run on is_genuine(), as a current release accepting every gated_options, and under a polkit sharing what it keeps (unshared_release()). That keeps the call counts these tests assert on measuring credential probes alone, and keeps a pretended host from probing the real one. A test about an impostor or an old release patches those methods itself instead.

A test that plants a stand-in escalator binary on PATH runs under this context. write_fake_executable answers every argv the same way, so the stand-in fails the identity probe and resolve_escalator falls through to the escalator of the host: run0 on a systemd runner, none on macOS. The test then passes locally and fails in CI. Clearing the cache alone makes that failure deterministic and does not remove it.

tests.test_sudo.prime_sudo_env(*, root=False, stdin_tty=None, stderr_tty=None)[source]

Patch the whole environment prime_sudo probes, yielding the run mock.

Pins the effective user (root) and the terminal state (None leaves the real descriptor unpatched, for tests that never reach the TTY check), and replaces subprocess.run so no test ever launches a real sudo. Callers set the mock’s return_value/side_effect to shape the probe and prompt outcomes.

The escalator is pinned to sudo rather than detected, since a Windows runner carries none on PATH and detection finding nothing would return from prime_sudo before the paths these tests exercise. A test wanting a different host nests its own only_escalator(), whose patch then wins.

tests.test_sudo.test_prime_sudo_skips_when_no_manager_escalates()[source]
tests.test_sudo.test_prime_sudo_skips_a_run_of_unprivileged_operations()[source]

A manager forced to escalate asks for nothing in a run without need.

Such a run reaches none of its privileged operations.

The case of gem under [mpm.overrides.gem] sudo = true: its sync runs gem sources --update, which carries no marker, so the password mpm sync would collect on a terminal serves no command.

tests.test_sudo.test_prime_sudo_counts_an_internal_escalator_on_its_policy(caplog, resets, prompted)[source]

A forced policy keeps an internal escalator in the prompt of every run.

mpm cannot see which of its commands call sudo (fink does it on sync), so the operations of the run leave it in.

A tool that drops the cached credentials before its own sudo is left out: the password would serve nothing, so one warning says the policy does nothing for it.

tests.test_sudo.test_prime_sudo_leaves_a_stock_windows_run_alone()[source]

A stock Windows run spends no subprocess, with no platform guard in the way.

What returns early is the empty selection, not the platform: no manager escalates by default on Windows, so only --sudo or a [mpm.overrides.<id>] sudo = true entry reaches the probe, which is what lets gsudo be probed at all when one does.

tests.test_sudo.test_prime_sudo_probes_gsudo_when_windows_escalation_is_asked_for()[source]

A manager forced to escalate reaches the gsudo probe.

The probe asks its status rather than trying an elevation that would raise a UAC dialog.

tests.test_sudo.test_prime_sudo_skips_when_root()[source]
tests.test_sudo.test_prime_sudo_skips_on_simulation(simulation_flag)[source]

Dry and plan runs both skip the sudo prompt.

Neither executes a state-changing CLI.

tests.test_sudo.test_prime_sudo_warns_without_tty(caplog)[source]

A cold cache off-terminal: the probe runs, then one warning.

The warning names the managers left to fail fast instead of blocking on a prompt.

tests.test_sudo.test_prime_sudo_skips_the_prompt_under_a_passwordless_policy(caplog)[source]

A cold cache whose policy still runs every escalated command unauthenticated.

sudo --validate refuses whenever any matching sudoers entry wants a password, so the NOPASSWD rule docs/sudo.md recommends reads as a cold cache once a distribution’s stock ALL ALL=(ALL) ALL sits beside it. openSUSE ships exactly that pair, where mpm would otherwise warn that managers “may fail” and then watch them succeed. Off-terminal on purpose: that is the path such a warning comes from, and a policy needing no password needs no terminal either.

tests.test_sudo.test_passwordless_probe_asks_about_escalated_binaries_alone(tmp_path)[source]

The policy probe names the binary of each manager mpm escalates.

It skips a manager whose commands run unescalated.

tests.test_sudo.test_passwordless_probe_reads_the_matched_rule(stdout, passwordless)[source]

The policy probe reads the rule that runs the command, where sudo prints it.

Each probe exits 0: sudo lists without a password as soon as one rule of the user carries NOPASSWD. The first two outputs are those of sudo 1.9.17p2 on Omarchy 4.5.0, whose stock policy asks a password for pacman, then with a NOPASSWD drop-in for pacman. sudo before 1.9.15 and sudo-rs print the path of the command alone.

tests.test_sudo.test_escalation_probes_run_in_the_forced_locale(monkeypatch, probe)[source]

Each probe whose answer mpm matches runs in the forced locale.

sudo translates a password is required, which its failure markers match. The user’s LANGUAGE is removed, as gettext reads it before LC_ALL.

tests.test_sudo.test_passwordless_probe_answers_no_when_unsure(monkeypatch, binary_found, probe_error)[source]

A manager without a binary, or a failed policy probe, answers False.

That covers an escalated manager whose binary was not found, and a policy probe that cannot run. The cold-cache path then handles the run.

tests.test_sudo.test_prime_sudo_authenticates_and_keeps_alive_on_tty()[source]

A cold cache on a terminal: probe, prompt, then keepalive.

The prompt is one branded password prompt, and the keepalive runs until the context closes.

tests.test_sudo.test_prime_sudo_is_idempotent()[source]

A second call on the same context is a no-op: one probe, ever.

tests.test_sudo.test_prime_sudo_warm_probe_stays_silent_on_tty(capsys)[source]

A warm credential cache gets no notice and no prompt, just a keepalive.

The cache is pre-authenticated or NOPASSWD, and the keepalive runs until the context closes.

tests.test_sudo.test_prime_sudo_warm_probe_keeps_alive_off_tty(caplog)[source]

The probe short-circuits before the terminal check.

A CI job with pre-cached credentials gets the keepalive instead of the no-terminal warning.

tests.test_sudo.test_keepalive_drop_warns_once_and_rearms(monkeypatch, caplog)[source]

A refresh finding the credentials gone clears the warm flag.

Homebrew resets them on purpose. Each drop gets one warning, and a later successful refresh sets the flag back, saying so at INFO.

tests.test_sudo.test_keepalive_refresh_of_warm_credentials_stays_at_debug(monkeypatch, caplog)[source]

A refresh finding the credentials still there keeps the flag set.

It says so at DEBUG alone: a routine tick is no news for the user.

tests.test_sudo.test_keepalive_drops_a_refresh_answered_after_the_stop_request(monkeypatch, caplog)[source]

A refresh still running when the run ends drops its answer.

The teardown clears the warm flag once it has joined the thread. An answer that lands after the stop request is about credentials the run no longer holds, so it must neither set the flag back nor report on them.

The refresh waits for the stop request before it answers, as a sudo slow to answer would. The module’s threading is replaced by a stand-in that records the stop event the keepalive creates, so the fake probe can wait for it.

tests.test_sudo.test_prime_sudo_narrates_warm_probe_at_info(caplog)[source]

A warm probe and the keepalive arming both tell their story at INFO.

A --verbosity INFO run then shows why no password prompt appeared.

tests.test_sudo.test_prime_sudo_narrates_internal_only_skip_at_info(caplog)[source]

Not prompting for an internal-only selection surfaces at INFO.

It never surfaces as a warning.

tests.test_sudo.test_prime_sudo_cold_internal_only_never_prompts_on_tty(capsys, caplog)[source]

A cold cache with only internal escalators probes, then never prompts.

A stock cask/fink selection is one: most such runs never escalate, and the stall notice covers the rare mid-run prompt instead.

tests.test_sudo.test_prime_sudo_cold_internal_only_stays_silent_off_tty(caplog)[source]

Off-terminal, an internal-only selection gets no warning.

Each manager’s own sudo fails fast and surfaces through its error path.

tests.test_sudo.test_prime_sudo_denied_user_skips_prompt(caplog)[source]

A probe reporting a sudoers denial skips the password prompt.

It does so even on a terminal: a password could only be collected then rejected. One warning names the managers and the remedy.

tests.test_sudo.test_prime_sudo_denied_doas_probe_speaks_for_its_command_alone(caplog)[source]

doas rules can match single commands, so the warning stays mild.

permit nopass tester as root cmd /sbin/apk denies the doas -n true probe while it runs apk unprompted, measured on Alpine’s doas 6.8.2. So the warning does not say the managers will fail, and no prompt is raised.

tests.test_sudo.test_prime_sudo_denied_internal_only_stays_silent(caplog)[source]

A sudoers denial with only internal escalators selected stays silent.

Each manager’s own sudo surfaces the denial through its error path.

tests.test_sudo.test_prime_sudo_audits_escalated_binaries(tmp_path, caplog, mode, expect_warning)[source]

The binary of an escalated manager gets the config-file tamper test.

Each manager gets one warning when others can modify it, and the escalation still proceeds (the probe still runs).

tests.test_sudo.test_prime_sudo_warns_when_sudo_cannot_run(caplog, probe_error)[source]

A UNIX host with a missing or non-executable sudo gets one warning.

The probe does not crash prime_sudo. Both cases raise an OSError subclass from the probe.

tests.test_sudo.test_prime_sudo_notice_names_managers_and_subcommand(capsys, manager_ids, expected_notice, expected_prompt)[source]

The password notice names the escalating managers and the subcommand.

The managers are sorted, with a verb agreeing in number. The sudo prompt is branded alike.

tests.test_sudo.test_sudo_prompt_respects_sudo_constraints(manager_ids)[source]

The --prompt argument of sudo stays within sudo’s constraints.

That holds whatever the prompt copy becomes.

Locks the properties rather than the wording, so a future rewording trips here only if it breaks sudo: the only expandable escapes left are the ones the prompt hands to sudo on purpose, every other % being doubled (sudo –prompt``expands``%h`/%H/%p/%u/%U, and a lone``%` is undefined), the prompt must be a single line (a newline would detach the ask from the input cursor), and it must end with a space so the typed password is not glued to the text.

A manager id is the untrusted half here: whatever it contains, it may never reach sudo as an escape.

tests.test_sudo.test_sudo_prompt_names_the_account_that_owns_the_password()[source]

The prompt defers to sudo’s %p for whose password is wanted.

Not the caller: a targetpw, rootpw or runaspw policy asks for another account, and openSUSE ships Defaults targetpw, so a prompt naming the invoking user sends every one of its users to type the wrong password.

tests.test_sudo.RUN0_REFUSAL = 'Failed to start transient service unit: Access denied as the requested operation requires interactive authentication. However, interactive authentication has not been enabled by the calling program.'

What run0 writes to <stderr> when polkit refuses a --no-ask-password call, measured on Arch Linux running systemd 261.

Signed by neither run0 nor any sudo wording, because systemd’s bus layer words it: the matchers need a branch of their own to see it at all.

tests.test_sudo.PKEXEC_NO_AGENT = "Error creating textual authentication agent: Error opening current controlling terminal for the process (`/dev/tty'): No such device or address"

What pkexec writes to <stderr> off a terminal, measured over SSH on Arch Linux running polkit 127.

It raises its own textual agent when no other answers, and that agent needs a controlling terminal. Like run0’s, the message names neither the escalator nor a sudo wording.

tests.test_sudo.test_is_sudo_auth_failure(error, expected)[source]
tests.test_sudo.test_is_sudo_denied(error, expected)[source]
tests.test_sudo.test_is_permission_failure(error, expected)[source]
tests.test_sudo.test_inspect_install_root(tmp_path)[source]

The snapshot carries the root’s owner; every dead end answers None.

tests.test_sudo.test_inspect_install_root_swallows_probe_failures()[source]

A probe that dies means “unknown”, never a crash of the command it decorates.

tests.test_sudo.test_inspect_install_root_needs_posix_ownership(monkeypatch, tmp_path)[source]

A host whose os has no getuid reports no install root.

It reports none even for a root that exists, as on Windows.

The module’s os is replaced by a stand-in without getuid, so the check runs on every platform: on POSIX, the real module would report the root.

tests.test_sudo.test_inspect_install_root_names_an_unknown_owner_by_uid(monkeypatch, tmp_path)[source]

A root owned by a uid with no account names its owner by the bare uid.

Files unpacked from a container image are such a case.

tests.test_sudo.test_install_root_probes_run_the_documented_verb(manager_id, expected_argv)[source]

Each override resolves its root through its own discovery verb.

Forced to run for real (force_exec), like the version probe.

tests.test_sudo.test_escalator_registry_prefers_sudo()[source]

sudo stays the first choice.

A host carrying several then keeps the behavior it has today, and only an explicit override moves it.

run0 sits behind them on purpose: it authorizes through polkit and needs one running, so it answers for the systemd hosts shipping neither of the others rather than displacing a working escalator.

win-sudo closes the list, behind gsudo. Microsoft’s sudo.exe is inbox from 24H2 where gsudo must be installed, but it caches nothing, so it is the fallback for a host carrying no gsudo rather than the default for one carrying both.

tests.test_sudo.test_escalator_binary_name_defaults_to_its_id()[source]

Only win-sudo looks for a file under a name other than its own.

Microsoft’s sudo.exe ships under the name the Unix escalator claims while sharing none of its dialect, so the two carry distinct ids and resolve_escalator has to be told which file to look for.

tests.test_sudo.test_win_sudo_identity_rejects_every_other_sudo(returncode, stdout, expected)[source]

The help banner names the tool outright.

--version prints a bare sudo 1.0.0 that another implementation shipping a 1.x would also match.

tests.test_sudo.test_win_sudo_probe_only_accepts_inline_mode(returncode, stdout, expected)[source]

--inline errors against any other mode rather than falling back.

A mode mpm cannot drive then has to read as cold before a manager reaches it.

tests.test_sudo.test_denial_matches_both_subjects_of_the_sudo_wording(error)[source]

The marker is unanchored on the subject so one phrase covers both.

Keyed on is not allowed, Microsoft’s second-person wording went unrecognized, and prime_sudo would prompt for a consent that cannot authorize the run.

tests.test_sudo.test_gsudo_probe_reads_its_output_not_its_exit_code(returncode, stdout, expected)[source]

gsudo status exits 0 whether or not elevation is ready.

The answer is then only in its JSON.

Reading the exit code alone would report every Windows host as warm, and the up-front prompt that keeps a UAC dialog out of the concurrent fan-out would never fire.

tests.test_sudo.test_run0_forwards_a_forced_environment()[source]

run0 forks its payload off the service manager, which inherits nothing.

A forced variable then has to travel as an argument or be lost.

Spliced ahead of the -- the prefix closes on, leaving a manager’s own flags shielded, and sorted so the disclosed command is stable.

tests.test_sudo.test_escalator_without_a_template_forwards_nothing(escalator)[source]

An escalator declaring no template leaves the argv alone.

sudo and doas defer to a sudoers policy the host owns.

tests.test_sudo.test_run0_names_each_unit_after_mpm_and_its_manager()[source]

Each escalated run0 call runs in a unit of its own, named after mpm.

run0 names it after its own process, so a call outliving its timeout would look like any other run0 unit. The serial keeps two calls of one run from asking for one name.

tests.test_sudo.test_escalator_without_a_unit_template_names_nothing(escalator)[source]

An escalator running the command in mpm’s own process tree names no unit.

tests.test_sudo.test_probe_without_markers_reads_the_exit_code(escalator)[source]

An escalator declaring no marker keeps the exit code as the answer.

That is what sudo, doas, run0 and pkexec report through.

tests.test_sudo.test_pkexec_probe_substitutes_the_running_pid()[source]

pkcheck asks about a subject and refuses to guess one.

The probe then carries a {pid} token mpm fills with its own.

Left unsubstituted it would reach polkit as a literal, and every probe would report a cold cache on a host that authorizes the action.

tests.test_sudo.test_probe_args_resolve_for_every_escalator(escalator)[source]

Resolving a probe leaves no placeholder behind, whether it carried one or not.

tests.test_sudo.test_prompt_args_resolve_for_every_escalator(escalator)[source]

Resolving a prompt leaves no placeholder, whether it carried one or not.

tests.test_sudo.test_run0_prompts_in_a_unit_named_after_mpm()[source]

The up-front run0 call names its unit after mpm, on a terminal of its own.

polkit’s message names the transient unit, so the name puts mpm in the prompt. --pty keeps the user’s terminal out of the service: under --pipe, systemd gives it to root, and polkit then keeps nothing of the password for the calls after the prompt.

tests.test_sudo.test_resolve_escalator_detects_what_the_host_carries(installed, expected)[source]
tests.test_sudo.test_escalator_identity_probe_reads_the_binary(returncode, stdout, expected)[source]
tests.test_sudo.test_escalator_without_identity_args_is_genuine()[source]

An escalator opts into the identity check by declaring its argv.

The ones with no known stand-in then spend no subprocess.

tests.test_sudo.test_escalator_identity_probe_survives_a_missing_binary()[source]

A binary that vanished before the probe cannot be driven.

It vanished between which() and the probe, and the run does not crash.

tests.test_sudo.test_escalator_probes_never_reach_the_terminal(probe)[source]

A stand-in may prompt, so a probe gets no terminal to prompt on.

SliTaz’s /usr/bin/sudo wraps su -c, whose root password prompt landed in the captured output and blocked every command of a non-root user.

tests.test_sudo.test_pkexec_keeps_cwd_from_polkit_121(returncode, stdout, expected)[source]

An older pkexec reads --keep-cwd as the program to run.

tests.test_sudo.test_supported_options_survive_a_missing_binary()[source]

A release that cannot be read keeps every option.

tests.test_sudo.test_escalators_without_gated_options_spend_no_probe()[source]

Only an escalator gating an option reads its release.

tests.test_sudo.test_resolve_escalator_skips_an_impostor_for_the_real_thing()[source]

Alpine’s doas-sudo-shim puts a forwarding sudo on PATH.

It forwards to doas, accepting --non-interactive and no other option mpm sends. Driving doas directly is what keeps the probes answering.

tests.test_sudo.test_resolve_escalator_keeps_sudo_on_a_sudo_rs_host()[source]

A host whose sudo is sudo-rs keeps escalating through it.

It does so even when it carries doas too.

The identity probe reads the binary’s own banner, and sudo-rs prints sudo-rs <version> where the original prints Sudo version <version>. Matching only the latter made is_genuine reject Ubuntu’s default sudo, and the search then settled on doas, inverting the preference ESCALATORS documents.

tests.test_sudo.test_resolve_escalator_falls_back_to_a_lone_impostor_off_windows(windows, expected)[source]

An impostor still escalates, so it beats reporting no escalator.

Windows is the exception.

Disabled, Microsoft’s sudo.exe fails both identity probes: its --help prints only how to enable it. The fallback then drove it as POSIX sudo, whose --non-interactive it rejects as an unknown argument, which failed every choco install on the windows-11-arm runner image. The platform is pinned both ways, so neither case depends on the host running the suite.

tests.test_sudo.test_resolve_escalator_override_wins_over_detection()[source]

An override is honored even when its binary is missing.

The failure then names the escalator the user asked for.

tests.test_sudo.test_resolve_escalator_rejects_an_unknown_name(caplog)[source]
tests.test_sudo.test_escalator_choice_is_machine_level()[source]

The escalator is process-wide, never a manager attribute.

A machine runs one of them, and every manager escalating on it uses that one.

Guards both halves. No manager may carry the choice as an attribute, and the per-manager config section must refuse it, so [mpm.overrides.<id>] can never make one manager escalate differently from its peers.

tests.test_sudo.test_per_manager_sudo_command_is_refused(invoke, tmp_path)[source]

A [mpm.overrides.<id>] sudo_command entry is rejected outright.

The error names the fields that are per-manager, rather than silently escalating one manager through another binary.

tests.test_sudo.test_sudo_command_reaches_the_config_file(invoke, tmp_path)[source]

[mpm] sudo_command is honored, and validated like the flag.

It takes the same choices: an unknown escalator is a usage error, not a silent fallback.

tests.test_sudo.test_build_cli_escalates_with_the_selected_binary(escalator_id, expected_prefix)[source]
tests.test_sudo.test_build_cli_skips_escalation_without_an_escalator()[source]

A host carrying neither binary runs the command unprivileged.

It does not prefix a sudo that is not there.

tests.test_sudo.test_prime_sudo_warns_without_any_escalator(caplog)[source]
tests.test_sudo.test_prime_sudo_probes_and_prompts_through_doas(capsys)[source]

A doas host is probed with doas -n true.

On a cold cache, it is prompted with a bare doas true: it cannot be told what prompt to print, so the echoed notice carries the explanation instead.

tests.test_sudo.test_doas_gets_no_keepalive_thread(caplog)[source]

A doas cache is marked warm, without a thread refreshing it.

doas persistence is opt-in per rule, so a recurring probe would report a drop on every tick of a host that never asked for it.

tests.test_sudo.test_prime_sudo_names_the_remedy_without_tty(escalator, caplog)[source]

Off a terminal, the warning closes on what the escalator’s policy needs.

It does not close on a pre-authentication, which most escalators do not keep.

tests.test_sudo.PROMPT_BLIND = [Escalator(id='sudo', escalate_args=('sudo', '--non-interactive'), probe_args=('sudo', '--non-interactive', '--validate'), passwordless_probe_args=('sudo', '--non-interactive', '--list', '--list', '--'), prompt_args=('sudo', '--validate'), refreshable=True, brands_prompt=True, remedy='Allow the escalated commands without a password with a `NOPASSWD` rule, or authenticate first with `sudo --validate` in the same terminal.', binary=None, identity_args=('sudo', '--version'), identity_markers=('Sudo version', 'sudo-rs'), retention_markers=None, retention_probe_args=None, retention_since=None, probe_command=None, probe_success_markers=None, rule_markers=(re.compile('^\\s*Matched:', re.MULTILINE), re.compile('^\\s*Options:.*!authenticate\\b', re.MULTILINE)), env_forward_template=None, unit_template=None, gated_options=(), version_args=None), Escalator(id='doas', escalate_args=('doas', '-n'), probe_args=('doas', '-n', 'true'), passwordless_probe_args=None, prompt_args=('doas', 'true'), refreshable=False, brands_prompt=False, remedy='Add `persist` to the matching `doas.conf` rule to keep the authorization, or `nopass` to need no password.', binary=None, identity_args=None, identity_markers=None, retention_markers=None, retention_probe_args=None, retention_since=None, probe_command='true', probe_success_markers=None, rule_markers=None, env_forward_template=None, unit_template=None, gated_options=(), version_args=None), Escalator(id='gsudo', escalate_args=('gsudo',), probe_args=('gsudo', 'status', '--json'), passwordless_probe_args=None, prompt_args=('gsudo', 'cache', 'on', '--pid', '0'), refreshable=False, brands_prompt=False, remedy='Warm its cache first with `gsudo cache on --pid 0`.', binary=None, identity_args=('gsudo', '--version'), identity_markers=('gsudo',), retention_markers=None, retention_probe_args=None, retention_since=None, probe_command=None, probe_success_markers=('"IsElevated":true', '"CacheAvailable":true'), rule_markers=None, env_forward_template=None, unit_template=None, gated_options=(), version_args=None), Escalator(id='win-sudo', escalate_args=('sudo', '--inline', '--preserve-env'), probe_args=('reg', 'query', 'HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Sudo', '/v', 'Enabled'), passwordless_probe_args=None, prompt_args=('sudo', 'config'), refreshable=False, brands_prompt=False, remedy='Set Windows `sudo` to inline mode in Settings, or install `gsudo`, whose cache one UAC dialog warms for the whole run.', binary='sudo', identity_args=('sudo', '--help'), identity_markers=('Sudo for Windows',), retention_markers=None, retention_probe_args=None, retention_since=None, probe_command=None, probe_success_markers=('0x3',), rule_markers=None, env_forward_template=None, unit_template=None, gated_options=(), version_args=None)]

Escalators whose probe cannot tell ahead of a prompt whether it would keep the authorization.

tests.test_sudo.test_prime_sudo_warns_when_the_prompt_keeps_nothing(escalator, caplog)[source]

A prompt answered, then a probe still cold: the policy keeps nothing.

doas without persist and run0 over SSH are two such policies. One warning names the remedy, and no keepalive pretends otherwise.

tests.test_sudo.test_prime_sudo_prompts_only_when_polkit_keeps_it(caplog, probe_stdout, prompted)[source]

polkit says ahead of a prompt whether it keeps what the password buys.

Under auth_admin it keeps nothing, so no password is asked for at all.

tests.test_sudo.test_prime_sudo_asks_polkit_before_prompting_for_run0(caplog, pkcheck_stdout, prompted)[source]

pkcheck asks polkit about the action run0 authorizes against.

run0’s own probe cannot tell whether polkit keeps what a password buys.

tests.test_sudo.test_run0_reads_whether_polkit_shares_what_it_keeps(answer, expected)[source]

Before polkit 127, a kept authorization serves the one run0 call that asked.

A release that cannot be read counts as recent: the probe after the prompt still reports a drop. The banner of polkit 127 is the one measured on Fedora 44.

tests.test_sudo.test_prime_sudo_asks_no_run0_password_under_a_polkit_before_127(caplog)[source]

polkit 126 keeps what a password buys for the run0 call that asked alone.

So no password is asked for, and one warning names the fix.

tests.test_sudo.test_every_escalator_names_a_remedy(escalator)[source]

Warnings go on after the remedy, so it is a sentence of its own.

tests.test_sudo.test_credential_probe_keeps_the_terminal_but_not_its_input()[source]

The probe stays in mpm’s session, where sudo keys its cache.

sudo keys its credential cache on the controlling terminal. A stand-in asking for a password reads nothing instead of waiting.

tests.test_sudo.test_override_warns_on_a_stand_in(caplog, on_path, genuine, warns)[source]

--sudo-command is honored even for a stand-in, with one warning.

SliTaz’s sudo wraps su -c and exits 0 whatever happened.

tests.test_sudo.test_diagnose_escalation_on_slitaz()[source]

The whole report of a SliTaz host.

Auto-detection passes over the su wrapper there, and polkit would keep nothing a prompt buys.

tests.test_sudo.test_diagnose_escalation_as_root()[source]

Root runs every command as is, so nothing is probed.

tests.test_sudo.test_diagnose_doas_credentials(probe, expected)[source]

What the doas credential probe found, as the report words it.

tests.test_sudo.test_diagnose_escalation_without_escalator()[source]

A host carrying none names the ones mpm looks for, and the way out.

tests.test_sudo.test_diagnose_escalation_tells_unrunnable_from_stand_in()[source]

A binary that cannot run is no stand-in: the report says which it is.

tests.test_sudo.test_diagnose_escalation_under_a_passwordless_policy()[source]

A cold cache over a policy that clears every command is passwordless.

The credential cache is cold, but the policy clears every escalated command: no password to type, so no fix to apply.

The internal escalator beside it gets a line of its own, and stays out of the policy probe, which asks about the commands mpm escalates alone.

tests.test_sudo.test_diagnose_escalation_when_polkit_keeps_the_authorization()[source]

polkit under auth_admin_keep keeps what a password buys.

The report then says a prompt would hold for the whole run.

The pkcheck answer of the credential probe is the one measured on Fedora 44 with polkit 127.

tests.test_sudo.test_diagnose_run0_under_a_polkit_before_127()[source]

polkit 126 keeps what a password buys for the one run0 call that asked.

The report then says a prompt would not hold, even under auth_admin_keep. The run0 --version banner is the one measured on Fedora 44.

tests.test_sudo.test_diagnose_escalation_flags_a_chosen_stand_in()[source]

A stand-in named with --sudo-command is driven anyway.

The report then warns that its answers may be misread.

SliTaz’s wrapper exits 0 on the credential probe, which reads as ready: the identity line is what tells the user not to trust it.

tests.test_sudo.test_stall_watchdog_notices_silent_internal_escalator(monkeypatch, caplog)[source]

A silent internal escalator call on a cold cache warns once per silence.

The call is a mutating one, on a terminal. The warning names the manager and quotes its last output line.

tests.test_sudo.test_stall_watchdog_ignores_the_command_line(monkeypatch, caplog)[source]

The command line run_cli discloses is no output of the child.

A child that prints nothing gets the notice of a silence since the start, which quotes no line.

tests.test_sudo.test_stall_watchdog_notice_starts_on_a_fresh_line(monkeypatch, capsys)[source]

The notice breaks the line before writing, sparing a prompt sitting on it.

A tool’s own sudo writes Password: to /dev/tty with no trailing newline, and that never reaches the pipe mpm reads, so a notice written straight to stderr runs on from it as Password:warning:cask: No output for 30s… and buries the prompt mid-sentence.

The child here prints nothing, so its single silence episode leaves stderr carrying the notice alone: a leading newline is then the whole difference.

tests.test_sudo.test_stall_watchdog_notice_without_an_output_line(monkeypatch, caplog)[source]

A silence that started with the call has no line to quote, and says so.

The watchdog is armed alone, with no call behind it to feed it a line, and with no terminal to read: an echo the host terminal holds off would hold the notice back.

tests.test_sudo.test_stall_watchdog_reports_a_record_it_cannot_format(monkeypatch)[source]

A record that cannot be formatted goes to the handler’s error report.

logging expects that of every handler. The record is not raised into the call.

tests.test_sudo.terminal(monkeypatch)[source]

A pseudo-terminal that answers for the controlling one, as its path.

A test run has no terminal to hold in a password read, and under CI it has none at all, so the watchdog is pointed at this one.

tests.test_sudo.test_stall_watchdog_holds_the_notice_back_for_a_prompt_on_screen(monkeypatch, caplog, terminal, echo_at_start, prompts, held_back)[source]

A password prompt the terminal shows gets no notice written below it.

The notice ends on a newline, so it leaves the cursor on an empty line under the warning, with the prompt it must answer two lines above. The watchdog reads a prompt from the echo the child turned off, and stays silent.

The two controls prove the echo is what holds the notice back. A child that leaves the echo on draws the notice. So does one that finds it off already, since an echo that was never on proves nothing about a prompt.

A slow interpreter startup may trip a no-output-yet notice before the child reaches the terminal, so the assertions read the episode that quotes its line: the echo is off by the time that line is printed.

tests.test_sudo.test_timeout_names_an_unanswered_password_prompt(caplog, capsys, terminal, echo_at_start, prompts, named)[source]

A call that times out on a password prompt says so, and how to go on.

The kill leaves the echo as the child held it, so the timeout reads the prompt from the terminal the way the watchdog does. The prompt also holds the terminal line with no line break after it: the call ends that line, so the warning does not run on from Password:. The ✘ trail line of the attempt names the cause too. The killed child could not turn the echo back on, so the call does: it leaves the terminal as it found it.

The two controls time out the same way and keep the plain wording: a child that never touches the echo, and one that finds it off already.

tests.test_sudo.test_terminal_helpers_answer_nothing_on_windows(monkeypatch)[source]

Windows has no termios: the terminal reads as no modes.

Nothing is given back to it, and no echo is reported, before any terminal is opened.

The module’s sys is replaced by a stand-in naming Windows, so the check runs on every platform. On Windows, os.ctermid does not even exist.

tests.test_sudo.test_terminal_echo_reads_the_local_flags(echoes)[source]

The echo is the ECHO bit of the local flags.

The local flags are the fourth item termios.tcgetattr returns. A password read clears the bit, and a line read keeps it.

tests.test_sudo.test_terminal_modes_survive_a_terminal_that_cannot_answer(monkeypatch, tmp_path, present)[source]

A terminal that cannot answer reads as no modes, and gets none back.

The watchdog only reads the controlling terminal to word a warning, so it must never fail the call it watches.

A regular file stands in for a device that refuses the terminal requests, and a missing path for a terminal that went away during the call. The signal mask blocked around the write comes back as it was.

tests.test_sudo.test_stall_watchdog_negative_gates(monkeypatch, caplog, active_operation, internal_sudo, warm_cache, tty)[source]

Each arming gate individually disarms the watchdog.

No notice fires even when the call stays silent for far longer than the (shortened) delay.

tests.test_sudo.test_hidden_prompt_risk_holds_the_spinner_still(internal_sudo, warm, resets, expected_live)[source]

A call that may hide a sudo prompt builds its spinner disabled.

This leaves the terminal line the tool writes that prompt on: an animation repainting it erases the prompt and leaves the run to die at the mutating timeout. The control differs by the internal_sudo gate alone, and keeps live="auto", the auto-detected animation of an ordinary call.

A warm cache animates the call again, since the tool’s own sudo spends it without a prompt. A tool that drops the cache before that sudo prompts whatever the cache holds, so its call stays still.

tests.test_sudo.test_still_call_prints_its_label_once(monkeypatch, capsys, internal_sudo, progress, announced)[source]

A call whose spinner is held still names itself on a static line instead.

Without the line the terminal stays blank for the whole call. The line says a password may be asked, and ends before the child starts, so a prompt the tool raises lands on a fresh line below that hint.

tests.test_sudo.test_stall_watchdog_tee_gates_debug_lines_at_default_verbosity(caplog)[source]

An armed call still hides child output at the default WARNING level.

The tee forwards lines through the root logger’s level gate instead of bypassing it.

tests.test_sudo.test_stall_watchdog_tee_forwards_verbatim_at_debug(caplog)[source]

At DEBUG, an armed call streams child lines through the tee.

The lines keep the message, level and manager label of an un-teed run.