|
1 | 1 | import _compression |
2 | 2 | import array |
| 3 | +import binascii |
3 | 4 | from io import BytesIO, UnsupportedOperation, DEFAULT_BUFFER_SIZE |
4 | 5 | import os |
5 | 6 | import pickle |
|
8 | 9 | from test import support |
9 | 10 | import unittest |
10 | 11 |
|
11 | | -from test.support import _4G, bigmemtest |
| 12 | +from test.support import _1G, _4G, bigmemtest |
12 | 13 | from test.support.import_helper import import_module |
13 | 14 | from test.support.os_helper import ( |
14 | 15 | TESTFN, unlink, FakePath |
|
17 | 18 | lzma = import_module("lzma") |
18 | 19 | from lzma import LZMACompressor, LZMADecompressor, LZMAError, LZMAFile |
19 | 20 |
|
| 21 | +class ChecksumTestCase(unittest.TestCase): |
| 22 | + # checksum test cases |
| 23 | + def test_crc32start(self): |
| 24 | + self.assertEqual(lzma.crc32(b""), lzma.crc32(b"", 0)) |
| 25 | + self.assertTrue(lzma.crc32(b"abc", 0xffffffff)) |
| 26 | + |
| 27 | + def test_crc32empty(self): |
| 28 | + self.assertEqual(lzma.crc32(b"", 0), 0) |
| 29 | + self.assertEqual(lzma.crc32(b"", 1), 1) |
| 30 | + self.assertEqual(lzma.crc32(b"", 432), 432) |
| 31 | + |
| 32 | + def test_penguins(self): |
| 33 | + self.assertEqual(lzma.crc32(b"penguin", 0), 0x0e5c1a120) |
| 34 | + self.assertEqual(lzma.crc32(b"penguin", 1), 0x43b6aa94) |
| 35 | + self.assertEqual(lzma.crc32(b"penguin"), lzma.crc32(b"penguin", 0)) |
| 36 | + |
| 37 | + def test_crc32_unsigned(self): |
| 38 | + foo = b'abcdefghijklmnop' |
| 39 | + # explicitly test signed behavior |
| 40 | + self.assertEqual(lzma.crc32(foo), 2486878355) |
| 41 | + self.assertEqual(lzma.crc32(b'spam'), 1138425661) |
| 42 | + |
| 43 | + def test_same_as_binascii_crc32(self): |
| 44 | + foo = b'abcdefghijklmnop' |
| 45 | + crc = 2486878355 |
| 46 | + self.assertEqual(binascii.crc32(foo), crc) |
| 47 | + self.assertEqual(lzma.crc32(foo), crc) |
| 48 | + self.assertEqual(binascii.crc32(b'spam'), lzma.crc32(b'spam')) |
| 49 | + |
| 50 | + |
| 51 | +# GH-54485 - check that inputs >=4 GiB are handled correctly. |
| 52 | +class ChecksumBigBufferTestCase(unittest.TestCase): |
| 53 | + |
| 54 | + @bigmemtest(size=_4G + 4, memuse=1, dry_run=False) |
| 55 | + def test_big_buffer(self, size): |
| 56 | + data = b"nyan" * (_1G + 1) |
| 57 | + self.assertEqual(lzma.crc32(data), 1044521549) |
| 58 | + |
20 | 59 |
|
21 | 60 | class CompressorDecompressorTestCase(unittest.TestCase): |
22 | 61 |
|
|
0 commit comments