blob: 8ee4d2e878110ed1d213b13585afe114f0f8df03 [file] [log] [blame]
Raymond Hettinger2ae87532002-05-18 00:25:10 +00001import unittest
2import test_support
3import base64
4from binascii import Error as binascii_error
5
6class Base64TestCase(unittest.TestCase):
7 def test_encode_string(self):
8 """Testing encode string"""
9 test_support.verify(base64.encodestring("www.python.org") ==
10 "d3d3LnB5dGhvbi5vcmc=\n",
11 reason="www.python.org encodestring failed")
12 test_support.verify(base64.encodestring("a") ==
13 "YQ==\n",
14 reason="a encodestring failed")
15 test_support.verify(base64.encodestring("ab") ==
16 "YWI=\n",
17 reason="ab encodestring failed")
18 test_support.verify(base64.encodestring("abc") ==
19 "YWJj\n",
20 reason="abc encodestring failed")
21 test_support.verify(base64.encodestring("") ==
22 "",
23 reason="null encodestring failed")
24 test_support.verify(base64.encodestring(
25 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}") ==
26 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNTY3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n",
27 reason = "long encodestring failed")
28
29 def test_decode_string(self):
30 """Testing decode string"""
31 test_support.verify(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n") ==
32 "www.python.org",
33 reason="www.python.org decodestring failed")
34 test_support.verify(base64.decodestring("YQ==\n") ==
35 "a",
36 reason="a decodestring failed")
37 test_support.verify(base64.decodestring("YWI=\n") ==
38 "ab",
39 reason="ab decodestring failed")
40 test_support.verify(base64.decodestring("YWJj\n") ==
41 "abc",
42 reason="abc decodestring failed")
43 test_support.verify(base64.decodestring(
44 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNTY3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") ==
45 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}",
46 reason = "long decodestring failed")
47 try:
48 base64.decodestring("")
49 except binascii_error:
50 pass
51 else:
52 self.fail("expected a binascii.Error on null decode request")
53
54def test_main():
55 test_support.run_unittest(Base64TestCase)
56
57if __name__ == "__main__":
58 test_main()
59