File: test_lcd_configurator.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (81 lines) | stat: -rw-r--r-- 2,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from unittest import mock

import pytest

from pysnmp.hlapi.v3arch.asyncio import *
from pysnmp.hlapi.v3arch.asyncio.lcd import CommandGeneratorLcdConfigurator


@mock.patch("pysnmp.entity.config.add_v3_user")
@mock.patch("pysnmp.entity.config.delete_v3_user")
@pytest.mark.asyncio
async def test_usm_auth_cache_cleared(delete_v3_user, add_v3_user):
    """
    Ensure auth cache is cleared when auth data is changed.
    """
    with SnmpEngine() as snmpEngine:
        transportTarget = await UdpTransportTarget.create(("198.51.100.1", 161))

        authDataValues = {
            "userName": "username",
            "authKey": "authkey1",
            "authProtocol": USM_AUTH_HMAC96_MD5,
            "privKey": "privkey1",
            "privProtocol": USM_PRIV_CFB128_AES,
        }

        lcd = CommandGeneratorLcdConfigurator()
        initialAuthData = UsmUserData(**authDataValues)
        lcd.configure(snmpEngine, initialAuthData, transportTarget)
        add_v3_user.assert_called_with(
            snmpEngine,
            initialAuthData.userName,
            initialAuthData.authentication_protocol,
            initialAuthData.authentication_key,
            initialAuthData.privacy_protocol,
            initialAuthData.privacy_key,
            securityEngineId=initialAuthData.securityEngineId,
            securityName=initialAuthData.securityName,
            authKeyType=initialAuthData.authKeyType,
            privKeyType=initialAuthData.privKeyType,
        )

        # Ensure we do not add/delete if nothing changes
        add_v3_user.reset_mock()
        lcd.configure(snmpEngine, initialAuthData, transportTarget)
        add_v3_user.assert_not_called()
        delete_v3_user.assert_not_called()

        changeAuthValues = {
            "authKey": "authKey2",
            "privProtocol": USM_PRIV_CBC56_DES,
            "authProtocol": USM_AUTH_HMAC96_SHA,
            "privKey": "privKey2",
        }

        for field, value in changeAuthValues.items():
            add_v3_user.reset_mock()
            delete_v3_user.reset_mock()

            authDataValues[field] = value
            authData = UsmUserData(**authDataValues)
            lcd.configure(snmpEngine, authData, transportTarget)

            delete_v3_user.assert_called_with(
                snmpEngine,
                authData.userName,
                authData.securityEngineId,
            )

            add_v3_user.assert_called_with(
                snmpEngine,
                authData.userName,
                authData.authentication_protocol,
                authData.authentication_key,
                authData.privacy_protocol,
                authData.privacy_key,
                securityEngineId=authData.securityEngineId,
                securityName=authData.securityName,
                authKeyType=authData.authKeyType,
                privKeyType=authData.privKeyType,
            )