Skip to content

Commit

Permalink
Feature/pull in more upstream fixes (#134)
Browse files Browse the repository at this point in the history
* Update helpers.py

- fixes a regex issue seen in uptream code.

* - add test for new oid type
- ensured new method works with older types as well.

* - failing test down to just 6

* - added another test case that is currently failing, but should pass.

* - down to 203 passing and 9 failing

* Update helpers.py

- 206 passing
6 failing.

* Cleaner Approach

- stuck with current regex.
- added new regex.

* Flushed out Regex

- figured out a system of regex.
I don't want to get rid of the original regex's, instead we try the new ones first and fall back to the old ones just in case.

* Fix Broken UTs

- tweaking to get all UTs to pass, not just helper uts.
  • Loading branch information
carlkidcrypto authored Jul 30, 2024
1 parent c8fcaee commit 47670d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
23 changes: 18 additions & 5 deletions ezsnmp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re

# This regular expression is used to extract the index from an OID
# We attempt to extract the index from an OID (e.g. sysDescr.0
# or .iso.org.dod.internet.mgmt.mib-2.system.sysContact.0)
OID_INDEX_RE = re.compile(
r"""(
\.?\d+(?:\.\d+)* # numeric OID
Expand All @@ -16,6 +18,10 @@
re.VERBOSE,
)

# This regular expression takes something like 'SNMPv2::mib-2.17.7.1.4.3.1.2.300'
# and splits it into 'SNMPv2::mib-2' and '17.7.1.4.3.1.2.300'
OID_INDEX_RE2 = re.compile(r"^([^\.]+::[^\.]+)\.(.*)$")


def normalize_oid(oid, oid_index=None):
"""
Expand All @@ -27,10 +33,17 @@ def normalize_oid(oid, oid_index=None):

# Determine the OID index from the OID if not specified
if oid_index is None and oid is not None:
# We attempt to extract the index from an OID (e.g. sysDescr.0
# or .iso.org.dod.internet.mgmt.mib-2.system.sysContact.0)
match = OID_INDEX_RE.match(oid)
if match:
oid, oid_index = match.group(1, 2)
first_match = OID_INDEX_RE.match(oid)
second_match = OID_INDEX_RE2.match(oid)

if second_match:
oid, oid_index = second_match.group(1, 2)

elif first_match:
oid, oid_index = first_match.group(1, 2)

elif oid == ".":
oid = "."
oid_index = ""

return oid, oid_index
30 changes: 30 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,42 @@
from ezsnmp.helpers import normalize_oid


def test_normalize_oid_just_iso():
oid, oid_index = normalize_oid("oid")
assert oid == "oid"
assert oid_index == ""


def test_normalize_oid_just_period():
oid, oid_index = normalize_oid(".")
assert oid == "."
assert oid_index == ""


def test_normalize_oid_regular():
oid, oid_index = normalize_oid("sysContact.0")
assert oid == "sysContact"
assert oid_index == "0"


def test_normalize_oid_regular_2():
oid, oid_index = normalize_oid("SNMPv2::mib-2.17.7.1.4.3.1.2.300")
assert oid == "SNMPv2::mib-2"
assert oid_index == "17.7.1.4.3.1.2.300"


def test_normalize_oid_regular_3():
oid, oid_index = normalize_oid("nsCacheTimeout.1.3.6.1.2.1.2")
assert oid == "nsCacheTimeout"
assert oid_index == "1.3.6.1.2.1.2"


def test_normalize_oid_regular_4():
oid, oid_index = normalize_oid("iso.3.6.1.2.1.31.1.1.1.1.5035")
assert oid == "iso"
assert oid_index == "3.6.1.2.1.31.1.1.1.1.5035"


def test_normalize_oid_numeric():
oid, oid_index = normalize_oid(".1.3.6.1.2.1.1.1.0")
assert oid == ".1.3.6.1.2.1.1.1.0"
Expand Down

1 comment on commit 47670d0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valgrind Comment

Python Version Results OS Version
3.8 sh ==8245== LEAK SUMMARY: ==8245== definitely lost: 112 bytes in 7 blocks ==8245== indirectly lost: 0 bytes in 0 blocks ==8245== possibly lost: 73,792 bytes in 116 blocks ==8245== still reachable: 2,157,492 bytes in 1,174 blocks ==8245== suppressed: 74,488 bytes in 178 blocks ==8245== Reachable blocks (those to which a pointer was found) are not shown. ==8245== To see them, rerun with: --leak-check=full --show-leak-kinds=all ubuntu-latest
3.9 sh ==8223== LEAK SUMMARY: ==8223== definitely lost: 112 bytes in 7 blocks ==8223== indirectly lost: 0 bytes in 0 blocks ==8223== possibly lost: 305,181 bytes in 464 blocks ==8223== still reachable: 2,408,681 bytes in 1,434 blocks ==8223== suppressed: 68,037 bytes in 163 blocks ==8223== Reachable blocks (those to which a pointer was found) are not shown. ==8223== To see them, rerun with: --leak-check=full --show-leak-kinds=all ubuntu-latest
3.10 sh ==8214== LEAK SUMMARY: ==8214== definitely lost: 112 bytes in 7 blocks ==8214== indirectly lost: 0 bytes in 0 blocks ==8214== possibly lost: 260,260 bytes in 389 blocks ==8214== still reachable: 1,756,174 bytes in 1,367 blocks ==8214== suppressed: 68,403 bytes in 163 blocks ==8214== Reachable blocks (those to which a pointer was found) are not shown. ==8214== To see them, rerun with: --leak-check=full --show-leak-kinds=all ubuntu-latest
3.11 sh ==8297== LEAK SUMMARY: ==8297== definitely lost: 5,376 bytes in 12 blocks ==8297== indirectly lost: 11,479 bytes in 12 blocks ==8297== possibly lost: 4,752 bytes in 7 blocks ==8297== still reachable: 10,687,163 bytes in 141,002 blocks ==8297== suppressed: 69,898 bytes in 166 blocks ==8297== Reachable blocks (those to which a pointer was found) are not shown. ==8297== To see them, rerun with: --leak-check=full --show-leak-kinds=all ubuntu-latest
3.12 sh ==8201== LEAK SUMMARY: ==8201== definitely lost: 4,976 bytes in 12 blocks ==8201== indirectly lost: 1,395 bytes in 2 blocks ==8201== possibly lost: 5,121 bytes in 8 blocks ==8201== still reachable: 10,595,186 bytes in 140,953 blocks ==8201== suppressed: 68,382 bytes in 163 blocks ==8201== Reachable blocks (those to which a pointer was found) are not shown. ==8201== To see them, rerun with: --leak-check=full --show-leak-kinds=all ubuntu-latest

Please sign in to comment.