|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as assert from "assert"; |
| 5 | +import * as childProcess from "child_process"; |
| 6 | +import { EventEmitter } from "events"; |
| 7 | +import * as path from "path"; |
| 8 | +import { PassThrough } from "stream"; |
| 9 | + |
| 10 | +import { getProcesses, getProcessTree } from "../src/processTree"; |
| 11 | + |
| 12 | +suite("Windows process enumeration", () => { |
| 13 | + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform")!; |
| 14 | + const spawnDescriptor = Object.getOwnPropertyDescriptor(childProcess, "spawn")!; |
| 15 | + let child: EventEmitter; |
| 16 | + let stdout: PassThrough; |
| 17 | + let stderr: PassThrough; |
| 18 | + let executable: string; |
| 19 | + let spawnArguments: readonly string[]; |
| 20 | + let options: childProcess.SpawnOptions; |
| 21 | + |
| 22 | + const javaProcess = { |
| 23 | + ProcessId: 101, |
| 24 | + ParentProcessId: 42, |
| 25 | + CommandLine: '"C:\\Program Files\\Java\\bin\\java.exe" -agentlib:jdwp=transport=dt_socket,server=y,address=5005 Main', |
| 26 | + CreationDate: 20261028164300, |
| 27 | + }; |
| 28 | + |
| 29 | + setup(() => { |
| 30 | + child = new EventEmitter(); |
| 31 | + stdout = new PassThrough(); |
| 32 | + stderr = new PassThrough(); |
| 33 | + Object.defineProperty(process, "platform", { value: "win32" }); |
| 34 | + Object.defineProperty(childProcess, "spawn", { |
| 35 | + value: (command: string, args: readonly string[], spawnOptions: childProcess.SpawnOptions) => { |
| 36 | + executable = command; |
| 37 | + spawnArguments = args; |
| 38 | + options = spawnOptions; |
| 39 | + return { stdout, stderr, on: child.on.bind(child) }; |
| 40 | + }, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + teardown(() => { |
| 45 | + Object.defineProperty(process, "platform", platformDescriptor); |
| 46 | + Object.defineProperty(childProcess, "spawn", spawnDescriptor); |
| 47 | + stdout.destroy(); |
| 48 | + stderr.destroy(); |
| 49 | + }); |
| 50 | + |
| 51 | + function complete(output: string): void { |
| 52 | + stdout.write(output); |
| 53 | + child.emit("close", 0, null); |
| 54 | + } |
| 55 | + |
| 56 | + test("uses system Windows PowerShell without profiles or a visible window", async () => { |
| 57 | + const pending = getProcesses(() => undefined); |
| 58 | + assert.strictEqual(executable, path.join(process.env.WINDIR || "C:\\Windows", |
| 59 | + "System32", "WindowsPowerShell", "v1.0", "powershell.exe")); |
| 60 | + assert.deepStrictEqual(spawnArguments.slice(0, 4), ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]); |
| 61 | + assert.ok(spawnArguments[4].includes("Get-CimInstance Win32_Process")); |
| 62 | + assert.ok(spawnArguments[4].includes("ConvertTo-Json -InputObject $processes -Compress")); |
| 63 | + assert.ok(spawnArguments[4].includes("$ErrorActionPreference = 'Stop'")); |
| 64 | + assert.ok(spawnArguments[4].includes("[Console]::OutputEncoding")); |
| 65 | + assert.deepStrictEqual(options, { windowsHide: true }); |
| 66 | + complete("[]"); |
| 67 | + await pending; |
| 68 | + }); |
| 69 | + |
| 70 | + test("preserves quoted executable paths, JDWP arguments, IDs and creation dates", async () => { |
| 71 | + const actual: unknown[][] = []; |
| 72 | + const pending = getProcesses((...entry) => actual.push(entry)); |
| 73 | + complete(JSON.stringify([javaProcess])); |
| 74 | + await pending; |
| 75 | + assert.deepStrictEqual(actual, [[101, 42, "C:\\Program Files\\Java\\bin\\java.exe", |
| 76 | + "-agentlib:jdwp=transport=dt_socket,server=y,address=5005 Main", 20261028164300]]); |
| 77 | + }); |
| 78 | + |
| 79 | + test("parses multiple processes and skips unavailable or empty command lines", async () => { |
| 80 | + const actual: unknown[][] = []; |
| 81 | + const pending = getProcesses((...entry) => actual.push(entry)); |
| 82 | + complete(JSON.stringify([ |
| 83 | + { ...javaProcess, CommandLine: null }, |
| 84 | + { ...javaProcess, CommandLine: " " }, |
| 85 | + { ...javaProcess, CommandLine: "C:\\Java\\bin\\java.exe\t Main", CreationDate: null }, |
| 86 | + { ...javaProcess, ProcessId: 102, CommandLine: "java.exe" }, |
| 87 | + { ...javaProcess, ProcessId: 103, CommandLine: '"C:\\Program Files\\Java\\bin\\java.exe"' }, |
| 88 | + ])); |
| 89 | + await pending; |
| 90 | + assert.deepStrictEqual(actual, [ |
| 91 | + [101, 42, "C:\\Java\\bin\\java.exe", "Main", undefined], |
| 92 | + [102, 42, "java.exe", "", 20261028164300], |
| 93 | + [103, 42, "C:\\Program Files\\Java\\bin\\java.exe", "", 20261028164300], |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + test("decodes chunked UTF-8 output without requiring a final newline", async () => { |
| 98 | + const commandLine = 'java.exe -Dmessage="\u4f60\u597d \\"quoted\\"" Main'; |
| 99 | + const output = Buffer.from(JSON.stringify([{ ...javaProcess, CommandLine: commandLine }]), "utf8"); |
| 100 | + const split = output.indexOf(Buffer.from("\u4f60", "utf8")) + 1; |
| 101 | + const actual: string[] = []; |
| 102 | + const pending = getProcesses((_pid, _ppid, _command, args) => actual.push(args)); |
| 103 | + stdout.write(output.slice(0, split)); |
| 104 | + stdout.write(output.slice(split)); |
| 105 | + child.emit("close", 0, null); |
| 106 | + await pending; |
| 107 | + assert.deepStrictEqual(actual, [commandLine.slice("java.exe ".length)]); |
| 108 | + }); |
| 109 | + |
| 110 | + test("accepts an empty process list", async () => { |
| 111 | + let count = 0; |
| 112 | + const pending = getProcesses(() => count++); |
| 113 | + complete("[]\r\n"); |
| 114 | + await pending; |
| 115 | + assert.strictEqual(count, 0); |
| 116 | + }); |
| 117 | + |
| 118 | + for (const output of ["", "not JSON", "{}", JSON.stringify([null]), |
| 119 | + JSON.stringify([{ ...javaProcess, ProcessId: "101" }]), |
| 120 | + JSON.stringify([{ ...javaProcess, CommandLine: 123 }])]) { |
| 121 | + test(`rejects invalid process output: ${output}`, async () => { |
| 122 | + const pending = getProcesses(() => undefined); |
| 123 | + complete(output); |
| 124 | + await assert.rejects(pending); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + test("rejects PowerShell startup failures", async () => { |
| 129 | + const pending = getProcesses(() => undefined); |
| 130 | + child.emit("error", new Error("PowerShell unavailable")); |
| 131 | + await assert.rejects(pending, /PowerShell unavailable/); |
| 132 | + }); |
| 133 | + |
| 134 | + test("rejects CIM query errors reported on stderr", async () => { |
| 135 | + const pending = getProcesses(() => undefined); |
| 136 | + stderr.write("Get-CimInstance: Access denied"); |
| 137 | + child.emit("close", 1, null); |
| 138 | + await assert.rejects(pending, /Access denied/); |
| 139 | + }); |
| 140 | + |
| 141 | + test("rejects a nonzero exit even when stdout contains valid JSON", async () => { |
| 142 | + let count = 0; |
| 143 | + const pending = getProcesses(() => count++); |
| 144 | + stdout.write(JSON.stringify([javaProcess])); |
| 145 | + child.emit("close", 1, null); |
| 146 | + await assert.rejects(pending, /exit code: 1/); |
| 147 | + assert.strictEqual(count, 0); |
| 148 | + }); |
| 149 | + |
| 150 | + test("builds the tree used to resolve an explicit process ID", async () => { |
| 151 | + const pending = getProcessTree(42); |
| 152 | + complete(JSON.stringify([ |
| 153 | + { ...javaProcess, ProcessId: 42, ParentProcessId: 0, CommandLine: "parent.exe" }, |
| 154 | + javaProcess, |
| 155 | + ])); |
| 156 | + const root = await pending; |
| 157 | + assert.strictEqual(root?.pid, 42); |
| 158 | + assert.strictEqual(root.children?.[0].pid, 101); |
| 159 | + assert.strictEqual(root.children?.[0].command, "C:\\Program Files\\Java\\bin\\java.exe"); |
| 160 | + assert.ok(root.children?.[0].args.includes("address=5005")); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +suite("Windows process enumeration integration", () => { |
| 165 | + test("enumerates the current process without WMIC", async function() { |
| 166 | + if (process.platform !== "win32") { |
| 167 | + this.skip(); |
| 168 | + } |
| 169 | + this.timeout(30000); |
| 170 | + const processes: number[] = []; |
| 171 | + await getProcesses((pid) => processes.push(pid)); |
| 172 | + assert.ok(processes.includes(process.pid)); |
| 173 | + const root = await getProcessTree(process.pid); |
| 174 | + assert.strictEqual(root?.pid, process.pid); |
| 175 | + assert.ok(root.command.length > 0); |
| 176 | + }); |
| 177 | +}); |
0 commit comments