applicationIcons.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { describe, expect, it, vi } from "vitest";
  2. vi.mock("electron", () => ({ app: { getFileIcon: vi.fn() } }));
  3. import {
  4. createApplicationIconLoader,
  5. resolveDarwinApplicationPath,
  6. } from "../src/main/applicationIcons.js";
  7. import { createWindowsAumidIconReader } from "../src/main/windowsAumidIcon.js";
  8. function nativeImage(dataUrl: string) {
  9. return { isEmpty: () => false, toDataURL: () => dataUrl };
  10. }
  11. describe("application icon locators", () => {
  12. it("reads AUMID icons from the Windows AppsFolder Shell namespace", async () => {
  13. const execute = vi.fn(async () => Buffer.from("png").toString("base64"));
  14. const readIcon = createWindowsAumidIconReader({
  15. execute,
  16. systemRoot: "C:\\Windows",
  17. });
  18. await expect(readIcon("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")).resolves.toEqual({
  19. iconDataUrl: "data:image/png;base64,cG5n",
  20. });
  21. const [command, args, timeout] = execute.mock.calls[0]!;
  22. expect(command).toBe("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
  23. expect(timeout).toBe(5_000);
  24. const encodedCommand = Buffer.from(args[4]!, "base64").toString("utf16le");
  25. expect(encodedCommand).toContain("SHCreateItemInKnownFolder");
  26. expect(encodedCommand).toContain("1e87508d-89c2-42f0-8a7e-645a0f50ca58");
  27. expect(encodedCommand).not.toContain("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
  28. });
  29. it("按大小写不敏感解析 darwin bundle id,Spotlight 与兜底索引口径一致", async () => {
  30. // CUA producer 的 appKey 是 `darwin:<bundleId.toLowerCase()>`,而 Info.plist 里是
  31. // `com.apple.Notes`。Spotlight 查询本来就带 `c` 后缀(大小写不敏感),兜底索引若保持精确
  32. // 匹配,就只在 Spotlight 不可用的机器上取不到 CUA 工具卡的 App 图标。
  33. const listDirectory = vi.fn(async (path: string) =>
  34. path === "/Applications" ? ["Notes.app", "NotAnApp.txt"] : [],
  35. );
  36. const execute = vi.fn(async (command: string, args: readonly string[]) => {
  37. if (command === "/usr/bin/mdfind") throw new Error("spotlight disabled");
  38. expect(command).toBe("/usr/libexec/PlistBuddy");
  39. expect(args.at(-1)).toBe("/Applications/Notes.app/Contents/Info.plist");
  40. return "com.apple.Notes\n";
  41. });
  42. const dependencies = {
  43. execute,
  44. listDirectory,
  45. homeDirectory: "/Users/tester",
  46. now: () => 0,
  47. };
  48. await expect(resolveDarwinApplicationPath("com.apple.notes", dependencies)).resolves.toBe(
  49. "/Applications/Notes.app",
  50. );
  51. // 原始大小写同样命中,不能为了兼容小写反过来把旧输入判错。
  52. await expect(resolveDarwinApplicationPath("com.apple.Notes", dependencies)).resolves.toBe(
  53. "/Applications/Notes.app",
  54. );
  55. // 不存在的 bundle id 仍返回 null,而不是退化成任意一个已索引应用。
  56. await expect(
  57. resolveDarwinApplicationPath("com.example.missing", dependencies),
  58. ).resolves.toBeNull();
  59. });
  60. it("reads a normal Windows executable with Electron getFileIcon", async () => {
  61. const getFileIcon = vi.fn(async () => nativeImage("data:image/png;base64,EXE"));
  62. const load = createApplicationIconLoader({
  63. platform: "win32",
  64. getFileIcon,
  65. resolveDarwinApplicationPath: vi.fn(),
  66. });
  67. await expect(
  68. load({
  69. locators: [
  70. { kind: "windows-executable-path", value: "C:\\Windows\\System32\\notepad.exe" },
  71. ],
  72. }),
  73. ).resolves.toEqual({ iconDataUrl: "data:image/png;base64,EXE" });
  74. expect(getFileIcon).toHaveBeenCalledWith("C:\\Windows\\System32\\notepad.exe", {
  75. size: "normal",
  76. });
  77. });
  78. it("rejects UNC and device executable paths before Electron file access", async () => {
  79. const getFileIcon = vi.fn(async () => nativeImage("data:image/png;base64,REMOTE"));
  80. const load = createApplicationIconLoader({
  81. platform: "win32",
  82. getFileIcon,
  83. resolveDarwinApplicationPath: vi.fn(),
  84. });
  85. for (const request of [
  86. {
  87. locators: [
  88. { kind: "windows-executable-path" as const, value: "\\\\attacker\\share\\a.exe" },
  89. ],
  90. },
  91. {
  92. locators: [
  93. { kind: "windows-executable-path" as const, value: "\\\\?\\C:\\Windows\\a.exe" },
  94. ],
  95. },
  96. "\\\\attacker\\share\\a.exe",
  97. "C:\\Windows\\System32\\notepad.exe",
  98. ]) {
  99. await expect(load(request)).resolves.toBeNull();
  100. }
  101. expect(getFileIcon).not.toHaveBeenCalled();
  102. });
  103. it("uses the Windows AppsFolder Shell identity for an AUMID", async () => {
  104. const getFileIcon = vi.fn(async () => nativeImage("data:image/png;base64,UWP"));
  105. const readWindowsAumidIcon = vi.fn(async () => ({
  106. iconDataUrl: "data:image/png;base64,UWP",
  107. }));
  108. const load = createApplicationIconLoader({
  109. platform: "win32",
  110. getFileIcon,
  111. resolveDarwinApplicationPath: vi.fn(),
  112. readWindowsAumidIcon,
  113. });
  114. await load({
  115. locators: [
  116. {
  117. kind: "windows-aumid",
  118. value: "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App",
  119. },
  120. {
  121. kind: "windows-executable-path",
  122. value: "C:\\Windows\\System32\\ApplicationFrameHost.exe",
  123. },
  124. ],
  125. });
  126. expect(readWindowsAumidIcon).toHaveBeenCalledWith(
  127. "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App",
  128. );
  129. expect(getFileIcon).not.toHaveBeenCalled();
  130. });
  131. it("rejects ApplicationFrameHost when no AUMID was authorized", async () => {
  132. const getFileIcon = vi.fn();
  133. const load = createApplicationIconLoader({
  134. platform: "win32",
  135. getFileIcon,
  136. resolveDarwinApplicationPath: vi.fn(),
  137. });
  138. await expect(
  139. load({
  140. locators: [
  141. {
  142. kind: "windows-executable-path",
  143. value: "C:\\Windows\\System32\\ApplicationFrameHost.exe",
  144. },
  145. ],
  146. }),
  147. ).resolves.toBeNull();
  148. expect(getFileIcon).not.toHaveBeenCalled();
  149. });
  150. });