test_idna_codec.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. import codecs
  3. import sys
  4. import unittest
  5. import idna.codec
  6. class IDNACodecTests(unittest.TestCase):
  7. def testCodec(self):
  8. pass
  9. def testIncrementalDecoder(self):
  10. # Tests derived from Python standard library test/test_codecs.py
  11. incremental_tests = (
  12. ("python.org", b"python.org"),
  13. ("python.org.", b"python.org."),
  14. ("pyth\xf6n.org", b"xn--pythn-mua.org"),
  15. ("pyth\xf6n.org.", b"xn--pythn-mua.org."),
  16. )
  17. for decoded, encoded in incremental_tests:
  18. self.assertEqual("".join(codecs.iterdecode((bytes([c]) for c in encoded), "idna")),
  19. decoded)
  20. decoder = codecs.getincrementaldecoder("idna")()
  21. self.assertEqual(decoder.decode(b"xn--xam", ), "")
  22. self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
  23. self.assertEqual(decoder.decode(b"rg"), "")
  24. self.assertEqual(decoder.decode(b"", True), "org")
  25. decoder.reset()
  26. self.assertEqual(decoder.decode(b"xn--xam", ), "")
  27. self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.")
  28. self.assertEqual(decoder.decode(b"rg."), "org.")
  29. self.assertEqual(decoder.decode(b"", True), "")
  30. def testIncrementalEncoder(self):
  31. # Tests derived from Python standard library test/test_codecs.py
  32. incremental_tests = (
  33. ("python.org", b"python.org"),
  34. ("python.org.", b"python.org."),
  35. ("pyth\xf6n.org", b"xn--pythn-mua.org"),
  36. ("pyth\xf6n.org.", b"xn--pythn-mua.org."),
  37. )
  38. for decoded, encoded in incremental_tests:
  39. self.assertEqual(b"".join(codecs.iterencode(decoded, "idna")),
  40. encoded)
  41. encoder = codecs.getincrementalencoder("idna")()
  42. self.assertEqual(encoder.encode("\xe4x"), b"")
  43. self.assertEqual(encoder.encode("ample.org"), b"xn--xample-9ta.")
  44. self.assertEqual(encoder.encode("", True), b"org")
  45. encoder.reset()
  46. self.assertEqual(encoder.encode("\xe4x"), b"")
  47. self.assertEqual(encoder.encode("ample.org."), b"xn--xample-9ta.org.")
  48. self.assertEqual(encoder.encode("", True), b"")
  49. if __name__ == '__main__':
  50. unittest.main()