[Git][NTPsec/ntpsec][master] 2 commits: Added tests for Packet(), tweaked Packet to not use magic number.

Ian Bruene gitlab at mg.gitlab.com
Sat Aug 19 15:07:38 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
ef2e206f by Ian Bruene at 2017-08-19T10:05:40-05:00
Added tests for Packet(), tweaked Packet to not use magic number.

- - - - -
6ca988d3 by Ian Bruene at 2017-08-19T10:06:31-05:00
Merge branch 'master' of gitlab.com:NTPsec/ntpsec

- - - - -


2 changed files:

- pylib/packet.py
- tests/pylib/test_packet.py


Changes:

=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -345,7 +345,8 @@ class Packet:
         self.li_vn_mode = 0     # leap, version, mode (uint8_t)
         # Subclasses have variable fields here
         self.extension = b''     # extension data
-        self.li_vn_mode = Packet.PKT_LI_VN_MODE(3, version, mode)
+        self.li_vn_mode = Packet.PKT_LI_VN_MODE(ntp.magic.LEAP_NOTINSYNC,
+                                                version, mode)
 
     # These decorators will allow us to assign the extension Python 3 strings
     @property


=====================================
tests/pylib/test_packet.py
=====================================
--- a/tests/pylib/test_packet.py
+++ b/tests/pylib/test_packet.py
@@ -224,6 +224,53 @@ class AuthenticatorJig:
                                                    keytype, passwd))
         return "mac"
 
+# ==========================================================
+#  Tests
+# =========================================================
+
+
+class TestPacket(unittest.TestCase):
+    target = ntp.packet.Packet
+
+    def test_VN_MODE(self):
+        f = self.target.VN_MODE
+        self.assertEqual(f(0, 0), 0x00)
+        self.assertEqual(f(0, 6), 0x06)
+        self.assertEqual(f(1, 6), 0x0E)
+        self.assertEqual(f(7, 6), 0x3E)
+        self.assertEqual(f(9, 6), 0x0E)  # overflow version
+        self.assertEqual(f(0, 9), 0x01)  # overflow mode
+
+    def test_PKT_LI_VM_MODE(self):
+        f = self.target.PKT_LI_VN_MODE
+        self.assertEqual(f(0, 0, 0), 0x00)
+        self.assertEqual(f(1, 4, 6), 0x66)
+        self.assertEqual(f(3, 4, 6), 0xE6)
+        self.assertEqual(f(5, 4, 6), 0x66)  # overflow leap
+
+    def test_Packet(self):
+        # Test __init__, basic
+        cls = self.target()
+        self.assertEqual(cls.session, None)
+        self.assertEqual(cls.li_vn_mode, 0xE3)
+        self.assertEqual(cls.extension, b'')
+        # Test __init__, custom
+        cls = self.target(2, 2, "foo")
+        self.assertEqual(cls.session, "foo")
+        self.assertEqual(cls.li_vn_mode, 0xD2)
+        # Test leap
+        self.assertEqual(cls.leap(), "unsync")
+        cls.li_vn_mode = self.target.PKT_LI_VN_MODE(0, 1, 1)
+        self.assertEqual(cls.leap(), "no-leap")
+        cls.li_vn_mode = self.target.PKT_LI_VN_MODE(1, 1, 1)
+        self.assertEqual(cls.leap(), "add-leap")
+        cls.li_vn_mode = self.target.PKT_LI_VN_MODE(2, 3, 4)
+        self.assertEqual(cls.leap(), "del-leap")
+        # Test version
+        self.assertEqual(cls.version(), 3)
+        # Test mode
+        self.assertEqual(cls.mode(), 4)
+
 
 class TestMisc(unittest.TestCase):
     def test_Peer(self):



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/9073d1e0033d803742cc9fb51b12c43997242e15...6ca988d3b7dd1c2cf9e5f086526c2438f1db7f61

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/9073d1e0033d803742cc9fb51b12c43997242e15...6ca988d3b7dd1c2cf9e5f086526c2438f1db7f61
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/20170819/e03d3f74/attachment.html>


More information about the vc mailing list