kasten/tests/test_pack.py

64 lines
2.3 KiB
Python
Raw Normal View History

2020-04-11 07:48:28 +00:00
import unittest
import os
2020-04-16 07:07:05 +00:00
from time import time
from math import floor
from msgpack import unpackb
2020-04-11 07:48:28 +00:00
2020-04-14 09:33:17 +00:00
from kasten.generator import pack
2020-04-11 07:48:28 +00:00
from kasten import exceptions
class TestPack(unittest.TestCase):
def test_unsigned_pack(self):
data = os.urandom(10)
2020-04-16 07:07:05 +00:00
t = floor(time())
2020-04-11 07:48:28 +00:00
packed = pack.pack(data, 'bin', 0)
parts = packed.split(b'\n', 1)
2020-04-16 07:07:05 +00:00
unpacked = unpackb(parts[0])
self.assertEqual(unpacked[0], 'bin')
self.assertEqual(unpacked[1], 0)
self.assertAlmostEqual(unpacked[2], t)
2020-12-11 03:27:52 +00:00
self.assertEqual(len(unpacked), 5)
2020-04-16 07:07:05 +00:00
def test_unsigned_with_meta(self):
data = os.urandom(10)
t = floor(time())
packed = pack.pack(data, 'bin', 0, app_metadata={"meme": "doge"})
parts = packed.split(b'\n', 1)
unpacked = unpackb(parts[0])
self.assertEqual(unpacked[0], 'bin')
self.assertEqual(unpacked[1], 0)
self.assertAlmostEqual(unpacked[2], t)
2020-12-11 03:27:52 +00:00
self.assertEqual(unpacked[4], {"meme": "doge"})
self.assertEqual(len(unpacked), 5)
2020-04-11 07:48:28 +00:00
def test_linebreak_data(self):
2020-04-16 07:07:05 +00:00
data = os.urandom(10) + b'\n'
t = floor(time())
2020-04-11 07:48:28 +00:00
packed = pack.pack(data, 'bin', 0)
parts = packed.split(b'\n', 1)
2020-04-16 07:07:05 +00:00
unpacked = unpackb(parts[0])
self.assertEqual(unpacked[0], 'bin')
self.assertEqual(unpacked[1], 0)
self.assertAlmostEqual(unpacked[2], t)
self.assertEqual(len(unpacked[0]), 3)
2020-04-11 07:48:28 +00:00
def test_invalid_data_type(self):
data = os.urandom(10)
self.assertRaises(exceptions.InvalidKastenTypeLength, pack.pack, data, '', 1)
data = os.urandom(10)
self.assertRaises(exceptions.InvalidKastenTypeLength, pack.pack, data, 'aaaaa', 1)
2020-04-13 00:53:57 +00:00
def test_invalid_enc_mode(self):
data = os.urandom(10)
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', None)
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', "100")
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', 100)
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', -1)
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', -5)
self.assertRaises(exceptions.InvalidEncryptionMode, pack.pack, data, 'txt', "test")
2020-04-11 07:48:28 +00:00
unittest.main()