Skip to content

Commit

Permalink
Fix incorrect datatype (#7)
Browse files Browse the repository at this point in the history
* Update snmp.py

Fix incorrect datatype- decode is no more required.

* changes as per code review

* add more optimization

* show that wrong type is still used

* fix tests alongwith adding optimization and black formatting

* fix unidiomatic-typecheck and add isort

* clean imports

---------

Co-authored-by: A200146603 <[email protected]>
Co-authored-by: Christian Sahlmann <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 158aee9 commit 151e9a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ repos:
rev: v0.8.0
hooks:
- id: yamlfmt
- repo: https://github.com/pycqa/isort
rev: 5.11.2
hooks:
- id: isort
name: isort (python)
4 changes: 2 additions & 2 deletions mb_netmgmt/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def to_varbind(oid, response):
value = response["val"]
try:
value = b64decode(value, validate=True)
asn1_class = ASN1_Class_UNIVERSAL.__dict__[response["tag"]]
value = asn1_class.asn1_object(value)
except (binascii.Error, TypeError):
pass
asn1_class = ASN1_Class_UNIVERSAL.__dict__[response["tag"]]
value = asn1_class.asn1_object(value)
del response["val"]
del response["tag"]
return SNMPvarbind(oid=oid, value=value, **response)
2 changes: 1 addition & 1 deletion mb_netmgmt/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def handle_username(self):
password_prompt = result[1].string.encode()
self.wfile.write(password_prompt)
except AttributeError:
self.handle_request({"command": self.username.decode()}, None)
self.handle_request({"command": self.username.decode()}, None)


def extract_username(byte_string):
Expand Down
17 changes: 17 additions & 0 deletions test/test_mb_netmgmt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import os
import re
from base64 import b64encode
from threading import Thread
from urllib.parse import urlparse

Expand All @@ -9,6 +10,7 @@
import pytest
from ncclient.devices.default import DefaultDeviceHandler
from ncclient.transport.session import BASE_NS_1_0, MSG_DELIM, to_ele
from scapy.asn1.asn1 import ASN1_OID
from scapy.layers.snmp import ASN1_NULL, SNMPvarbind

from mb_netmgmt import mb, netconf, snmp, ssh, use_scalar_strings, yaml
Expand Down Expand Up @@ -287,3 +289,18 @@ def test_parse_to():
assert parse_to("telnet://localhost:23").port == 23
with pytest.raises(ValueError):
parse_to("localhost")


def test_to_varbind_with_base64encode_response():
result = snmp.to_varbind(
"1.3.6.1.2.1.1.2.0",
response=dict(val=b64encode(b"1.3.6.1.4.1.9.1.1709"), tag="OID"),
)
assert isinstance(result.value, ASN1_OID)


def test_to_varbind_without_base64encode_response():
result = snmp.to_varbind(
"1.3.6.1.2.1.1.2.0", response=dict(val="1.3.6.1.4.1.9.1.1709", tag="OID")
)
assert isinstance(result.value, ASN1_OID)

0 comments on commit 151e9a3

Please sign in to comment.