Cotton code
46 lines · 2.2 KB · TypeScriptRaw
/** * A COTTON APP INSTALLS BESIDE ANY OTHER: each Play app is signed with its own key, so a `<permission>` that two * Cotton apps both declare installs in whichever comes first and refuses the second * (INSTALL_FAILED_DUPLICATE_PERMISSION, which Play shows as "Can't install"). 10136 shipped exactly that: the main * manifest declared com.cottondb.permission.VOICE, so Messages would not install beside Books. * * The shared manifest (src/main) declares no permission at all, no flavour declares one, and * no manifest uses one that no Cotton app may hold: under per-app keys a signature permission reaches no sibling, so * which apps are Cotton's is the shell's CottonSigners (the registry's certificates), never a permission. */import { existsSync, readdirSync, readFileSync } from "node:fs";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");const SRC = join(REPO_ROOT, "apps", "android", "app", "src");function declared(manifest: string): string[] {  const xml = readFileSync(manifest, "utf8").replace(/<!--[\s\S]*?-->/g, "");  return [...xml.matchAll(/<permission\b[^>]*?android:name="([^"]+)"/g)].map((m) => m[1]);}const manifests = readdirSync(SRC)  .map((set) => ({ set, path: join(SRC, set, "AndroidManifest.xml") }))  .filter(({ path }) => existsSync(path));describe("android permissions", () => {  it("finds the shared manifest and the flavours' own", () => {    expect(manifests.map((m) => m.set)).toContain("main");    expect(manifests.map((m) => m.set)).toContain("voice");  });  it("the shared manifest declares no permission", () => {    expect(declared(join(SRC, "main", "AndroidManifest.xml"))).toEqual([]);  });  it("no manifest declares or uses a permission shared between Cotton apps", () => {    const shared = manifests.flatMap(({ set, path }) => {      const xml = readFileSync(path, "utf8").replace(/<!--[\s\S]*?-->/g, "");      const used = [...xml.matchAll(/<uses-permission\b[^>]*?android:name="(com\.cottondb\.[^"]+)"/g)].map((m) => m[1]);      return [...declared(path), ...used].map((name) => `${set}: ${name}`);    });    expect(shared).toEqual([]);  });});