| 1 |
# Copyright (C) 2014 Red Hat, Inc. |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Author: Stef Walter <[email protected]> |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from . import authorize |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize( |
| 11 |
("challenge", "expected_type", "expected_remainder"), |
| 12 |
[ |
| 13 |
("valid", "valid", ""), |
| 14 |
("Basic more-data", "basic", "more-data"), |
| 15 |
("Basic more-data", "basic", "more-data"), |
| 16 |
("valid:test", "valid", "test"), |
| 17 |
("valid1:", "valid1", ""), |
| 18 |
("valid2:test:test", "valid2", "test:test"), |
| 19 |
], |
| 20 |
) |
| 21 |
def test_parse_type( |
| 22 |
challenge: str, expected_type: str, expected_remainder: str |
| 23 |
) -> None: |
| 24 |
auth_type, remainder = authorize.parse_type(challenge) |
| 25 |
assert auth_type == expected_type |
| 26 |
assert remainder == expected_remainder |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
"challenge", |
| 31 |
[ |
| 32 |
":invalid", |
| 33 |
], |
| 34 |
) |
| 35 |
def test_parse_type_invalid(challenge: str) -> None: |
| 36 |
with pytest.raises(ValueError): |
| 37 |
authorize.parse_type(challenge) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize( |
| 41 |
("challenge", "expected_subject", "expected_remainder"), |
| 42 |
[ |
| 43 |
("valid:73637275666679:", "73637275666679", ""), |
| 44 |
("valid:73637275666679:more-data", "73637275666679", "more-data"), |
| 45 |
("valid:scruffy:", "scruffy", ""), |
| 46 |
( |
| 47 |
"X-Conversation conversationtoken more-data", |
| 48 |
"conversationtoken", |
| 49 |
"more-data", |
| 50 |
), |
| 51 |
( |
| 52 |
"X-Conversation conversationtoken more-data", |
| 53 |
"conversationtoken", |
| 54 |
"more-data", |
| 55 |
), |
| 56 |
], |
| 57 |
) |
| 58 |
def test_parse_subject( |
| 59 |
challenge: str, expected_subject: str, expected_remainder: str |
| 60 |
) -> None: |
| 61 |
subject, remainder = authorize.parse_subject(challenge) |
| 62 |
assert subject == expected_subject |
| 63 |
assert remainder == expected_remainder |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
"challenge", |
| 68 |
[ |
| 69 |
"invalid:", |
| 70 |
"invalid", |
| 71 |
], |
| 72 |
) |
| 73 |
def test_parse_subject_invalid(challenge: str) -> None: |
| 74 |
with pytest.raises(ValueError): |
| 75 |
authorize.parse_subject(challenge) |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize( |
| 79 |
("response", "expected_user", "expected_password", "expected_known_hosts"), |
| 80 |
[ |
| 81 |
("Basic c2NydWZmeTp6ZXJvZw==", "scruffy", "zerog", ""), |
| 82 |
("Basic", "", "", ""), |
| 83 |
], |
| 84 |
) |
| 85 |
def test_parse_basic( |
| 86 |
response: str, expected_user: str, expected_password: str, expected_known_hosts: str |
| 87 |
) -> None: |
| 88 |
user, password, known_hosts = authorize.parse_basic(response) |
| 89 |
assert user == expected_user |
| 90 |
assert password == expected_password |
| 91 |
assert known_hosts == expected_known_hosts |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize( |
| 95 |
"response", |
| 96 |
[ |
| 97 |
"Basic!c2NydWZmeTp6ZXJvZw==", |
| 98 |
"Basic c2Nyd!!ZmeTp6ZXJvZw==", |
| 99 |
"Basic c2NydWZmeXplcm9n", |
| 100 |
], |
| 101 |
) |
| 102 |
def test_parse_basic_invalid(response: str) -> None: |
| 103 |
with pytest.raises(ValueError): |
| 104 |
authorize.parse_basic(response) |
| 105 |
|
| 106 |
|
| 107 |
def test_build_basic() -> None: |
| 108 |
assert authorize.build_basic("scruffy", "zerog") == "Basic c2NydWZmeTp6ZXJvZw==" |
| 109 |
|
| 110 |
|
| 111 |
def test_build_basic_roundtrip() -> None: |
| 112 |
result = authorize.build_basic("user", "pass") |
| 113 |
user, password, known_hosts = authorize.parse_basic(result) |
| 114 |
assert user == "user" |
| 115 |
assert password == "pass" |
| 116 |
assert known_hosts == "" |
| 117 |
|
| 118 |
|
| 119 |
def test_build_basic_none() -> None: |
| 120 |
result = authorize.build_basic(None, None) |
| 121 |
user, password, known_hosts = authorize.parse_basic(result) |
| 122 |
assert user == "" |
| 123 |
assert password == "" |
| 124 |
assert known_hosts == "" |
| 125 |
|
| 126 |
|
| 127 |
def test_build_basic_with_known_hosts() -> None: |
| 128 |
result = authorize.build_basic("user", "pass", "host1.example.com ssh-rsa AAAA...") |
| 129 |
user, password, known_hosts = authorize.parse_basic(result) |
| 130 |
assert user == "user" |
| 131 |
assert password == "pass" |
| 132 |
assert known_hosts == "host1.example.com ssh-rsa AAAA..." |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize( |
| 136 |
("response", "expected"), |
| 137 |
[ |
| 138 |
("Negotiate c2NydWZmeTp6ZXJvZw==", b"scruffy:zerog"), |
| 139 |
("Negotiate", b""), |
| 140 |
], |
| 141 |
) |
| 142 |
def test_parse_negotiate(response: str, expected: bytes) -> None: |
| 143 |
assert authorize.parse_negotiate(response) == expected |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize( |
| 147 |
"response", |
| 148 |
[ |
| 149 |
"Negotiate!c2NydWZmeTp6ZXJvZw==", |
| 150 |
"Negotiate c2Nyd!!ZmeTp6ZXJvZw==", |
| 151 |
], |
| 152 |
) |
| 153 |
def test_parse_negotiate_invalid(response: str) -> None: |
| 154 |
with pytest.raises(ValueError): |
| 155 |
authorize.parse_negotiate(response) |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize( |
| 159 |
("data", "expected"), |
| 160 |
[ |
| 161 |
(b"scruffy:zerog", "Negotiate c2NydWZmeTp6ZXJvZw=="), |
| 162 |
(None, "Negotiate"), |
| 163 |
(b"", "Negotiate"), |
| 164 |
], |
| 165 |
) |
| 166 |
def test_build_negotiate(data: bytes | None, expected: str) -> None: |
| 167 |
assert authorize.build_negotiate(data) == expected |
| 168 |
|
| 169 |
|
| 170 |
def test_build_negotiate_roundtrip() -> None: |
| 171 |
data = b"some binary \x00 data" |
| 172 |
result = authorize.build_negotiate(data) |
| 173 |
assert authorize.parse_negotiate(result) == data |
| 174 |
|
| 175 |
|
| 176 |
@pytest.mark.parametrize( |
| 177 |
("response", "expected_conversation", "expected_prompt"), |
| 178 |
[ |
| 179 |
("X-Conversation abcdefghi c2NydWZmeTp6ZXJvZw==", "abcdefghi", "scruffy:zerog"), |
| 180 |
("X-Conversation abcdefghi", "abcdefghi", ""), |
| 181 |
], |
| 182 |
) |
| 183 |
def test_parse_x_conversation( |
| 184 |
response: str, expected_conversation: str, expected_prompt: str |
| 185 |
) -> None: |
| 186 |
conversation, prompt = authorize.parse_x_conversation(response) |
| 187 |
assert conversation == expected_conversation |
| 188 |
assert prompt == expected_prompt |
| 189 |
|
| 190 |
|
| 191 |
def test_parse_x_conversation_invalid() -> None: |
| 192 |
with pytest.raises(ValueError): |
| 193 |
authorize.parse_x_conversation("X-Conversation abcdefghi c2NydW!!meTp6ZXJvZw==") |
| 194 |
|
| 195 |
|
| 196 |
def test_build_x_conversation_with_prompt() -> None: |
| 197 |
result, conv = authorize.build_x_conversation("scruffy:zerog", "abcdefghi") |
| 198 |
assert result == "X-Conversation abcdefghi c2NydWZmeTp6ZXJvZw==" |
| 199 |
assert conv == "abcdefghi" |
| 200 |
|
| 201 |
|
| 202 |
def test_build_x_conversation_no_prompt() -> None: |
| 203 |
result, conv = authorize.build_x_conversation("", "abcdefghi") |
| 204 |
assert result == "X-Conversation abcdefghi" |
| 205 |
assert conv == "abcdefghi" |
| 206 |
|
| 207 |
|
| 208 |
def test_build_x_conversation_generate() -> None: |
| 209 |
result, conv = authorize.build_x_conversation("scruffy:zerog", None) |
| 210 |
assert conv |
| 211 |
assert "c2NydWZmeTp6ZXJvZw==" in result |
| 212 |
assert conv in result |
| 213 |
|
| 214 |
|
| 215 |
def test_build_x_conversation_empty_invalid() -> None: |
| 216 |
with pytest.raises(ValueError): |
| 217 |
authorize.build_x_conversation("scruffy:zerog", "") |
| 218 |
|
| 219 |
|
| 220 |
def test_build_x_conversation_roundtrip() -> None: |
| 221 |
prompt = "test prompt" |
| 222 |
result, conv = authorize.build_x_conversation(prompt, None) |
| 223 |
parsed_conv, parsed_prompt = authorize.parse_x_conversation(result) |
| 224 |
assert parsed_conv == conv |
| 225 |
assert parsed_prompt == prompt |