]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Make sure all 'strings' passed to the txtbase constructor are encoded if
authorBob Halley <halley@dnspython.org>
Sun, 19 Feb 2017 21:37:14 +0000 (13:37 -0800)
committerBob Halley <halley@dnspython.org>
Sun, 19 Feb 2017 21:37:14 +0000 (13:37 -0800)
needed.  [Issue #239]

dns/rdtypes/txtbase.py
tests/test_bugs.py

index 352b027bf8c003a41812ddd47ffc6d721a54914b..aa341f1acdb91225b92028ca939d4cf3780bdbad 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
+# Copyright (C) 2006-2017 Nominum, Inc.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose with or without fee is hereby granted,
@@ -20,24 +20,29 @@ import struct
 import dns.exception
 import dns.rdata
 import dns.tokenizer
-from dns._compat import binary_type
+from dns._compat import binary_type, string_types
 
 
 class TXTBase(dns.rdata.Rdata):
 
     """Base class for rdata that is like a TXT record
 
-    @ivar strings: the text strings
-    @type strings: list of string
+    @ivar strings: the strings
+    @type strings: list of binary
     @see: RFC 1035"""
 
     __slots__ = ['strings']
 
     def __init__(self, rdclass, rdtype, strings):
         super(TXTBase, self).__init__(rdclass, rdtype)
-        if isinstance(strings, str):
+        if isinstance(strings, binary_type) or \
+           isinstance(strings, string_types):
             strings = [strings]
-        self.strings = strings[:]
+        self.strings = []
+        for string in strings:
+            if isinstance(string, string_types):
+                string = string.encode()
+            self.strings.append(string)
 
     def to_text(self, origin=None, relativize=True, **kw):
         txt = ''
index 717cdce5e92911f65cc6ec8986cf61d85dbf2470..c153c543ed058efac55d0f3d11a9f123cf2197cc 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
+# Copyright (C) 2006-2017 Nominum, Inc.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose with or without fee is hereby granted,
@@ -24,6 +24,7 @@ import binascii
 import dns.rdata
 import dns.rdataclass
 import dns.rdatatype
+import dns.rdtypes.ANY.TXT
 import dns.ttl
 
 class BugsTestCase(unittest.TestCase):
@@ -82,5 +83,18 @@ class BugsTestCase(unittest.TestCase):
         text6 = binascii.hexlify(out6).decode('ascii')
         self.failUnless(text6 == '0002018f000000000000000000000000000010')
 
+    def test_TXT_conversions(self):
+        t1 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT,
+                                     [b'foo'])
+        t2 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT,
+                                     b'foo')
+        t3 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT,
+                                     'foo')
+        t4 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT,
+                                     ['foo'])
+        self.failUnless(t1 == t2)
+        self.failUnless(t1 == t2)
+        self.failUnless(t1 == t4)
+
 if __name__ == '__main__':
     unittest.main()