File: test_imap_utf7.py

package info (click to toggle)
python-imapclient 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 5,355; sh: 14; makefile: 11
file content (50 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright (c) 2014, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses

import unittest

from imapclient.imap_utf7 import decode, encode


class IMAP4UTF7TestCase(unittest.TestCase):
    tests = [
        ["Foo", b"Foo"],
        ["Foo Bar", b"Foo Bar"],
        ["Stuff & Things", b"Stuff &- Things"],
        ["Hello world", b"Hello world"],
        ["Hello & world", b"Hello &- world"],
        ["Hello\xffworld", b"Hello&AP8-world"],
        ["\xff\xfe\xfd\xfc", b"&AP8A,gD9APw-"],
        [
            "~peter/mail/\u65e5\u672c\u8a9e/\u53f0\u5317",
            b"~peter/mail/&ZeVnLIqe-/&U,BTFw-",
        ],  # example from RFC 2060
        ["\x00foo", b"&AAA-foo"],
        ["foo\r\n\nbar\n", b"foo&AA0ACgAK-bar&AAo-"],  # see imapclient/#187 issue
    ]

    def test_encode(self):
        for input, output in self.tests:
            encoded = encode(input)
            self.assertIsInstance(encoded, bytes)
            self.assertEqual(encoded, output)

    def test_decode(self):
        for input, output in self.tests:
            decoded = decode(output)
            self.assertIsInstance(decoded, str)
            self.assertEqual(input, decoded)

    def test_printable_singletons(self):
        """
        The IMAP4 modified UTF-7 implementation encodes all printable
        characters which are in ASCII using the corresponding ASCII byte.
        """
        # All printables represent themselves
        for o in list(range(0x20, 0x26)) + list(range(0x27, 0x7F)):
            self.assertEqual(bytes((o,)), encode(chr(o)))
            self.assertEqual(chr(o), decode(bytes((o,))))
        self.assertEqual(encode("&"), b"&-")
        self.assertEqual(encode("&"), b"&-")
        self.assertEqual(decode(b"&-"), "&")