[Git][NTPsec/ntpsec][master] 4 commits: Less dumb mru kv list assembly

Hal Murray gitlab at mg.gitlab.com
Sun Mar 8 09:53:40 UTC 2020



Hal Murray pushed to branch master at NTPsec / ntpsec


Commits:
dd6a88fd by James Browning at 2020-03-07T03:03:27-08:00
Less dumb mru  kv list assembly


- - - - -
6d648e44 by James Browning at 2020-03-07T03:05:35-08:00
rework test to work w/ new reassembly


- - - - -
7075f4ea by James Browning at 2020-03-07T08:00:42-08:00
should sort mru kv(s) metadata then numbered records


- - - - -
51f41df5 by James Browning at 2020-03-07T08:07:53-08:00
Ignore a macOS specific file


- - - - -


3 changed files:

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


Changes:

=====================================
.gitignore
=====================================
@@ -3,3 +3,4 @@
 *.pyc
 build
 attic/clock
+.DS_Store


=====================================
pylib/packet.py
=====================================
@@ -1310,8 +1310,10 @@ This combats source address spoofing
         mru = None
         nonce = None
         items = list(variables.items())
-##      if items:                   # See issue #642
-##          items.sort()
+        fake_list = []
+        fake_dict = {}
+        if items:                   # See issue #642
+            items.sort(key=mru_kv_key)
         for (tag, val) in items:
             self.warndbg("tag=%s, val=%s" % (tag, val), 4)
             if tag == "nonce":
@@ -1334,19 +1336,20 @@ This combats source address spoofing
                         idx = int(idx)
                     except ValueError:
                         raise ControlException(SERR_BADTAG % tag)
-                    if idx != curidx:
-                        # This makes duplicates
-                        curidx = idx
-
-                        if mru:
-                            # Can't have partial slots on list
-                            # or printing crashes after ^C
-                            # Append full slot now
-                            span.entries.append(mru)
-                        mru = MRUEntry()
-                        self.slots += 1
-                    setattr(mru, prefix, val)
-        if mru:
+                    ### Does not check missing/gappy entries
+                    if idx not in fake_list:
+                        fake_dict[str(idx)] = {}
+                        fake_list.append(idx)
+                    fake_dict[str(idx)][member] = val
+        fake_list.sort()
+        for idx in fake_list:
+            mru = MRUEntry()
+            # Always 6 in practice, in the tests not so much
+#            if len(fake_dict[str(idx)]) != 6:
+#                continue
+            for prefix in ("addr", "last", "first", "ct", "mv", "rs"):
+                if prefix in fake_dict[str(idx)]:  # dodgy test needs this line
+                    setattr(mru, prefix, fake_dict[str(idx)][prefix])
             span.entries.append(mru)
         if direct is not None:
             direct(span.entries)
@@ -1635,6 +1638,16 @@ def generate_mru_lastseen(span, existingBufferSize):
     return buf
 
 
+def mru_kv_key(token):
+    bits = token[0].split('.')
+    if len(bits) == 1:
+        return -2
+    try:
+        return int(bits[1])
+    except ValueError:
+        return -1
+
+
 class Authenticator:
     "MAC authentication manager for NTP packets."
 


=====================================
tests/pylib/test_packet.py
=====================================
@@ -1630,11 +1630,11 @@ class TestControlSession(unittest.TestCase):
     def test___mru_analyze(self):
         # data: nonce, last.older, addr.older, now, last.newest
         #       addr, last, first, ct, mv, rs
-        vars = {"nonce":"noncevalue", "last.older":"FAIL0",
-                "addr.older":"FAIL1", "now":"0xcfba1ce0.80000000",
-                "last.newest":"FAIL2", "addr.1":"addrtest",
-                "last.2":"lasttest", "first.3":"firsttest", "ct.4":"cttest",
-                "mv.5":"mvtest", "rs.6":"rstest"}
+        vars = odict((("nonce", "noncevalue"), ("last.older", "FAIL0"),
+                ("addr.older", "FAIL1"), ("now", "0xcfba1ce0.80000000"),
+                ("last.newest", "FAIL2"), ("addr.1", "addrtest"),
+                ("last.2", "lasttest"), ("first.3", "firsttest"), ("ct.4", "cttest"),
+                ("mv.5", "mvtest"), ("rs.6", "rstest")))
         cls = self.target()
         span = ntpp.MRUList()
         nonce = cls._ControlSession__mru_analyze(vars, span, None)
@@ -1652,9 +1652,8 @@ class TestControlSession(unittest.TestCase):
         m5.mv = "mvtest"
         m6 = ntpp.MRUEntry()
         m6.rs = "rstest"
-        expected = [m1, m4, m3, m2, m5, m6] # sort order
+        expected = [m1, m2, m3, m4, m5, m6] # sort order
         self.assertEqual(len(span.entries), len(expected))
-        if 1: return          # see issue #642
         for i in range(len(span.entries)):
             self.assertEqual(span.entries[i].addr, expected[i].addr)
             self.assertEqual(span.entries[i].last, expected[i].last)



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/f0bd02b25de9c2cc2c54be50c17fbaed8338f926...51f41df59751ce28aab12e999ba3e6472e573756

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/f0bd02b25de9c2cc2c54be50c17fbaed8338f926...51f41df59751ce28aab12e999ba3e6472e573756
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/20200308/44f74616/attachment-0001.htm>


More information about the vc mailing list