Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cernopendata_client/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def get_file_checksum(afile):
:return: Adler32 checksum of file
:rtype: str
"""
return "adler32:" + hex(zlib.adler32(open(afile, "rb").read(), 1) & 0xFFFFFFFF)[2:]
return "adler32:{:08x}".format(
zlib.adler32(open(afile, "rb").read(), 1) & 0xFFFFFFFF
)


def get_file_info_local(recid):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

import os
import pytest
import requests
import subprocess
import tempfile

from click.testing import CliRunner
from cernopendata_client.cli import download_files, verify_files
Expand All @@ -35,6 +37,27 @@ def test_get_file_checksum():
assert get_file_checksum(afile) == "adler32:fa91da1e"


def test_get_file_checksum_zero_padding():
"""Test get_file_checksum() zero-pads checksums to 8 hex characters.

Uses a real open data file from recid 93773 whose checksum has a leading
zero.
"""
file_url = "http://opendata.cern.ch/eos/opendata/delphi/simulated-data/cern/hzha03pyth6156/va0u/206.5/hzha03pyth6156_hattbb_206.5_70_90_22432.xsdst"
file_checksum = "adler32:03d9681c"

with tempfile.NamedTemporaryFile(delete=False) as tmp:
tmp_path = tmp.name
response = requests.get(file_url)
tmp.write(response.content)

try:
result = get_file_checksum(tmp_path)
assert result == file_checksum
finally:
os.remove(tmp_path)


def test_get_file_info_local_wrong_input():
"""Test get_file_info_local() for wrong inputs."""

Expand Down
Loading