Skip to content

Commit 344bbf1

Browse files
committed
added tests for versify deversify with bad version strings
1 parent e973f42 commit 344bbf1

File tree

2 files changed

+145
-14
lines changed

2 files changed

+145
-14
lines changed

src/keri/kering.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def rematch(match, *, version=None):
9090
raise ProtocolError(f"Invalid protocol type = {protocol}.")
9191
vrsn = Versionage(major=b64ToInt(major), minor=b64ToInt(minor))
9292
if vrsn.major < 2: # version2 vs but major < 2
93-
VersionError(f"Incompatible {vrsn=} with version string.")
93+
raise VersionError(f"Incompatible {vrsn=} with version string.")
9494
if version is not None: # compatible version with vrsn
9595
if (vrsn.major > version.major or
9696
(vrsn.major == version.major and vrsn.minor > version.minor)):
@@ -113,7 +113,7 @@ def rematch(match, *, version=None):
113113
raise ProtocolError(f"Invalid protocol type = {protocol}.")
114114
vrsn = Versionage(major=int(major, 16), minor=int(minor, 16))
115115
if vrsn.major > 1: # version1 vs but major > 1
116-
VersionError(f"Incompatible {vrsn=} with version string.")
116+
raise VersionError(f"Incompatible {vrsn=} with version string.")
117117
if version is not None and vrsn != version:
118118
raise VersionError(f"Expected {version=}, got "
119119
f"{vrsn=}.")
@@ -146,7 +146,7 @@ def versify(protocol=Protos.keri, version=Version, kind=Serials.json, size=0):
146146
return VERFMT.format(protocol, version.major, version.minor, kind, size, VERRAWSIZE)
147147
else: # version 2+ version string
148148
return (f"{protocol}{intToB64(version.major)}"
149-
f"{intToB64(version.minor, l=2)}{kind}{intToB64(size, l=4)})")
149+
f"{intToB64(version.minor, l=2)}{kind}{intToB64(size, l=4)}.")
150150

151151

152152

tests/test_kering.py

Lines changed: 142 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
versify, deversify, Rever)
2020
from keri.kering import (VER1FULLSPAN, VER1TERM, VEREX1,
2121
VER2FULLSPAN, VER2TERM, VEREX2, VEREX)
22+
from keri.kering import VersionError, ProtocolError, KindError
2223
from keri.help.helping import (intToB64, intToB64b, b64ToInt, B64_CHARS,
2324
codeB64ToB2, codeB2ToB64, Reb64, nabSextets)
2425

@@ -287,13 +288,18 @@ def test_serials():
287288
"""Done Test"""
288289

289290

290-
def test_versify():
291+
def test_versify_v1():
291292
"""
292293
Test Versify support
293294
"""
294-
vs = versify(kind=Serials.json, size=0)
295+
296+
assert VER1FULLSPAN == MAXVERFULLSPAN
297+
298+
# default version is version 1
299+
300+
vs = versify() # defaults
295301
assert vs == "KERI10JSON000000_"
296-
assert len(vs) == MAXVERFULLSPAN
302+
assert len(vs) == VER1FULLSPAN
297303
proto, version, kind, size = deversify(vs)
298304
assert proto == Protos.keri
299305
assert kind == Serials.json
@@ -302,7 +308,7 @@ def test_versify():
302308

303309
vs = versify(kind=Serials.json, size=65)
304310
assert vs == "KERI10JSON000041_"
305-
assert len(vs) == MAXVERFULLSPAN
311+
assert len(vs) == VER1FULLSPAN
306312
proto, version, kind, size = deversify(vs)
307313
assert proto == Protos.keri
308314
assert kind == Serials.json
@@ -311,7 +317,7 @@ def test_versify():
311317

312318
vs = versify(protocol=Protos.acdc, kind=Serials.json, size=86)
313319
assert vs == "ACDC10JSON000056_"
314-
assert len(vs) == MAXVERFULLSPAN
320+
assert len(vs) == VER1FULLSPAN
315321
proto, version, kind, size = deversify(vs)
316322
assert proto == Protos.acdc
317323
assert kind == Serials.json
@@ -320,7 +326,7 @@ def test_versify():
320326

321327
vs = versify(kind=Serials.mgpk, size=0)
322328
assert vs == "KERI10MGPK000000_"
323-
assert len(vs) == MAXVERFULLSPAN
329+
assert len(vs) == VER1FULLSPAN
324330
proto, version, kind, size = deversify(vs)
325331
assert proto == Protos.keri
326332
assert kind == Serials.mgpk
@@ -329,7 +335,7 @@ def test_versify():
329335

330336
vs = versify(kind=Serials.mgpk, size=65)
331337
assert vs == "KERI10MGPK000041_"
332-
assert len(vs) == MAXVERFULLSPAN
338+
assert len(vs) == VER1FULLSPAN
333339
proto, version, kind, size = deversify(vs)
334340
assert proto == Protos.keri
335341
assert kind == Serials.mgpk
@@ -338,7 +344,7 @@ def test_versify():
338344

339345
vs = versify(kind=Serials.cbor, size=0)
340346
assert vs == "KERI10CBOR000000_"
341-
assert len(vs) == MAXVERFULLSPAN
347+
assert len(vs) == VER1FULLSPAN
342348
proto, version, kind, size = deversify(vs)
343349
assert proto == Protos.keri
344350
assert kind == Serials.cbor
@@ -347,12 +353,137 @@ def test_versify():
347353

