From 414ae394f08f781b24982b5266557c886d2ab7dc Mon Sep 17 00:00:00 2001 From: David Ford Date: Sun, 13 Mar 2022 20:44:22 -0400 Subject: [PATCH] and make the appropriate test_* units so we actually test branching in my_conv based on the msg_style --- tests/test_internals.py | 149 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/tests/test_internals.py b/tests/test_internals.py index a3cf0c1..4c92b0d 100644 --- a/tests/test_internals.py +++ b/tests/test_internals.py @@ -1,18 +1,23 @@ import os import pytest +from ctypes import cdll from ctypes import c_void_p +from ctypes import pointer from pam.__internals import PAM_SYSTEM_ERR from pam.__internals import PAM_SUCCESS from pam.__internals import PAM_SESSION_ERR from pam.__internals import PAM_AUTH_ERR from pam.__internals import PAM_USER_UNKNOWN +from pam.__internals import PAM_PROMPT_ECHO_OFF +from pam.__internals import PAM_PROMPT_ECHO_ON from pam.__internals import PamConv from pam.__internals import PamHandle from pam.__internals import PamMessage from pam.__internals import PamResponse from pam.__internals import PamAuthenticator +from pam.__internals import my_conv # In order to run some tests, we need a working user/pass combo # you can specify these on the command line @@ -41,12 +46,12 @@ def test_PamMessage__repr(): x.msg_style = 1 x.msg = b'1' str(x) - assert "" == repr(x) + assert "" == repr(x) def test_PamResponse__repr(): x = PamResponse() - assert "" == repr(x) + assert "" == repr(x) def test_PamAuthenticator__setup(): @@ -284,3 +289,143 @@ def test_PamAuthenticator__close_session_unauthenticated(pam_obj): pam_obj.pam_start(b'', b'', pam_conv, pam_obj.handle) rv = pam_obj.close_session() assert PAM_SESSION_ERR == rv + + +def test_PamAuthenticator__conversation_callback_prompt_echo_off(pam_obj): + '''Verify that the password is stuffed into the pp_response structure and the + response code is set to zero + ''' + n_messages = 1 + + messages = PamMessage(PAM_PROMPT_ECHO_OFF, b'Password: ') + pp_messages = pointer(pointer(messages)) + + response = PamResponse(b'overwrite', -1) + pp_response = pointer(pointer(response)) + + encoding = 'utf-8' + password = b'blank' + msg_list = [] + + libc = cdll.LoadLibrary(None) + + rv = my_conv(n_messages, + pp_messages, + pp_response, + libc, + msg_list, + password, + encoding) + + assert b'blank' == pp_response.contents.contents.resp + assert 0 == pp_response.contents.contents.resp_retcode + assert PAM_SUCCESS == rv + + +def test_PamAuthenticator__conversation_callback_prompt_echo_on(pam_obj): + '''Verify that the stuffed PamResponse "overwrite" is copied into the output + and the resp_retcode is set to zero + ''' + n_messages = 1 + + messages = PamMessage(PAM_PROMPT_ECHO_ON, b'Password: ') + pp_messages = pointer(pointer(messages)) + + response = PamResponse(b'overwrite', -1) + pp_response = pointer(pointer(response)) + + encoding = 'utf-8' + password = b'blank' + msg_list = [] + + libc = cdll.LoadLibrary(None) + + rv = my_conv(n_messages, + pp_messages, + pp_response, + libc, + msg_list, + password, + encoding) + + assert None is pp_response.contents.contents.resp + assert 0 == pp_response.contents.contents.resp_retcode + assert PAM_SUCCESS == rv + + +def test_PamAuthenticator__conversation_callback_multimessage_OFF_ON(pam_obj): + '''Verify that the stuffed PamResponse "overwrite" is copied into the output + and the resp_retcode is set to zero + ''' + n_messages = 2 + + msg1 = PamMessage(PAM_PROMPT_ECHO_OFF, b'overwrite with PAM_PROMPT_ECHO_OFF') + msg2 = PamMessage(PAM_PROMPT_ECHO_ON, b'overwrite with PAM_PROMPT_ECHO_ON') + + ptr1 = pointer(msg1) + ptr2 = pointer(msg2) + + ptrs = pointer(ptr1) + ptrs[1] = ptr2 + + pp_messages = pointer(ptrs[0]) + + response = PamResponse(b'overwrite', -1) + pp_response = pointer(pointer(response)) + + encoding = 'utf-8' + password = b'blank' + msg_list = [] + + libc = cdll.LoadLibrary(None) + + rv = my_conv(n_messages, + pp_messages, + pp_response, + libc, + msg_list, + password, + encoding) + + assert b'blank' == pp_response.contents.contents.resp + assert 0 == pp_response.contents.contents.resp_retcode + assert PAM_SUCCESS == rv + + +def test_PamAuthenticator__conversation_callback_multimessage_ON_OFF(pam_obj): + '''Verify that the stuffed PamResponse "overwrite" is copied into the output + and the resp_retcode is set to zero + ''' + n_messages = 2 + + msg1 = PamMessage(PAM_PROMPT_ECHO_ON, b'overwrite with PAM_PROMPT_ECHO_ON') + msg2 = PamMessage(PAM_PROMPT_ECHO_OFF, b'overwrite with PAM_PROMPT_ECHO_OFF') + + ptr1 = pointer(msg1) + ptr2 = pointer(msg2) + + ptrs = pointer(ptr1) + ptrs[1] = ptr2 + + pp_messages = pointer(ptrs[0]) + + response = PamResponse(b'overwrite', -1) + pp_response = pointer(pointer(response)) + + encoding = 'utf-8' + password = b'blank' + msg_list = [] + + libc = cdll.LoadLibrary(None) + + rv = my_conv(n_messages, + pp_messages, + pp_response, + libc, + msg_list, + password, + encoding) + + assert None is pp_response.contents.contents.resp + assert 0 == pp_response.contents.contents.resp_retcode + assert PAM_SUCCESS == rv