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

XFESystem

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

公开
关注 0 Fork 0 Star 0
UTF-8
"""Negative controls for native first-paint pixel evidence; no guest execution is claimed."""
import io
import unittest

from PIL import Image, ImageDraw, ImageFont

from TestDocumentFirstPaint import verify_pixels, verify_first_frame_binding, bytes_fingerprint


class DocumentFirstPaintPixelGuards(unittest.TestCase):
    foreground = (224, 224, 224)
    background = (24, 26, 27)

    @staticmethod
    def proof():
        def element(text, y):
            return {"Text": text, "Runs": [{"Text": text, "Color": 0xE0E0E0,
                    "Bounds": {"X": 12, "Y": y, "Width": 174, "Height": 23}}]}
        return {"Viewport": {"X": 5, "Y": 7, "Width": 200, "Height": 70},
                "Heading": element("XFESystem preview", 4),
                "Paragraph": element("Actual document body text.", 36)}

    def image(self, heading=False, paragraph=False, fill=None):
        image = Image.new("RGB", (220, 90), self.background if fill is None else fill)
        draw = ImageDraw.Draw(image)
        font = ImageFont.load_default()
        proof = self.proof()
        for kind, enabled in (("Heading", heading), ("Paragraph", paragraph)):
            if enabled:
                run = proof[kind]["Runs"][0]
                draw.text((proof["Viewport"]["X"] + run["Bounds"]["X"],
                           proof["Viewport"]["Y"] + run["Bounds"]["Y"]),
                          run["Text"], font=font, fill=self.foreground)
        return image

    def verify(self, image, scale=1):
        if scale != 1:
            image = image.resize((image.width * scale, image.height * scale), Image.Resampling.NEAREST)
        stream = io.BytesIO()
        image.save(stream, format="PNG")
        stream.seek(0)
        return verify_pixels(stream, self.proof(), scale)

    def test_foreground_colored_blank_image_is_rejected(self):
        # This uniform #E0E0E0 image passed the original foreground-count check.
        for scale in (1, 2):
            with self.subTest(scale=scale), self.assertRaises(AssertionError):
                self.verify(self.image(fill=self.foreground), scale)

    def test_background_colored_blank_image_is_rejected(self):
        for scale in (1, 2):
            with self.subTest(scale=scale), self.assertRaises(AssertionError):
                self.verify(self.image(), scale)

    def test_heading_alone_does_not_prove_document_body_was_painted(self):
        with self.assertRaises(AssertionError):
            self.verify(self.image(heading=True))

    def test_contrasting_rasterized_heading_and_body_are_accepted(self):
        for scale in (1, 2):
            with self.subTest(scale=scale):
                result = self.verify(self.image(heading=True, paragraph=True), scale)
                self.assertTrue(result["Passed"])
                self.assertTrue(result["HeadingAndParagraphPainted"])
                self.assertEqual({"Heading", "Paragraph"},
                                 {sample["Kind"] for sample in result["Samples"] if sample["TextInkAccepted"]})

    def bound_frame(self):
        image = self.image(heading=True, paragraph=True)
        proof = self.proof()
        for kind in ("Heading", "Paragraph"):
            run = proof[kind]["Runs"][0]["Bounds"]
            x = proof["Viewport"]["X"] + run["X"]; y = proof["Viewport"]["Y"] + run["Y"]
            width = run["Width"]; height = run["Height"]
            proof[kind]["PresentedPixels"] = {"FingerprintAlgorithm": "RGB24-FNV1a64",
                "Fingerprint": bytes_fingerprint(image.crop((x, y, x + width, y + height)).tobytes()),
                "NativeBounds": {"X": x, "Y": y, "Width": width, "Height": height}, "PresentationSequence": 19}
        return image, proof

    def verify_binding(self, image, proof):
        stream = io.BytesIO(); image.save(stream, format="PNG"); stream.seek(0)
        return verify_first_frame_binding(stream, proof)

    def test_exact_presented_rgb_regions_are_bound_to_screenshot(self):
        image, proof = self.bound_frame()
        self.assertTrue(self.verify_binding(image, proof)["FirstPresentedScanoutRegionsMatchScreenshot"])

    def test_later_changed_frame_is_rejected(self):
        image, proof = self.bound_frame()
        rect = proof["Paragraph"]["PresentedPixels"]["NativeBounds"]
        image.putpixel((rect["X"], rect["Y"]), (0, 255, 0))
        with self.assertRaises(AssertionError):
            self.verify_binding(image, proof)

    def test_different_presentation_sequences_are_rejected(self):
        image, proof = self.bound_frame()
        proof["Paragraph"]["PresentedPixels"]["PresentationSequence"] += 1
        with self.assertRaises(AssertionError):
            self.verify_binding(image, proof)


if __name__ == "__main__":
    unittest.main(verbosity=2)