Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_binascii.BytearrayBinASCIITest Class Reference
Inheritance diagram for test.test_binascii.BytearrayBinASCIITest:
Inheritance graph
[legend]
Collaboration diagram for test.test_binascii.BytearrayBinASCIITest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def test_exceptions
def test_functions
def test_returned_value
def test_base64valid
def test_base64invalid
def test_uu
def test_crc32
def test_hqx
def test_hex
def test_qp
def test_empty_string
def test_unicode_strings

Public Attributes

 data

Static Public Attributes

 type2test = bytearray
string rawdata = "The quick brown fox jumps over the lazy dog.\r\n"

Detailed Description

Definition at line 227 of file test_binascii.py.


Member Function Documentation

def test.test_binascii.BinASCIITest.setUp (   self) [inherited]

Definition at line 25 of file test_binascii.py.

00025 
00026     def setUp(self):
00027         self.data = self.type2test(self.rawdata)

Here is the caller graph for this function:

Definition at line 77 of file test_binascii.py.

00077 
00078     def test_base64invalid(self):
00079         # Test base64 with random invalid characters sprinkled throughout
00080         # (This requires a new version of binascii.)
00081         MAX_BASE64 = 57
00082         lines = []
00083         for i in range(0, len(self.data), MAX_BASE64):
00084             b = self.type2test(self.rawdata[i:i+MAX_BASE64])
00085             a = binascii.b2a_base64(b)
00086             lines.append(a)
00087 
00088         fillers = bytearray()
00089         valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
00090         for i in range(256):
00091             if i not in valid:
00092                 fillers.append(i)
00093         def addnoise(line):
00094             noise = fillers
00095             ratio = len(line) // len(noise)
00096             res = bytearray()
00097             while line and noise:
00098                 if len(line) // len(noise) > ratio:
00099                     c, line = line[0], line[1:]
00100                 else:
00101                     c, noise = noise[0], noise[1:]
00102                 res.append(c)
00103             return res + noise + line
00104         res = bytearray()
00105         for line in map(addnoise, lines):
00106             a = self.type2test(line)
00107             b = binascii.a2b_base64(a)
00108             res += b
00109         self.assertEqual(res, self.rawdata)
00110 
00111         # Test base64 with just invalid characters, which should return
00112         # empty strings. TBD: shouldn't it raise an exception instead ?
00113         self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'')

Here is the call graph for this function:

Definition at line 62 of file test_binascii.py.

00062 
00063     def test_base64valid(self):
00064         # Test base64 with valid data
00065         MAX_BASE64 = 57
00066         lines = []
00067         for i in range(0, len(self.rawdata), MAX_BASE64):
00068             b = self.type2test(self.rawdata[i:i+MAX_BASE64])
00069             a = binascii.b2a_base64(b)
00070             lines.append(a)
00071         res = bytes()
00072         for line in lines:
00073             a = self.type2test(line)
00074             b = binascii.a2b_base64(a)
00075             res += b
00076         self.assertEqual(res, self.rawdata)

Here is the call graph for this function:

def test.test_binascii.BinASCIITest.test_crc32 (   self) [inherited]

Definition at line 139 of file test_binascii.py.

00139 
00140     def test_crc32(self):
00141         crc = binascii.crc32(self.type2test(b"Test the CRC-32 of"))
00142         crc = binascii.crc32(self.type2test(b" this string."), crc)
00143         self.assertEqual(crc, 1571220330)
00144 
00145         self.assertRaises(TypeError, binascii.crc32)

Here is the call graph for this function:

Definition at line 197 of file test_binascii.py.

00197 
00198     def test_empty_string(self):
00199         # A test for SF bug #1022953.  Make sure SystemError is not raised.
00200         empty = self.type2test(b'')
00201         for func in all_functions:
00202             if func == 'crc_hqx':
00203                 # crc_hqx needs 2 arguments
00204                 binascii.crc_hqx(empty, 0)
00205                 continue
00206             f = getattr(binascii, func)
00207             try:
00208                 f(empty)
00209             except Exception as err:
00210                 self.fail("{}({!r}) raises {!r}".format(func, empty, err))

Here is the call graph for this function:

Definition at line 28 of file test_binascii.py.

00028 
00029     def test_exceptions(self):
00030         # Check module exceptions
00031         self.assertTrue(issubclass(binascii.Error, Exception))
00032         self.assertTrue(issubclass(binascii.Incomplete, Exception))

Here is the call graph for this function:

Definition at line 33 of file test_binascii.py.

00033 
00034     def test_functions(self):
00035         # Check presence of all functions
00036         for name in all_functions:
00037             self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
00038             self.assertRaises(TypeError, getattr(binascii, name))

Here is the call graph for this function:

def test.test_binascii.BinASCIITest.test_hex (   self) [inherited]

Definition at line 156 of file test_binascii.py.

