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_padding_error(self): 143 self.assertRaises(TypeError, base64.b64decode, 'abc') 144 145 def test_b64decode_invalid_chars(self): 146 # issue 1466065: Test some invalid characters. 147 tests = ((b'%3d==', b'\xdd'), 148 (b'$3d==', b'\xdd'), 149 (b'[==', b''), 150 (b'YW]3=', b'am'), 151 (b'3{d==', b'\xdd'), 152 (b'3d}==', b'\xdd'), 153 (b'@@', b''), 154 (b'!', b''), 155 (b'YWJj\nYWI=', b'abcab')) 156 for bstr, res in tests: 157 self.assertEqual(base64.b64decode(bstr), res) 158 self.assertEqual(base64.standard_b64decode(bstr), res) 159 self.assertEqual(base64.urlsafe_b64decode(bstr), res) 160 161 # Normal alphabet characters not discarded when alternative given 162 res = b'\xFB\xEF\xBE\xFF\xFF\xFF' 163 self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res) 164 self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res) 165 166 def test_b32encode(self): 167 eq = self.assertEqual 168 eq(base64.b32encode(''), '') 169 eq(base64.b32encode('\x00'), 'AA======') 170 eq(base64.b32encode('a'), 'ME======') 171 eq(base64.b32encode('ab'), 'MFRA====') 172 eq(base64.b32encode('abc'), 'MFRGG===') 173 eq(base64.b32encode('abcd'), 'MFRGGZA=') 174 eq(base64.b32encode('abcde'), 'MFRGGZDF') 175 # Non-bytes 176 eq(base64.b32encode(bytearray('abcd')), 'MFRGGZA=') 177 178 def test_b32decode(self): 179 eq = self.assertEqual 180 eq(base64.b32decode(''), '') 181 eq(base64.b32decode('AA======'), '\x00') 182 eq(base64.b32decode('ME======'), 'a') 183 eq(base64.b32decode('MFRA===='), 'ab') 184 eq(base64.b32decode('MFRGG==='), 'abc') 185 eq(base64.b32decode('MFRGGZA='), 'abcd') 186 eq(base64.b32decode('MFRGGZDF'), 'abcde') 187 # Non-bytes 188 self.assertRaises(TypeError, base64.b32decode, bytearray('MFRGG===')) 189 190 def test_b32decode_casefold(self): 191 eq = self.assertEqual 192 eq(base64.b32decode('', True), '') 193 eq(base64.b32decode('ME======', True), 'a') 194 eq(base64.b32decode('MFRA====', True), 'ab') 195 eq(base64.b32decode('MFRGG===', True), 'abc') 196 eq(base64.b32decode('MFRGGZA=', True), 'abcd') 197 eq(base64.b32decode('MFRGGZDF', True), 'abcde') 198 # Lower cases 199 eq(base64.b32decode('me======', True), 'a') 200 eq(base64.b32decode('mfra====', True), 'ab') 201 eq(base64.b32decode('mfrgg===', True), 'abc') 202 eq(base64.b32decode('mfrggza=', True), 'abcd') 203 eq(base64.b32decode('mfrggzdf', True), 'abcde') 204 # Expected exceptions 205 self.assertRaises(TypeError, base64.b32decode, 'me======') 206 # Mapping zero and one 207 eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe') 208 eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe') 209 eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe') 210 211 def test_b32decode_error(self): 212 self.assertRaises(TypeError, base64.b32decode, 'abc') 213 self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==') 214 215 def test_b16encode(self): 216 eq = self.assertEqual 217 eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF') 218 eq(base64.b16encode('\x00'), '00') 219 # Non-bytes 220 eq(base64.b16encode(bytearray('\x01\x02\xab\xcd\xef')), '0102ABCDEF') 221 222 def test_b16decode(self): 223 eq = self.assertEqual 224 eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef') 225 eq(base64.b16decode('00'), '\x00') 226 # Lower case is not allowed without a flag 227 self.assertRaises(TypeError, base64.b16decode, '0102abcdef') 228 # Case fold 229 eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef') 230 # Non-bytes 231 eq(base64.b16decode(bytearray("0102ABCDEF")), '\x01\x02\xab\xcd\xef') 232 # Non-alphabet characters 233 self.assertRaises(TypeError, base64.b16decode, '0102AG') 234 # Incorrect "padding" 235 self.assertRaises(TypeError, base64.b16decode, '010') 236 237 238 240 def test_main(): 241 test_support.run_unittest(__name__) 242 243 if __name__ == '__main__': 244 test_main() 245