Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

test(media): prove ffmpeg resolution continues past a bad candidate

Etienne Lescot1a7a40f8c4e6
diff --git a/electron/media/audioPeaks.test.ts b/electron/media/audioPeaks.test.ts
index ba7a46679..a49c28b06 100644
--- a/electron/media/audioPeaks.test.ts
+++ b/electron/media/audioPeaks.test.ts
@@ -2,7 +2,7 @@
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
-import { describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { ffmpegCandidates, peakBlockCount, resolveFfmpeg } from "./audioPeaks";
const ROOT = path.resolve(__dirname, "..", "..");
@@ -115,6 +115,81 @@ describe("ffmpeg resolution", () => {
}
},
);
+
+ /**
+ * REJECTING IS NOT THE SAME AS CONTINUING, and only the second is the
+ * property the predicate exists for: swallowing every failure is what stops
+ * one bad path from denying a later working one. The tests above prove the
+ * first — with a single candidate staged, `null` is equally consistent with
+ * "skipped it" and "gave up on the whole list".
+ *
+ * `OPENSCREEN_FFMPEG_PATH` is the vehicle because `ffmpegCandidates` puts it
+ * FIRST, so a bad value there is the one case that could shadow every real
+ * candidate behind it.
+ */
+ describe("falling through to a later candidate", () => {
+ let here: string;
+ let staged: string;
+
+ beforeEach(() => {
+ here = mkdtempSync(path.join(tmpdir(), "openscreen-ffmpeg-"));
+ staged = path.join(
+ here,
+ "electron",
+ "native",
+ "bin",
+ `${process.platform}-${process.arch}`,
+ process.platform === "win32" ? "ffmpeg-shared.exe" : "ffmpeg",
+ );
+ mkdirSync(path.dirname(staged), { recursive: true });
+ writeFileSync(staged, "", { mode: 0o755 });
+ });
+
+ afterEach(() => {
+ delete process.env.OPENSCREEN_FFMPEG_PATH;
+ rmSync(here, { recursive: true, force: true });
+ });
+
+ it("passes over a leading candidate that does not exist", () => {
+ process.env.OPENSCREEN_FFMPEG_PATH = path.join(here, "nowhere", "ffmpeg");
+
+ expect(resolveFfmpeg(here)).toBe(staged);
+ });
+
+ it("passes over a leading candidate that is a directory", () => {
+ const decoy = path.join(here, "decoy-ffmpeg");
+ mkdirSync(decoy, { recursive: true });
+ writeFileSync(path.join(decoy, "libavcodec.so.62"), "");
+ process.env.OPENSCREEN_FFMPEG_PATH = decoy;
+
+ expect(resolveFfmpeg(here)).toBe(staged);
+ });
+
+ it.runIf(process.platform !== "win32")(
+ "passes over a leading candidate that is not executable",
+ () => {
+ const decoy = path.join(here, "decoy-ffmpeg");
+ writeFileSync(decoy, "", { mode: 0o644 });
+ process.env.OPENSCREEN_FFMPEG_PATH = decoy;
+
+ expect(resolveFfmpeg(here)).toBe(staged);
+ },
+ );
+
+ // An unreadable-but-executable binary is legitimate on Unix, so it must be
+ // ACCEPTED rather than fallen through — `X_OK` is deliberately not paired
+ // with `R_OK`. Skipped as root, for whom access checks always pass.
+ it.runIf(process.platform !== "win32" && process.getuid?.() !== 0)(
+ "still accepts an execute-only binary",
+ () => {
+ const executableOnly = path.join(here, "exec-only-ffmpeg");
+ writeFileSync(executableOnly, "", { mode: 0o111 });
+ process.env.OPENSCREEN_FFMPEG_PATH = executableOnly;
+
+ expect(resolveFfmpeg(here)).toBe(executableOnly);
+ },
+ );
+ });
});
// Only runs where the binary is actually staged; skipped elsewhere rather than
diff --git a/electron/media/audioPeaks.ts b/electron/media/audioPeaks.ts
index 762f1a0fa..f1beabeb5 100644
--- a/electron/media/audioPeaks.ts
+++ b/electron/media/audioPeaks.ts
@@ -109,17 +109,21 @@ let cachedFfmpeg: string | null | undefined;
* later as `spawn … EACCES` — a message that blames permissions rather than
* saying the wrong candidate was chosen.
*
- * Every failure mode is swallowed on purpose. A candidate that is missing,
- * unreadable, or not executable is simply not this one; throwing out of
+ * Every failure mode is swallowed on purpose. A candidate that is absent, not a
+ * regular file, or not executable is simply not this one; throwing out of
* resolution would let a single bad path deny a later, working one.
+ *
+ * `X_OK` and not `X_OK | R_OK`: executing a binary needs the execute bit, not
+ * the read bit, so an install shipped `--x` is legitimate and must not be
+ * refused. On Windows `X_OK` is not enforced at all — there the `isFile` check
+ * is the whole guard, which is enough for the failure this exists to stop,
+ * since `ffmpegCandidates` only ever proposes `.exe` names of its own.
*/
function isExecutableFile(candidate: string): boolean {
try {
if (!statSync(candidate).isFile()) {
return false;
}
- // A no-op for the caller on Windows, where X_OK is not enforced — but the
- // `isFile` check above is what matters there anyway.
accessSync(candidate, fsConstants.X_OK);
return true;
} catch {