| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | from imapclient.imap_utf7 import decode, encode, FolderNameError |
|---|
| 6 | from imapclient.test.util import unittest |
|---|
| 7 | |
|---|
| 8 | class IMAP4UTF7TestCase(unittest.TestCase): |
|---|
| 9 | tests = [ |
|---|
| 10 | ['Foo', 'Foo'], |
|---|
| 11 | ['Foo Bar', 'Foo Bar'], |
|---|
| 12 | ['Stuff & Things', 'Stuff &- Things'], |
|---|
| 13 | [u'Hello world', 'Hello world'], |
|---|
| 14 | [u'Hello & world', 'Hello &- world'], |
|---|
| 15 | [u'Hello\xffworld', 'Hello&AP8-world'], |
|---|
| 16 | [u'\xff\xfe\xfd\xfc', '&AP8A,gD9APw-'], |
|---|
| 17 | [u'~peter/mail/\u65e5\u672c\u8a9e/\u53f0\u5317', |
|---|
| 18 | '~peter/mail/&ZeVnLIqe-/&U,BTFw-'], |
|---|
| 19 | ['\x00foo', '&AAA-foo'], |
|---|
| 20 | ] |
|---|
| 21 | |
|---|
| 22 | def test_encode(self): |
|---|
| 23 | for (input, output) in self.tests: |
|---|
| 24 | self.assertEquals(encode(input), output) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | def test_decode(self): |
|---|
| 28 | for (input, output) in self.tests: |
|---|
| 29 | decoded = decode(output) |
|---|
| 30 | self.assertEquals(input, decoded) |
|---|
| 31 | self.assert_(isinstance(decoded, unicode)) |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | def test_illegal_chars(self): |
|---|
| 35 | not_valid_as_str = [ |
|---|
| 36 | 'blah' + chr(0x80) + 'sne', |
|---|
| 37 | chr(0xaa) + 'foo', |
|---|
| 38 | 'blah' + chr(0xff) + 'sne'] |
|---|
| 39 | |
|---|
| 40 | for name in not_valid_as_str: |
|---|
| 41 | self.assertRaises(FolderNameError, encode, name) |
|---|
| 42 | |
|---|
| 43 | unicode_names = [unicode(name, 'latin-1') for name in not_valid_as_str] |
|---|
| 44 | for name in unicode_names: |
|---|
| 45 | assert isinstance(encode(name), str) |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | def test_printableSingletons(self): |
|---|
| 49 | """ |
|---|
| 50 | The IMAP4 modified UTF-7 implementation encodes all printable |
|---|
| 51 | characters which are in ASCII using the corresponding ASCII byte. |
|---|
| 52 | """ |
|---|
| 53 | |
|---|
| 54 | for o in range(0x20, 0x26) + range(0x27, 0x7f): |
|---|
| 55 | self.failUnlessEqual(chr(o), encode(chr(o))) |
|---|
| 56 | self.failUnlessEqual(chr(o), decode(chr(o))) |
|---|
| 57 | self.failUnlessEqual(encode('&'), '&-') |
|---|
| 58 | self.failUnlessEqual(decode('&-'), '&') |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | def test_FolderNameError_super(self): |
|---|
| 62 | self.assert_(issubclass(FolderNameError, ValueError)) |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | if __name__ == '__main__': |
|---|
| 66 | unittest.main() |
|---|