[Git][NTPsec/ntpsec][master] Added compareOID function and tests

Ian Bruene gitlab at mg.gitlab.com
Fri Aug 4 20:01:02 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
cb3ba11a by Ian Bruene at 2017-08-04T14:48:32-05:00
Added compareOID function and tests

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -889,6 +889,35 @@ def decode_searchrange_list_nullterm(data, header):
 # =========================================
 
 
+def compareOID(one, two):
+    if one == two:  # Behold! The magic of a high level language
+        return 0
+    lone = len(one)
+    ltwo = len(two)
+    if lone > ltwo:
+        x = two
+        y = one
+        lx = ltwo
+        flipped = True
+    else:
+        x = one
+        y = two
+        lx = lone
+        flipped = False
+    for i in range(lx):
+        if x[i] == y[i]:
+            continue
+        else:
+            c = cmp(x[i], y[i])
+            c = -c if flipped is True else c
+            return c
+    # Only reach this if shorter, and each index is equal
+    if flipped is True:
+        return 1
+    else:
+        return -1
+
+
 def getendian(bigEndian):
     return ">" if bigEndian is True else "<"
 


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -2046,6 +2046,29 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
             fail = True
         self.assertEqual(fail, True)
 
+    def test_compareOID(self):
+        f = ntp.agentx.compareOID
+
+        # Test equal
+        self.assertEqual(f((1, 2, 3, 4), (1, 2, 3, 4)), 0)
+
+        # Test equal length, one < two
+        self.assertEqual(f((1, 2, 3, 4), (1, 2, 3, 5)), -1)
+        # Test equal length, one > two
+        self.assertEqual(f((1, 2, 3, 4), (1, 2, 3, 0)), 1)
+        # Test one shorter, less than two, equal for length
+        self.assertEqual(f((1, 2, 3), (1, 2, 3, 4)), -1)
+        # Test one shorter, less than two
+        self.assertEqual(f((1, 2, 3), (1, 2, 4, 5)), -1)
+        # Test one shorter, greater than two
+        self.assertEqual(f((1, 2, 3), (1, 2, 2, 4)), 1)
+        # Test two shorter, less than one, equal for length
+        self.assertEqual(f((1, 2, 3, 4), (1, 2, 3)), 1)
+        # Test two shorter, less than one
+        self.assertEqual(f((1, 2, 4, 5), (1, 2, 3)), 1)
+        # Test two shorter, greater than one
+        self.assertEqual(f((1, 2, 2, 4), (1, 2, 3)), -1)
+
 
 if __name__ == "__main__":
     unittest.main()



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

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/cb3ba11a5043d7b91cba8384b945545784533519
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/20170804/370f35c4/attachment.html>


More information about the vc mailing list