|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +try: |
| 4 | + from unittest import mock |
| 5 | +except ImportError: |
| 6 | + import mock |
| 7 | + |
| 8 | +from kafka.sasl import get_sasl_mechanism |
| 9 | +from kafka.sasl.gssapi import SaslMechanismGSSAPI |
| 10 | + |
| 11 | + |
| 12 | +def test_gssapi(): |
| 13 | + config = { |
| 14 | + 'sasl_kerberos_domain_name': 'foo', |
| 15 | + 'sasl_kerberos_service_name': 'bar', |
| 16 | + } |
| 17 | + client_ctx = mock.Mock() |
| 18 | + client_ctx.step.side_effect = [b'init', b'exchange', b'complete', b'final', b'xxxx'] |
| 19 | + client_ctx.complete = False |
| 20 | + def mocked_message_wrapper(msg, *args): |
| 21 | + wrapped = mock.Mock() |
| 22 | + type(wrapped).message = mock.PropertyMock(return_value=msg) |
| 23 | + return wrapped |
| 24 | + client_ctx.unwrap.side_effect = mocked_message_wrapper |
| 25 | + client_ctx.wrap.side_effect = mocked_message_wrapper |
| 26 | + with mock.patch('kafka.sasl.gssapi.gssapi.SecurityContext', return_value=client_ctx) as patched: |
| 27 | + gssapi = get_sasl_mechanism('GSSAPI')(**config) |
| 28 | + assert isinstance(gssapi, SaslMechanismGSSAPI) |
| 29 | + client_ctx.step.assert_called_with(None) |
| 30 | + |
| 31 | + while not gssapi.is_done(): |
| 32 | + send_token = gssapi.auth_bytes() |
| 33 | + receive_token = send_token # not realistic, but enough for testing |
| 34 | + if send_token == b'\x00cbar@foo': # final wrapped message |
| 35 | + receive_token = b'' # final message gets an empty response |
| 36 | + gssapi.receive(receive_token) |
| 37 | + if send_token == b'exchange': |
| 38 | + client_ctx.complete = True |
| 39 | + |
| 40 | + assert gssapi.is_done() |
| 41 | + assert gssapi.is_authenticated() |
0 commit comments