Skip to content

Commit

Permalink
fixed bug in code promotion logic added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SmithSamuelM committed Mar 21, 2024
1 parent 9f5ddd0 commit 6f1ec09
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/keri/core/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def __init__(self, tag=None, *, code = None, count=None, countB64=None,
raise kering.InvalidVarIndexError(f"Invalid {ss=} "
f"for {code=}.")
# dynamically promote code based on count
if code[0] != '0' and count > (64 ** 2 - 1): # small code but large count
if code[1] != '0' and count > (64 ** 2 - 1): # small code but large count
# elevate code due to large count
code = f"-0{code[1]}" # promote hard
ss = 5
Expand Down
86 changes: 83 additions & 3 deletions tests/core/test_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ def test_counter_v2():
with pytest.raises(TypeError):
counter = Counter(qb2=ims, strip=True, version=Vrsn_2_0)

# test with big codes index=1024
# test with big codes count=1024
count = 1024
qsc = CtrDex.BigGenericGroup + intToB64(count, l=5)
assert qsc == '-0AAAAQA'
Expand Down Expand Up @@ -1106,7 +1106,7 @@ def test_counter_v2():
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

# test ims with big codes index=1024
# test ims with big codes count=1024
count = 1024
qsc = CtrDex.BigGenericGroup + intToB64(count, l=5)
assert qsc == '-0AAAAQA'
Expand All @@ -1131,8 +1131,88 @@ def test_counter_v2():
assert counter.qb2 == qscb2
assert not ims

# test with big codes count=8193
count = 8193
qsc = CtrDex.BigGenericGroup + intToB64(count, l=5)
assert qsc == '-0AAACAB'
qscb = qsc.encode("utf-8")
qscb2 = decodeB64(qscb)

counter = Counter(code=CtrDex.BigGenericGroup, count=count, version=Vrsn_2_0)
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

counter = Counter(qb64b=qscb, version=Vrsn_2_0) # test with bytes not str
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

counter = Counter(qb64=qsc, version=Vrsn_2_0) # test with str not bytes
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

counter = Counter(qb2=qscb2, version=Vrsn_2_0) # test with qb2
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

# test ims with big codes count=8193
count = 8193
qsc = CtrDex.BigGenericGroup + intToB64(count, l=5)
assert qsc == '-0AAACAB'
qscb = qsc.encode("utf-8")
qscb2 = decodeB64(qscb)

ims = bytearray(qscb)
counter = Counter(qb64b=ims, strip=True, version=Vrsn_2_0) # test with bytes not str
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2
assert not ims

ims = bytearray(qscb2)
counter = Counter(qb2=ims, strip=True, version=Vrsn_2_0) # test with qb2
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2
assert not ims

# test with promotion from small to big codes with count=8193
count = 8193
qsc = CtrDex.BigGenericGroup + intToB64(count, l=5)
assert qsc == '-0AAACAB'
qscb = qsc.encode("utf-8")
qscb2 = decodeB64(qscb)

counter = Counter(code=CtrDex.GenericGroup, count=count, version=Vrsn_2_0)
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

counter = Counter(tag=AllTags.GenericGroup, count=count, version=Vrsn_2_0)
assert counter.code == CtrDex.BigGenericGroup
assert counter.count == count
assert counter.qb64b == qscb
assert counter.qb64 == qsc
assert counter.qb2 == qscb2

# test protocol genus with CESR version
# test with big codes index=1024
genverint = 0
genver = intToB64(genverint, l=3)
assert genver == 'AAA'
Expand Down

0 comments on commit 6f1ec09

Please sign in to comment.