#!/usr/bin/env python3 import os import sqlite3 import tempfile import time import unittest class TestSuDatabase(unittest.TestCase): def setUp(self): self.temp_dir = tempfile.TemporaryDirectory() self.db_path = os.path.join(self.temp_dir.name, "sumanager.db") self.conf_path = os.path.join(self.temp_dir.name, "policy.conf") self.conn = sqlite3.connect(self.db_path) self.init_schema() def tearDown(self): self.conn.close() self.temp_dir.cleanup() def init_schema(self): cur = self.conn.cursor() cur.execute(""" CREATE TABLE IF NOT EXISTS policies ( uid INTEGER PRIMARY KEY, package_name TEXT NOT NULL, policy INTEGER NOT NULL, until INTEGER NOT NULL, updated_at INTEGER NOT NULL ) """) cur.execute(""" CREATE TABLE IF NOT EXISTS access_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER NOT NULL, package_name TEXT NOT NULL, command TEXT, action INTEGER NOT NULL, timestamp INTEGER NOT NULL ) """) self.conn.commit() def sync_conf(self): cur = self.conn.cursor() cur.execute("SELECT uid, policy, until FROM policies") rows = cur.fetchall() with open(self.conf_path, "w") as f: f.write("# SlopRoot SuManager policy cache\n") f.write(f"# Generated: {int(time.time())}\n") for uid, policy, until in rows: f.write(f"{uid} {policy} {until}\n") def test_insert_and_sync_policy(self): cur = self.conn.cursor() now = int(time.time()) # Insert allow forever (until=0) cur.execute("INSERT OR REPLACE INTO policies VALUES (?, ?, ?, ?, ?)", (10055, "com.example.app", 1, 0, now)) self.conn.commit() self.sync_conf() with open(self.conf_path, "r") as f: content = f.read() self.assertIn("10055 1 0", content) def test_toggle_policy(self): cur = self.conn.cursor() now = int(time.time()) cur.execute("INSERT OR REPLACE INTO policies VALUES (?, ?, ?, ?, ?)", (10080, "org.test.terminal", 1, 0, now)) self.conn.commit() # Toggle from 1 (allow) to 2 (deny) cur.execute("SELECT policy FROM policies WHERE uid = ?", (10080,)) current_policy = cur.fetchone()[0] new_policy = 2 if current_policy == 1 else 1 cur.execute("UPDATE policies SET policy = ?, updated_at = ? WHERE uid = ?", (new_policy, now + 1, 10080)) self.conn.commit() self.sync_conf() cur.execute("SELECT policy FROM policies WHERE uid = ?", (10080,)) self.assertEqual(cur.fetchone()[0], 2) with open(self.conf_path, "r") as f: content = f.read() self.assertIn("10080 2 0", content) def test_delete_policy(self): cur = self.conn.cursor() now = int(time.time()) cur.execute("INSERT INTO policies VALUES (?, ?, ?, ?, ?)", (10099, "com.temp.tool", 1, 0, now)) self.conn.commit() self.sync_conf() cur.execute("DELETE FROM policies WHERE uid = ?", (10099,)) self.conn.commit() self.sync_conf() with open(self.conf_path, "r") as f: content = f.read() self.assertNotIn("10099", content) def test_access_log_pruning(self): cur = self.conn.cursor() now = int(time.time()) # Insert 150 log entries for i in range(150): cur.execute("INSERT INTO access_logs (uid, package_name, command, action, timestamp) VALUES (?, ?, ?, ?, ?)", (10055, "com.example.app", f"id -u {i}", 1, now + i)) self.conn.commit() cur.execute("SELECT COUNT(*) FROM access_logs") self.assertEqual(cur.fetchone()[0], 150) # Prune older than latest 50 entries cur.execute(""" DELETE FROM access_logs WHERE id NOT IN ( SELECT id FROM access_logs ORDER BY timestamp DESC LIMIT 50 ) """) self.conn.commit() cur.execute("SELECT COUNT(*) FROM access_logs") self.assertEqual(cur.fetchone()[0], 50) if __name__ == "__main__": unittest.main()