ZZZ Digital Lab

Back to WaitLens

tests/test_waitlens.py

import contextlib
import hashlib
import io
import json
from pathlib import Path
import struct
import tempfile
import unittest
from unittest.mock import patch

from waitlens.cli import main, load_snapshot
from waitlens.core import InputError, analyze, compare, cycle_components, parse
from waitlens.report import render
from scripts.build_qnx import elf_machine
from scripts.check_evidence import check

ROOT = Path(__file__).resolve().parents[1]
HEADER = 'pid tid name prio STATE Blocked\n'

def setUpModule():
    (ROOT / 'tmp').mkdir(exist_ok=True)

def fixture(name):
    return (ROOT / 'fixtures' / name).read_text(encoding='utf-8')

class ParserTests(unittest.TestCase):
    def test_documentation_excerpt(self):
        result = analyze(fixture('public-doc.txt'), provenance='documentation-excerpt')
        self.assertEqual(result['summary']['threads'], 2)
        self.assertEqual(result['edges'], [])
        self.assertEqual(result['summary']['states'], {'READY': 1, 'RECEIVE': 1})

    def test_crlf_bom_spaces_and_blocked_tail(self):
        threads, _ = parse('\ufeff' + HEADER.replace('\n', '\r\n') + '12 2 service with spaces 24r MUTEX 7-1 #2\r\n')
        self.assertEqual(threads[0].name, 'service with spaces')
        self.assertEqual(threads[0].blocked, '7-1 #2')

    def test_receive_is_channel_not_process(self):
        result = analyze(fixture('cycle.txt'))
        self.assertEqual(len(result['edges']), 2)
        self.assertFalse(any(e['source_pid'] == 400 for e in result['edges']))
        self.assertEqual(result['cycle_candidates'], [[200, 300]])

    def test_send_reply_point_to_process_not_arbitrary_thread(self):
        result = analyze(HEADER + '7 2 client 10r REPLY 8\n8 1 server 10r RECEIVE 1\n8 2 server 10r READY\n')
        self.assertEqual(result['edges'][0]['destination_pid'], 8)
        self.assertNotIn('destination_tid', result['edges'][0])

    def test_missing_destination_is_reported(self):
        result = analyze(HEADER + '9 1 client 10r SEND 888\n')
        self.assertFalse(result['edges'][0]['destination_observed'])
        self.assertIn('888', result['warnings'][0])

    def test_zombie_has_no_thread_state(self):
        result = analyze(HEADER + '42 (Zombie)\n')
        self.assertEqual(result['zombie_pids'], [42])
        self.assertEqual(result['summary']['threads'], 0)
        self.assertFalse(result['edges'])

    def test_no_silent_partial_success(self):
        invalid = ['', HEADER, 'garbage', HEADER + '1 1 app 10r SEND\n',
                   HEADER + '1 1 app 10r SEND 2 (1,2,3)\n',
                   HEADER + '1 1 app 10r FUTURE_STATE\n',
                   HEADER + '1 1 app 999r READY\n',
                   HEADER + '0 1 app 10r READY\n',
                   HEADER + '1 1 app 10r READY\n1 1 app 10r READY\n',
                   HEADER + '1 1 app 10r READY\n' + HEADER,
                   HEADER + '1 (Zombie)\n1 1 app 10r READY\n',
                   HEADER + '1 1 app 10r READY\npidin: permission denied\n']
        for sample in invalid:
            with self.subTest(sample=sample), self.assertRaises(InputError):
                parse(sample)

    def test_line_number_in_diagnostic(self):
        with self.assertRaisesRegex(InputError, 'line 3'):
            parse(HEADER + '1 1 app 10r READY\nbroken\n')

