Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,15 @@ protocol version, for example the one in the SDK's `platform-tools`. Nothing
is reported when no server is listening on that port. Advisory only, like the
findings above — there is no `--fix` for it.

`doctor` also reports a `driver-advisory` finding (code
`device-profile-hidden`) for each profile in your Android `devices.xml` whose
name one of Simlock's own Android profiles answers to, in the file's order. The
message names the `devices.xml` profile and the Simlock profile that is used
instead; Simlock reads its own profile first, so the one in `devices.xml` is
never used. An SDK profile that a Simlock profile hides is not reported, only
a `devices.xml` one. Rename the profile in `devices.xml` to use it. Advisory
only — there is no `--fix` for it.

`doctor` also checks the [prerequisites](#prerequisites) of each platform
this host can run (iOS only on macOS), every time it runs. Each one missing is
a `prerequisite-missing` finding with `platform`, `prerequisite` (for example
Expand Down
26 changes: 26 additions & 0 deletions src/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,32 @@ describe("CLI: daemon status (ADR 0003 §11)", () => {
expect(JSON.parse(output.stdout)).toEqual({ findings });
});

it("simlock doctor prints a driver-advisory finding to stderr as `Warning [<platform>] <code>: <message>`", async () => {
const output = outputCapture();
const findings = [
{
code: "device-profile-hidden",
kind: "driver-advisory" as const,
message: '"Pixel 9" in devices.xml is not used',
platform: "android" as const,
},
];

await expect(
runCli(
["doctor"],
output.environmentWith({
connectAdmin: async () =>
fakeClient({ runDoctor: () => Promise.resolve({ findings } as never) }),
}),
),
).resolves.toBe(0);

expect(output.stderr.split("\n")).toContain(
'Warning [android] device-profile-hidden: "Pixel 9" in devices.xml is not used',
);
});

it("simlock doctor writes agent-device fail <message>; <remedy> on stderr, one line of at most 500 characters per part, after the advisory, prerequisite and warm-pool lines, prints the report on stdout unchanged, and exits 0", async () => {
const output = outputCapture();
const findings = [
Expand Down
23 changes: 22 additions & 1 deletion src/drivers/android/device-profile-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ function matchProfile(
);
}

interface HiddenUserProfile {
readonly userName: string;
readonly libraryName: string;
}

export async function hiddenUserProfiles(
library: DeviceProfileSource,
user: DeviceProfileSource,
): Promise<readonly HiddenUserProfile[]> {
const libraryProfiles = await library.profiles();
const hidden: HiddenUserProfile[] = [];
for (const profile of await user.profiles()) {
const shadow = profile.names
.map((name) => matchProfile(libraryProfiles, name))
.find((match) => match !== undefined);
if (shadow !== undefined) {
hidden.push({ libraryName: shadow.name, userName: profile.name });
}
}
return hidden;
}

/** Today's behavior, refactored behind `DeviceProfileSource`: resolves against `avdmanager list device`. */
export class BuiltinDeviceProfileSource implements DeviceProfileSource {
readonly #avdmanager: string;
Expand All @@ -240,7 +262,6 @@ export class BuiltinDeviceProfileSource implements DeviceProfileSource {
* profiles come before custom ones, so a real SDK profile wins a name clash with a
* devices.xml one whatever order `avdmanager` prints them in.
*/
// fallow-ignore-next-line unused-class-member -- reached through the DeviceProfileSource port by DeviceProfileRegistry.
async profiles(): Promise<readonly DeviceProfile[]> {
const profiles = (await this.#avdmanagerProfiles()).map(
(profile): DeviceProfile => ({
Expand Down
127 changes: 127 additions & 0 deletions src/drivers/android/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,133 @@ describe("AndroidDriver", () => {
expect(device?.mark).toBeUndefined();
});

describe("device-profile-hidden advisories", () => {
const hiddenMessage = (xmlName: string, libraryName: string) =>
`"${xmlName}" in devices.xml is not used: Simlock's own ${libraryName} profile answers to that name`;

// why: the fake adb server answers no version, so the physical handler adds its own advisory.
async function hiddenProfileAdvisories(driver: AndroidDriver) {
return (await driver.advisories()).filter(
(advisory) => advisory.code === "device-profile-hidden",
);
}

async function advisoriesFor(devicesXml: string | undefined, listing = pixelDevices) {
const filesystem = await androidFilesystem();
if (devicesXml !== undefined) await writeDevicesXml(filesystem, devicesXml);
const runner = new ScriptedProcessRunner([
processResult(binaries.avdmanager, ["list", "device"], listing),
]);
const driver = await createDriver(filesystem, runner);
return { advisories: await hiddenProfileAdvisories(driver), runner };
}

it("reports `device-profile-hidden` for a `devices.xml` profile named like a library profile, with the message naming both", async () => {
const { advisories } = await advisoriesFor(customDeviceXml("Pixel 9", 4096));

expect(advisories).toEqual([
{ code: "device-profile-hidden", message: hiddenMessage("Pixel 9", "Pixel 9") },
]);
});

it("reports one matching a library profile's other name, in any letter case, naming the library profile's display name", async () => {
const { advisories } = await advisoriesFor(
customDeviceXml("pixel_9", 4096) + customDeviceXml("PIXEL 9", 4096),
);

expect(advisories).toEqual([
{ code: "device-profile-hidden", message: hiddenMessage("pixel_9", "Pixel 9") },
{ code: "device-profile-hidden", message: hiddenMessage("PIXEL 9", "Pixel 9") },
]);
});

it("reports two hidden profiles as two advisories, in `devices.xml` order", async () => {
const { advisories } = await advisoriesFor(
customDeviceXml("Pixel 9", 4096) + customDeviceXml("Pixel 8", 4096),
);

expect(advisories.map((advisory) => advisory.message)).toEqual([
hiddenMessage("Pixel 9", "Pixel 9"),
hiddenMessage("Pixel 8", "Pixel 8"),
]);
});

it("reports nothing for a `devices.xml` profile no library profile answers to", async () => {
const { advisories } = await advisoriesFor(customDeviceXml("My Tablet", 4096));

expect(advisories).toEqual([]);
});

it("reports nothing for a library name the SDK also lists", async () => {
const sdkListing = `Available devices:\nid: 0 or "pixel_9"\n Name: Pixel 9\n OEM : Google\n`;

const { advisories } = await advisoriesFor(undefined, sdkListing);

expect(advisories).toEqual([]);
});

it("reports one advisory per profile when `avdmanager list device` also lists it with `OEM : User`, and runs no `avdmanager`", async () => {
const userListing = `Available devices:\nid: 0 or "Pixel 9"\n Name: Pixel 9\n OEM : User\n`;

const { advisories, runner } = await advisoriesFor(
customDeviceXml("Pixel 9", 4096),
userListing,
);

expect(advisories).toEqual([
{ code: "device-profile-hidden", message: hiddenMessage("Pixel 9", "Pixel 9") },
]);
expect(runner.calls.map((call) => call.command)).not.toContain(binaries.avdmanager);
});

it("reports nothing when `devices.xml` is missing or unreadable", async () => {
const missing = await advisoriesFor(undefined);
const filesystem = await androidFilesystem();
await filesystem.mkdirp(`${home}/.android`);
await filesystem.writeFileAtomic(`${home}/.android/devices.xml`, "not xml at all {{{");
const driver = await createDriver(filesystem, new ScriptedProcessRunner([]));

expect(missing.advisories).toEqual([]);
expect(await hiddenProfileAdvisories(driver)).toEqual([]);
});

it("keeps the physical handler's advisories, before the new ones", async () => {
const base = await androidFilesystem();
await writeDevicesXml(base, customDeviceXml("Pixel 9", 4096));
let broken = false;
const filesystem = new Proxy(base, {
get(target, property) {
if (property === "stat" && broken) {
return async () => {
throw new Error("EACCES");
};
}
const value = Reflect.get(target, property) as unknown;
return typeof value === "function" ? (value as () => unknown).bind(target) : value;
},
});
const driver = await createDriver(filesystem, new ScriptedProcessRunner([]));
broken = true;

const advisories = await driver.advisories();

expect(advisories.map((advisory) => advisory.code)).toEqual([
"adb-protocol-mismatch",
"device-profile-hidden",
]);
});

it("reports no hidden profiles for a driver built with injected `deviceProfileSources`", async () => {
const filesystem = await androidFilesystem();
await writeDevicesXml(filesystem, customDeviceXml("Pixel 9", 4096));
const driver = await createDriver(filesystem, new ScriptedProcessRunner([]), {
deviceProfileSources: [],
});

expect(await hiddenProfileAdvisories(driver)).toEqual([]);
});
});

describe("device-profile sources", () => {
it("resolves a devices.xml-only model and applies its properties to config.ini before the config hash is captured", async () => {
const filesystem = await androidFilesystem();
Expand Down
39 changes: 31 additions & 8 deletions src/drivers/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { isAndroidDriverData, type AndroidDriverData } from "./data.js";
import { AndroidPhysicalHandler, PHYSICAL_RECLAIM_ESTIMATE_MS } from "./physical-handler.js";
import {
BuiltinDeviceProfileSource,
hiddenUserProfiles,
containsForbiddenCharacter,
DeviceProfileRegistry,
parseAvdmanagerDeviceProfiles,
Expand Down Expand Up @@ -462,6 +463,7 @@ export class AndroidDriver implements Driver {
readonly #acceptAndroidLicenses: boolean;
readonly #clock: Clock;
readonly #deviceProfiles: DeviceProfileRegistry;
readonly #profileSources: ReturnType<typeof defaultDeviceProfileSources> | undefined;
readonly #devices = new Map<string, DeviceState>();
readonly #deviceRoot: string;
readonly #filesystem: Filesystem;
Expand Down Expand Up @@ -529,9 +531,14 @@ export class AndroidDriver implements Driver {
this.#legacyAvdHome =
options.env.ANDROID_AVD_HOME ?? join(options.homeDirectory, ".android", "avd");
this.#portAllocator = portAllocatorFor(options.processRunner, sdk.adb);
const defaults =
options.deviceProfileSources === undefined
? defaultDeviceProfileSources(options, sdk, this.#onDiagnostic, () => this.#env())
: undefined;
this.#profileSources = defaults;
this.#deviceProfiles = new DeviceProfileRegistry(
options.deviceProfileSources ??
defaultDeviceProfileSources(options, sdk, this.#onDiagnostic, () => this.#env()),
(defaults === undefined ? [] : [defaults.library, defaults.builtin, defaults.user]),
);
}

Expand Down Expand Up @@ -1069,7 +1076,19 @@ export class AndroidDriver implements Driver {
}

async advisories(): Promise<readonly DriverAdvisory[]> {
return this.#physical.advisories();
const physical = await this.#physical.advisories();
if (this.#profileSources === undefined) return physical;
const hidden = await hiddenUserProfiles(
this.#profileSources.library,
this.#profileSources.user,
);
return [
...physical,
...hidden.map(({ libraryName, userName }) => ({
code: "device-profile-hidden",
message: `"${userName}" in devices.xml is not used: Simlock's own ${libraryName} profile answers to that name`,
})),
];
}

async listManaged(): Promise<DriverReality> {
Expand Down Expand Up @@ -2566,18 +2585,22 @@ function defaultDeviceProfileSources(
sdk: AndroidSdkPaths,
onDiagnostic: ((diagnostic: AndroidDriverDiagnostic) => void) | undefined,
env: () => NodeJS.ProcessEnv,
): readonly DeviceProfileSource[] {
): {
readonly library: DeviceProfileSource;
readonly builtin: DeviceProfileSource;
readonly user: DeviceProfileSource;
} {
const { ANDROID_PREFS_ROOT, ANDROID_SDK_HOME, ANDROID_USER_HOME } = options.env;
const devicesXmlPath =
ANDROID_USER_HOME !== undefined
? `${ANDROID_USER_HOME}/devices.xml`
: `${ANDROID_PREFS_ROOT ?? ANDROID_SDK_HOME ?? options.homeDirectory}/.android/devices.xml`;
return [
new LibraryDeviceProfileSource(),
return {
library: new LibraryDeviceProfileSource(),
// Scoped like every other call: an unscoped `avdmanager` would read the user's `~/.android`.
new BuiltinDeviceProfileSource(sdk.avdmanager, options.processRunner, env),
new UserDeviceProfileSource(devicesXmlPath, options.filesystem, onDiagnostic),
];
builtin: new BuiltinDeviceProfileSource(sdk.avdmanager, options.processRunner, env),
user: new UserDeviceProfileSource(devicesXmlPath, options.filesystem, onDiagnostic),
};
}

function portAllocatorFor(processRunner: ProcessRunner, adb: string): PortAllocator {
Expand Down
Loading