[Git][NTPsec/ntpsec][master] Fully split SNMP uint32 vs int32 handlers

Ian Bruene gitlab at mg.gitlab.com
Thu Oct 26 22:04:57 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
29fd6fa3 by Ian Bruene at 2017-10-26T17:03:39-05:00
Fully split SNMP uint32 vs int32 handlers

- - - - -


2 changed files:

- pylib/agentx.py
- tests/pylib/test_agentx.py


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -994,14 +994,14 @@ class Varbind:
 
 def encode_integer32(bigEndian, num):
     endianToken = getendian(bigEndian)
-    return struct.pack(endianToken + "I", num)
+    return struct.pack(endianToken + "i", num)
 
 
 def decode_integer32(data, header):
     flags = header["flags"]
     endianToken = getendian(flags["bigEndian"])
     num, data = slicedata(data, 4)
-    num = struct.unpack(endianToken + "I", num)[0]
+    num = struct.unpack(endianToken + "i", num)[0]
     return (num, data)
 
 
@@ -1010,6 +1010,19 @@ def sanity_integer32(data):
         raise TypeError
 
 
+def encode_unsigned32(bigEndian, num):
+    endianToken = getendian(bigEndian)
+    return struct.pack(endianToken + "I", num)
+
+
+def decode_unsigned32(data, header):
+    flags = header["flags"]
+    endianToken = getendian(flags["bigEndian"])
+    num, data = slicedata(data, 4)
+    num = struct.unpack(endianToken + "I", num)[0]
+    return (num, data)
+
+
 def sanity_unsigned32(data):
     if data != (data & 0xFFFFFFFF):
         raise ValueError
@@ -1338,14 +1351,14 @@ definedValueTypes = {  # Used by the varbind functions
                     encode_ipaddr,
                     decode_ipaddr),
     VALUE_COUNTER32: (sanity_unsigned32,
-                      encode_integer32,
-                      decode_integer32),
+                      encode_unsigned32,
+                      decode_unsigned32),
     VALUE_GAUGE32: (sanity_unsigned32,
-                    encode_integer32,
-                    decode_integer32),
+                    encode_unsigned32,
+                    decode_unsigned32),
     VALUE_TIME_TICKS: (sanity_unsigned32,
-                       encode_integer32,
-                       decode_integer32),
+                       encode_unsigned32,
+                       decode_unsigned32),
     VALUE_OPAQUE: (sanity_octetstr,
                    encode_octetstr,
                    decode_octetstr),


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -1971,12 +1971,13 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                          b"\x00\x40\x00\x00\x03\x00\x00\x00"
                          b"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
                          b"\x00\x00\x00\x04\x10\x20\x30\x40")
-        # Test integer32 types
-        cls = target(a.VALUE_INTEGER, (1, 2, 3), 42)
+        # Test integer32 type
+        cls = target(a.VALUE_INTEGER, (1, 2, 3), -42)
         self.assertEqual(cls.encode(True),
                          b"\x00\x02\x00\x00\x03\x00\x00\x00"
                          b"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
-                         b"\x00\x00\x00\x2A")
+                         b"\xFF\xFF\xFF\xD6")
+        # Test unsigned32 types
         cls = target(a.VALUE_COUNTER32, (1, 2, 3), 42)
         self.assertEqual(cls.encode(True),
                          b"\x00\x41\x00\x00\x03\x00\x00\x00"
@@ -2057,13 +2058,13 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                                     a.OID((1, 2, 3), False),
                                     (16, 32, 48, 64)),
                           b""))
-        # Test integer32 types
+        # Test integer32 type
         self.assertEqual(f(b"\x00\x02\x00\x00\x03\x00\x00\x00"
                            b"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
-                           b"\x00\x00\x00\x2A", standardFlags),
+                           b"\xFF\xFF\xFF\xD6", standardFlags),
                          (a.Varbind(a.VALUE_INTEGER,
                                     a.OID((1, 2, 3), False),
-                                    42),
+                                    -42),
                           b""))
         self.assertEqual(f(b"\x00\x41\x00\x00\x03\x00\x00\x00"
                            b"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
@@ -2072,6 +2073,7 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                                     a.OID((1, 2, 3), False),
                                     42),
                           b""))
+        # Test unsigned32 types
         self.assertEqual(f(b"\x00\x42\x00\x00\x03\x00\x00\x00"
                            b"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
                            b"\x00\x00\x00\x2A", standardFlags),



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/29fd6fa3c2a280aebf0de76f76ee006aac44a968

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/29fd6fa3c2a280aebf0de76f76ee006aac44a968
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20171026/5fa86035/attachment.html>


More information about the vc mailing list