Skip to content

armgit / bla

Public Imported from GitHub
0

🛡️ BLA (Blind LLM Agents) — Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

test(python): probe python3/python and skip when unavailable (#59)

Dery Ferdikaa1e4dabba464
diff --git a/tests/helpers/python-interpreter.ts b/tests/helpers/python-interpreter.ts
new file mode 100644
index 0000000..c2ee483
--- /dev/null
+++ b/tests/helpers/python-interpreter.ts
@@ -0,0 +1,40 @@
+import { execFileSync } from "node:child_process";
+
+/**
+ * Resolves a usable Python 3 interpreter for tests.
+ *
+ * Prefers `python3`, falls back to `python` (Windows installs often expose the
+ * real interpreter only as `python`, while `python3` resolves to the Microsoft
+ * Store stub that always fails). Requires Python >= 3.8 so the produced .pyc
+ * matches a layout the weave understands. Returns null when nothing usable
+ * exists — callers must `it.skipIf(...)` instead of failing.
+ */
+export function resolvePython3(): string | null {
+ for (const candidate of ["python3", "python"]) {
+ try {
+ execFileSync(
+ candidate,
+ ["-c", "import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)"],
+ { stdio: "pipe" },
+ );
+ return candidate;
+ } catch {
+ /* try the next candidate */
+ }
+ }
+ return null;
+}
+
+/** Interpreter resolved once per test file. Null when unavailable. */
+export const PY3: string | null = resolvePython3();
+
+/**
+ * Non-null accessor for use inside tests guarded by `it.skipIf(!PY3)`.
+ * Throws instead of silently passing `null` to `execFileSync`.
+ */
+export function python3(): string {
+ if (!PY3) {
+ throw new Error("No Python >= 3.8 interpreter available (test should be skipped)");
+ }
+ return PY3;
+}
diff --git a/tests/python-ast-weave.test.ts b/tests/python-ast-weave.test.ts
index 7c0329e..68809e4 100644
--- a/tests/python-ast-weave.test.ts
+++ b/tests/python-ast-weave.test.ts
@@ -10,6 +10,7 @@ import {
weavePythonDecoyCalls,
} from "../src/shields/formats/python-ast-weave.js";
import { SeededPrng } from "../src/core/prng.js";
+import { PY3, python3 } from "./helpers/python-interpreter.js";
const temps: string[] = [];
afterEach(() => {
@@ -115,7 +116,7 @@ describe("python structural decoy weave", () => {
expect(body!).not.toMatch(/\w+\(\d+,\s*0\)/);
});
- it("woven output still py_compile-clean when python3 is available", () => {
+ it.skipIf(!PY3)("woven output still py_compile-clean when python3 is available", () => {
const customer = [
"#!/usr/bin/env python3",
"# -*- coding: utf-8 -*-",
@@ -137,7 +138,7 @@ describe("python structural decoy weave", () => {
temps.push(dir);
const file = join(dir, "woven.py");
writeFileSync(file, out.code, "utf8");
- execFileSync("python3", ["-m", "py_compile", file], { stdio: "pipe" });
+ execFileSync(python3(), ["-m", "py_compile", file], { stdio: "pipe" });
});
it("PRNG selects decoys without business-name rule tables", () => {
diff --git a/tests/python-pyc-weave.test.ts b/tests/python-pyc-weave.test.ts
index f5dd45e..fe97eac 100644
--- a/tests/python-pyc-weave.test.ts
+++ b/tests/python-pyc-weave.test.ts
@@ -12,6 +12,7 @@ import {
weavePythonPycBuffer,
} from "../src/shields/formats/python-pyc-weave.js";
import { buildWeavePlan } from "../src/shields/weave/weave-decoy.js";
+import { PY3, python3 } from "./helpers/python-interpreter.js";
function compileSamplePyc(): Buffer {
const dir = mkdtempSync(join(tmpdir(), "bla-pyc-weave-"));
@@ -34,7 +35,7 @@ function compileSamplePyc(): Buffer {
"utf8",
);
execFileSync(
- "python3",
+ python3(),
["-c", `import py_compile; py_compile.compile(${JSON.stringify(srcPath)}, cfile=${JSON.stringify(pycPath)}, doraise=True)`],
{ stdio: "pipe" },
);
@@ -58,7 +59,7 @@ print(mod.calculate(2, 3))
print(mod.convert(7))
print(mod.stats([1, 2, 3, 4]))
`;
- const out = execFileSync("python3", ["-c", script], {
+ const out = execFileSync(python3(), ["-c", script], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
@@ -93,7 +94,7 @@ while stack:
print(n)
`;
return Number(
- execFileSync("python3", ["-c", script], { encoding: "utf8" }).trim(),
+ execFileSync(python3(), ["-c", script], { encoding: "utf8" }).trim(),
);
} finally {
rmSync(dir, { recursive: true, force: true });
@@ -107,13 +108,13 @@ describe("python-pyc-weave", () => {
}
});
- it("maps current CPython magic to a known layout", () => {
+ it.skipIf(!PY3)("maps current CPython magic to a known layout", () => {
const pyc = compileSamplePyc();
const magic = pyc.readUInt16LE(0);
expect(layoutForPycMagic(magic)).toBe("py311"); // 3.11+ marshal CODE layout (3.11–3.15 magics)
});
- it("weave_decoys:true injects decoy TYPE_CODE into customer co_consts without breaking import", () => {
+ it.skipIf(!PY3)("weave_decoys:true injects decoy TYPE_CODE into customer co_consts without breaking import", () => {
const raw = compileSamplePyc();
const before = importAndCall(raw);
expect(before.calculate).toBe(5);
@@ -141,7 +142,7 @@ describe("python-pyc-weave", () => {
expect(woven!.buffer.subarray(0, 16).equals(raw.subarray(0, 16))).toBe(true);
});
- it("protectPythonPycBuffer weaveDecoys:false stays trailer-only (no co_consts CODE growth)", () => {
+ it.skipIf(!PY3)("protectPythonPycBuffer weaveDecoys:false stays trailer-only (no co_consts CODE growth)", () => {
const raw = compileSamplePyc();
const before = countCodeObjects(raw);
const res = protectPythonPycBuffer(raw, {
@@ -158,7 +159,7 @@ describe("python-pyc-weave", () => {
expect(importAndCall(res.buffer).calculate).toBe(5);
});
- it("protectPythonPycBuffer weaveDecoys:true weaves CODE + trailers", () => {
+ it.skipIf(!PY3)("protectPythonPycBuffer weaveDecoys:true weaves CODE + trailers", () => {
const raw = compileSamplePyc();
const before = countCodeObjects(raw);
const res = protectPythonPycBuffer(raw, {
@@ -179,7 +180,7 @@ describe("python-pyc-weave", () => {
});
});
- it("weave planner marks python-pyc as woven", () => {
+ it.skipIf(!PY3)("weave planner marks python-pyc as woven", () => {
const dir = mkdtempSync(join(tmpdir(), "bla-pyc-plan-"));
try {
const pycPath = join(dir, "app.pyc");