test_idna_codec.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. import codecs
  3. import io
  4. import unittest
  5. import idna.codec
  6. CODEC_NAME = 'idna2008'
  7. class IDNACodecTests(unittest.TestCase):
  8. def setUp(self):
  9. from . import test_idna
  10. self.idnatests = test_idna.IDNATests()
  11. self.idnatests.setUp()
  12. def testCodec(self):
  13. self.assertIs(codecs.lookup(CODEC_NAME).incrementalencoder, idna.codec.IncrementalEncoder)
  14. def testDirectDecode(self):
  15. self.idnatests.test_decode(decode=lambda obj: codecs.decode(obj, CODEC_NAME))
  16. def testIndirectDecode(self):
  17. self.idnatests.test_decode(decode=lambda obj: obj.decode(CODEC_NAME), skip_str=True)
  18. def testDirectEncode(self):
  19. self.idnatests.test_encode(encode=lambda obj: codecs.encode(obj, CODEC_NAME))
  20. def testIndirectEncode(self):
  21. self.idnatests.test_encode(encode=lambda obj: obj.encode(CODEC_NAME), skip_bytes=True)
  22. def testStreamReader(self):
  23. def decode(obj):
  24. if isinstance(obj, str):
  25. obj = bytes(obj, 'ascii')
  26. buffer = io.BytesIO(obj)
  27. stream = codecs.getreader(CODEC_NAME)(buffer)
  28. return stream.read()
  29. return self.idnatests.test_decode(decode=decode, skip_str=True)
  30. def testStreamWriter(self):
  31. def encode(obj):
  32. buffer = io.BytesIO()
  33. stream = codecs.getwriter(CODEC_NAME)(buffer)
  34. stream.write(obj)
  35. stream.flush()
  36. return buffer.getvalue()
  37. return self.idnatests.test_encode(encode=encode)
  38. def testIncrementalDecoder(self):
  39. # Tests derived from Python standard library test/test_codecs.py
  40. incremental_tests = (
  41. ("python.org", b"python.org"),
  42. ("python.org.", b"python.org."),
  43. ("pyth\xf6n.org", b"xn--pythn-mua.org"),
  44. ("pyth\xf6n.org.", b"xn--pythn-mua.org."),
  45. )
  46. for decoded, encoded in incremental_tests:
  47. self.assertEqual("".join(codecs.iterdecode((bytes([c]) for c in encoded), CODEC_NAME)),
  48. decoded)
  49. decoder = codecs.getincrementaldecoder(CODEC_NAME)()
  50. self.assertEqual(decoder.decode(b"xn--xam", ), "")
  51. self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
  52. self.assertEqual(decoder.decode(b"rg"), "")
  53. self.assertEqual(decoder.decode(b"", True), "org")
  54. decoder.reset()
  55. self.assertEqual(decoder.decode(b"xn--xam", ), "")
  56. self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
  57. self.assertEqual(decoder.decode(b"rg."), "org.")
  58. self.assertEqual(decoder.decode(b"", True), "")
  59. def testIncrementalEncoder(self):
  60. # Tests derived from Python standard library test/test_codecs.py
  61. incremental_tests = (
  62. ("python.org", b"python.org"),
  63. ("python.org.", b"python.org."),
  64. ("pyth\xf6n.org", b"xn--pythn-mua.org"),
  65. ("pyth\xf6n.org.", b"xn--pythn-mua.org."),
  66. )
  67. for decoded, encoded in incremental_tests:
  68. self.assertEqual(b"".join(codecs.iterencode(decoded, CODEC_NAME)),
  69. encoded)
  70. encoder = codecs.getincrementalencoder(CODEC_NAME)()
  71. self.assertEqual(encoder.encode("\xe4x"), b"")
  72. self.assertEqual(encoder.encode("ample.org"), b"xn--xample-9ta.")
  73. self.assertEqual(encoder.encode("", True), b"org")
  74. encoder.reset()
  75. self.assertEqual(encoder.encode("\xe4x"), b"")
  76. self.assertEqual(encoder.encode("ample.org."), b"xn--xample-9ta.org.")
  77. self.assertEqual(encoder.encode("", True), b"")
  78. if __name__ == '__main__':
  79. unittest.main()