| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import re |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import spnego.exceptions as exceptions |
| 10 |
|
| 11 |
GSSError = exceptions.GSSError |
| 12 |
WinError = exceptions.WinError |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.parametrize( |
| 16 |
"option, expected", |
| 17 |
[ |
| 18 |
( |
| 19 |
exceptions.NegotiateOptions.negotiate_kerberos, |
| 20 |
"The Python gssapi library is not installed so Kerberos cannot be negotiated.", |
| 21 |
), |
| 22 |
( |
| 23 |
exceptions.NegotiateOptions.wrapping_iov, |
| 24 |
"The system is missing the GSSAPI IOV extension headers or CredSSP is being requested, " |
| 25 |
"cannot utilize wrap_iov and unwrap_iov", |
| 26 |
), |
| 27 |
( |
| 28 |
exceptions.NegotiateOptions.wrapping_winrm, |
| 29 |
"The system is missing the GSSAPI IOV extension headers required for WinRM encryption with Kerberos.", |
| 30 |
), |
| 31 |
(exceptions.NegotiateOptions.session_key, "The protocol selected does not support getting the session key."), |
| 32 |
], |
| 33 |
) |
| 34 |
def test_feature_missing_error(option, expected): |
| 35 |
err = exceptions.FeatureMissingError(option) |
| 36 |
assert str(err) == expected |
| 37 |
assert err.message == expected |
| 38 |
|
| 39 |
|
| 40 |
def test_spnego_error_no_code_fail(): |
| 41 |
expected = "SpnegoError requires either an error_code or base_error" |
| 42 |
with pytest.raises(ValueError, match=re.escape(expected)): |
| 43 |
exceptions.SpnegoError() |
| 44 |
|
| 45 |
|
| 46 |
def test_spnego_error_invalid_base_error_fail(): |
| 47 |
expected = "base_error of type 'Exception' is not supported, must be a gssapi.exceptions.GSSError or WindowsError" |
| 48 |
with pytest.raises(ValueError, match=re.escape(expected)): |
| 49 |
exceptions.SpnegoError(base_error=Exception()) |
| 50 |
|
| 51 |
|
| 52 |
def test_spnego_error_unknown_error(): |
| 53 |
actual = exceptions.SpnegoError(1024) |
| 54 |
|
| 55 |
assert isinstance(actual, exceptions.SpnegoError) |
| 56 |
assert actual.message == "SpnegoError (1024): Unknown error code" |
| 57 |
|
| 58 |
|
| 59 |
def test_invalid_token_error(): |
| 60 |
actual = exceptions.SpnegoError(exceptions.ErrorCode.invalid_token) |
| 61 |
|
| 62 |
assert isinstance(actual, exceptions.InvalidTokenError) |
| 63 |
assert actual.ERROR_CODE == exceptions.ErrorCode.invalid_token |
| 64 |
assert actual.base_error is None |
| 65 |
assert actual.message == "SpnegoError (9): A token was invalid, or the logon was denied" |
| 66 |
|
| 67 |
|
| 68 |
def test_invalid_token_error_with_context(): |
| 69 |
actual = exceptions.InvalidTokenError(context_msg="Context") |
| 70 |
|
| 71 |
assert isinstance(actual, exceptions.InvalidTokenError) |
| 72 |
assert actual.ERROR_CODE == exceptions.ErrorCode.invalid_token |
| 73 |
assert actual.base_error is None |
| 74 |
assert actual.message == "SpnegoError (9): A token was invalid, or the logon was denied, Context: Context" |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.skipif(GSSError == Exception, reason="Need a GSSError to test this out") |
| 78 |
def test_invalid_token_from_gssapi(): |
| 79 |
base_error = GSSError(589824, 0) |
| 80 |
|
| 81 |
actual = exceptions.SpnegoError(base_error=base_error) |
| 82 |
assert isinstance(actual, exceptions.InvalidTokenError) |
| 83 |
assert actual.ERROR_CODE == exceptions.ErrorCode.invalid_token |
| 84 |
assert actual.base_error == base_error |
| 85 |
assert actual.message.startswith("SpnegoError (9): Major (589824)") |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.skipif(WinError == Exception, reason="Need a WindowsError to test this out") |
| 89 |
def test_invalid_token_from_sspi(): |
| 90 |
base_error = WinError("Error") |
| 91 |
setattr(base_error, "winerror", -2146893048) |
| 92 |
|
| 93 |
actual = exceptions.SpnegoError(base_error=base_error) |
| 94 |
assert isinstance(actual, exceptions.InvalidTokenError) |
| 95 |
assert actual.ERROR_CODE == exceptions.ErrorCode.invalid_token |
| 96 |
assert actual.base_error == base_error |
| 97 |
assert actual.message.startswith("SpnegoError (9): ") |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.skipif(WinError == Exception, reason="Need a WindowsError to test this out") |
| 101 |
def test_invalid_token_from_sspi_logon_denied(): |
| 102 |
base_error = WinError("Error") |
| 103 |
setattr(base_error, "winerror", -2146893044) |
| 104 |
|
| 105 |
actual = exceptions.SpnegoError(base_error=base_error) |
| 106 |
assert isinstance(actual, exceptions.InvalidTokenError) |
| 107 |
assert actual.ERROR_CODE == exceptions.ErrorCode.invalid_token |
| 108 |
assert actual.base_error == base_error |
| 109 |
assert actual.message.startswith("SpnegoError (9): ") |
| 110 |
|
| 111 |
|
| 112 |
def test_operation_not_available_error(): |
| 113 |
actual = exceptions.SpnegoError(exceptions.ErrorCode.unavailable) |
| 114 |
|
| 115 |
assert isinstance(actual, exceptions.OperationNotAvailableError) |
| 116 |
assert actual.ERROR_CODE == exceptions.ErrorCode.unavailable |
| 117 |
assert actual.base_error is None |
| 118 |
assert actual.message == "SpnegoError (16): Operation not supported or available" |
| 119 |
|
| 120 |
|
| 121 |
def test_operation_not_available_error_with_context(): |
| 122 |
actual = exceptions.OperationNotAvailableError(context_msg="Context") |
| 123 |
|
| 124 |
assert isinstance(actual, exceptions.OperationNotAvailableError) |
| 125 |
assert actual.ERROR_CODE == exceptions.ErrorCode.unavailable |
| 126 |
assert actual.base_error is None |
| 127 |
assert actual.message == "SpnegoError (16): Operation not supported or available, Context: Context" |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.skipif(GSSError == Exception, reason="Need a GSSError to test this out") |
| 131 |
def test_operation_not_available_from_gssapi(): |
| 132 |
base_error = GSSError(1048576, 0) |
| 133 |
|
| 134 |
actual = exceptions.SpnegoError(base_error=base_error) |
| 135 |
assert isinstance(actual, exceptions.OperationNotAvailableError) |
| 136 |
assert actual.ERROR_CODE == exceptions.ErrorCode.unavailable |
| 137 |
assert actual.base_error == base_error |
| 138 |
assert actual.message.startswith("SpnegoError (16): Major (1048576): ") |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.skipif(WinError == Exception, reason="Need a WindowsError to test this out") |
| 142 |
def test_operation_not_available_from_sspi(): |
| 143 |
base_error = WinError("Error") |
| 144 |
setattr(base_error, "winerror", -2146893054) |
| 145 |
|
| 146 |
actual = exceptions.SpnegoError(base_error=base_error) |
| 147 |
assert isinstance(actual, exceptions.OperationNotAvailableError) |
| 148 |
assert actual.ERROR_CODE == exceptions.ErrorCode.unavailable |
| 149 |
assert actual.base_error == base_error |
| 150 |
assert actual.message.startswith("SpnegoError (16): ") |