|
7 | 7 | import os |
8 | 8 | import pytest |
9 | 9 | import re |
| 10 | +import sys |
10 | 11 | import time |
11 | 12 |
|
12 | 13 | import debugpy |
13 | 14 | from debugpy.common import log, messaging |
14 | | -from tests import debug, test_data |
| 15 | +from tests import debug, test_data, timeline |
15 | 16 | from tests.debug import runners, targets |
16 | 17 | from tests.patterns import some |
17 | 18 |
|
@@ -90,6 +91,50 @@ def code_to_debug(): |
90 | 91 | assert "ok" in session.output("stdout") |
91 | 92 |
|
92 | 93 |
|
| 94 | +@pytest.mark.skipif(sys.platform == "win32", reason="sudo not available on Windows") |
| 95 | +@pytest.mark.parametrize("run", runners.all_launch) |
| 96 | +def test_sudo(pyfile, tmpdir, run, target): |
| 97 | + # Since the test can't rely on sudo being allowed for the user, create a dummy |
| 98 | + # sudo script that doesn't actually elevate, but sets an environment variable |
| 99 | + # that can be checked in the debuggee. |
| 100 | + sudo = tmpdir / "sudo" |
| 101 | + sudo.write( |
| 102 | + """#!/bin/sh |
| 103 | + exec env DEBUGPY_SUDO=1 "$@" |
| 104 | + """ |
| 105 | + ) |
| 106 | + os.chmod(sudo.strpath, 0o777) |
| 107 | + |
| 108 | + @pyfile |
| 109 | + def code_to_debug(): |
| 110 | + import os |
| 111 | + |
| 112 | + import debuggee |
| 113 | + from debuggee import backchannel |
| 114 | + |
| 115 | + debuggee.setup() |
| 116 | + backchannel.send(os.getenv("DEBUGPY_SUDO", "0")) |
| 117 | + |
| 118 | + with debug.Session() as session: |
| 119 | + session.config["sudo"] = True |
| 120 | + session.config.env["PATH"] = tmpdir.strpath + ":" + os.environ["PATH"] |
| 121 | + |
| 122 | + backchannel = session.open_backchannel() |
| 123 | + with run(session, target(code_to_debug)): |
| 124 | + pass |
| 125 | + |
| 126 | + # The "runInTerminal" request sent by the adapter to spawn the launcher, |
| 127 | + # if any, shouldn't be using sudo. |
| 128 | + assert all( |
| 129 | + "sudo" not in req.arguments["args"] |
| 130 | + for req in session.all_occurrences_of(timeline.Request("runInTerminal")) |
| 131 | + ) |
| 132 | + |
| 133 | + # The launcher, however, should use our dummy sudo to spawn the debuggee, |
| 134 | + # and the debuggee should report the environment variable accordingly. |
| 135 | + assert backchannel.receive() == "1" |
| 136 | + |
| 137 | + |
93 | 138 | @pytest.mark.parametrize( |
94 | 139 | # Can't test "internalConsole", because we don't have debuggee stdin to press the key. |
95 | 140 | "run", |
|
0 commit comments