XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
"""Offline negative controls for the full-screen console evidence boundary."""
import unittest

from IndependentBingEvidence import json_blocks
from IndependentConsoleEvidence import (CONSOLE_SNAPSHOT, FAILURE_MARKERS,
                                        FORBIDDEN_CONSOLE_MARKERS, assert_console_isolation,
                                        terminal_command_output)
from TestBoot import boot_menu_labels_present


def snapshot(command='cat CliProof/Command.txt', output='NativeConsoleReady'):
    prompt = 'X:\\Users\\Default\\Documents> '
    return {'Busy': False, 'Input': '', 'LastCommand': command, 'Prompt': prompt, 'Version': '0.3.0-dev.4',
            'Text': prompt + 'write CliProof/Command.txt NativeConsoleReady\n' + prompt + command + '\n' + output + '\n'}


class IndependentConsoleEvidenceTests(unittest.TestCase):
    def test_boot_menu_requires_both_labels_and_accepts_per_character_ansi_positioning(self):
        self.assertFalse(boot_menu_labels_present('XFESystem Desktop'))
        self.assertFalse(boot_menu_labels_present('XFE_SYSTEM_EDITION|Console'))
        plain = 'XFESystem Desktop\nXFESystem Console'
        mirrored = ''.join('\x1b[12;32H' + character for character in plain)
        self.assertTrue(boot_menu_labels_present(plain))
        self.assertTrue(boot_menu_labels_present(mirrored))

    def test_console_identity_is_required(self):
        assert_console_isolation('XFE_SYSTEM_EDITION|Console\nXFE_INDEPENDENT_CONSOLE_FIRST_FRAME\n')
        for log in ('', 'XFE_SYSTEM_EDITION|Desktop\n', 'XFE_SYSTEM_EDITION|Console\nXFE_SYSTEM_EDITION|Desktop\n'):
            with self.subTest(log=log), self.assertRaises(AssertionError): assert_console_isolation(log)

    def test_late_desktop_or_browser_fallback_cannot_qualify(self):
        for marker in FORBIDDEN_CONSOLE_MARKERS:
            with self.subTest(marker=marker), self.assertRaises(AssertionError):
                assert_console_isolation('XFE_SYSTEM_EDITION|Console\nXFE_INDEPENDENT_CONSOLE_FIRST_FRAME\n' + marker)

    def test_each_runtime_failure_is_rejected(self):
        for marker in FAILURE_MARKERS:
            with self.subTest(marker=marker), self.assertRaises(AssertionError):
                assert_console_isolation('XFE_SYSTEM_EDITION|Console\n' + marker)

    def test_file_data_in_write_arguments_is_never_cat_output(self):
        self.assertEqual(['NativeConsoleReady'], terminal_command_output(snapshot(), 'cat CliProof/Command.txt'))
        self.assertEqual([], terminal_command_output(snapshot(output=''), 'cat CliProof/Command.txt'))

    def test_busy_stale_input_and_another_command_are_rejected(self):
        for field, value in (('Busy', True), ('Input', 'cat CliProof/Command.txt'), ('LastCommand', 'help'),
                             ('Prompt', ''), ('Version', ''), ('Text', 'NativeConsoleReady')):
            item = snapshot(); item[field] = value
            with self.subTest(field=field), self.assertRaises(AssertionError):
                terminal_command_output(item, 'cat CliProof/Command.txt')

    def test_latest_repeated_echo_and_managed_failure_own_the_result(self):
        item = snapshot(); item['Text'] += item['Prompt'] + item['LastCommand'] + '\nactual latest body\n'
        self.assertEqual(['actual latest body'], terminal_command_output(item, item['LastCommand']))
        for failure in ('Error: File not found.', 'Unknown command: cat', 'Managed program failed: unsupported IL'):
            with self.subTest(failure=failure), self.assertRaises(AssertionError):
                terminal_command_output(snapshot(output=failure), 'cat CliProof/Command.txt')

    def test_desktop_packets_and_partial_json_are_not_console_packets(self):
        desktop = 'XFE_TERMINAL_SNAPSHOT_BEGIN\n{}\nXFE_TERMINAL_SNAPSHOT_END'
        partial = CONSOLE_SNAPSHOT + '_BEGIN\n{"Busy":false'
        self.assertEqual([], json_blocks(desktop + '\n' + partial, CONSOLE_SNAPSHOT))


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