From: Bob Halley Date: Tue, 30 Jun 2020 15:24:41 +0000 (-0700) Subject: increase name test coverage X-Git-Tag: v2.0.0rc2~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e16e7fee759893b17abb9257bdbe96e7edc65f8;p=thirdparty%2Fdnspython.git increase name test coverage --- diff --git a/tests/test_name.py b/tests/test_name.py index f5c6b6d8..0e8bd8ef 100644 --- a/tests/test_name.py +++ b/tests/test_name.py @@ -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 copy import operator import unittest @@ -962,5 +963,70 @@ class NameTestCase(unittest.TestCase): dns.name.from_wire(123, 0) self.assertRaises(ValueError, bad) + def testBadPunycode(self): + c = dns.name.IDNACodec() + with self.assertRaises(dns.name.IDNAException): + c.decode(b'xn--0000h') + + def testRootLabel2003StrictDecode(self): + c = dns.name.IDNA_2003_Strict + self.assertEqual(c.decode(b''), '') + + @unittest.skipUnless(dns.name.have_idna_2008, + 'Python idna cannot be imported; no IDNA2008') + def testRootLabel2008StrictDecode(self): + c = dns.name.IDNA_2008_Strict + self.assertEqual(c.decode(b''), '') + + @unittest.skipUnless(dns.name.have_idna_2008, + 'Python idna cannot be imported; no IDNA2008') + def testCodecNotFoundRaises(self): + dns.name.have_idna_2008 = False + with self.assertRaises(dns.name.NoIDNA2008): + c = dns.name.IDNA2008Codec() + c.encode('Königsgäßchen') + with self.assertRaises(dns.name.NoIDNA2008): + c = dns.name.IDNA2008Codec(strict_decode=True) + c.decode('xn--eckwd4c7c.xn--zckzah.') + dns.name.have_idna_2008 = True + + @unittest.skipUnless(dns.name.have_idna_2008, + 'Python idna cannot be imported; no IDNA2008') + def testBadPunycodeStrict2008(self): + c = dns.name.IDNA2008Codec(strict_decode=True) + with self.assertRaises(dns.name.IDNAException): + c.decode(b'xn--0000h') + + def testRelativizeSubtractionSyntax(self): + n = dns.name.from_text('foo.example.') + o = dns.name.from_text('example.') + e = dns.name.from_text('foo', None) + self.assertEqual(n - o, e) + + def testCopy(self): + n1 = dns.name.from_text('foo.example.') + n2 = copy.copy(n1) + self.assertTrue(n1 is not n2) + # the Name constructor always copies labels, so there is no + # difference between copy and deepcopy + self.assertTrue(n1.labels is not n2.labels) + self.assertEqual(len(n1.labels), len(n2.labels)) + for i, l in enumerate(n1.labels): + self.assertTrue(l is n2[i]) + + def testDeepCopy(self): + n1 = dns.name.from_text('foo.example.') + n2 = copy.deepcopy(n1) + self.assertTrue(n1 is not n2) + self.assertTrue(n1.labels is not n2.labels) + self.assertEqual(len(n1.labels), len(n2.labels)) + for i, l in enumerate(n1.labels): + self.assertTrue(l is n2[i]) + + def testNoAttributeDeletion(self): + n = dns.name.from_text('foo.example.') + with self.assertRaises(TypeError): + del n.labels + if __name__ == '__main__': unittest.main()