Skip to content

Commit

Permalink
Added unit tests and dummy ilk to test all the features of alls, opts…
Browse files Browse the repository at this point in the history
…, alts, strict and extras for configuring protocols message types and fields
SmithSamuelM committed Mar 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 86a4e3f commit c94c385
Showing 4 changed files with 94 additions and 47 deletions.
35 changes: 12 additions & 23 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
@@ -385,6 +385,12 @@ class Serder:
opts=dict(u='', ri='', a='', A='', e='', r=''),
alts=dict(a="A", A="a"),
saids={Saids.d: DigDex.Blake3_256}),
Ilks.ace: FieldDom(alls=dict(v='', t='', d='', u='', i='',
ri='', s='', a='', A='', e='', r=''),
opts=dict(u='', ri='', a='', A='', e='', r=''),
alts=dict(a="A", A="a"),
saids={Saids.d: DigDex.Blake3_256},
strict=False),
},
Vrsn_2_0:
{
@@ -398,6 +404,12 @@ class Serder:
opts=dict(u='', rd='', a='', A='', e='', r=''),
alts=dict(a="A", A="a"),
saids={Saids.d: DigDex.Blake3_256}),
Ilks.ace: FieldDom(alls=dict(v='', t='', d='', u='', i='',
rd='', s='', a='', A='', e='', r=''),
opts=dict(u='', rd='', a='', A='', e='', r=''),
alts=dict(a="A", A="a"),
saids={Saids.d: DigDex.Blake3_256},
strict=False),
Ilks.sch: FieldDom(alls=dict(v='', t='', d='', s=''),
saids={Saids.d: DigDex.Blake3_256}),
Ilks.att: FieldDom(alls=dict(v='', t='', d='', a=''),
@@ -611,8 +623,6 @@ def _verify(self):
raise MissingFieldError(f"Missing or out-of-order field = {label} "
f"from = {list(osalls)} in sad.")



# said field labels are not order dependent with respect to all fields
# in sad so use set() to test inclusion
saids = copy.copy(fields.saids) # get copy of saidive field labels and defaults values
@@ -764,16 +774,7 @@ def makify(self, sad, *, version=None,
if 't' in sad: # when packet type field then force ilk
sad['t'] = ilk # assign ilk

# ensure all required fields are in sad. If not provide default
#for label in oreqs: # ensure provided sad has all required fields
#if label not in sad: # supply default
#value = alls[label]
#if helping.nonStringIterable(value): # copy iterable defaults
#value = copy.copy(value)
#sad[label] = value

# ensure required fields are present and all fields are ordered wrt alls

oskeys = oset(sad) # ordered set of keys in sad (skeys)
osexts = oskeys - oalls # get ordered set of extras in sad (sexts)
if osexts and fields.strict:
@@ -797,18 +798,6 @@ def makify(self, sad, *, version=None,
raise SerializeError(f"Missing or out-of-order field = {label} "
f"from = {list(osalls)} in sad.")




#keys = list(sad) # get list of keys of self.sad
#for key in list(keys): # make copy to mutate
#if key not in alls:
#del keys[keys.index(key)] # remove non required fields

#if list(alls) != keys: # ensure ordering of fields matches alls
#raise SerializeError(f"Mismatch one or more of all required fields "
#f" = {list(alls)} in sad = {sad}.")

# said field labels are not order dependent with respect to all fields
# in sad so use set() to test inclusion
_saids = copy.copy(fields.saids) # get copy of defaults
6 changes: 3 additions & 3 deletions src/keri/kering.py
Original file line number Diff line number Diff line change
@@ -323,14 +323,14 @@ def sniff(ims):
# KERI/ACDC protocol packet (message) types
Ilkage = namedtuple("Ilkage", ('icp rot ixn dip drt rct qry rpy xip exn '
'pro bar vcp vrt iss rev bis brv rip upd '
'acd sch att agg edg rul '))
'acd ace sch att agg edg rul '))

Ilks = Ilkage(icp='icp', rot='rot', ixn='ixn', dip='dip', drt='drt',
rct='rct',
qry='qry', rpy='rpy', xip='xip', exn='exn', pro='pro', bar='bar',
vcp='vcp', vrt='vrt', iss='iss', rev='rev', bis='bis', brv='brv',
rip='rip', upd='upd', acd='acd', sch='sch', att='att', agg='agg',
edg='edg', rul='rul')
rip='rip', upd='upd', acd='acd', ace='ace',
sch='sch', att='att', agg='agg', edg='edg', rul='rul')

# Ilks needs to be versioned for Protocol versions or else use Serder.Fields

94 changes: 75 additions & 19 deletions tests/core/test_serdering.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
import msgpack

import pytest
from ordered_set import OrderedSet as oset

from keri import kering
from keri.kering import Versionage, Version
@@ -51,24 +52,16 @@ def test_serder():

assert Serder.Fields

# Ensure all Serder.Fields all and opts and alts are correct subsets
# Ensure all Serder.Fields all and opts and alts and saids are correct subsets
# iterate through all FieldDoms
#fields = self.Fields[proto][vrsn][ilk] # get FieldDom of fields
#if not (set(fields.opts) <= set(fields.alls)):
#raise SerializeError(f"Opts = {fields.opts} not subset of alls = "
#f" {fields.alls}.")

