Skip to content

Commit

Permalink
Add unit tests for digest generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aydarng committed Aug 20, 2024
1 parent 44cf0a4 commit e8c66e2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/verifier/core/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from keri.core import coring, Siger, MtrDex

from verifier.core.basing import ReportStats

from verifier.core.utils import DigerBuilder

# Report Statuses.
Reportage = namedtuple("Reportage", "accepted verified failed")
Expand Down Expand Up @@ -55,13 +55,6 @@ def loadEnds(app, hby, vdb, filer):
app.add_route("/reports/{aid}/{dig}", reportEnd)


def get_non_prefixed_digest(dig):
prefix, digest = dig.split("_", 1)
if not digest:
raise kering.ValidationError(f"Digest ({dig}) must start with prefix")
return digest


class Filer:
""" Report status filer
Expand Down Expand Up @@ -100,10 +93,8 @@ def create(self, aid, dig, filename, typ, stream):
)

idx = 0
non_pref_dig = get_non_prefixed_digest(dig) # Temporarily remove prefix
non_pref_dig = bytes.fromhex(non_pref_dig)
diger = coring.Diger(raw=non_pref_dig, code=MtrDex.SHA2_256)

diger = DigerBuilder.sha256(dig)
report = b''
while True:
chunk = stream.read(4096)
Expand Down Expand Up @@ -156,9 +147,7 @@ def create(self, aid, dig, filename, typ, stream):
f = open(fullpath, 'rb')
file_object = f.read()
f.close()
non_pref_dig = get_non_prefixed_digest(digest["dig"]) # Remove prefix
non_pref_dig = bytes.fromhex(non_pref_dig)
tmp_diger = coring.Diger(raw=non_pref_dig, code=MtrDex.SHA2_256)
tmp_diger = DigerBuilder.sha256(digest["dig"])
if not tmp_diger.verify(file_object):
raise kering.ValidationError(f"Invalid digest for file {fullpath}")
except KeyError as e:
Expand Down Expand Up @@ -187,9 +176,7 @@ def get(self, dig):
ReportStats: Report stats for report with digest dig or None
"""
non_pref_dig = get_non_prefixed_digest(dig) # Temporarily remove prefix
non_pref_dig = bytes.fromhex(non_pref_dig)
diger = coring.Diger(raw=non_pref_dig, code=MtrDex.SHA2_256)
diger = DigerBuilder.sha256(dig)
if (stats := self.vdb.stats.get(keys=(diger.qb64,))) is None:
return None

Expand Down
24 changes: 24 additions & 0 deletions src/verifier/core/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from keri import kering
from keri.core import MtrDex, coring


class DigerBuilder:
@staticmethod
def sha256(dig):
try:
non_pref_dig = DigerBuilder._get_non_prefixed_digest(dig) # Temporarily remove prefix
non_pref_dig = bytes.fromhex(non_pref_dig)
diger = coring.Diger(raw=non_pref_dig, code=MtrDex.SHA2_256)
return diger
except Exception as e:
raise e

@staticmethod
def _get_non_prefixed_digest(dig):
try:
prefix, digest = dig.split("_", 1)
except ValueError:
raise kering.ValidationError(f"Digest ({dig}) must start with prefix")
except Exception:
raise kering.ValidationError(f"Invalid digest {dig}")
return digest
32 changes: 32 additions & 0 deletions tests/core/test_reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from hashlib import sha256

from keri import kering

from src.verifier.core.utils import DigerBuilder


def test_diger_builder():
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode()
dig = sha256(BASE_STR).hexdigest()
dig = f"sha256_{dig}"
diger = DigerBuilder.sha256(dig)
assert diger.verify(BASE_STR) is True


def test_diger_builder_fail():
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode()
WRONG_BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KDT".encode()
dig = sha256(BASE_STR).hexdigest()
dig = f"sha256_{dig}"
diger = DigerBuilder.sha256(dig)
assert diger.verify(WRONG_BASE_STR) is False


def test_diger_builder_wrong_dig():
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode()
dig = sha256(BASE_STR).hexdigest()
# Here the dig is not prefixed
with pytest.raises(kering.ValidationError) as exc_info:
diger = DigerBuilder.sha256(dig)

0 comments on commit e8c66e2

Please sign in to comment.