00156 
00157     def test_hex(self):
00158         # test hexlification
00159         s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
00160         t = binascii.b2a_hex(self.type2test(s))
00161         u = binascii.a2b_hex(self.type2test(t))
00162         self.assertEqual(s, u)
00163         self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
00164         self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
00165 
00166         self.assertEqual(binascii.hexlify(b'a'), b'61')

Here is the call graph for this function:

def test.test_binascii.BinASCIITest.test_hqx (   self) [inherited]

Definition at line 146 of file test_binascii.py.

00146 
00147     def test_hqx(self):
00148         # Perform binhex4 style RLE-compression
00149         # Then calculate the hexbin4 binary-to-ASCII translation
00150         rle = binascii.rlecode_hqx(self.data)
00151         a = binascii.b2a_hqx(self.type2test(rle))
00152         b, _ = binascii.a2b_hqx(self.type2test(a))
00153         res = binascii.rledecode_hqx(b)
00154 
00155         self.assertEqual(res, self.rawdata)

Here is the call graph for this function:

def test.test_binascii.BinASCIITest.test_qp (   self) [inherited]

Definition at line 167 of file test_binascii.py.

00167 
00168     def test_qp(self):
00169         # A test for SF bug 534347 (segfaults without the proper fix)
00170         try:
00171             binascii.a2b_qp(b"", **{1:1})
00172         except TypeError:
00173             pass
00174         else:
00175             self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
00176         self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
00177         self.assertEqual(binascii.a2b_qp(b"=="), b"=")
00178         self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
00179         self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
00180         self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
00181         self.assertEqual(
00182             binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
00183             b"=FF\r\n=FF\r\n=FF")
00184         self.assertEqual(
00185             binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
00186             b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")
00187 
00188         self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
00189         self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
00190         self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
00191         self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
00192                          b'foo=09bar=09\n')
00193 
00194         self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
00195         self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
00196         self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n')

Here is the call graph for this function:

Definition at line 39 of file test_binascii.py.

00039 
00040     def test_returned_value(self):
00041         # Limit to the minimum of all limits (b2a_uu)
00042         MAX_ALL = 45
00043         raw = self.rawdata[:MAX_ALL]
00044         for fa, fb in zip(a2b_functions, b2a_functions):
00045             a2b = getattr(binascii, fa)
00046             b2a = getattr(binascii, fb)
00047             try:
00048                 a = b2a(self.type2test(raw))
00049                 res = a2b(self.type2test(a))
00050             except Exception as err:
00051                 self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
00052             if fb == 'b2a_hqx':
00053                 # b2a_hqx returns a tuple
00054                 res, _ = res
00055             self.assertEqual(res, raw, "{}/{} conversion: "
00056                              "{!r} != {!r}".format(fb, fa, res, raw))
00057             self.assertIsInstance(res, bytes)
00058             self.assertIsInstance(a, bytes)
00059             self.assertLess(max(a), 128)
00060         self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
00061         self.assertIsInstance(binascii.crc32(raw), int)

Here is the call graph for this function:

Definition at line 211 of file test_binascii.py.

00211 
00212     def test_unicode_strings(self):
00213         # Unicode strings are not accepted.
00214         for func in all_functions:
00215             try:
00216                 self.assertRaises(TypeError, getattr(binascii, func), "test")
00217             except Exception as err:
00218                 self.fail('{}("test") raises {!r}'.format(func, err))
00219         # crc_hqx needs 2 arguments
00220         self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
00221 

Here is the call graph for this function:

def test.test_binascii.BinASCIITest.test_uu (   self) [inherited]

Definition at line 114 of file test_binascii.py.

00114 
00115     def test_uu(self):
00116         MAX_UU = 45
00117         lines = []
00118         for i in range(0, len(self.data), MAX_UU):
00119             b = self.type2test(self.rawdata[i:i+MAX_UU])
00120             a = binascii.b2a_uu(b)
00121             lines.append(a)
00122         res = bytes()
00123         for line in lines:
00124             a = self.type2test(line)
00125             b = binascii.a2b_uu(a)
00126             res += b
00127         self.assertEqual(res, self.rawdata)
00128 
00129         self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31)
00130         self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32)
00131         self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31)
00132         self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00")
00133         self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!")
00134 
00135         self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")
00136 
00137         # Issue #7701 (crash on a pydebug build)
00138         self.assertEqual(binascii.b2a_uu(b'x'), b'!>   \n')

Here is the call graph for this function:


Member Data Documentation

Definition at line 26 of file test_binascii.py.

string test.test_binascii.BinASCIITest.rawdata = "The quick brown fox jumps over the lazy dog.\r\n" [static, inherited]

Definition at line 20 of file test_binascii.py.

Reimplemented from test.test_binascii.BinASCIITest.

Definition at line 228 of file test_binascii.py.


The documentation for this class was generated from the following file: