Skip to content

Commit

Permalink
and make the appropriate test_* units so we actually test branching i…
Browse files Browse the repository at this point in the history
…n my_conv based on the msg_style
  • Loading branch information
FirefighterBlu3 committed Mar 14, 2022
1 parent 0307e0b commit 414ae39
Showing 1 changed file with 147 additions and 2 deletions.
149 changes: 147 additions & 2 deletions tests/test_internals.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,12 +46,12 @@ def test_PamMessage__repr():
x.msg_style = 1
x.msg = b'1'
str(x)
assert "<PamMessage 1 'b'1''>" == repr(x)
assert "<PamMessage style: 1, content: b'1' >" == repr(x)


def test_PamResponse__repr():
x = PamResponse()
assert "<PamResponse 0 'None'>" == repr(x)
assert "<PamResponse code: 0, content: None >" == repr(x)


def test_PamAuthenticator__setup():
Expand Down Expand Up @@ -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

0 comments on commit 414ae39

Please sign in to comment.