]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
improve coverage
authorBob Halley <halley@dnspython.org>
Fri, 19 Jun 2020 18:14:12 +0000 (11:14 -0700)
committerBob Halley <halley@dnspython.org>
Fri, 19 Jun 2020 18:14:12 +0000 (11:14 -0700)
tests/test_name.py

index a7cb48318398db8b0bd964b803ff6dc174895780..f5c6b6d8bc584f6c10267f608fce8bda223f1d56 100644 (file)
@@ -17,6 +17,7 @@
 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 from typing import Dict # pylint: disable=unused-import
+import operator
 import unittest
 
 from io import BytesIO
@@ -776,6 +777,8 @@ class NameTestCase(unittest.TestCase):
         s = n.to_unicode()
         self.assertEqual(s, 'foo.bar.')
 
+    @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('ドメイン.テスト',
@@ -811,6 +814,20 @@ class NameTestCase(unittest.TestCase):
         self.assertEqual(n.to_unicode(True, dns.name.IDNA_2008),
                          'groß.com')
 
+    def testIDNA2003Misc(self):
+        self.assertEqual(dns.name.IDNA_2003.encode(''), b'')
+        self.assertRaises(dns.name.LabelTooLong,
+                          lambda: dns.name.IDNA_2003.encode('x' * 64))
+
+    @unittest.skipUnless(dns.name.have_idna_2008,
+                         'Python idna cannot be imported; no IDNA2008')
+    def testIDNA2008Misc(self):
+        self.assertEqual(dns.name.IDNA_2008.encode(''), b'')
+        self.assertRaises(dns.name.LabelTooLong,
+                          lambda: dns.name.IDNA_2008.encode('x' * 64))
+        self.assertRaises(dns.name.LabelTooLong,
+                          lambda: dns.name.IDNA_2008.encode('groß' + 'x' * 60))
+
     def testReverseIPv4(self):
         e = dns.name.from_text('1.0.0.127.in-addr.arpa.')
         n = dns.reversename.from_address('127.0.0.1')
@@ -891,5 +908,59 @@ class NameTestCase(unittest.TestCase):
         self.assertRaises(dns.exception.SyntaxError,
                           lambda: dns.e164.to_e164(n))
 
+    def test_incompatible_relations(self):
+        n1 = dns.name.from_text('example')
+        n2 = 'abc'
+        for oper in [operator.lt, operator.le, operator.ge, operator.gt]:
+            self.assertRaises(TypeError, lambda: oper(n1, n2))
+        self.assertFalse(n1 == n2)
+        self.assertTrue(n1 != n2)
+
+    def testFromUnicodeSimpleEscape(self):
+        n = dns.name.from_unicode(r'a.\b')
+        e = dns.name.from_unicode(r'a.b')
+        self.assertEqual(n, e)
+
+    def testFromUnicodeBadEscape(self):
+        def bad1():
+            n = dns.name.from_unicode(r'a.b\0q1.c.')
+        self.assertRaises(dns.name.BadEscape, bad1)
+        def bad2():
+            n = dns.name.from_unicode(r'a.b\0')
+        self.assertRaises(dns.name.BadEscape, bad2)
+
+    def testFromUnicodeNotString(self):
+        def bad():
+            dns.name.from_unicode(b'123')
+        self.assertRaises(ValueError, bad)
+
+    def testFromUnicodeBadOrigin(self):
+        def bad():
+            dns.name.from_unicode('example', 123)
+        self.assertRaises(ValueError, bad)
+
+    def testFromUnicodeEmptyLabel(self):
+        def bad():
+            dns.name.from_unicode('a..b.example')
+        self.assertRaises(dns.name.EmptyLabel, bad)
+
+    def testFromUnicodeEmptyName(self):
+        self.assertEqual(dns.name.from_unicode('@', None), dns.name.empty)
+
+    def testFromTextNotString(self):
+        def bad():
+            dns.name.from_text(123)
+        self.assertRaises(ValueError, bad)
+
+    def testFromTextBadOrigin(self):
+        def bad():
+            dns.name.from_text('example', 123)
+        self.assertRaises(ValueError, bad)
+
+    def testFromWireNotBytes(self):
+        def bad():
+            dns.name.from_wire(123, 0)
+        self.assertRaises(ValueError, bad)
+
 if __name__ == '__main__':
     unittest.main()