]> granicus.if.org Git - python/commitdiff
Issue #17842. Added base64 module tests with bytearray arguments.
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 28 Apr 2013 12:53:08 +0000 (15:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 28 Apr 2013 12:53:08 +0000 (15:53 +0300)
Original patch by Kushal Das.

Lib/test/test_base64.py

index 25694762a98c1c7bc36f8951b2c515ede6987bdd..7ba1fe717025e9b0f10320794b56d55fff65d129 100644 (file)
@@ -22,6 +22,8 @@ class LegacyBase64TestCase(unittest.TestCase):
            b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
            b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
            b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
+        # Non-bytes
+        eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n')
         self.assertRaises(TypeError, base64.encodebytes, "")
 
     def test_decodebytes(self):
@@ -37,6 +39,8 @@ class LegacyBase64TestCase(unittest.TestCase):
            b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            b"0123456789!@#0^&*();:<>,. []{}")
         eq(base64.decodebytes(b''), b'')
+        # Non-bytes
+        eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc')
         self.assertRaises(TypeError, base64.decodebytes, "")
 
     def test_encode(self):
@@ -78,6 +82,10 @@ class BaseXYTestCase(unittest.TestCase):
            b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
         # Test with arbitrary alternative characters
         eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd')
+        # Non-bytes
+        eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==')
+        eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
+           b'01a*b$cd')
         # Check if passing a str object raises an error
         self.assertRaises(TypeError, base64.b64encode, "")
         self.assertRaises(TypeError, base64.b64encode, b"", altchars="")
@@ -93,11 +101,14 @@ class BaseXYTestCase(unittest.TestCase):
            b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
            b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
            b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
+        # Non-bytes
+        eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
         # Check if passing a str object raises an error
         self.assertRaises(TypeError, base64.standard_b64encode, "")
-        self.assertRaises(TypeError, base64.standard_b64encode, b"", altchars="")
         # Test with 'URL safe' alternative characters
         eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd')
+        # Non-bytes
+        eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_cd')
         # Check if passing a str object raises an error
         self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
 
@@ -121,6 +132,8 @@ class BaseXYTestCase(unittest.TestCase):
         for data, res in tests.items():
             eq(base64.b64decode(data), res)
             eq(base64.b64decode(data.decode('ascii')), res)
+        # Non-bytes
+        eq(base64.b64decode(bytearray(b"YWJj")), b"abc")
 
         # Test with arbitrary alternative characters
         tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d',
@@ -138,6 +151,8 @@ class BaseXYTestCase(unittest.TestCase):
         for data, res in tests.items():
             eq(base64.standard_b64decode(data), res)
             eq(base64.standard_b64decode(data.decode('ascii')), res)
+        # Non-bytes
+        eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc")
 
         # Test with 'URL safe' alternative characters
         tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
@@ -146,6 +161,8 @@ class BaseXYTestCase(unittest.TestCase):
         for data, res in tests_urlsafe.items():
             eq(base64.urlsafe_b64decode(data), res)
             eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
+        # Non-bytes
+        eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1d')
 
     def test_b64decode_padding_error(self):
         self.assertRaises(binascii.Error, base64.b64decode, b'abc')
@@ -179,6 +196,8 @@ class BaseXYTestCase(unittest.TestCase):
         eq(base64.b32encode(b'abc'), b'MFRGG===')
         eq(base64.b32encode(b'abcd'), b'MFRGGZA=')
         eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
+        # Non-bytes
+        eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=')
         self.assertRaises(TypeError, base64.b32encode, "")
 
     def test_b32decode(self):
@@ -194,6 +213,8 @@ class BaseXYTestCase(unittest.TestCase):
         for data, res in tests.items():
             eq(base64.b32decode(data), res)
             eq(base64.b32decode(data.decode('ascii')), res)
+        # Non-bytes
+        eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc')
 
     def test_b32decode_casefold(self):
         eq = self.assertEqual
@@ -245,6 +266,8 @@ class BaseXYTestCase(unittest.TestCase):
         eq = self.assertEqual
         eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
         eq(base64.b16encode(b'\x00'), b'00')
+        # Non-bytes
+        eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
         self.assertRaises(TypeError, base64.b16encode, "")
 
     def test_b16decode(self):
@@ -259,6 +282,8 @@ class BaseXYTestCase(unittest.TestCase):
         # Case fold
         eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef')
         eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
+        # Non-bytes
+        eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
 
     def test_decode_nonascii_str(self):
         decode_funcs = (base64.b64decode,