Debian Codegraph @

tests/test_imap_utf7.py

raw
1 # Copyright (c) 2014, Menno Smits
2 # Released subject to the New BSD License
3 # Please see http://en.wikipedia.org/wiki/BSD_licenses
4
5 import unittest
6
7 from imapclient.imap_utf7 import decode, encode
8
9
10 class IMAP4UTF7TestCase(unittest.TestCase):
11 tests = [
12 ["Foo", b"Foo"],
13 ["Foo Bar", b"Foo Bar"],
14 ["Stuff & Things", b"Stuff &- Things"],
15 ["Hello world", b"Hello world"],
16 ["Hello & world", b"Hello &- world"],
17 ["Hello\xffworld", b"Hello&AP8-world"],
18 ["\xff\xfe\xfd\xfc", b"&AP8A,gD9APw-"],
19 [
20 "~peter/mail/\u65e5\u672c\u8a9e/\u53f0\u5317",
21 b"~peter/mail/&ZeVnLIqe-/&U,BTFw-",
22 ], # example from RFC 2060
23 ["\x00foo", b"&AAA-foo"],
24 ["foo\r\n\nbar\n", b"foo&AA0ACgAK-bar&AAo-"], # see imapclient/#187 issue
25 ]
26
27 def test_encode(self):
28 for input, output in self.tests:
29 encoded = encode(input)
30 self.assertIsInstance(encoded, bytes)
31 self.assertEqual(encoded, output)
32
33 def test_decode(self):
34 for input, output in self.tests:
35 decoded = decode(output)
36 self.assertIsInstance(decoded, str)
37 self.assertEqual(input, decoded)
38
39 def test_printable_singletons(self):
40 """
41 The IMAP4 modified UTF-7 implementation encodes all printable
42 characters which are in ASCII using the corresponding ASCII byte.
43 """
44 # All printables represent themselves
45 for o in list(range(0x20, 0x26)) + list(range(0x27, 0x7F)):
46 self.assertEqual(bytes((o,)), encode(chr(o)))
47 self.assertEqual(chr(o), decode(bytes((o,))))
48 self.assertEqual(encode("&"), b"&-")
49 self.assertEqual(encode("&"), b"&-")
50 self.assertEqual(decode(b"&-"), "&")