]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Improve name coverage slightly by testing the uts_46 branch in 2008 decode.
authorBob Halley <halley@dnspython.org>
Thu, 23 Jul 2020 13:33:27 +0000 (06:33 -0700)
committerBob Halley <halley@dnspython.org>
Thu, 23 Jul 2020 13:33:27 +0000 (06:33 -0700)
I'm not sure how useful in practice this is, as we don't offer a codec
configuration default for it, but I'm not sure you'd never do it, and the
existing code was in the wrong order.

dns/name.py
tests/test_name.py

index 529ae7f9fe994e0671aaf616d57ada0d048713de..94d7b936055bbcb288b512029289d0ca726cffcd 100644 (file)
@@ -215,9 +215,10 @@ class IDNA2008Codec(IDNACodec):
         if not have_idna_2008:
             raise NoIDNA2008
         try:
+            ulabel = idna.ulabel(label)
             if self.uts_46:
-                label = idna.uts46_remap(label, False, False)
-            return _escapify(idna.ulabel(label))
+                ulabel = idna.uts46_remap(ulabel, False, self.transitional)
+            return _escapify(ulabel)
         except (idna.IDNAError, UnicodeError) as e:
             raise IDNAException(idna_exception=e)
 
index dec8c5f5dee2b3526165393c5876dcdf93f431f1..0d3f968b6d92663aba479c0cf420040ad66b1c79 100644 (file)
@@ -781,12 +781,30 @@ class NameTestCase(unittest.TestCase):
     @unittest.skipUnless(dns.name.have_idna_2008,
                          'Python idna cannot be imported; no IDNA2008')
     def testToUnicode4(self):
-        if dns.name.have_idna_2008:
-            n = dns.name.from_text('ドメイン.テスト',
-                                   idna_codec=dns.name.IDNA_2008)
-            s = n.to_unicode()
-            self.assertEqual(str(n), 'xn--eckwd4c7c.xn--zckzah.')
-            self.assertEqual(s, 'ドメイン.テスト.')
+        n = dns.name.from_text('ドメイン.テスト',
+                               idna_codec=dns.name.IDNA_2008)
+        s = n.to_unicode()
+        self.assertEqual(str(n), 'xn--eckwd4c7c.xn--zckzah.')
+        self.assertEqual(s, 'ドメイン.テスト.')
+
+    @unittest.skipUnless(dns.name.have_idna_2008,
+                         'Python idna cannot be imported; no IDNA2008')
+    def testToUnicode5(self):
+        # Exercise UTS 46 remapping in decode.  This doesn't normally happen
+        # as you can see from us having to instantiate the codec as
+        # transitional with strict decoding, not one of our usual choices.
+        codec = dns.name.IDNA2008Codec(True, True, False, True)
+        n = dns.name.from_text('xn--gro-7ka.com')
+        self.assertEqual(n.to_unicode(idna_codec=codec),
+                         'gross.com.')
+
+    @unittest.skipUnless(dns.name.have_idna_2008,
+                         'Python idna cannot be imported; no IDNA2008')
+    def testToUnicode6(self):
+        # Test strict 2008 decoding without UTS 46
+        n = dns.name.from_text('xn--gro-7ka.com')
+        self.assertEqual(n.to_unicode(idna_codec=dns.name.IDNA_2008_Strict),
+                         'groß.com.')
 
     def testDefaultDecodeIsJustPunycode(self):
         # groß.com. in IDNA2008 form, pre-encoded.