Skip to content

Commit 1595909

Browse files
erichermanace-dvmkms15
committed
test hashing files larger than blocksize
Co-authored-by: Ace Medlock <ace.medlock@gmail.com> Co-authored-by: Kendrick Shaw <kms15@case.edu>
1 parent 600dbb2 commit 1595909

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

meshfssync/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import hashlib
44

5-
def sha256_for_path(fp, blocksize = (4 * 1024 * 1024)):
5+
def sha256_for_file(fp, blocksize = (4 * 1024 * 1024)):
66
hasher = hashlib.sha256()
77
buf = fp.read(blocksize)
8-
# while len(buf) > 0:
9-
assert len(buf) < blocksize
10-
hasher.update(buf)
11-
# buf = fp.read(blocksize)
8+
while len(buf) > 0:
9+
hasher.update(buf)
10+
buf = fp.read(blocksize)
1211
return hasher.hexdigest()
1312

1413
def get_dir_state(dirpath):
1514
ds = {}
1615
for path in dirpath.iterdir():
1716
with open(path, 'rb') as fp:
18-
ds[path.name] = { 'sha256': sha256_for_path(fp) }
17+
ds[path.name] = { 'sha256': sha256_for_file(fp) }
1918
return ds

test/test_init.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
import tempfile
33
from pathlib import Path
44

5-
6-
5+
def test_sha_for_file():
6+
# $ sha256sum COPYING
7+
# 8ceb4b9ee5adedde47b31e975c1d90c73ad27b6b165a1dcd80c7c545eb65b903 COPYING
8+
sha = '8ceb4b9ee5adedde47b31e975c1d90c73ad27b6b165a1dcd80c7c545eb65b903'
9+
blocksize_4k = (4 * 1024)
10+
with open('./COPYING', 'rb') as fp:
11+
class WrappedFp:
12+
def read(self, blocksize):
13+
assert blocksize <= blocksize_4k
14+
return fp.read(blocksize_4k);
15+
h = meshfssync.sha256_for_file(WrappedFp(), blocksize_4k)
16+
assert h == sha
717

818
def test_get_dir_state():
919
# $ echo 'foo' > foo.txt

0 commit comments

Comments
 (0)