Skip to content

Commit 453beb0

Browse files
bluecmdclaude
andcommitted
test: fix SNMPv3 negative test against USM key cache
The wrong-password test passed authentication because net-snmp caches localized USM keys per (engineID, username) for the process lifetime. An earlier test had already authenticated as testuser with the correct passphrase, so the cached key was reused and the bad passphrase never reached the agent -- the test was asserting on the cache, not the agent. Use a dedicated 'baduser' that no passing test ever authenticates as, so its cache entry is only ever populated from the wrong passphrase. Also add an unknown-user case, which is rejected agent-side and so cannot be affected by client caching at all. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014KG1c9XtKawkvig5PnZjC8
1 parent 3604154 commit 453beb0

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

netsnmp/tests/system/snmpd.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ rwcommunity private 127.0.0.1
77
# netsnmp/tests/system/common.py.
88
createUser testuser SHA "auth_pass" AES "priv_pass"
99
rwuser testuser authPriv
10+
11+
# Only ever contacted with a deliberately wrong passphrase, by
12+
# test_wrong_auth_password_is_rejected. It must not be used by any passing
13+
# test: net-snmp caches localized USM keys per (engineID, username) for the
14+
# life of the process, so a successful auth would prime that cache and mask
15+
# the rejection we are asserting.
16+
createUser baduser SHA "correct_auth_pass" AES "correct_priv_pass"
17+
rouser baduser authPriv

netsnmp/tests/system/test_v3.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,24 @@ def test_session_get(self):
4646
varlist = netsnmp.VarList(netsnmp.Varbind(SYS_DESCR, '0'))
4747
assert_value(self, session.get(varlist))
4848

49+
def test_unknown_user_is_rejected(self):
50+
session = netsnmp.Session(**dict(V3_ARGS, SecName='nosuchuser'))
51+
varlist = netsnmp.VarList(netsnmp.Varbind(SYS_DESCR, '0'))
52+
values = session.get(varlist)
53+
self.assertFalse(values and values[0])
54+
4955
def test_wrong_auth_password_is_rejected(self):
50-
"""Proves the agent really enforces authPriv, so the tests above
51-
are not silently passing over an unauthenticated session."""
52-
args = dict(V3_ARGS, AuthPass='wrong_auth_pass')
53-
session = netsnmp.Session(**args)
56+
"""Proves the agent really enforces authPriv, so the tests above are
57+
not silently passing over an unauthenticated session.
58+
59+
Uses a dedicated user rather than the one above: net-snmp caches
60+
localized USM keys per (engineID, username) process-wide, so reusing
61+
a name that already authenticated successfully would reuse the good
62+
key and the wrong passphrase would never reach the agent.
63+
"""
64+
session = netsnmp.Session(
65+
**dict(V3_ARGS, SecName='baduser', AuthPass='wrong_auth_pass',
66+
PrivPass='wrong_priv_pass'))
5467
varlist = netsnmp.VarList(netsnmp.Varbind(SYS_DESCR, '0'))
5568
values = session.get(varlist)
5669
self.assertFalse(values and values[0])

0 commit comments

Comments
 (0)