| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ansible._internal._powershell import _script |
| 6 |
|
| 7 |
|
| 8 |
# print_argv is a binary that echoes back the argv it receives. |
| 9 |
ENCODED_CMD_CASES = [ |
| 10 |
( |
| 11 |
# print_argv {'foo'} |
| 12 |
"'foo'", |
| 13 |
[], |
| 14 |
['-EncodedCommand', 'JwBmAG8AbwAnAA=='], |
| 15 |
), |
| 16 |
( |
| 17 |
# print_argv {'foo'} -args 'simple', '_x005F_', ([char]::ConvertFromUtf32(0x1F3B5)) |
| 18 |
"'foo'", |
| 19 |
['simple', '_x005F_', "\U0001F3B5"], |
| 20 |
['-EncodedCommand', 'JwBmAG8AbwAnAA==', '-EncodedArguments', ( |
| 21 |
'PABPAGIAagBzACAAeABtAGwAbgBzAD0AIgBoAHQAdABwADoALwAvAHMAYwBoAGUAbQBhAHMALgBtAGkAYwByAG8AcwBvAGYAdAAuAGMAb' |
| 22 |
'wBtAC8AcABvAHcAZQByAHMAaABlAGwAbAAvADIAMAAwADQALwAwADQAIgAgAFYAZQByAHMAaQBvAG4APQAiADEALgAxAC4AMAAuADEAIg' |
| 23 |
'A+ADwATwBiAGoAIABSAGUAZgBJAGQAPQAiADAAIgA+ADwAVABOACAAUgBlAGYASQBkAD0AIgAwACIAPgA8AFQAPgBTAHkAcwB0AGUAbQA' |
| 24 |
'uAEMAbwBsAGwAZQBjAHQAaQBvAG4AcwAuAEEAcgByAGEAeQBMAGkAcwB0ADwALwBUAD4APABUAD4AUwB5AHMAdABlAG0ALgBPAGIAagBl' |
| 25 |
'AGMAdAA8AC8AVAA+ADwALwBUAE4APgA8AEwAUwBUAD4APABTAD4AcwBpAG0AcABsAGUAPAAvAFMAPgA8AFMAPgBfAHgAMAAwADUARgBfA' |
| 26 |
'HgAMAAwADUARgBfADwALwBTAD4APABTAD4AXwB4AEQAOAAzAEMAXwBfAHgARABGAEIANQBfADwALwBTAD4APAAvAEwAUwBUAD4APAAvAE' |
| 27 |
'8AYgBqAD4APAAvAE8AYgBqAHMAPgA=' |
| 28 |
)], |
| 29 |
) |
| 30 |
] |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize('value, expected', [ |
| 34 |
('foo', 'foo'), |
| 35 |
('foo_bar', 'foo_bar'), |
| 36 |
('foo-bar', 'foo-bar'), |
| 37 |
('123', '123'), |
| 38 |
(r'C:\temp\pwsh.exe', r'C:\temp\pwsh.exe'), |
| 39 |
('C:/temp/pwsh.exe', 'C:/temp/pwsh.exe'), |
| 40 |
('', "''"), |
| 41 |
('foo bar', "'foo bar'"), |
| 42 |
('@foo', "'@foo'"), |
| 43 |
("foo'bar", "'foo''bar'"), |
| 44 |
("foo\u2018bar", "'foo\u2018\u2018bar'"), |
| 45 |
("foo\u2019bar", "'foo\u2019\u2019bar'"), |
| 46 |
("foo\u201abar", "'foo\u201a\u201abar'"), |
| 47 |
("foo\u201bbar", "'foo\u201b\u201bbar'"), |
| 48 |
]) |
| 49 |
def test_quote_argument(value, expected): |
| 50 |
actual = _script.quote_pwsh_argument(value) |
| 51 |
assert actual == expected |
| 52 |
|
| 53 |
|
| 54 |
def test_quote_argument_force(): |
| 55 |
actual = _script.quote_pwsh_argument('foo', force_quote=True) |
| 56 |
assert actual == "'foo'" |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize('cmd, args, expected', ENCODED_CMD_CASES) |
| 60 |
def test_build_encoded_command(cmd, args, expected): |
| 61 |
actual = _script._get_encoded_arguments(cmd, args) |
| 62 |
assert actual == expected |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize('expected_cmd, expected_args, cmd_args', ENCODED_CMD_CASES) |
| 66 |
def test_parse_encoded_command(expected_cmd, expected_args, cmd_args): |
| 67 |
actual_cmd, actual_args = _script.parse_encoded_cmdline(" ".join(cmd_args)) |
| 68 |
assert actual_cmd == expected_cmd |
| 69 |
assert actual_args == expected_args |
| 70 |
|
| 71 |
|
| 72 |
def test_parse_encoded_command_no_encoded_command(): |
| 73 |
actual = _script.parse_encoded_cmdline('pwsh -EncodedArguments YQA=') |
| 74 |
assert actual is None |
| 75 |
|
| 76 |
|
| 77 |
def test_parse_encoded_command_no_value(): |
| 78 |
actual = _script.parse_encoded_cmdline('pwsh -EncodedCommand') |
| 79 |
assert actual is None |
| 80 |
|
| 81 |
|
| 82 |
def test_parse_encoded_command_no_args(): |
| 83 |
actual_script, actual_args = _script.parse_encoded_cmdline('pwsh -EncodedCommand YQA= foo') |
| 84 |
assert actual_script == 'a' |
| 85 |
assert actual_args == [] |
| 86 |
|
| 87 |
|
| 88 |
def test_parse_encoded_command_quoted(): |
| 89 |
actual_script, actual_args = _script.parse_encoded_cmdline("pwsh '-EncodedCommand' YQA= foo") |
| 90 |
assert actual_script == 'a' |
| 91 |
assert actual_args == [] |
| 92 |
|
| 93 |
|
| 94 |
def test_parse_encoded_command_no_value_after_args(): |
| 95 |
actual = _script.parse_encoded_cmdline('pwsh -EncodedCommand YQA= -EncodedArguments') |
| 96 |
assert actual is None |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.parametrize( |
| 100 |
('cmd', 'args', 'expected'), |
| 101 |
( |
| 102 |
pytest.param( |
| 103 |
'foo.exe', |
| 104 |
None, |
| 105 |
'foo.exe', |
| 106 |
id='no-args-relative-path', |
| 107 |
), |
| 108 |
pytest.param( |
| 109 |
r'C:\Program Files\foo.exe', |
| 110 |
None, |
| 111 |
r"& 'C:\Program Files\foo.exe'", |
| 112 |
id='no-args-absolute-path', |
| 113 |
), |
| 114 |
pytest.param( |
| 115 |
'foo.exe', |
| 116 |
['simple', 'with space'], |
| 117 |
"foo.exe simple 'with space'", |
| 118 |
id='whitespace', |
| 119 |
), |
| 120 |
pytest.param( |
| 121 |
'foo.exe', |
| 122 |
["with 'single' quote"], |
| 123 |
"foo.exe 'with ''single'' quote'", |
| 124 |
id='single-quote', |
| 125 |
), |
| 126 |
pytest.param( |
| 127 |
'C:/path with space/test', |
| 128 |
['arg1', 'arg 2'], |
| 129 |
"& 'C:/path with space/test' arg1 'arg 2'", |
| 130 |
id='spaced-path', |
| 131 |
), |
| 132 |
), |
| 133 |
) |
| 134 |
def test_build_pwsh_cmd_statement(cmd, args, expected): |
| 135 |
actual = _script.build_pwsh_cmd_statement(cmd, args) |
| 136 |
assert actual == expected |