diff --git a/winrm/protocol.py b/winrm/protocol.py index 29a40967..0f64cf9a 100644 --- a/winrm/protocol.py +++ b/winrm/protocol.py @@ -52,6 +52,16 @@ def __init__( @param bool message_encryption_enabled: Will encrypt the WinRM messages if set to True and the transport auth supports message encryption (Default True). """ + try: + read_timeout_sec = int(read_timeout_sec) + except ValueError as ve: + raise ValueError("failed to parse read_timeout_sec as int: %s" % str(ve)) + + try: + operation_timeout_sec = int(operation_timeout_sec) + except ValueError as ve: + raise ValueError("failed to parse operation_timeout_sec as int: %s" % str(ve)) + if operation_timeout_sec >= read_timeout_sec or operation_timeout_sec < 1: raise WinRMError("read_timeout_sec must exceed operation_timeout_sec, and both must be non-zero") diff --git a/winrm/tests/test_protocol.py b/winrm/tests/test_protocol.py index b60e274c..5a524f0c 100644 --- a/winrm/tests/test_protocol.py +++ b/winrm/tests/test_protocol.py @@ -1,3 +1,8 @@ +import pytest + +from winrm.protocol import Protocol + + def test_open_shell_and_close_shell(protocol_fake): shell_id = protocol_fake.open_shell() assert shell_id == '11111111-1111-1111-1111-111111111113' @@ -34,3 +39,35 @@ def test_get_command_output(protocol_fake): protocol_fake.cleanup_command(shell_id, command_id) protocol_fake.close_shell(shell_id) + + +def test_set_timeout_as_sec(): + protocol = Protocol('endpoint', + username='username', + password='password', + read_timeout_sec='30', + operation_timeout_sec='29') + assert protocol.read_timeout_sec == 30 + assert protocol.operation_timeout_sec == 29 + + +def test_fail_set_read_timeout_as_sec(): + with pytest.raises(ValueError) as exc: + protocol = Protocol('endpoint', + username='username', + password='password', + read_timeout_sec='30a', + operation_timeout_sec='29') + assert str(exc.value) == "failed to parse read_timeout_sec as int: " \ + "invalid literal for int() with base 10: '30a'" + + +def test_fail_set_operation_timeout_as_sec(): + with pytest.raises(ValueError) as exc: + protocol = Protocol('endpoint', + username='username', + password='password', + read_timeout_sec=30, + operation_timeout_sec='29a') + assert str(exc.value) == "failed to parse operation_timeout_sec as int: " \ + "invalid literal for int() with base 10: '29a'"