61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
"""Tests for the crypto-signed audit log."""
|
|
|
|
import unittest
|
|
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
|
|
from deltatrace.audit_log import AuditLog
|
|
|
|
|
|
class TestAuditLog(unittest.TestCase):
|
|
def setUp(self):
|
|
self.key = Ed25519PrivateKey.generate()
|
|
self.log = AuditLog(self.key)
|
|
|
|
def test_append_entry(self):
|
|
entry = self.log.append("evt-1", "processed:md_tick", {"symbol": "AAPL"})
|
|
self.assertEqual(entry.sequence, 0)
|
|
self.assertEqual(entry.event_id, "evt-1")
|
|
self.assertEqual(entry.action, "processed:md_tick")
|
|
|
|
def test_chain_linkage(self):
|
|
e1 = self.log.append("evt-1", "action1")
|
|
e2 = self.log.append("evt-2", "action2")
|
|
self.assertEqual(e2.prev_hash, e1.entry_hash)
|
|
|
|
def test_verify_valid_chain(self):
|
|
for i in range(5):
|
|
self.log.append(f"evt-{i}", f"action-{i}")
|
|
self.assertTrue(self.log.verify())
|
|
|
|
def test_verify_with_public_key(self):
|
|
self.log.append("evt-1", "action1")
|
|
public_key = self.key.public_key()
|
|
self.assertTrue(self.log.verify(public_key))
|
|
|
|
def test_tampered_entry_fails_verify(self):
|
|
self.log.append("evt-1", "action1")
|
|
self.log.append("evt-2", "action2")
|
|
# Tamper with an entry
|
|
self.log._entries[0] = self.log._entries[0].__class__(
|
|
sequence=0,
|
|
timestamp_ns=self.log._entries[0].timestamp_ns,
|
|
event_id="TAMPERED",
|
|
action=self.log._entries[0].action,
|
|
details=self.log._entries[0].details,
|
|
prev_hash=self.log._entries[0].prev_hash,
|
|
entry_hash=self.log._entries[0].entry_hash,
|
|
signature=self.log._entries[0].signature,
|
|
)
|
|
self.assertFalse(self.log.verify())
|
|
|
|
def test_to_dict(self):
|
|
self.log.append("evt-1", "test")
|
|
data = self.log.to_dict()
|
|
self.assertEqual(len(data["entries"]), 1)
|
|
self.assertIn("signature", data["entries"][0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|