class GraphTests(unittest.TestCase):
    @staticmethod
    def edge(a, b):
        return {'source_pid': a, 'destination_pid': b}

    def test_disjoint_cycles_self_wait_and_acyclic_tail(self):
        edges = [self.edge(a,b) for a,b in [(1,2),(2,1),(2,3),(4,4),(5,6),(6,5),(6,7)]]
        self.assertEqual(cycle_components(edges), [[1,2],[4],[5,6]])

    def test_large_chain_does_not_recurse(self):
        self.assertEqual(cycle_components([self.edge(i,i+1) for i in range(4000)]), [])

    def test_components_match_reachability_oracle(self):
        # Exhaust all directed graphs on three nodes, including self edges.
        pairs = [(a,b) for a in range(3) for b in range(3)]
        for mask in range(1 << len(pairs)):
            pairs_in_graph = [(a,b) for bit,(a,b) in enumerate(pairs) if mask & (1 << bit)]
            reachable = {(a,b) for a,b in pairs_in_graph}
            for k in range(3):
                for a in range(3):
                    for b in range(3):
                        if (a,k) in reachable and (k,b) in reachable:
                            reachable.add((a,b))
            expected, seen = [], set()
            for a in range(3):
                if a in seen:
                    continue
                group = [b for b in range(3) if (a,b) in reachable and (b,a) in reachable]
                if group:
                    expected.append(group)
                    seen.update(group)
            self.assertEqual(cycle_components([self.edge(a,b) for a,b in pairs_in_graph]), sorted(expected))

class CompareTests(unittest.TestCase):
    def test_send_to_reply_transition(self):
        report = compare(analyze(fixture('send.txt')), analyze(fixture('reply.txt')))
        self.assertEqual(len(report['changes']), 1)
        self.assertEqual(report['changes'][0]['before']['state'], 'SEND')
        self.assertEqual(report['changes'][0]['after']['state'], 'REPLY')

    def test_appearance_disappearance_identity_change_and_priority(self):
        before = analyze(HEADER + '1 1 old 10r READY\n2 1 gone 10r READY\n4 1 stable 10r READY\n')
        after = analyze(HEADER + '1 1 new 10r READY\n3 1 arrived 10r READY\n4 1 stable 20r READY\n')
        self.assertEqual([c['kind'] for c in compare(before, after)['changes']],
                         ['identity-changed', 'disappeared', 'appeared', 'changed'])

    def test_unchanged_has_no_change(self):
        report = analyze(fixture('send.txt'))
        self.assertEqual(compare(report, report)['changes'], [])

class ReportAndCliTests(unittest.TestCase):
    def test_html_complete_and_capture_text_inert(self):
        report = analyze(HEADER + '1 1 <script>alert(1)</script> 10r READY\n', label='<img src=x>')
        html = render(report)
        self.assertIn('&lt;script&gt;', html)
        self.assertNotIn('<script>', html)
        self.assertIn('&lt;img src=x&gt;', html)
        self.assertIn('</html>', html)
        self.assertIn("default-src 'none'", html)
        self.assertIn('not an atomic snapshot', html)

    def test_comparison_html_contains_both_snapshots(self):
        html = render(compare(analyze(fixture('send.txt'), label='before'), analyze(fixture('reply.txt'), label='after')))
        self.assertIn('Observed changes', html)
        self.assertIn('<h2>before</h2>', html)
        self.assertIn('<h2>after</h2>', html)

    def test_cli_json_html_and_invalid_exit(self):
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            directory = Path(temp)
            before, after = directory / 'before.txt', directory / 'after.txt'
            before.write_text(fixture('send.txt'), encoding='utf-8')
            after.write_text(fixture('reply.txt'), encoding='utf-8')
            out_json, out_html = directory / 'out.json', directory / 'out.html'
            with contextlib.redirect_stdout(io.StringIO()):
                code = main(['compare', str(before), str(after), '--provenance', 'synthetic', '--json', str(out_json), '--html', str(out_html)])
            self.assertEqual(code, 0)
            data = json.loads(out_json.read_text(encoding='utf-8'))
            self.assertEqual(data['before']['provenance'], 'synthetic')
            self.assertIn('</html>', out_html.read_text(encoding='utf-8'))
            before.write_text('invalid', encoding='utf-8')
            with contextlib.redirect_stderr(io.StringIO()):
                code = main(['analyze', str(before), '--json', str(directory / 'bad.json')])
            self.assertEqual(code, 2)
            self.assertFalse((directory / 'bad.json').exists())

    def test_cannot_overwrite_input_or_collide_outputs(self):
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            path = Path(temp) / 'in.txt'
            path.write_text(fixture('send.txt'), encoding='utf-8')
            with contextlib.redirect_stderr(io.StringIO()):
                self.assertEqual(main(['analyze', str(path), '--json', str(path)]), 2)
                self.assertEqual(main(['analyze', str(path), '--json', str(path)+'.out', '--html', str(path)+'.out']), 2)
            self.assertEqual(path.read_text(encoding='utf-8'), fixture('send.txt'))

    def test_size_and_encoding_limit(self):
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            path = Path(temp) / 'input.txt'
            path.write_bytes(b'x' * 33)
            with patch('waitlens.cli.MAX_BYTES', 32), self.assertRaises(InputError):
                load_snapshot(path, 'synthetic')
            path.write_bytes(b'\xff\xfe')
            with self.assertRaises(UnicodeError):
                load_snapshot(path, 'synthetic')

