2020-04-16 07:07:05 +00:00
|
|
|
import unittest
|
|
|
|
from hashlib import sha3_384
|
|
|
|
|
|
|
|
from kasten import Kasten
|
|
|
|
from kasten import exceptions
|
2020-12-10 01:30:26 +00:00
|
|
|
from kasten.generator import KastenBaseGenerator, pack
|
2020-04-16 07:07:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestKastenBaseGenerator(unittest.TestCase):
|
|
|
|
def test_kasten(self):
|
2020-12-11 03:27:52 +00:00
|
|
|
k = b'\x95\xa3tst\x00\x00\x92\xc4\x00\xc4\x00\xc0\ntest data'
|
2020-04-16 07:07:05 +00:00
|
|
|
K = Kasten(sha3_384(k).digest(), k, KastenBaseGenerator)
|
|
|
|
|
|
|
|
def test_kasten_invalid(self):
|
2020-12-11 03:27:52 +00:00
|
|
|
k = b'\x95\xa3tst\x00\x00\x92\xc4\x00\xc4\x00\xc0\ntest data'
|
2020-12-10 01:30:26 +00:00
|
|
|
self.assertRaises(
|
|
|
|
exceptions.InvalidID,
|
|
|
|
Kasten, sha3_384(k + b'invalid').digest(), k, KastenBaseGenerator)
|
|
|
|
|
|
|
|
def test_kasten_get_metadata(self):
|
|
|
|
metadata = {"name": "john", "raw": b"are we having fun yet?"}
|
|
|
|
packed = pack.pack(b"test msg", "tst", 0, app_metadata=metadata)
|
|
|
|
K = Kasten(sha3_384(packed).digest(), packed, KastenBaseGenerator)
|
|
|
|
self.assertEqual(K.get_metadata(), metadata)
|
|
|
|
|
|
|
|
def test_kasten_unsafe_deserialize(self):
|
|
|
|
class Person:
|
|
|
|
def __init__(self, n):
|
|
|
|
self.name = n
|
|
|
|
kevin = Person("kevin")
|
|
|
|
metadata = {
|
|
|
|
"name": "john", "raw": b"are we having fun yet?", "person": kevin}
|
|
|
|
try:
|
|
|
|
packed = pack.pack(b"test msg", "tst", 0, app_metadata=metadata)
|
|
|
|
K = Kasten(sha3_384(packed).digest(), packed, KastenBaseGenerator)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception("Serialized custom type in packer")
|
|
|
|
|
2020-04-16 07:07:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
unittest.main()
|