Home | History | Annotate | Download | only in test
      1 import unittest
      2 from test import test_support
      3 import base64
      4 
      5 
      6 
      8 class LegacyBase64TestCase(unittest.TestCase):
      9     def test_encodestring(self):
     10         eq = self.assertEqual
     11         eq(base64.encodestring("www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n")
     12         eq(base64.encodestring("a"), "YQ==\n")
     13         eq(base64.encodestring("ab"), "YWI=\n")
     14         eq(base64.encodestring("abc"), "YWJj\n")
     15         eq(base64.encodestring(""), "")
     16         eq(base64.encodestring("abcdefghijklmnopqrstuvwxyz"
     17                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     18                                "0123456789!@#0^&*();:<>,. []{}"),
     19            "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
     20            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
     21            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
     22         # Non-bytes
     23         eq(base64.encodestring(bytearray('abc')), 'YWJj\n')
     24 
     25     def test_decodestring(self):
     26         eq = self.assertEqual
     27         eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
     28         eq(base64.decodestring("YQ==\n"), "a")
     29         eq(base64.decodestring("YWI=\n"), "ab")
     30         eq(base64.decodestring("YWJj\n"), "abc")
     31         eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
     32                                "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
     33                                "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
     34            "abcdefghijklmnopqrstuvwxyz"
     35            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     36            "0123456789!@#0^&*();:<>,. []{}")
     37         eq(base64.decodestring(''), '')
     38         # Non-bytes
     39         eq(base64.decodestring(bytearray("YWJj\n")), "abc")
     40 
     41     def test_encode(self):
     42         eq = self.assertEqual
     43         from cStringIO import StringIO
     44         infp = StringIO('abcdefghijklmnopqrstuvwxyz'
     45                         'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     46                         '0123456789!@#0^&*();:<>,. []{}')
     47         outfp = StringIO()
     48         base64.encode(infp, outfp)
     49         eq(outfp.getvalue(),
     50            'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
     51            'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
     52            'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
     53 
     54     def test_decode(self):
     55         from cStringIO import StringIO
     56         infp = StringIO('d3d3LnB5dGhvbi5vcmc=')
     57         outfp = StringIO()
     58         base64.decode(infp, outfp)
     59         self.assertEqual(outfp.getvalue(), 'www.python.org')
     60 
     61 
     62 
     64 class BaseXYTestCase(unittest.TestCase):
     65     def test_b64encode(self):
     66         eq = self.assertEqual
     67         # Test default alphabet
     68         eq(base64.b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
     69         eq(base64.b64encode('\x00'), 'AA==')
     70         eq(base64.b64encode("a"), "YQ==")
     71         eq(base64.b64encode("ab"), "YWI=")
     72         eq(base64.b64encode("abc"), "YWJj")
     73         eq(base64.b64encode(""), "")
     74         eq(base64.b64encode("abcdefghijklmnopqrstuvwxyz"
     75                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     76                             "0123456789!@#0^&*();:<>,. []{}"),
     77            "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
     78            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
     79            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
     80         # Test with arbitrary alternative characters
     81         eq(base64.b64encode('\xd3V\xbeo\xf7\x1d', altchars='*$'), '01a*b$cd')
     82         # Non-bytes
     83         eq(base64.b64encode(bytearray('abcd')), 'YWJjZA==')
     84         self.assertRaises(TypeError, base64.b64encode,
     85                           '\xd3V\xbeo\xf7\x1d', altchars=bytearray('*$'))
     86         # Test standard alphabet
     87         eq(base64.standard_b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
     88         eq(base64.standard_b64encode("a"), "YQ==")
     89         eq(base64.standard_b64encode("ab"), "YWI=")
     90         eq(base64.standard_b64encode("abc"), "YWJj")
     91         eq(base64.standard_b64encode(""), "")
     92         eq(base64.standard_b64encode("abcdefghijklmnopqrstuvwxyz"
     93                                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     94                                      "0123456789!@#0^&*();:<>,. []{}"),
     95            "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
     96            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
     97            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
     98         # Non-bytes
     99         eq(base64.standard_b64encode(bytearray('abcd')), 'YWJjZA==')
    100         # Test with 'URL safe' alternative characters
    101         eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd')
    102         # Non-bytes
    103         eq(base64.urlsafe_b64encode(bytearray('\xd3V\xbeo\xf7\x1d')), '01a-b_cd')
    104 
    105     def test_b64decode(self):
    106         eq = self.assertEqual
    107         eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
    108         eq(base64.b64decode('AA=='), '\x00')
    109         eq(base64.b64decode("YQ=="), "a")
    110         eq(base64.b64decode("YWI="), "ab")
    111         eq(base64.b64decode("YWJj"), "abc")
    112         eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
    113                             "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
    114                             "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
    115            "abcdefghijklmnopqrstuvwxyz"
    116            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    117            "0123456789!@#0^&*();:<>,. []{}")
    118         eq(base64.b64decode(''), '')
    119         # Test with arbitrary alternative characters
    120         eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
    121         # Non-bytes
    122         eq(base64.b64decode(bytearray("YWJj")), "abc")
    123         # Test standard alphabet
    124         eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
    125         eq(base64.standard_b64decode("YQ=="), "a")
    126         eq(base64.standard_b64decode("YWI="), "ab")
    127         eq(base64.standard_b64decode("YWJj"), "abc")
    128         eq(base64.standard_b64decode(""), "")
    129         eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
    130                                      "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
    131                                      "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
    132            "abcdefghijklmnopqrstuvwxyz"
    133            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    134            "0123456789!@#0^&*();:<>,. []{}")
    135         # Non-bytes
    136         eq(base64.standard_b64decode(bytearray("YWJj")), "abc")
    137         # Test with 'URL safe' alternative characters
    138         eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
    139         # Non-bytes
    140         eq(base64.urlsafe_b64decode(bytearray('01a-b_cd')), '\xd3V\xbeo\xf7\x1d')
    141 
    142     def test_b64decode_error(self):
    143         self.assertRaises(TypeError, base64.b64decode, 'abc')
    144 
    145     def test_b32encode(self):
    146         eq = self.assertEqual
    147         eq(base64.b32encode(''), '')
    148         eq(base64.b32encode('\x00'), 'AA======')
    149         eq(base64.b32encode('a'), 'ME======')
    150         eq(base64.b32encode('ab'), 'MFRA====')
    151         eq(base64.b32encode('abc'), 'MFRGG===')
    152         eq(base64.b32encode('abcd'), 'MFRGGZA=')
    153         eq(base64.b32encode('abcde'), 'MFRGGZDF')
    154         # Non-bytes
    155         eq(base64.b32encode(bytearray('abcd')), 'MFRGGZA=')
    156 
    157     def test_b32decode(self):
    158         eq = self.assertEqual
    159         eq(base64.b32decode(''), '')
    160         eq(base64.b32decode('AA======'), '\x00')
    161         eq(base64.b32decode('ME======'), 'a')
    162         eq(base64.b32decode('MFRA===='), 'ab')
    163         eq(base64.b32decode('MFRGG==='), 'abc')
    164         eq(base64.b32decode('MFRGGZA='), 'abcd')
    165         eq(base64.b32decode('MFRGGZDF'), 'abcde')
    166         # Non-bytes
    167         self.assertRaises(TypeError, base64.b32decode, bytearray('MFRGG==='))
    168 
    169     def test_b32decode_casefold(self):
    170         eq = self.assertEqual
    171         eq(base64.b32decode('', True), '')
    172         eq(base64.b32decode('ME======', True), 'a')
    173         eq(base64.b32decode('MFRA====', True), 'ab')
    174         eq(base64.b32decode('MFRGG===', True), 'abc')
    175         eq(base64.b32decode('MFRGGZA=', True), 'abcd')
    176         eq(base64.b32decode('MFRGGZDF', True), 'abcde')
    177         # Lower cases
    178         eq(base64.b32decode('me======', True), 'a')
    179         eq(base64.b32decode('mfra====', True), 'ab')
    180         eq(base64.b32decode('mfrgg===', True), 'abc')
    181         eq(base64.b32decode('mfrggza=', True), 'abcd')
    182         eq(base64.b32decode('mfrggzdf', True), 'abcde')
    183         # Expected exceptions
    184         self.assertRaises(TypeError, base64.b32decode, 'me======')
    185         # Mapping zero and one
    186         eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe')
    187         eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe')
    188         eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe')
    189 
    190     def test_b32decode_error(self):
    191         self.assertRaises(TypeError, base64.b32decode, 'abc')
    192         self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==')
    193 
    194     def test_b16encode(self):
    195         eq = self.assertEqual
    196         eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF')
    197         eq(base64.b16encode('\x00'), '00')
    198         # Non-bytes
    199         eq(base64.b16encode(bytearray('\x01\x02\xab\xcd\xef')), '0102ABCDEF')
    200 
    201     def test_b16decode(self):
    202         eq = self.assertEqual
    203         eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef')
    204         eq(base64.b16decode('00'), '\x00')
    205         # Lower case is not allowed without a flag
    206         self.assertRaises(TypeError, base64.b16decode, '0102abcdef')
    207         # Case fold
    208         eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef')
    209         # Non-bytes
    210         eq(base64.b16decode(bytearray("0102ABCDEF")), '\x01\x02\xab\xcd\xef')
    211 
    212 
    213 
    215 def test_main():
    216     test_support.run_unittest(__name__)
    217 
    218 if __name__ == '__main__':
    219     test_main()
    220