Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(76506)

Delta Between Two Patch Sets: Lib/test/test_bz2.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_builtin.py ('k') | Lib/test/test_capi.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python3
2 from test import support 1 from test import support
3 from test.support import bigmemtest, _4G 2 from test.support import bigmemtest, _4G
4 3
5 import unittest 4 import unittest
6 from io import BytesIO 5 from io import BytesIO
7 import os 6 import os
8 import pickle 7 import pickle
9 import random 8 import random
10 import subprocess 9 import subprocess
11 import sys 10 import sys
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 b'nobody:x:65534:65534:Nobody:/home:\n', 42 b'nobody:x:65534:65534:Nobody:/home:\n',
44 b'postfix:x:100:101:postfix:/var/spool/postfix:\n', 43 b'postfix:x:100:101:postfix:/var/spool/postfix:\n',
45 b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n', 44 b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n',
46 b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n', 45 b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n',
47 b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n', 46 b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n',
48 b'www:x:103:104::/var/www:/bin/false\n', 47 b'www:x:103:104::/var/www:/bin/false\n',
49 ] 48 ]
50 TEXT = b''.join(TEXT_LINES) 49 TEXT = b''.join(TEXT_LINES)
51 DATA = b'BZh91AY&SY.\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\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x1 6\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
52 EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00' 51 EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00'
52 BAD_DATA = b'this is not a valid bzip2 file'
53 53
54 def setUp(self): 54 def setUp(self):
55 self.filename = support.TESTFN 55 self.filename = support.TESTFN
56 56
57 def tearDown(self): 57 def tearDown(self):
58 if os.path.isfile(self.filename): 58 if os.path.isfile(self.filename):
59 os.unlink(self.filename) 59 os.unlink(self.filename)
60 60
61 if sys.platform == "win32": 61 if sys.platform == "win32":
62 # bunzip2 isn't available to run on Windows. 62 # bunzip2 isn't available to run on Windows.
(...skipping 10 matching lines...) Expand all
73 ret = pop.stdout.read() 73 ret = pop.stdout.read()
74 pop.stdout.close() 74 pop.stdout.close()
75 if pop.wait() != 0: 75 if pop.wait() != 0:
76 ret = bz2.decompress(data) 76 ret = bz2.decompress(data)
77 return ret 77 return ret
78 78
79 79
80 class BZ2FileTest(BaseTest): 80 class BZ2FileTest(BaseTest):
81 "Test the BZ2File class." 81 "Test the BZ2File class."
82 82
83 def createTempFile(self, streams=1): 83 def createTempFile(self, streams=1, suffix=b""):
84 with open(self.filename, "wb") as f: 84 with open(self.filename, "wb") as f:
85 f.write(self.DATA * streams) 85 f.write(self.DATA * streams)
86 f.write(suffix)
86 87
87 def testBadArgs(self): 88 def testBadArgs(self):
88 self.assertRaises(TypeError, BZ2File, 123.456) 89 self.assertRaises(TypeError, BZ2File, 123.456)
89 self.assertRaises(ValueError, BZ2File, "/dev/null", "z") 90 self.assertRaises(ValueError, BZ2File, "/dev/null", "z")
90 self.assertRaises(ValueError, BZ2File, "/dev/null", "rx") 91 self.assertRaises(ValueError, BZ2File, "/dev/null", "rx")
91 self.assertRaises(ValueError, BZ2File, "/dev/null", "rbt") 92 self.assertRaises(ValueError, BZ2File, "/dev/null", "rbt")
92 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=0) 93 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=0)
93 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=10) 94 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=10)
94 95
95 def testRead(self): 96 def testRead(self):
96 self.createTempFile() 97 self.createTempFile()
97 with BZ2File(self.filename) as bz2f: 98 with BZ2File(self.filename) as bz2f:
98 self.assertRaises(TypeError, bz2f.read, None) 99 self.assertRaises(TypeError, bz2f.read, None)
99 self.assertEqual(bz2f.read(), self.TEXT) 100 self.assertEqual(bz2f.read(), self.TEXT)
100 101
102 def testReadBadFile(self):
103 self.createTempFile(streams=0, suffix=self.BAD_DATA)
104 with BZ2File(self.filename) as bz2f:
105 self.assertRaises(OSError, bz2f.read)
106
101 def testReadMultiStream(self): 107 def testReadMultiStream(self):
102 self.createTempFile(streams=5) 108 self.createTempFile(streams=5)
103 with BZ2File(self.filename) as bz2f: 109 with BZ2File(self.filename) as bz2f:
104 self.assertRaises(TypeError, bz2f.read, None) 110 self.assertRaises(TypeError, bz2f.read, None)
105 self.assertEqual(bz2f.read(), self.TEXT * 5) 111 self.assertEqual(bz2f.read(), self.TEXT * 5)
106 112
107 def testReadMonkeyMultiStream(self): 113 def testReadMonkeyMultiStream(self):
108 # Test BZ2File.read() on a multi-stream archive where a stream 114 # Test BZ2File.read() on a multi-stream archive where a stream
109 # boundary coincides with the end of the raw read buffer. 115 # boundary coincides with the end of the raw read buffer.
110 buffer_size = bz2._BUFFER_SIZE 116 buffer_size = bz2._BUFFER_SIZE
111 bz2._BUFFER_SIZE = len(self.DATA) 117 bz2._BUFFER_SIZE = len(self.DATA)
112 try: 118 try:
113 self.createTempFile(streams=5) 119 self.createTempFile(streams=5)
114 with BZ2File(self.filename) as bz2f: 120 with BZ2File(self.filename) as bz2f:
115 self.assertRaises(TypeError, bz2f.read, None) 121 self.assertRaises(TypeError, bz2f.read, None)
116 self.assertEqual(bz2f.read(), self.TEXT * 5) 122 self.assertEqual(bz2f.read(), self.TEXT * 5)
117 finally: 123 finally:
118 bz2._BUFFER_SIZE = buffer_size 124 bz2._BUFFER_SIZE = buffer_size
125
126 def testReadTrailingJunk(self):
127 self.createTempFile(suffix=self.BAD_DATA)
128 with BZ2File(self.filename) as bz2f:
129 self.assertEqual(bz2f.read(), self.TEXT)
130
131 def testReadMultiStreamTrailingJunk(self):
132 self.createTempFile(streams=5, suffix=self.BAD_DATA)
133 with BZ2File(self.filename) as bz2f:
134 self.assertEqual(bz2f.read(), self.TEXT * 5)
119 135
120 def testRead0(self): 136 def testRead0(self):
121 self.createTempFile() 137 self.createTempFile()
122 with BZ2File(self.filename) as bz2f: 138 with BZ2File(self.filename) as bz2f:
123 self.assertRaises(TypeError, bz2f.read, None) 139 self.assertRaises(TypeError, bz2f.read, None)
124 self.assertEqual(bz2f.read(0), b"") 140 self.assertEqual(bz2f.read(0), b"")
125 141
126 def testReadChunk10(self): 142 def testReadChunk10(self):
127 self.createTempFile() 143 self.createTempFile()
128 with BZ2File(self.filename) as bz2f: 144 with BZ2File(self.filename) as bz2f:
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 text = bz2d.decompress(self.DATA+unused_data) 678 text = bz2d.decompress(self.DATA+unused_data)
663 self.assertEqual(text, self.TEXT) 679 self.assertEqual(text, self.TEXT)
664 self.assertEqual(bz2d.unused_data, unused_data) 680 self.assertEqual(bz2d.unused_data, unused_data)
665 681
666 def testEOFError(self): 682 def testEOFError(self):
667 bz2d = BZ2Decompressor() 683 bz2d = BZ2Decompressor()
668 text = bz2d.decompress(self.DATA) 684 text = bz2d.decompress(self.DATA)
669 self.assertRaises(EOFError, bz2d.decompress, b"anything") 685 self.assertRaises(EOFError, bz2d.decompress, b"anything")
670 self.assertRaises(EOFError, bz2d.decompress, b"") 686 self.assertRaises(EOFError, bz2d.decompress, b"")
671 687
672 @bigmemtest(size=_4G + 100, memuse=3) 688 @bigmemtest(size=_4G + 100, memuse=3.3)
673 def testDecompress4G(self, size): 689 def testDecompress4G(self, size):
674 # "Test BZ2Decompressor.decompress() with >4GiB input" 690 # "Test BZ2Decompressor.decompress() with >4GiB input"
675 blocksize = 10 * 1024 * 1024 691 blocksize = 10 * 1024 * 1024
676 block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little') 692 block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little')
677 try: 693 try:
678 data = block * (size // blocksize + 1) 694 data = block * (size // blocksize + 1)
679 compressed = bz2.compress(data) 695 compressed = bz2.compress(data)
680 bz2d = BZ2Decompressor() 696 bz2d = BZ2Decompressor()
681 decompressed = bz2d.decompress(compressed) 697 decompressed = bz2d.decompress(compressed)
682 self.assertEqual(decompressed, data) 698 self.assertEqual(decompressed, data)
(...skipping 24 matching lines...) Expand all
707 text = bz2.decompress(b"") 723 text = bz2.decompress(b"")
708 self.assertEqual(text, b"") 724 self.assertEqual(text, b"")
709 725
710 def testDecompressToEmptyString(self): 726 def testDecompressToEmptyString(self):
711 text = bz2.decompress(self.EMPTY_DATA) 727 text = bz2.decompress(self.EMPTY_DATA)
712 self.assertEqual(text, b'') 728 self.assertEqual(text, b'')
713 729
714 def testDecompressIncomplete(self): 730 def testDecompressIncomplete(self):
715 self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10]) 731 self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10])
716 732
733 def testDecompressBadData(self):
734 self.assertRaises(OSError, bz2.decompress, self.BAD_DATA)
735
717 def testDecompressMultiStream(self): 736 def testDecompressMultiStream(self):
718 text = bz2.decompress(self.DATA * 5) 737 text = bz2.decompress(self.DATA * 5)
738 self.assertEqual(text, self.TEXT * 5)
739
740 def testDecompressTrailingJunk(self):
741 text = bz2.decompress(self.DATA + self.BAD_DATA)
742 self.assertEqual(text, self.TEXT)
743
744 def testDecompressMultiStreamTrailingJunk(self):
745 text = bz2.decompress(self.DATA * 5 + self.BAD_DATA)
719 self.assertEqual(text, self.TEXT * 5) 746 self.assertEqual(text, self.TEXT * 5)
720 747
721 748
722 class OpenTest(BaseTest): 749 class OpenTest(BaseTest):
723 "Test the open function." 750 "Test the open function."
724 751
725 def open(self, *args, **kwargs): 752 def open(self, *args, **kwargs):
726 return bz2.open(*args, **kwargs) 753 return bz2.open(*args, **kwargs)
727 754
728 def test_binary_modes(self): 755 def test_binary_modes(self):
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 BZ2FileTest, 871 BZ2FileTest,
845 BZ2CompressorTest, 872 BZ2CompressorTest,
846 BZ2DecompressorTest, 873 BZ2DecompressorTest,
847 CompressDecompressTest, 874 CompressDecompressTest,
848 OpenTest, 875 OpenTest,
849 ) 876 )
850 support.reap_children() 877 support.reap_children()
851 878
852 if __name__ == '__main__': 879 if __name__ == '__main__':
853 test_main() 880 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+