Skip to content

Commit 9e938dd

Browse files
committed
TEST: Constrain gzip outputs to have deterministic checksums by default
1 parent a0d2534 commit 9e938dd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

nibabel/tests/test_openers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from gzip import GzipFile
1313
from io import BytesIO, UnsupportedOperation
1414
from distutils.version import StrictVersion
15+
import hashlib
16+
import time
1517

1618
from numpy.compat.py3k import asstr, asbytes
1719
from ..openers import Opener, ImageOpener, HAVE_INDEXED_GZIP, BZ2File
@@ -347,3 +349,34 @@ def test_iter():
347349
lobj = Opener(Lunk(''))
348350
with pytest.raises(TypeError):
349351
list(lobj)
352+
353+
354+
def md5sum(fname):
355+
with open(fname, "rb") as fobj:
356+
return hashlib.md5(fobj.read()).hexdigest()
357+
358+
359+
def test_bitwise_determinism():
360+
with InTemporaryDirectory():
361+
msg = b"Hello, I'd like to have an argument."
362+
# Canonical reference: No filename, no mtime
363+
# Use default compresslevel
364+
with open("ref.gz", "wb") as fobj:
365+
with GzipFile(filename="", mode="wb",
366+
compresslevel=1, fileobj=fobj,
367+
mtime=0) as gzobj:
368+
gzobj.write(msg)
369+
anon_chksum = md5sum("ref.gz")
370+
371+
# Different times, different filenames
372+
now = time.time()
373+
with mock.patch("time.time") as t:
374+
t.return_value = now
375+
with Opener("a.gz", "wb") as fobj:
376+
fobj.write(msg)
377+
t.return_value = now + 1
378+
with Opener("b.gz", "wb") as fobj:
379+
fobj.write(msg)
380+
381+
assert md5sum("a.gz") == anon_chksum
382+
assert md5sum("b.gz") == anon_chksum

0 commit comments

Comments
 (0)