[Git][NTPsec/ntpsec][master] 3 commits: Moved makeflags function from test_agentx.py to agentx.py

Ian Bruene gitlab at mg.gitlab.com
Sun Oct 1 22:40:39 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
73e17791 by Ian Bruene at 2017-10-01T17:40:16-05:00
Moved makeflags function from test_agentx.py to agentx.py

Changed format of makeflags to be useful for more than just tests

- - - - -
a296ef0f by Ian Bruene at 2017-10-01T17:40:16-05:00
Changed decode_pduheader to use makeflags()

- - - - -
da6839e5 by Ian Bruene at 2017-10-01T17:40:16-05:00
Added, tested, and put to use new flagbyte functions

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -1145,11 +1145,36 @@ def decode_varbindlist(data, header):
     return varbinds
 
 
+def encode_flagbyte(flags):
+    flagbyte = 0
+    flagbyte |= flags["instReg"]
+    flagbyte |= flags["newIndex"] << 1
+    flagbyte |= flags["anyIndex"] << 2
+    flagbyte |= flags["contextP"] << 3  # nonDefaultContext
+    flagbyte |= flags["bigEndian"] << 4
+    return flagbyte
+
+
+def decode_flagbyte(flags):
+    flagDict = makeflags(bool(flags & 0x1), bool(flags & 0x2),
+                         bool(flags & 0x4), bool(flags & 0x8),
+                         bool(flags & 0x10))
+    return flagDict
+
+
 # =========================================
 # Utilities, glue, and misc
 # =========================================
 
 
+def makeflags(iR, nI, aI, cP, bE):
+    return {"instReg": iR,
+            "newIndex": nI,
+            "anyIndex": aI,
+            "contextP": cP,
+            "bigEndian": bE}
+
+
 def getendian(bigEndian):
     return ">" if bigEndian is True else "<"
 
@@ -1164,12 +1189,9 @@ def encode_pduheader(pduType, instanceRegistration, newIndex,
     # payload_length
     if pduType not in definedPDUTypes:
         raise ValueError("PDU type %s not in defined types" % pduType)
-    flags = 0
-    flags |= instanceRegistration
-    flags |= newIndex << 1
-    flags |= anyIndex << 2
-    flags |= nonDefaultContext << 3
-    flags |= bigEndian << 4
+    flags = encode_flagbyte(makeflags(instanceRegistration, newIndex,
+                                      anyIndex, nonDefaultContext,
+                                      bigEndian))
     # Yes, this is a kluge, it is less problematic then the next best kluge
     endianToken = getendian(bigEndian)
     data = struct.pack(endianToken + "BBBxIIII", 1, pduType, flags,
@@ -1184,12 +1206,7 @@ def decode_pduheader(data):  # Endianness is controlled from the PDU header
     if pduType not in definedPDUTypes:
         raise ValueError("PDU type %s not in defined types" % pduType)
     # Slice up the flags
-    flagDict = {}
-    flagDict["instReg"] = bool(flags & 0x1)
-    flagDict["newIndex"] = bool(flags & 0x2)
-    flagDict["anyIndex"] = bool(flags & 0x4)
-    flagDict["contextP"] = bool(flags & 0x8)
-    flagDict["bigEndian"] = bool(flags & 0x10)
+    flagDict = decode_flagbyte(flags)
     # Chop the remaining parts of the header from the rest of the datastream
     # then parse them
     fmt = getendian(flagDict["bigEndian"]) + "IIII"


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -4,7 +4,7 @@
 import unittest
 import ntp.agentx
 
-from ntp.agentx import slicedata, decode_pduheader
+from ntp.agentx import slicedata, decode_pduheader, makeflags
 
 extraData = b"Would you kindly ignore this?"
 
@@ -52,30 +52,10 @@ maximumOIDstr = b"""\x80\x00\x00\x00\
 """
 
 # The most commonly used flag setups, some tests use custom flags
-standardFlags = {"flags": {"instReg": False,
-                           "newIndex": False,
-                           "anyIndex": False,
-                           "contextP": False,
-                           "bigEndian": True}}
-standardFlags_bare = standardFlags["flags"]
-lilEndianFlags = {"flags": {"instReg": False,
-                            "newIndex": False,
-                            "anyIndex": False,
-                            "contextP": False,
-                            "bigEndian": False}}
-contextFlags = {"flags": {"instReg": False,
-                          "newIndex": False,
-                          "anyIndex": False,
-                          "contextP": True,
-                          "bigEndian": True}}
-
-
-def makeFlags(iR, nI, aI, cP, bE):
-    return {"flags": {"instReg": iR,
-                      "newIndex": nI,
-                      "anyIndex": aI,
-                      "contextP": cP,
-                      "bigEndian": bE}}
+standardFlags_bare = makeflags(False, False, False, False, True)
+standardFlags = {"flags": standardFlags_bare}
+lilEndianFlags = {"flags": makeflags(False, False, False, False, False)}
+contextFlags = {"flags": makeflags(False, False, False, True, True)}
 
 
 def test_pducore(tester, pdu, pduType, endian, sID, tactID, pktID):
@@ -2117,9 +2097,28 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                                     42),
                           b""))
 
+    def test_encode_flagbyte(self):
+        f = ntp.agentx.encode_flagbyte
+        self.assertEqual(f(makeflags(False, True, False, True, False)), 0x0A)
+        self.assertEqual(f(makeflags(True, False, True, False, True)), 0x15)
+
+    def test_decode_flagbyte(self):
+        f = ntp.agentx.decode_flagbyte
+        self.assertEqual(f(0x0A), makeflags(False, True, False, True, False))
+        self.assertEqual(f(0x15), makeflags(True, False, True, False, True))
+
     #
     # Misc tests
     #
+    def test_makeflags(self):
+        f = ntp.agentx.makeflags
+        self.assertEqual(f(True, False, True, False, True),
+                         {"instReg": True,
+                          "newIndex": False,
+                          "anyIndex":  True,
+                          "contextP": False,
+                          "bigEndian": True})
+
     def test_getendian(self):
         f = ntp.agentx.getendian
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/c5fea507d3459830448f1468d042b4755737dc69...da6839e5c9e55b7c420e75716c61d885a17c5535

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/c5fea507d3459830448f1468d042b4755737dc69...da6839e5c9e55b7c420e75716c61d885a17c5535
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/20171001/d7935a68/attachment.html>


More information about the vc mailing list