blob: 13695de67e652f576d889f205ef664189b73d45b [file] [log] [blame]
Walter Dörwald21d3a322003-05-01 17:45:56 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Walter Dörwald21d3a322003-05-01 17:45:56 +00003import base64
Guido van Rossum4581ae52007-05-22 21:56:47 +00004import binascii
Vinay Sajipf9596182012-03-02 01:01:13 +00005import os
Victor Stinner479736b2010-05-25 21:12:34 +00006import sys
7import subprocess
Raymond Hettinger2ae87532002-05-18 00:25:10 +00008
Raymond Hettinger2ae87532002-05-18 00:25:10 +00009
Ezio Melottib3aedd42010-11-20 19:04:17 +000010
Barry Warsaw4f019d32004-01-04 01:13:02 +000011class LegacyBase64TestCase(unittest.TestCase):
Georg Brandlb54d8012009-06-04 09:11:51 +000012 def test_encodebytes(self):
Barry Warsaw4f019d32004-01-04 01:13:02 +000013 eq = self.assertEqual
Georg Brandlb54d8012009-06-04 09:11:51 +000014 eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
15 eq(base64.encodebytes(b"a"), b"YQ==\n")
16 eq(base64.encodebytes(b"ab"), b"YWI=\n")
17 eq(base64.encodebytes(b"abc"), b"YWJj\n")
18 eq(base64.encodebytes(b""), b"")
19 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz"
Guido van Rossum4581ae52007-05-22 21:56:47 +000020 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21 b"0123456789!@#0^&*();:<>,. []{}"),
22 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
23 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
24 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
Serhiy Storchaka017523c2013-04-28 15:53:08 +030025 # Non-bytes
26 eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n')
Georg Brandlb54d8012009-06-04 09:11:51 +000027 self.assertRaises(TypeError, base64.encodebytes, "")
Guido van Rossumcb682582002-08-22 19:18:56 +000028
Georg Brandlb54d8012009-06-04 09:11:51 +000029 def test_decodebytes(self):
Barry Warsaw4f019d32004-01-04 01:13:02 +000030 eq = self.assertEqual
Georg Brandlb54d8012009-06-04 09:11:51 +000031 eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
32 eq(base64.decodebytes(b"YQ==\n"), b"a")
33 eq(base64.decodebytes(b"YWI=\n"), b"ab")
34 eq(base64.decodebytes(b"YWJj\n"), b"abc")
35 eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
Guido van Rossum4581ae52007-05-22 21:56:47 +000036 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
37 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
38 b"abcdefghijklmnopqrstuvwxyz"
39 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 b"0123456789!@#0^&*();:<>,. []{}")
Georg Brandlb54d8012009-06-04 09:11:51 +000041 eq(base64.decodebytes(b''), b'')
Serhiy Storchaka017523c2013-04-28 15:53:08 +030042 # Non-bytes
43 eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc')
Georg Brandlb54d8012009-06-04 09:11:51 +000044 self.assertRaises(TypeError, base64.decodebytes, "")
Barry Warsaw4f019d32004-01-04 01:13:02 +000045
46 def test_encode(self):
47 eq = self.assertEqual
Serhiy Storchakaabac0a72013-04-28 15:56:11 +030048 from io import BytesIO, StringIO
Guido van Rossum34d19282007-08-09 01:03:29 +000049 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz'
50 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
51 b'0123456789!@#0^&*();:<>,. []{}')
52 outfp = BytesIO()
Barry Warsaw4f019d32004-01-04 01:13:02 +000053 base64.encode(infp, outfp)
54 eq(outfp.getvalue(),
Guido van Rossum34d19282007-08-09 01:03:29 +000055 b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
56 b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
57 b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
Serhiy Storchakaabac0a72013-04-28 15:56:11 +030058 # Non-binary files
59 self.assertRaises(TypeError, base64.encode, StringIO('abc'), BytesIO())
60 self.assertRaises(TypeError, base64.encode, BytesIO(b'abc'), StringIO())
61 self.assertRaises(TypeError, base64.encode, StringIO('abc'), StringIO())
Barry Warsaw4f019d32004-01-04 01:13:02 +000062
63 def test_decode(self):
Serhiy Storchakaabac0a72013-04-28 15:56:11 +030064 from io import BytesIO, StringIO
Guido van Rossum34d19282007-08-09 01:03:29 +000065 infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=')
66 outfp = BytesIO()
Barry Warsaw4f019d32004-01-04 01:13:02 +000067 base64.decode(infp, outfp)
Guido van Rossum34d19282007-08-09 01:03:29 +000068 self.assertEqual(outfp.getvalue(), b'www.python.org')
Serhiy Storchakaabac0a72013-04-28 15:56:11 +030069 # Non-binary files
70 self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), BytesIO())
71 self.assertRaises(TypeError, base64.encode, BytesIO(b'YWJj\n'), StringIO())
72 self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), StringIO())
Barry Warsaw4f019d32004-01-04 01:13:02 +000073
Ezio Melottib3aedd42010-11-20 19:04:17 +000074
Barry Warsaw4f019d32004-01-04 01:13:02 +000075class BaseXYTestCase(unittest.TestCase):
76 def test_b64encode(self):
77 eq = self.assertEqual
78 # Test default alphabet
Guido van Rossum4581ae52007-05-22 21:56:47 +000079 eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=")
80 eq(base64.b64encode(b'\x00'), b'AA==')
81 eq(base64.b64encode(b"a"), b"YQ==")
82 eq(base64.b64encode(b"ab"), b"YWI=")
83 eq(base64.b64encode(b"abc"), b"YWJj")
84 eq(base64.b64encode(b""), b"")
85 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz"
86 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
87 b"0123456789!@#0^&*();:<>,. []{}"),
88 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
89 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
90 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
Barry Warsaw4f019d32004-01-04 01:13:02 +000091 # Test with arbitrary alternative characters
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +000092 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd')
Serhiy Storchaka017523c2013-04-28 15:53:08 +030093 # Non-bytes
94 eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==')
95 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
96 b'01a*b$cd')
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +000097 # Check if passing a str object raises an error
98 self.assertRaises(TypeError, base64.b64encode, "")
99 self.assertRaises(TypeError, base64.b64encode, b"", altchars="")
Barry Warsaw4f019d32004-01-04 01:13:02 +0000100 # Test standard alphabet
Guido van Rossum4581ae52007-05-22 21:56:47 +0000101 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=")
102 eq(base64.standard_b64encode(b"a"), b"YQ==")
103 eq(base64.standard_b64encode(b"ab"), b"YWI=")
104 eq(base64.standard_b64encode(b"abc"), b"YWJj")
105 eq(base64.standard_b64encode(b""), b"")
106 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz"
107 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108 b"0123456789!@#0^&*();:<>,. []{}"),
109 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
110 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
111 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300112 # Non-bytes
113 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +0000114 # Check if passing a str object raises an error
115 self.assertRaises(TypeError, base64.standard_b64encode, "")
Barry Warsaw4f019d32004-01-04 01:13:02 +0000116 # Test with 'URL safe' alternative characters
Guido van Rossum4581ae52007-05-22 21:56:47 +0000117 eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd')
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300118 # Non-bytes
119 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_cd')
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +0000120 # Check if passing a str object raises an error
121 self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
Barry Warsaw4f019d32004-01-04 01:13:02 +0000122
123 def test_b64decode(self):
124 eq = self.assertEqual
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100125
126 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org",
127 b'AA==': b'\x00',
128 b"YQ==": b"a",
129 b"YWI=": b"ab",
130 b"YWJj": b"abc",
131 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
132 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
133 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==":
134
135 b"abcdefghijklmnopqrstuvwxyz"
136 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
137 b"0123456789!@#0^&*();:<>,. []{}",
138 b'': b'',
139 }
140 for data, res in tests.items():
141 eq(base64.b64decode(data), res)
142 eq(base64.b64decode(data.decode('ascii')), res)
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300143 # Non-bytes
144 eq(base64.b64decode(bytearray(b"YWJj")), b"abc")
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100145
Barry Warsaw4f019d32004-01-04 01:13:02 +0000146 # Test with arbitrary alternative characters
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100147 tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d',
148 }
149 for (data, altchars), res in tests_altchars.items():
150 data_str = data.decode('ascii')
151 altchars_str = altchars.decode('ascii')
152
153 eq(base64.b64decode(data, altchars=altchars), res)
154 eq(base64.b64decode(data_str, altchars=altchars), res)
155 eq(base64.b64decode(data, altchars=altchars_str), res)
156 eq(base64.b64decode(data_str, altchars=altchars_str), res)
157
Barry Warsaw4f019d32004-01-04 01:13:02 +0000158 # Test standard alphabet
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100159 for data, res in tests.items():
160 eq(base64.standard_b64decode(data), res)
161 eq(base64.standard_b64decode(data.decode('ascii')), res)
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300162 # Non-bytes
163 eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc")
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100164
Barry Warsaw4f019d32004-01-04 01:13:02 +0000165 # Test with 'URL safe' alternative characters
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100166 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
167 b'': b'',
168 }
169 for data, res in tests_urlsafe.items():
170 eq(base64.urlsafe_b64decode(data), res)
171 eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300172 # Non-bytes
173 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1d')
Barry Warsaw4f019d32004-01-04 01:13:02 +0000174
R. David Murray64951362010-11-11 20:09:20 +0000175 def test_b64decode_padding_error(self):
Guido van Rossum4581ae52007-05-22 21:56:47 +0000176 self.assertRaises(binascii.Error, base64.b64decode, b'abc')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100177 self.assertRaises(binascii.Error, base64.b64decode, 'abc')
Barry Warsaw4f019d32004-01-04 01:13:02 +0000178
R. David Murray64951362010-11-11 20:09:20 +0000179 def test_b64decode_invalid_chars(self):
180 # issue 1466065: Test some invalid characters.
181 tests = ((b'%3d==', b'\xdd'),
182 (b'$3d==', b'\xdd'),
183 (b'[==', b''),
184 (b'YW]3=', b'am'),
185 (b'3{d==', b'\xdd'),
186 (b'3d}==', b'\xdd'),
187 (b'@@', b''),
188 (b'!', b''),
189 (b'YWJj\nYWI=', b'abcab'))
190 for bstr, res in tests:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000191 self.assertEqual(base64.b64decode(bstr), res)
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100192 self.assertEqual(base64.b64decode(bstr.decode('ascii')), res)
R. David Murray64951362010-11-11 20:09:20 +0000193 with self.assertRaises(binascii.Error):
194 base64.b64decode(bstr, validate=True)
Antoine Pitroudff46fa2012-02-20 19:46:26 +0100195 with self.assertRaises(binascii.Error):
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100196 base64.b64decode(bstr.decode('ascii'), validate=True)
R. David Murray64951362010-11-11 20:09:20 +0000197
Barry Warsaw4f019d32004-01-04 01:13:02 +0000198 def test_b32encode(self):
199 eq = self.assertEqual
Guido van Rossum4581ae52007-05-22 21:56:47 +0000200 eq(base64.b32encode(b''), b'')
201 eq(base64.b32encode(b'\x00'), b'AA======')
202 eq(base64.b32encode(b'a'), b'ME======')
203 eq(base64.b32encode(b'ab'), b'MFRA====')
204 eq(base64.b32encode(b'abc'), b'MFRGG===')
205 eq(base64.b32encode(b'abcd'), b'MFRGGZA=')
206 eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300207 # Non-bytes
208 eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=')
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +0000209 self.assertRaises(TypeError, base64.b32encode, "")
Barry Warsaw4f019d32004-01-04 01:13:02 +0000210
211 def test_b32decode(self):
212 eq = self.assertEqual
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100213 tests = {b'': b'',
214 b'AA======': b'\x00',
215 b'ME======': b'a',
216 b'MFRA====': b'ab',
217 b'MFRGG===': b'abc',
218 b'MFRGGZA=': b'abcd',
219 b'MFRGGZDF': b'abcde',
220 }
221 for data, res in tests.items():
222 eq(base64.b32decode(data), res)
223 eq(base64.b32decode(data.decode('ascii')), res)
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300224 # Non-bytes
225 eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc')
Barry Warsaw4f019d32004-01-04 01:13:02 +0000226
227 def test_b32decode_casefold(self):
228 eq = self.assertEqual
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100229 tests = {b'': b'',
230 b'ME======': b'a',
231 b'MFRA====': b'ab',
232 b'MFRGG===': b'abc',
233 b'MFRGGZA=': b'abcd',
234 b'MFRGGZDF': b'abcde',
235 # Lower cases
236 b'me======': b'a',
237 b'mfra====': b'ab',
238 b'mfrgg===': b'abc',
239 b'mfrggza=': b'abcd',
240 b'mfrggzdf': b'abcde',
241 }
242
243 for data, res in tests.items():
244 eq(base64.b32decode(data, True), res)
245 eq(base64.b32decode(data.decode('ascii'), True), res)
246
Serhiy Storchakaea2b4902013-05-28 15:27:29 +0300247 self.assertRaises(binascii.Error, base64.b32decode, b'me======')
248 self.assertRaises(binascii.Error, base64.b32decode, 'me======')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100249
Barry Warsaw4f019d32004-01-04 01:13:02 +0000250 # Mapping zero and one
Guido van Rossum4581ae52007-05-22 21:56:47 +0000251 eq(base64.b32decode(b'MLO23456'), b'b\xdd\xad\xf3\xbe')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100252 eq(base64.b32decode('MLO23456'), b'b\xdd\xad\xf3\xbe')
253
254 map_tests = {(b'M1023456', b'L'): b'b\xdd\xad\xf3\xbe',
255 (b'M1023456', b'I'): b'b\x1d\xad\xf3\xbe',
256 }
257 for (data, map01), res in map_tests.items():
258 data_str = data.decode('ascii')
259 map01_str = map01.decode('ascii')
260
261 eq(base64.b32decode(data, map01=map01), res)
262 eq(base64.b32decode(data_str, map01=map01), res)
263 eq(base64.b32decode(data, map01=map01_str), res)
264 eq(base64.b32decode(data_str, map01=map01_str), res)
Serhiy Storchakaea2b4902013-05-28 15:27:29 +0300265 self.assertRaises(binascii.Error, base64.b32decode, data)
266 self.assertRaises(binascii.Error, base64.b32decode, data_str)
Barry Warsaw4f019d32004-01-04 01:13:02 +0000267
268 def test_b32decode_error(self):
Serhiy Storchakaea2b4902013-05-28 15:27:29 +0300269 for data in [b'abc', b'ABCDEF==', b'==ABCDEF']:
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100270 with self.assertRaises(binascii.Error):
271 base64.b32decode(data)
Antoine Pitroudff46fa2012-02-20 19:46:26 +0100272 with self.assertRaises(binascii.Error):
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100273 base64.b32decode(data.decode('ascii'))
Barry Warsaw4f019d32004-01-04 01:13:02 +0000274
275 def test_b16encode(self):
276 eq = self.assertEqual
Guido van Rossum4581ae52007-05-22 21:56:47 +0000277 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
278 eq(base64.b16encode(b'\x00'), b'00')
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300279 # Non-bytes
280 eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
Alexandre Vassalotti5209857f2008-05-03 04:39:38 +0000281 self.assertRaises(TypeError, base64.b16encode, "")
Barry Warsaw4f019d32004-01-04 01:13:02 +0000282
283 def test_b16decode(self):
284 eq = self.assertEqual
Guido van Rossum4581ae52007-05-22 21:56:47 +0000285 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100286 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
Guido van Rossum4581ae52007-05-22 21:56:47 +0000287 eq(base64.b16decode(b'00'), b'\x00')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100288 eq(base64.b16decode('00'), b'\x00')
Barry Warsaw4f019d32004-01-04 01:13:02 +0000289 # Lower case is not allowed without a flag
Guido van Rossum4581ae52007-05-22 21:56:47 +0000290 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100291 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef')
Barry Warsaw4f019d32004-01-04 01:13:02 +0000292 # Case fold
Guido van Rossum4581ae52007-05-22 21:56:47 +0000293 eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100294 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
Serhiy Storchaka017523c2013-04-28 15:53:08 +0300295 # Non-bytes
296 eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
Antoine Pitrouea6b4d52012-02-20 19:30:23 +0100297
298 def test_decode_nonascii_str(self):
299 decode_funcs = (base64.b64decode,
300 base64.standard_b64decode,
301 base64.urlsafe_b64decode,
302 base64.b32decode,
303 base64.b16decode)
304 for f in decode_funcs:
305 self.assertRaises(ValueError, f, 'with non-ascii \xcb')
Guido van Rossum4581ae52007-05-22 21:56:47 +0000306
307 def test_ErrorHeritage(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000308 self.assertTrue(issubclass(binascii.Error, ValueError))
Barry Warsaw4f019d32004-01-04 01:13:02 +0000309
310
Ezio Melottib3aedd42010-11-20 19:04:17 +0000311
Victor Stinner479736b2010-05-25 21:12:34 +0000312class TestMain(unittest.TestCase):
Vinay Sajipf9596182012-03-02 01:01:13 +0000313 def tearDown(self):
314 if os.path.exists(support.TESTFN):
315 os.unlink(support.TESTFN)
316
Victor Stinner479736b2010-05-25 21:12:34 +0000317 def get_output(self, *args, **options):
318 args = (sys.executable, '-m', 'base64') + args
319 return subprocess.check_output(args, **options)
320
321 def test_encode_decode(self):
322 output = self.get_output('-t')
323 self.assertSequenceEqual(output.splitlines(), (
324 b"b'Aladdin:open sesame'",
325 br"b'QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n'",
326 b"b'Aladdin:open sesame'",
327 ))
328
329 def test_encode_file(self):
330 with open(support.TESTFN, 'wb') as fp:
331 fp.write(b'a\xffb\n')
332
333 output = self.get_output('-e', support.TESTFN)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000334 self.assertEqual(output.rstrip(), b'Yf9iCg==')
Victor Stinner479736b2010-05-25 21:12:34 +0000335
336 with open(support.TESTFN, 'rb') as fp:
337 output = self.get_output('-e', stdin=fp)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000338 self.assertEqual(output.rstrip(), b'Yf9iCg==')
Victor Stinner479736b2010-05-25 21:12:34 +0000339
340 def test_decode(self):
341 with open(support.TESTFN, 'wb') as fp:
342 fp.write(b'Yf9iCg==')
343 output = self.get_output('-d', support.TESTFN)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000344 self.assertEqual(output.rstrip(), b'a\xffb')
Victor Stinner479736b2010-05-25 21:12:34 +0000345
346
Ezio Melottib3aedd42010-11-20 19:04:17 +0000347
Raymond Hettinger2ae87532002-05-18 00:25:10 +0000348def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000349 support.run_unittest(__name__)
Barry Warsaw4f019d32004-01-04 01:13:02 +0000350
351if __name__ == '__main__':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000352 test_main()