0001 import unittest 0002 from test import test_support 0003 import base64 0004 0005 0006 0007 class LegacyBase64TestCase(unittest.TestCase): 0008 def test_encodestring(self): 0009 eq = self.assertEqual 0010 eq(base64.encodestring("www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n") 0011 eq(base64.encodestring("a"), "YQ==\n") 0012 eq(base64.encodestring("ab"), "YWI=\n") 0013 eq(base64.encodestring("abc"), "YWJj\n") 0014 eq(base64.encodestring(""), "") 0015 eq(base64.encodestring("abcdefghijklmnopqrstuvwxyz" 0016 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0017 "0123456789!@#0^&*();:<>,. []{}"), 0018 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0019 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 0020 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") 0021 0022 def test_decodestring(self): 0023 eq = self.assertEqual 0024 eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org") 0025 eq(base64.decodestring("YQ==\n"), "a") 0026 eq(base64.decodestring("YWI=\n"), "ab") 0027 eq(base64.decodestring("YWJj\n"), "abc") 0028 eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0029 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 0030 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), 0031 "abcdefghijklmnopqrstuvwxyz" 0032 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0033 "0123456789!@#0^&*();:<>,. []{}") 0034 eq(base64.decodestring(''), '') 0035 0036 def test_encode(self): 0037 eq = self.assertEqual 0038 from cStringIO import StringIO 0039 infp = StringIO('abcdefghijklmnopqrstuvwxyz' 0040 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 0041 '0123456789!@#0^&*();:<>,. []{}') 0042 outfp = StringIO() 0043 base64.encode(infp, outfp) 0044 eq(outfp.getvalue(), 0045 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE' 0046 'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT' 0047 'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n') 0048 0049 def test_decode(self): 0050 from cStringIO import StringIO 0051 infp = StringIO('d3d3LnB5dGhvbi5vcmc=') 0052 outfp = StringIO() 0053 base64.decode(infp, outfp) 0054 self.assertEqual(outfp.getvalue(), 'www.python.org') 0055 0056 0057 0058 class BaseXYTestCase(unittest.TestCase): 0059 def test_b64encode(self): 0060 eq = self.assertEqual 0061 # Test default alphabet 0062 eq(base64.b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=") 0063 eq(base64.b64encode("a"), "YQ==") 0064 eq(base64.b64encode("ab"), "YWI=") 0065 eq(base64.b64encode("abc"), "YWJj") 0066 eq(base64.b64encode(""), "") 0067 eq(base64.b64encode("abcdefghijklmnopqrstuvwxyz" 0068 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0069 "0123456789!@#0^&*();:<>,. []{}"), 0070 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0071 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 0072 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 0073 # Test with arbitrary alternative characters 0074 eq(base64.b64encode('\xd3V\xbeo\xf7\x1d', altchars='*$'), '01a*b$cd') 0075 # Test standard alphabet 0076 eq(base64.standard_b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=") 0077 eq(base64.standard_b64encode("a"), "YQ==") 0078 eq(base64.standard_b64encode("ab"), "YWI=") 0079 eq(base64.standard_b64encode("abc"), "YWJj") 0080 eq(base64.standard_b64encode(""), "") 0081 eq(base64.standard_b64encode("abcdefghijklmnopqrstuvwxyz" 0082 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0083 "0123456789!@#0^&*();:<>,. []{}"), 0084 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0085 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 0086 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 0087 # Test with 'URL safe' alternative characters 0088 eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd') 0089 0090 def test_b64decode(self): 0091 eq = self.assertEqual 0092 eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org") 0093 eq(base64.b64decode("YQ=="), "a") 0094 eq(base64.b64decode("YWI="), "ab") 0095 eq(base64.b64decode("YWJj"), "abc") 0096 eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0097 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 0098 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="), 0099 "abcdefghijklmnopqrstuvwxyz" 0100 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0101 "0123456789!@#0^&*();:<>,. []{}") 0102 eq(base64.b64decode(''), '') 0103 # Test with arbitrary alternative characters 0104 eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d') 0105 # Test standard alphabet 0106 eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org") 0107 eq(base64.standard_b64decode("YQ=="), "a") 0108 eq(base64.standard_b64decode("YWI="), "ab") 0109 eq(base64.standard_b64decode("YWJj"), "abc") 0110 eq(base64.standard_b64decode(""), "") 0111 eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 0112 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 0113 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="), 0114 "abcdefghijklmnopqrstuvwxyz" 0115 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 0116 "0123456789!@#0^&*();:<>,. []{}") 0117 # Test with 'URL safe' alternative characters 0118 eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d') 0119 0120 def test_b64decode_error(self): 0121 self.assertRaises(TypeError, base64.b64decode, 'abc') 0122 0123 def test_b32encode(self): 0124 eq = self.assertEqual 0125 eq(base64.b32encode(''), '') 0126 eq(base64.b32encode('a'), 'ME======') 0127 eq(base64.b32encode('ab'), 'MFRA====') 0128 eq(base64.b32encode('abc'), 'MFRGG===') 0129 eq(base64.b32encode('abcd'), 'MFRGGZA=') 0130 eq(base64.b32encode('abcde'), 'MFRGGZDF') 0131 0132 def test_b32decode(self): 0133 eq = self.assertEqual 0134 eq(base64.b32decode(''), '') 0135 eq(base64.b32decode('ME======'), 'a') 0136 eq(base64.b32decode('MFRA===='), 'ab') 0137 eq(base64.b32decode('MFRGG==='), 'abc') 0138 eq(base64.b32decode('MFRGGZA='), 'abcd') 0139 eq(base64.b32decode('MFRGGZDF'), 'abcde') 0140 0141 def test_b32decode_casefold(self): 0142 eq = self.assertEqual 0143 eq(base64.b32decode('', True), '') 0144 eq(base64.b32decode('ME======', True), 'a') 0145 eq(base64.b32decode('MFRA====', True), 'ab') 0146 eq(base64.b32decode('MFRGG===', True), 'abc') 0147 eq(base64.b32decode('MFRGGZA=', True), 'abcd') 0148 eq(base64.b32decode('MFRGGZDF', True), 'abcde') 0149 # Lower cases 0150 eq(base64.b32decode('me======', True), 'a') 0151 eq(base64.b32decode('mfra====', True), 'ab') 0152 eq(base64.b32decode('mfrgg===', True), 'abc') 0153 eq(base64.b32decode('mfrggza=', True), 'abcd') 0154 eq(base64.b32decode('mfrggzdf', True), 'abcde') 0155 # Expected exceptions 0156 self.assertRaises(TypeError, base64.b32decode, 'me======') 0157 # Mapping zero and one 0158 eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe') 0159 eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe') 0160 eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe') 0161 0162 def test_b32decode_error(self): 0163 self.assertRaises(TypeError, base64.b32decode, 'abc') 0164 self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==') 0165 0166 def test_b16encode(self): 0167 eq = self.assertEqual 0168 eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF') 0169 0170 def test_b16decode(self): 0171 eq = self.assertEqual 0172 eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef') 0173 # Lower case is not allowed without a flag 0174 self.assertRaises(TypeError, base64.b16decode, '0102abcdef') 0175 # Case fold 0176 eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef') 0177 0178 0179 0180 def suite(): 0181 suite = unittest.TestSuite() 0182 suite.addTest(unittest.makeSuite(LegacyBase64TestCase)) 0183 suite.addTest(unittest.makeSuite(BaseXYTestCase)) 0184 return suite 0185 0186 0187 def test_main(): 0188 test_support.run_suite(suite()) 0189 0190 0191 if __name__ == '__main__': 0192 unittest.main(defaultTest='suite') 0193
Generated by PyXR 0.9.4