61 lines
3.1 KiB
JavaScript
61 lines
3.1 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
import test from "node:test";
|
|
import ts from "typescript";
|
|
|
|
const root = path.resolve(import.meta.dirname, "../../..");
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "glb-negative-unit-"));
|
|
const sourcePath = path.join(root, "web/protocol/glb-import.ts");
|
|
const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
fileName: sourcePath,
|
|
reportDiagnostics: true,
|
|
});
|
|
assert.deepEqual(transpiled.diagnostics, []);
|
|
const modulePath = path.join(temporary, "glb-import.mjs");
|
|
fs.writeFileSync(modulePath, transpiled.outputText);
|
|
const protocol = await import(pathToFileURL(modulePath));
|
|
const source = fs.readFileSync(path.join(root, "tests/files/web/m12_glb_desktop_v1/mesh.glb"));
|
|
|
|
function arrayBuffer(bytes) {
|
|
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
}
|
|
|
|
function rewriteJson(mutator) {
|
|
const jsonLength = source.readUInt32LE(12);
|
|
const document = JSON.parse(source.subarray(20, 20 + jsonLength).toString("utf8").trim());
|
|
mutator(document);
|
|
const json = Buffer.from(JSON.stringify(document));
|
|
const paddedLength = (json.length + 3) & ~3;
|
|
const output = Buffer.alloc(12 + 8 + paddedLength + (source.length - (20 + jsonLength)));
|
|
output.writeUInt32LE(0x46546c67, 0);
|
|
output.writeUInt32LE(2, 4);
|
|
output.writeUInt32LE(output.length, 8);
|
|
output.writeUInt32LE(paddedLength, 12);
|
|
output.writeUInt32LE(0x4e4f534a, 16);
|
|
json.copy(output, 20);
|
|
output.fill(0x20, 20 + json.length, 20 + paddedLength);
|
|
output.writeUInt32LE(source.readUInt32LE(20 + jsonLength), 20 + paddedLength);
|
|
output.writeUInt32LE(source.readUInt32LE(24 + jsonLength), 24 + paddedLength);
|
|
source.subarray(28 + jsonLength).copy(output, 28 + paddedLength);
|
|
return output;
|
|
}
|
|
|
|
test("M12-06F rejects sparse accessors, extensions and external URIs", () => {
|
|
assert.throws(() => protocol.importGLBSemantics(arrayBuffer(rewriteJson((document) => { document.accessors[0].sparse = { count: 1 }; }))), /GLB_SPARSE_ACCESSOR_UNSUPPORTED/);
|
|
assert.throws(() => protocol.importGLBSemantics(arrayBuffer(rewriteJson((document) => { document.extensionsUsed = ["KHR_draco_mesh_compression"]; }))), /GLB_EXTENSION_UNSUPPORTED/);
|
|
assert.throws(() => protocol.importGLBSemantics(arrayBuffer(rewriteJson((document) => { document.buffers[0].uri = "external.bin"; }))), /GLB_EXTERNAL_URI_BLOCKED/);
|
|
});
|
|
|
|
test("M12-06F rejects over-budget GLB bytes and table counts", () => {
|
|
const oversized = Buffer.alloc(protocol.GLB_IMPORT_BUDGET.maxBytes + 1);
|
|
source.copy(oversized);
|
|
assert.throws(() => protocol.importGLBSemantics(arrayBuffer(oversized)), /GLB_IMPORT_BUDGET_EXCEEDED/);
|
|
assert.throws(() => protocol.importGLBSemantics(arrayBuffer(rewriteJson((document) => { document.bufferViews = Array.from({ length: protocol.GLB_IMPORT_BUDGET.maxBufferViews + 1 }, () => ({ buffer: 0, byteLength: 0 })); }))), /GLB_IMPORT_BUDGET_EXCEEDED/);
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|