348354
vs = versify(kind=Serials.cbor, size=65)
349355
assert vs == "KERI10CBOR000041_"
350-
assert len(vs) == MAXVERFULLSPAN
356+
assert len(vs) == VER1FULLSPAN
351357
proto, version, kind, size = deversify(vs)
352358
assert proto == Protos.keri
353359
assert kind == Serials.cbor
354360
assert version == Version
355361
assert size == 65
362+
363+
vs = versify(version=Versionage(major=1, minor=1)) # defaults
364+
assert vs == "KERI11JSON000000_"
365+
assert len(vs) == VER1FULLSPAN
366+
proto, vrsn, kind, size = deversify(vs)
367+
assert proto == Protos.keri
368+
assert kind == Serials.json
369+
assert vrsn == (1, 1)
370+
assert size == 0
371+
372+
# test bad version strings
373+
vs = "KERI20JSON000000_"
374+
with pytest.raises(VersionError):
375+
smellage = deversify(vs)
376+
377+
vs = "ABLE10JSON000000_"
378+
with pytest.raises(ProtocolError):
379+
smellage = deversify(vs)
380+
381+
vs = "KERI10MSON000000_"
382+
with pytest.raises(KindError):
383+
smellage = deversify(vs)
384+
385+
386+
"""End Test"""
387+
388+
389+
def test_versify_v2():
390+
"""
391+
Test Versify support
392+
"""
393+
version = Versionage(major=2, minor=0)
394+
assert version == (2, 0)
395+
396+
vs = versify(version=version) # defaults
397+
assert vs == "KERICAAJSONAAAA."
398+
assert len(vs) == VER2FULLSPAN
399+
proto, vrsn, kind, size = deversify(vs)
400+
assert proto == Protos.keri
401+
assert kind == Serials.json
402+
assert vrsn == version
403+
assert size == 0
404+
405+
vs = versify(version=version, kind=Serials.json, size=65)
406+
assert vs == "KERICAAJSONAABB."
407+
assert len(vs) == VER2FULLSPAN
408+
proto, vrsn, kind, size = deversify(vs)
409+
assert proto == Protos.keri
410+
assert kind == Serials.json
411+
assert vrsn == version
412+
assert size == 65
413+
414+
vs = versify(protocol=Protos.acdc, version=version, kind=Serials.json, size=86)
415+
assert vs == "ACDCCAAJSONAABW."
416+
assert len(vs) == VER2FULLSPAN
417+
proto, vrsn, kind, size = deversify(vs)
418+
assert proto == Protos.acdc
419+
assert kind == Serials.json
420+
assert version == version
421+
assert size == 86
422+
423+
vs = versify(version=version, kind=Serials.mgpk, size=0)
424+
assert vs == 'KERICAAMGPKAAAA.'
425+
assert len(vs) == VER2FULLSPAN
426+
proto, vrsn, kind, size = deversify(vs)
427+
assert proto == Protos.keri
428+
assert kind == Serials.mgpk
429+
assert vrsn == version
430+
assert size == 0
431+
432+
vs = versify(version=version, kind=Serials.mgpk, size=65)
433+
assert vs == 'KERICAAMGPKAABB.'
434+
assert len(vs) == VER2FULLSPAN
435+
proto, vrsn, kind, size = deversify(vs)
436+
assert proto == Protos.keri
437+
assert kind == Serials.mgpk
438+
assert vrsn == version
439+
assert size == 65
440+
441+
vs = versify(version=version, kind=Serials.cbor, size=0)
442+
assert vs == 'KERICAACBORAAAA.'
443+
assert len(vs) == VER2FULLSPAN
444+
proto, vrsn, kind, size = deversify(vs)
445+
assert proto == Protos.keri
446+
assert kind == Serials.cbor
447+
assert vrsn == version
448+
assert size == 0
449+
450+
vs = versify(version=version, kind=Serials.cbor, size=65)
451+
assert vs == 'KERICAACBORAABB.'
452+
assert len(vs) == VER2FULLSPAN
453+
proto, vrsn, kind, size = deversify(vs)
454+
assert proto == Protos.keri
455+
assert kind == Serials.cbor
456+
assert vrsn == version
457+
assert size == 65
458+
459+
vs = versify(version=Versionage(major=2, minor=1)) # defaults
460+
assert vs == "KERICABJSONAAAA."
461+
assert len(vs) == VER2FULLSPAN
462+
proto, vrsn, kind, size = deversify(vs)
463+
assert proto == Protos.keri
464+
assert kind == Serials.json
465+
assert vrsn == (2, 1)
466+
assert size == 0
467+
468+
# test bad version strings
469+
vs = "KERIBAAJSONAAAA."
470+
with pytest.raises(VersionError):
471+
smellage = deversify(vs)
472+
473+
vs = "KERI20JSON000000"
474+
with pytest.raises(VersionError):
475+
smellage = deversify(vs)
476+
477+
vs = "ABLECAAJSONAAAA."
478+
with pytest.raises(ProtocolError):
479+
smellage = deversify(vs)
480+
481+
vs = "KERICAAMSONAAAA."
482+
with pytest.raises(KindError):
483+
smellage = deversify(vs)
484+
485+
486+
356487
"""End Test"""
357488

358489

@@ -414,9 +545,9 @@ def test_ilks():
414545

415546

416547
if __name__ == "__main__":
417-
test_b64_conversions()
418548
test_protos()
419549
test_version_regex()
420550
test_serials()
421-
test_versify()
551+
test_versify_v1()
552+
test_versify_v2()
422553
test_ilks()

0 commit comments

Comments
 (0)