From 806132b5bdc69d217245496d7e3171b32cdc480d Mon Sep 17 00:00:00 2001 From: Lex Li Date: Sun, 4 Feb 2024 17:08:46 -0500 Subject: [PATCH] Added more test cases. --- .../manager/cmdgen/test_usm_none_none.py | 21 +++++- .../cmdgen/test_usm_none_none_wrong_user.py | 21 ------ .../manager/cmdgen/test_usm_sha_aes128.py | 70 +++++++++++++++++++ .../cmdgen/test_usm_sha_aes128_wrong_user.py | 26 ------- .../manager/cmdgen/test_usm_sha_none.py | 22 ++++++ .../cmdgen/test_usm_sha_none_wrong_user.py | 24 ------- 6 files changed, 112 insertions(+), 72 deletions(-) delete mode 100644 tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none_wrong_user.py delete mode 100644 tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128_wrong_user.py delete mode 100644 tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none_wrong_user.py diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none.py index 2c9f795df..e84017e54 100644 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none.py +++ b/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none.py @@ -1,5 +1,6 @@ import pytest from pysnmp.hlapi.asyncio import * +from pysnmp.proto.errind import UnknownUserName @pytest.mark.asyncio async def test_usm_no_auth_no_priv(): @@ -19,4 +20,22 @@ async def test_usm_no_auth_no_priv(): assert errorStatus == 0 assert len(varBinds) == 1 assert varBinds[0][0].prettyPrint() == "SNMPv2-MIB::sysDescr.0" - isinstance(varBinds[0][1], OctetString) \ No newline at end of file + isinstance(varBinds[0][1], OctetString) + +@pytest.mark.asyncio +async def test_usm_no_auth_no_priv_wrong_user(): + snmpEngine = SnmpEngine() + authData = UsmUserData( + "usr-none-none-not-exist" + ) + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( + snmpEngine, + authData, + UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), + ContextData(), + ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), + ) + + assert isinstance(errorIndication, UnknownUserName) + assert str(errorIndication) == 'Unknown USM user' + snmpEngine.transportDispatcher.closeDispatcher() diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none_wrong_user.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none_wrong_user.py deleted file mode 100644 index 15247b445..000000000 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_none_none_wrong_user.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest -from pysnmp.hlapi.asyncio import * -from pysnmp.proto.errind import UnknownUserName - -@pytest.mark.asyncio -async def test_usm_no_auth_no_priv_wrong_user(): - snmpEngine = SnmpEngine() - authData = UsmUserData( - "usr-none-none-not-exist" - ) - errorIndication, errorStatus, errorIndex, varBinds = await getCmd( - snmpEngine, - authData, - UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), - ContextData(), - ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), - ) - - assert isinstance(errorIndication, UnknownUserName) - assert str(errorIndication) == 'Unknown USM user' - snmpEngine.transportDispatcher.closeDispatcher() diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128.py index 3490983fd..04cd7c4a9 100644 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128.py +++ b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128.py @@ -1,5 +1,6 @@ import pytest from pysnmp.hlapi.asyncio import * +from pysnmp.proto.errind import RequestTimedOut, UnknownUserName @pytest.mark.asyncio async def test_usm_sha_aes128(): @@ -26,3 +27,72 @@ async def test_usm_sha_aes128(): isinstance(varBinds[0][1], OctetString) snmpEngine.transportDispatcher.closeDispatcher() + +@pytest.mark.asyncio +async def test_usm_sha_aes128_wrong_auth(): + snmpEngine = SnmpEngine() + authData = UsmUserData( + "usr-sha-aes", + "authkey1", + "privkey1", + authProtocol=usmHMACMD5AuthProtocol, # wrongly use usmHMACMD5AuthProtocol + privProtocol=usmAesCfb128Protocol + ) + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( + snmpEngine, + authData, + UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), + ContextData(), + ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), + ) + + assert isinstance(errorIndication, RequestTimedOut) + assert str(errorIndication) == 'No SNMP response received before timeout' + + snmpEngine.transportDispatcher.closeDispatcher() + +@pytest.mark.asyncio +async def test_usm_sha_aes128_wrong_priv(): + snmpEngine = SnmpEngine() + authData = UsmUserData( + "usr-sha-aes", + "authkey1", + "privkey1", + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmDESPrivProtocol, # wrongly use usmDESPrivProtocol + ) + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( + snmpEngine, + authData, + UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), + ContextData(), + ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), + ) + + assert isinstance(errorIndication, RequestTimedOut) + assert str(errorIndication) == 'No SNMP response received before timeout' + + snmpEngine.transportDispatcher.closeDispatcher() + +@pytest.mark.asyncio +async def test_usm_sha_aes128_wrong_user(): + snmpEngine = SnmpEngine() + authData = UsmUserData( + "usr-sha-aes-not-exist", + "authkey1", + "privkey1", + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol, + ) + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( + snmpEngine, + authData, + UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), + ContextData(), + ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), + ) + + assert isinstance(errorIndication, UnknownUserName) + assert str(errorIndication) == 'Unknown USM user' + + snmpEngine.transportDispatcher.closeDispatcher() diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128_wrong_user.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128_wrong_user.py deleted file mode 100644 index 977ed648b..000000000 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128_wrong_user.py +++ /dev/null @@ -1,26 +0,0 @@ -import pytest -from pysnmp.hlapi.asyncio import * -from pysnmp.proto.errind import UnknownUserName - -@pytest.mark.asyncio -async def test_usm_sha_aes128_wrong_user(): - snmpEngine = SnmpEngine() - authData = UsmUserData( - "usr-sha-aes-not-exist", - "authkey1", - "privkey1", - authProtocol=usmHMACSHAAuthProtocol, - privProtocol=usmAesCfb128Protocol, - ) - errorIndication, errorStatus, errorIndex, varBinds = await getCmd( - snmpEngine, - authData, - UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), - ContextData(), - ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), - ) - - assert isinstance(errorIndication, UnknownUserName) - assert str(errorIndication) == 'Unknown USM user' - - snmpEngine.transportDispatcher.closeDispatcher() diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none.py index d9adfb013..b4e9fb451 100644 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none.py +++ b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none.py @@ -1,5 +1,6 @@ import pytest from pysnmp.hlapi.asyncio import * +from pysnmp.proto.errind import UnknownUserName @pytest.mark.asyncio async def test_usm_sha_none(): @@ -24,3 +25,24 @@ async def test_usm_sha_none(): isinstance(varBinds[0][1], OctetString) snmpEngine.transportDispatcher.closeDispatcher() + +@pytest.mark.asyncio +async def test_usm_sha_none_wrong_user(): + snmpEngine = SnmpEngine() + authData = UsmUserData( + "usr-sha-none-not-exist", + "authkey1", + authProtocol=usmHMACSHAAuthProtocol, + ) + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( + snmpEngine, + authData, + UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), + ContextData(), + ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), + ) + + assert isinstance(errorIndication, UnknownUserName) + assert str(errorIndication) == 'Unknown USM user' + + snmpEngine.transportDispatcher.closeDispatcher() diff --git a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none_wrong_user.py b/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none_wrong_user.py deleted file mode 100644 index 4eaf84e4d..000000000 --- a/tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_none_wrong_user.py +++ /dev/null @@ -1,24 +0,0 @@ -import pytest -from pysnmp.hlapi.asyncio import * -from pysnmp.proto.errind import UnknownUserName - -@pytest.mark.asyncio -async def test_usm_sha_none_wrong_user(): - snmpEngine = SnmpEngine() - authData = UsmUserData( - "usr-sha-none-not-exist", - "authkey1", - authProtocol=usmHMACSHAAuthProtocol, - ) - errorIndication, errorStatus, errorIndex, varBinds = await getCmd( - snmpEngine, - authData, - UdpTransportTarget(("demo.pysnmp.com", 161), retries=0), - ContextData(), - ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), - ) - - assert isinstance(errorIndication, UnknownUserName) - assert str(errorIndication) == 'Unknown USM user' - - snmpEngine.transportDispatcher.closeDispatcher()