Skip to content

Commit c33650a

Browse files
committed
Deal with test_cpio.py's test_xz failure on python3
The following assertion boiled down to a difference in the defaul CRC algorithm between pylzma on python2 and the now-standard python3 lzma module... but brought many improvements along the way. tests/test_cpio.py:89: in archiveCreate self.assertEqual(f.read(2), b'\x00\x01') E AssertionError: b'\x00\x04' != b'\x00\x01'
2 parents 0062e71 + a3a826f commit c33650a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

tests/test_cpio.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import subprocess, shutil
1515

1616
def writeRandomFile(fn, size, start=b'', add=b'a'):
17+
"Create a pseudo-random reproducible file from seeds `start` amd `add`"
1718
with open(fn, 'wb') as f:
1819
m = md5()
1920
m.update(start)
@@ -43,7 +44,12 @@ def setUp(self):
4344
writeRandomFile('archive/data', 10491)
4445
with open('archive/data', 'rb') as fd:
4546
self.md5data = md5(fd.read()).hexdigest()
46-
check_call("find archive | cpio -o -H newc > archive.cpio")
47+
# fixed timestamps for cpio reproducibility
48+
os.utime('archive/data', (0, 0))
49+
os.utime('archive', (0, 0))
50+
51+
check_call(
52+
"find archive | cpio --reproducible -o -H newc > archive.cpio")
4753
check_call("gzip -c < archive.cpio > archive.cpio.gz")
4854
check_call("bzip2 -c < archive.cpio > archive.cpio.bz2")
4955
try:
@@ -55,7 +61,7 @@ def setUp(self):
5561
self.doXZ = False
5662

5763
def tearDown(self):
58-
check_call("rm -rf archive archive.cpio*")
64+
check_call("rm -rf archive archive.cpio* archive2.cpio*")
5965

6066
# TODO check with file (like 'r:*')
6167
# TODO use cat to check properly for pipes
@@ -71,7 +77,8 @@ def archiveExtract(self, fn, fmt='r|*'):
7177
self.assertTrue(found)
7278

7379
def archiveCreate(self, fn, fmt='w'):
74-
os.unlink(fn)
80+
if os.path.exists(fn):
81+
os.unlink(fn)
7582
arc = CpioFile.open(fn, fmt)
7683
f = arc.getcpioinfo('archive/data')
7784
arc.addfile(f, open('archive/data', 'rb'))
@@ -80,15 +87,23 @@ def archiveCreate(self, fn, fmt='w'):
8087
# special case for XZ, test check type (crc32)
8188
if fmt.endswith('xz'):
8289
with open(fn, 'rb') as f:
83-
f.seek(6)
84-
self.assertEqual(f.read(2), b'\x00\x01')
90+
# check xz magic
91+
self.assertEqual(f.read(6), b"\xfd7zXZ\0")
92+
# check stream flags
93+
if sys.version_info < (3, 0):
94+
expected_flags = b'\x00\x01' # pylzma defaults to CRC32
95+
else:
96+
expected_flags = b'\x00\x04' # python3 defaults to CRC64
97+
self.assertEqual(f.read(2), expected_flags)
8598
self.archiveExtract(fn)
8699

87100
def doArchive(self, fn, fmt=None):
88101
self.archiveExtract(fn)
89-
self.archiveCreate(fn, fmt is None and 'w' or 'w|%s' % fmt)
90-
if not fmt is None:
91-
self.archiveExtract(fn, 'r|%s' % fmt)
102+
fn2 = "archive2" + fn[len("archive"):]
103+
print("creating %s" % fn2)
104+
self.archiveCreate(fn2, fmt is None and 'w' or 'w|%s' % fmt)
105+
if fmt is not None:
106+
self.archiveExtract(fn2, 'r|%s' % fmt)
92107

93108
def test_plain(self):
94109
self.doArchive('archive.cpio')
@@ -101,7 +116,7 @@ def test_bz2(self):
101116

102117
def test_xz(self):
103118
if not self.doXZ:
104-
return
119+
raise unittest.SkipTest("lzma package or xz tool not available")
105120
print('Running test for XZ')
106121
self.doArchive('archive.cpio.xz', 'xz')
107122

xcp/cpiofile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def getcomptype(self):
478478
return "gz"
479479
if self.buf.startswith(b"BZh91"):
480480
return "bz2"
481-
if self.buf.startswith(b"\xfd7zXZ"):
481+
if self.buf.startswith(b"\xfd7zXZ\0"):
482482
return "xz"
483483
return "cpio"
484484

0 commit comments

Comments
 (0)