]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
check for TTL type errors in rdataset/rrset from_text; allow text-form TTLs there.
authorBob Halley <halley@dnspython.org>
Tue, 1 Sep 2020 13:08:43 +0000 (06:08 -0700)
committerBob Halley <halley@dnspython.org>
Tue, 1 Sep 2020 13:08:43 +0000 (06:08 -0700)
dns/rdataset.py
dns/ttl.py
tests/test_rdata.py

index 2a42e424279f8b6d8592638f0b6e4a39c58d9acd..2e3b4d4ad609369cb459228a6b5ec2c48e6a9d69 100644 (file)
@@ -80,9 +80,9 @@ class Rdataset(dns.set.Set):
         TTL or the specified TTL.  If the set contains no rdatas, set the TTL
         to the specified TTL.
 
-        *ttl*, an ``int``.
+        *ttl*, an ``int`` or ``str``.
         """
-
+        ttl = dns.ttl.make(ttl)
         if len(self) == 0:
             self.ttl = ttl
         elif ttl < self.ttl:
index 1a2aaeb73fb19d578802aa8fd328772952fafcab..8ea521359aaa5c45d5e73571cc872b28545a61e9 100644 (file)
@@ -73,3 +73,12 @@ def from_text(text):
     if total < 0 or total > MAX_TTL:
         raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)")
     return total
+
+
+def make(value):
+    if isinstance(value, int):
+        return value
+    elif isinstance(value, str):
+        return dns.ttl.from_text(value)
+    else:
+        raise ValueError('cannot convert value to TTL')
index 407140371fc7f1ff6cf38d6bbdf10f3de933be1f..66ed67cca388dce5c11df21fb1384b709d1a231f 100644 (file)
@@ -35,6 +35,7 @@ from dns.rdtypes.ANY.GPOS import GPOS
 import dns.rdtypes.ANY.RRSIG
 import dns.rdtypes.util
 import dns.tokenizer
+import dns.ttl
 import dns.wire
 
 import tests.stxt_module
@@ -743,5 +744,16 @@ class UtilTestCase(unittest.TestCase):
             dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX,
                                 r'\# 4 000aC000')
 
+    def test_rdataset_ttl_conversion(self):
+        rds1 = dns.rdataset.from_text('in', 'a', 300, '10.0.0.1')
+        self.assertEqual(rds1.ttl, 300)
+        rds2 = dns.rdataset.from_text('in', 'a', '5m', '10.0.0.1')
+        self.assertEqual(rds2.ttl, 300)
+        with self.assertRaises(ValueError):
+            dns.rdataset.from_text('in', 'a', 1.6, '10.0.0.1')
+        with self.assertRaises(dns.ttl.BadTTL):
+            dns.rdataset.from_text('in', 'a', '10.0.0.1', '10.0.0.2')
+
+
 if __name__ == '__main__':
     unittest.main()