Spaces:
Running
Running
File size: 2,873 Bytes
770a91a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import json
import struct
import tempfile
import unittest
from pathlib import Path
from preflight import inspect_glb
class PreflightTests(unittest.TestCase):
def write_fixture(self, root, faces, copies=1):
# Valid unindexed triangles. Repeated triangle vertices exercise the
# parser with real binary payload, without a 2M-face Python object graph.
count = faces * 3
doc = {"asset":{"version":"2.0"}, "buffers":[{"byteLength":count*12}],
"bufferViews":[{"buffer":0,"byteLength":count*12}],
"accessors":[{"bufferView":0,"componentType":5126,"count":count,
"type":"VEC3","min":[0,0,0],"max":[1,1,0]}],
"meshes":[{"primitives":[{"attributes":{"POSITION":0}}]}],
"nodes":[{"mesh":0} for _ in range(copies)],
"scenes":[{"nodes":list(range(copies))}],"scene":0}
payload = json.dumps(doc).encode()
payload += b" " * (-len(payload) % 4)
p = Path(root)/"fixture.glb"
with p.open("wb") as stream:
stream.write(struct.pack("<4sIIII", b"glTF",2,28+len(payload)+count*12,len(payload),0x4E4F534A))
stream.write(payload)
stream.write(struct.pack("<II",count*12,0x004E4942))
triangle = struct.pack("<9f",0,0,0,1,0,0,0,1,0)
block = triangle * 1000
for _ in range(faces//1000):
stream.write(block)
stream.write(triangle * (faces%1000))
return p
def test_normal(self):
with tempfile.TemporaryDirectory() as root:
result = inspect_glb(self.write_fixture(root,12))
self.assertTrue(result["inference_allowed"])
self.assertEqual(result["triangles"],12)
def test_two_million_faces_preserved_and_blocked(self):
with tempfile.TemporaryDirectory() as root:
path = self.write_fixture(root,2_000_000)
before = path.stat()
result = inspect_glb(path)
self.assertEqual(result["triangles"],2_000_000)
self.assertFalse(result["inference_allowed"])
self.assertIn("decimation required",result["warning"])
self.assertEqual(path.stat().st_size,before.st_size)
self.assertEqual(path.stat().st_mtime_ns,before.st_mtime_ns)
def test_instances_count_towards_limit(self):
with tempfile.TemporaryDirectory() as root:
result = inspect_glb(self.write_fixture(root,100_000,6))
self.assertFalse(result["inference_allowed"])
self.assertEqual(result["instantiated_triangles"],600_000)
def test_truncated_rejected(self):
with tempfile.TemporaryDirectory() as root:
path=Path(root)/"bad.glb"
path.write_bytes(b"glTF")
with self.assertRaises(ValueError):
inspect_glb(path)
|