class EvidenceTests(unittest.TestCase):
    def test_elf_is_architecture_check_only(self):
        data = bytearray(64)
        data[:6] = b'\x7fELF\x02\x01'
        struct.pack_into('<H', data, 18, 62)
        self.assertEqual(elf_machine(data), 62)
        with self.assertRaises(ValueError):
            elf_machine(b'not elf')

    def test_missing_real_evidence_fails(self):
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp, self.assertRaises(OSError):
            check(Path(temp), Path(temp))

    def test_receipt_schema_rejects_non_object(self):
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            directory = Path(temp)
            (directory / 'build-receipt.json').write_text('[]', encoding='utf-8')
            with self.assertRaisesRegex(ValueError, 'JSON object'):
                check(directory, directory)

    def test_consistency_gate_and_tampering(self):
        # Entirely synthetic unit-test data. Never write it to evidence/.
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            directory = Path(temp)
            build, capture = directory / 'build', directory / 'capture'
            build.mkdir(); capture.mkdir()
            data = bytearray(64); data[:6] = b'\x7fELF\x02\x01'
            struct.pack_into('<H', data, 18, 62)
            for path in [build / 'waitlens_probe', capture / 'waitlens_probe']:
                path.write_bytes(data)
            receipt = {'status': 'built', 'compiler_exit': 0, 'variant': 'gcc_ntox86_64',
                       'source_sha256': hashlib.sha256((ROOT / 'target/waitlens_probe.c').read_bytes()).hexdigest(),
                       'binary_sha256': hashlib.sha256(data).hexdigest()}
            (build / 'build-receipt.json').write_text(json.dumps(receipt), encoding='utf-8')
            (capture / 'platform.txt').write_text('QNX\n8.0.0\n', encoding='utf-8')
            (capture / 'probe.log').write_text('compiler_qnx=800\nserver=100 client=101 phase=delay_before_receive\nreply=OK\nWaitLens probe: completed\n', encoding='utf-8')
            for name in ['send.txt', 'reply.txt']:
                (capture / name).write_text(fixture(name), encoding='utf-8')
            self.assertEqual(check(build, capture)['status'], 'consistent-local-evidence')
            self.assertFalse(check(build, capture)['independently_authenticated'])
            (capture / 'send.txt').write_text(fixture('reply.txt'), encoding='utf-8')
            with self.assertRaisesRegex(ValueError, 'expected SEND'):
                check(build, capture)
            (capture / 'send.txt').write_text(fixture('send.txt'), encoding='utf-8')
            (capture / 'reply.txt').write_text(fixture('reply.txt').replace('101 1', '101 2'), encoding='utf-8')
            with self.assertRaisesRegex(ValueError, 'same client TID'):
                check(build, capture)
            (capture / 'reply.txt').write_text(fixture('reply.txt'), encoding='utf-8')
            (capture / 'waitlens_probe').write_bytes(b'changed')
            with self.assertRaisesRegex(ValueError, 'differs'):
                check(build, capture)

    def test_failed_build_replaces_stale_success_receipt(self):
        from scripts import build_qnx
        with tempfile.TemporaryDirectory(dir=ROOT / 'tmp') as temp:
            directory = Path(temp)
            (directory / 'target').mkdir()
            (directory / 'target/waitlens_probe.c').write_text('test-only', encoding='utf-8')
            previous = directory / 'build/qnx/build-receipt.json'
            previous.parent.mkdir(parents=True)
            previous.write_text('{"status": "built"}', encoding='utf-8')
            qcc = directory / 'not-invoked.exe'; qcc.touch()
            with patch.object(build_qnx, 'ROOT', directory), patch.object(build_qnx.subprocess, 'run') as run:
                run.return_value.returncode = 1
                with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
                    self.assertEqual(build_qnx.main(['--qcc', str(qcc)]), 2)
            receipt = json.loads((directory / 'build/qnx/build-receipt.json').read_text(encoding='utf-8'))
            self.assertEqual(receipt['status'], 'failed')
            self.assertFalse(receipt['runtime_verified'])

if __name__ == '__main__':
    unittest.main()