#if not (set(fields.alts) <= set(fields.opts)):
#raise SerializeError(f"Alts = {fields.alts} not subset of opts = "
#f" {fields.opts}.")
for kp, kv in Serder.Fields.items(): # iterate through protocols
for kv, vv in kv.items(): # iterate through versions for each protocol
for kf, vf in vv.items(): # iterate through fields for each version
assert oset(vf.opts) <= oset(vf.alls)
assert oset(vf.alts) <= oset(vf.opts)
assert oset(vf.saids) <= oset(vf.alls)

#if not (set(fields.opts) <= set(fields.alls)):
#raise SerializeError(f"Opts = {fields.opts} not subset of alls = "
#f" {fields.alls}.")

#if not (set(fields.alts) <= set(fields.opts)):
#raise SerializeError(f"Alts = {fields.alts} not subset of opts = "
#f" {fields.opts}.")

assert Serder.Fields[kering.Protos.acdc][kering.Vrsn_1_0][None].saids == {'d': 'E'}
assert (Serder.Fields[kering.Protos.acdc][kering.Vrsn_1_0][None].alls ==
@@ -576,7 +569,6 @@ def test_serder():
assert serder.ilk == ilk

# test opts
#Test Serder bare makify bootstrap for ACDC JSON
serder = Serder(makify=True, proto=kering.Protos.acdc) # make defaults for ACDC
assert serder.sad == {'v': 'ACDC10JSON00005a_',
'd': 'EMk7BvrqO_2sYjpI_-BmSELOFNie-muw4XTi3iYCz6pT',
@@ -586,19 +578,83 @@ def test_serder():
b'BmSELOFNie-muw4XTi3iYCz6pT","i":"","s":""}')
assert serder.verify()
sad = serder.sad
raw = serder.raw
said = serder.said
size = serder.size

sad['a'] = ""
sad['e'] = ""
sad['r'] = ""

serder = Serder(makify=True, sad=sad) # make using sad
assert serder.raw == (b'{"v":"ACDC10JSON00006f_","d":"EBE7-v1veGz54DF2PIYmUoSG2BCLsEcIQSDSIYFsn9uw",'
b'"i":"","s":"","a":"","e":"","r":""}')
assert serder.verify()

# out of order field
sad = serder.sad
sad["ri"] = ""

with pytest.raises(kering.SerializeError):
serder = Serder(makify=True, sad=sad) # make using sad


# extra field with strict
sad = serder.sad
assert 'ri' not in sad
sad["x"] = ""

with pytest.raises(kering.SerializeError):
serder = Serder(makify=True, sad=sad) # make using sad

# test alts
serder = Serder(makify=True, proto=kering.Protos.acdc) # make defaults for ACDC
assert serder.sad == {'v': 'ACDC10JSON00005a_',
'd': 'EMk7BvrqO_2sYjpI_-BmSELOFNie-muw4XTi3iYCz6pT',
'i': '',
's': ''}
assert serder.verify()
sad = serder.sad

sad['a'] = ""
sad['A'] = "" # both alts
sad['e'] = ""
sad['r'] = ""

with pytest.raises(kering.SerializeError):
serder = Serder(makify=True, sad=sad) # make using sad

# test not strict
# test opts
serder = Serder(makify=True, proto=kering.Protos.acdc, ilk=kering.Ilks.ace) # make defaults for ACDC
assert serder.sad == {'v': 'ACDC10JSON000064_',
't': 'ace',
'd': 'EKFsN95K2h5I6pJC6eTrNKiX8uHyn5o-SYHy6IelbPK8',
'i': '',
's': ''}
assert serder.verify()

sad = serder.sad
sad["x"] = ""
serder = Serder(makify=True, sad=sad) # make using sad
assert serder.sad == {'v': 'ACDC10JSON00006b_',
't': 'ace',
'd': 'ECmOYyE7X5TVvBiM7PtApT-w9wsj7ZYI0jQt1TTcTa-1',
'i': '',
's': '',
'x': ''}
assert serder.verify()

# out of order with extra
sad = serder.sad
sad["ri"] = ""
with pytest.raises(kering.SerializeError):
serder = Serder(makify=True, sad=sad) # make using sad

# ToDo: create malicious raw values to test verify more thoroughly
# ToDo: create bad sad values to test makify more thoroughly
# unhappy paths




"""End Test"""

def test_serderkeri():
6 changes: 4 additions & 2 deletions tests/test_kering.py
Original file line number Diff line number Diff line change
@@ -493,8 +493,8 @@ def test_ilks():
rct='rct',
qry='qry', rpy='rpy', xip='xip', exn='exn', pro='pro', bar='bar',
vcp='vcp', vrt='vrt', iss='iss', rev='rev', bis='bis', brv='brv',
rip='rip', upd='upd', acd='acd', sch='sch', att='att', agg='agg',
edg='edg', rul='rul')
rip='rip', upd='upd', acd='acd', ace='ace',
sch='sch', att='att', agg='agg', edg='edg', rul='rul')

assert isinstance(Ilks, Ilkage)

@@ -547,6 +547,8 @@ def test_ilks():

assert 'acd' in Ilks
assert Ilks.acd == 'acd'
assert 'ace' in Ilks
assert Ilks.ace == 'ace'
assert 'sch' in Ilks
assert Ilks.sch == 'sch'
assert 'att' in Ilks

0 comments on commit c94c385

Please sign in to comment.