# E2E-Smoke.ps1 - 发布前的基础功能端到端冒烟测试 # # 做什么: # 1. 启动 Debug 服务端(Bin\Yama_x64d.exe,读 Bin\settings.ini,默认端口 6543); # 2. 起一个测试客户端:默认用服务端内置的「运行测试程序」(菜单 ID_TOOL_RUN_TEST), # 加 -ExternalClient 则启动 x64\Debug\ghost.exe 127.0.0.1 6543; # 3. 等它在在线列表(IDC_ONLINE=1001)上线并选中; # 4. 按工具栏的命令 ID 逐个触发,断言每个命令在超时内弹出新的 #32770 对话框; # 5. 每一步之后检查服务端/客户端进程是否还活着——像「点视频管理客户端崩溃」 # 这类回归会直接判失败; # 6. 输出 PASS/FAIL 汇总表,任何一项失败则退出码为 1。 # # 用法: # powershell -File test\e2e\E2E-Smoke.ps1 # 全部功能 # powershell -File test\e2e\E2E-Smoke.ps1 -Only Video,File,Desktop # powershell -File test\e2e\E2E-Smoke.ps1 -Build # 先重新编译 Debug 服务端/客户端 # powershell -File test\e2e\E2E-Smoke.ps1 -ExternalClient # powershell -File test\e2e\E2E-Smoke.ps1 -RequireVideo # 有摄像头的机器上把视频弹窗设为必须 # # 注意: # - 不要同时运行两个 Debug 服务端;脚本开始前会检查 6543/6545/8080 是否被占用。 # - 正在运行的 Release 服务端(443/6544/9000)不受影响,脚本不会动它。 # - 所有中文输出都走 Unicode API / UTF-8 控制台,避免控制台代码页把标题显示成乱码。 param( [string]$ServerExe = "", [string]$ClientExe = "", [switch]$ExternalClient, [switch]$Build, [int]$ServerTimeoutSec = 90, [int]$ClientOnlineTimeoutSec = 60, [int]$DefaultTimeoutSec = 15, [string[]]$Only = @(), [switch]$RequireVideo, [switch]$KeepOpen ) $ErrorActionPreference = 'Stop' try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch { } $repo = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) # test\e2e -> repo if (-not $ServerExe) { $ServerExe = Join-Path $repo 'Bin\Yama_x64d.exe' } if (-not $ClientExe) { $ClientExe = Join-Path $repo 'x64\Debug\ghost.exe' } # ---------------------------------------------------------------- Win32 辅助 Add-Type -TypeDefinition @' using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; public static class E2E { public delegate bool EnumProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l); [DllImport("user32.dll")] static extern bool EnumChildWindows(IntPtr parent, EnumProc cb, IntPtr l); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid); [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetClassName(IntPtr h, StringBuilder sb, int max); [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowText(IntPtr h, StringBuilder sb, int max); [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h); [DllImport("user32.dll")] public static extern bool IsWindow(IntPtr h); [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr h, uint m, IntPtr w, IntPtr l); [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr h, uint m, IntPtr w, IntPtr l); [DllImport("user32.dll")] static extern int GetDlgCtrlID(IntPtr h); [DllImport("user32.dll")] static extern IntPtr GetMenu(IntPtr h); [DllImport("user32.dll")] static extern IntPtr GetWindow(IntPtr h, uint cmd); public static string ClassName(IntPtr h) { var sb = new StringBuilder(256); GetClassName(h, sb, sb.Capacity); return sb.ToString(); } public static string Text(IntPtr h) { var sb = new StringBuilder(2048); GetWindowText(h, sb, sb.Capacity); return sb.ToString(); } public static bool HasMenu(IntPtr h) { return GetMenu(h) != IntPtr.Zero; } // 某个进程的可视顶层对话框(#32770),排除主窗口本身。 public static IntPtr[] TopDialogs(uint pid, IntPtr exclude) { var list = new List(); EnumWindows((h, l) => { uint p; GetWindowThreadProcessId(h, out p); if (p != pid || h == exclude) return true; if (!IsWindowVisible(h)) return true; if (ClassName(h) != "#32770") return true; list.Add(h); return true; }, IntPtr.Zero); return list.ToArray(); } // 某个进程的所有可视顶层窗口(不限类名)。搜索栏这类不是 #32770 的自绘窗口 // 靠它判断「点击有反应」。 public static IntPtr[] TopWindows(uint pid, IntPtr exclude) { var list = new List(); EnumWindows((h, l) => { uint p; GetWindowThreadProcessId(h, out p); if (p != pid || h == exclude) return true; if (!IsWindowVisible(h)) return true; list.Add(h); return true; }, IntPtr.Zero); return list.ToArray(); } [DllImport("winmm.dll")] public static extern int waveInGetNumDevs(); // 对话框里的静态文本/按钮文字,失败时用来判断是不是错误提示框。 public static string ChildTexts(IntPtr dlg) { var sb = new StringBuilder(); EnumChildWindows(dlg, (h, l) => { string t = Text(h); if (!string.IsNullOrWhiteSpace(t)) { if (sb.Length > 0) sb.Append(" / "); sb.Append(t.Trim()); } return true; }, IntPtr.Zero); string s = sb.ToString(); if (s.Length > 300) s = s.Substring(0, 300) + "..."; return s; } public static IntPtr FindChild(IntPtr parent, string cls, int ctrlId) { IntPtr found = IntPtr.Zero; EnumChildWindows(parent, (h, l) => { if (ClassName(h) == cls && GetDlgCtrlID(h) == ctrlId) { found = h; return false; } return true; }, IntPtr.Zero); return found; } public static int SendInt(IntPtr h, uint msg, int w, int l) { return (int)SendMessage(h, msg, (IntPtr)w, (IntPtr)l); } public static void PostCommand(IntPtr main, int id) { PostMessage(main, 0x0111 /*WM_COMMAND*/, (IntPtr)id, IntPtr.Zero); } public static void Click(IntPtr h, int x, int y) { IntPtr lp = (IntPtr)((y << 16) | (x & 0xFFFF)); PostMessage(h, 0x0201 /*WM_LBUTTONDOWN*/, (IntPtr)1, lp); PostMessage(h, 0x0202 /*WM_LBUTTONUP*/, IntPtr.Zero, lp); } public static void Close(IntPtr h) { PostMessage(h, 0x0010 /*WM_CLOSE*/, IntPtr.Zero, IntPtr.Zero); } } '@ $WM_COMMAND = 0x0111 $LVM_GETITEMCOUNT = 0x1004 $LVM_GETITEMSTATE = 0x102C $LVIS_SELECTED = 0x0002 function Wait-Until([scriptblock]$Condition, [int]$TimeoutSec, [int]$IntervalMs = 250) { $deadline = (Get-Date).AddSeconds($TimeoutSec) while ((Get-Date) -lt $deadline) { try { if (& $Condition) { return $true } } catch { } Start-Sleep -Milliseconds $IntervalMs } return $false } function Test-Alive($proc) { if ($null -eq $proc) { return $false } try { return -not $proc.HasExited } catch { return $false } } function Get-ListeningPorts { try { return @(Get-NetTCPConnection -State Listen -ErrorAction Stop | Select-Object -ExpandProperty LocalPort) } catch { return @() } } # ---------------------------------------------------------------- 阶段 1:准备 Write-Host "==> E2E smoke test" -ForegroundColor Cyan Write-Host " server : $ServerExe" Write-Host " client : $(if ($ExternalClient) { $ClientExe } else { 'in-process (Run Test)' })" $running = @(Get-Process -Name 'Yama_x64d' -ErrorAction SilentlyContinue) if ($running.Count -gt 0) { throw "已有 Debug 服务端在运行(PID=$($running[0].Id))。请先关闭它,避免端口/实例冲突。" } if (-not (Test-Path $ServerExe)) { throw "找不到 Debug 服务端: $ServerExe(可加 -Build 先编译)" } if ($ExternalClient -and -not (Test-Path $ClientExe)) { throw "找不到 Debug 客户端: $ClientExe" } # 端口检查:6543 是客户端必须能连上的端口,被占用就直接失败。 # MCP/Web 端口(settings.ini 的 McpPort/WebSvrPort)如果已被别的实例占用(例如正在跑的 # Release 服务端也用 6545),临时在 settings.ini 里关掉它们,测试结束再恢复原文件。 $settingsPath = Join-Path (Split-Path $ServerExe) 'settings.ini' $settingsBackup = $null if (Test-Path $settingsPath) { $settingsBackup = [System.IO.File]::ReadAllText($settingsPath, [System.Text.Encoding]::Default) } function Restore-Settings { if ($script:settingsBackup -ne $null) { [System.IO.File]::WriteAllText($script:settingsPath, $script:settingsBackup, [System.Text.Encoding]::Default) } } function Set-IniValue([string]$text, [string]$key, [string]$value) { $pattern = '(?m)^' + [regex]::Escape($key) + '=.*$' if ($text -match $pattern) { return [regex]::Replace($text, $pattern, "$key=$value") } return $text } $ports = Get-ListeningPorts $udp = @() try { $udp = @(Get-NetUDPEndpoint -ErrorAction Stop | Select-Object -ExpandProperty LocalPort) } catch { } if (($ports -contains 6543) -or ($udp -contains 6543)) { throw "端口 6543 已被占用,无法启动隔离的 Debug 测试实例。" } if ($settingsBackup -ne $null) { $cfg = $settingsBackup if (($ports -contains 6545) -or ($udp -contains 6545)) { Write-Host " 6545 已被占用,本次测试临时关闭 Debug 实例的 MCP" -ForegroundColor DarkYellow $cfg = Set-IniValue $cfg 'McpEnabled' '0' } if ($ports -contains 8080) { Write-Host " 8080 已被占用,本次测试临时关闭 Debug 实例的 Web 控制台" -ForegroundColor DarkYellow $cfg = Set-IniValue $cfg 'WebSvrPort' '0' } if ($cfg -ne $settingsBackup) { [System.IO.File]::WriteAllText($settingsPath, $cfg, [System.Text.Encoding]::Default) } } if ($Build) { Write-Host "==> 编译 Debug 服务端/客户端 ..." -ForegroundColor Cyan & powershell -NoProfile -File (Join-Path $repo 'build.ps1') -Config Debug -Platform x64 | Out-Null & powershell -NoProfile -File (Join-Path $repo 'build.ps1') -Config Debug -Platform x64 -ClientOnly | Out-Null if ($LASTEXITCODE -ne 0) { throw "编译失败(退出码 $LASTEXITCODE)" } } $server = $null $client = $null $results = New-Object System.Collections.ArrayList function Add-Result($name, $status, $detail, $ms) { [void]$results.Add([pscustomobject]@{ Feature = $name; Status = $status; Detail = $detail; Ms = $ms }) $color = switch ($status) { 'PASS' { 'Green' } 'FAIL' { 'Red' } 'WARN' { 'Yellow' } 'SKIP' { 'DarkYellow' } default { 'Gray' } } Write-Host (" {0,-5} {1,-14} {2}" -f $status, $name, $detail) -ForegroundColor $color } try { # ------------------------------------------------------------ 阶段 2:启动服务端 Write-Host "==> 启动 Debug 服务端 ..." -ForegroundColor Cyan $server = Start-Process -FilePath $ServerExe -WorkingDirectory (Split-Path $ServerExe) -PassThru $main = [IntPtr]::Zero $ok = Wait-Until { $script:server.Refresh() if ($script:server.HasExited) { return $false } $h = $script:server.MainWindowHandle if ($h -eq [IntPtr]::Zero) { return $false } if (([E2E]::ClassName($h) -ne '#32770')) { return $false } $script:main = $h return $true } $ServerTimeoutSec if (-not $ok) { throw "Debug 服务端在 ${ServerTimeoutSec}s 内没有出现主窗口(是否弹了 UAC/登录框/端口错误框?)" } $list = [IntPtr]::Zero $ok = Wait-Until { $script:list = [E2E]::FindChild($script:main, 'SysListView32', 1001) return ($script:list -ne [IntPtr]::Zero) } 30 if (-not $ok) { throw "找不到在线列表控件(IDC_ONLINE=1001)" } # ------------------------------------------------------------ 阶段 3:客户端上线 if ($ExternalClient) { Write-Host "==> 启动外部 Debug 客户端 ..." -ForegroundColor Cyan $client = Start-Process -FilePath $ClientExe -ArgumentList @('127.0.0.1', '6543') ` -WorkingDirectory (Split-Path $ClientExe) -PassThru } else { Write-Host "==> 触发内置测试客户端(ID_TOOL_RUN_TEST)..." -ForegroundColor Cyan [E2E]::PostCommand($main, 33106) } $count = 0 $ok = Wait-Until { if ($script:server.HasExited) { throw '服务端进程已退出(客户端崩溃或被安全软件拦截)' } if ($ExternalClient -and $script:client.HasExited) { throw '外部客户端进程已退出' } $script:count = [E2E]::SendInt($script:list, $LVM_GETITEMCOUNT, 0, 0) return ($script:count -ge 1) } $ClientOnlineTimeoutSec if (-not $ok) { # 内置客户端失败时通常会弹一个消息框(例如内核 DLL 未就绪),把它的文字打出来。 $diag = @([E2E]::TopDialogs($server.Id, $main) | ForEach-Object { [E2E]::Text($_) } | Where-Object { $_ }) throw ("客户端 ${ClientOnlineTimeoutSec}s 内没有上线(在线列表 0 条)。弹窗: " + ($diag -join ' | ')) } Write-Host " 在线主机: $count" -ForegroundColor Green # 选中第一行;列表项需要先选中,工具栏命令才会对这台主机下发。 [E2E]::Click($list, 40, 40) $selected = Wait-Until { return ([E2E]::SendInt($script:list, $LVM_GETITEMSTATE, 0, $LVIS_SELECTED) -ne 0) } 5 if (-not $selected) { throw "在线列表第 1 行没有被选中(点击坐标/列表布局可能变了)" } # ------------------------------------------------------------ 阶段 4:逐项触发 # Id:工具栏/菜单命令 ID;Timeout:等弹窗的秒数;Expect:dialog / none $features = @( @{ Name = 'Terminal'; Id = 32776; Timeout = 20 }, @{ Name = 'Process'; Id = 32777; Timeout = 15 }, @{ Name = 'Window'; Id = 32778; Timeout = 15 }, @{ Name = 'Desktop'; Id = 32779; Timeout = 30 }, @{ Name = 'File'; Id = 32780; Timeout = 20 }, @{ Name = 'Audio'; Id = 32781; Timeout = 15; Requires = 'mic' }, @{ Name = 'Video'; Id = 32782; Timeout = 12; Optional = (-not $RequireVideo) }, @{ Name = 'Services'; Id = 32783; Timeout = 15; Requires = 'admin' }, @{ Name = 'Registry'; Id = 32784; Timeout = 15 }, @{ Name = 'Keylogger'; Id = 2117; Timeout = 15 }, @{ Name = 'Settings'; Id = 32794; Timeout = 10 }, @{ Name = 'BuildClient'; Id = 32786; Timeout = 15 }, @{ Name = 'Search'; Id = 32788; Timeout = 5; Expect = 'any' }, @{ Name = 'About'; Id = 32787; Timeout = 10 }, @{ Name = 'McpSettings'; Id = 33077; Timeout = 10 }, @{ Name = 'Connections'; Id = 33086; Timeout = 15 }, @{ Name = 'Talk'; Id = 33089; Timeout = 15 }, @{ Name = 'MachineManage'; Id = 32931; Timeout = 10 } ) # powershell -File 会把 "A,B" 原样作为单个字符串传进来,这里统一拆开 $Only = @($Only | ForEach-Object { $_ -split '[,;\s]+' } | Where-Object { $_ }) if ($Only.Count -gt 0) { $features = @($features | Where-Object { $Only -contains $_.Name }) if ($features.Count -eq 0) { throw " -Only 没有匹配到任何功能: $($Only -join ', ')" } } # 环境前提:没有录音设备时音频会话根本建立不起来;服务端未提权时服务枚举会失败。 # 这两种情况跳过而不是判 FAIL,避免冒烟脚本因为"机器没麦/没提权"天天红。 $hasMic = ([E2E]::waveInGetNumDevs() -gt 0) $isAdmin = (New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $hasMic) { Write-Host " 提示: 本机没有录音设备,Audio 将跳过" -ForegroundColor DarkYellow } if (-not $isAdmin) { Write-Host " 提示: 服务端未提权,Services 将跳过(管理员身份运行脚本可覆盖)" -ForegroundColor DarkYellow } foreach ($f in $features) { if ($f.Requires -eq 'mic' -and -not $hasMic) { Add-Result $f.Name 'SKIP' '本机没有录音设备(waveIn=0)' 0 continue } if ($f.Requires -eq 'admin' -and -not $isAdmin) { Add-Result $f.Name 'SKIP' '服务端未以管理员运行,服务枚举不可用' 0 continue } # 每条命令前先关掉可能残留的对话框,保证快照干净 foreach ($h in @([E2E]::TopDialogs($server.Id, $main))) { [E2E]::Close($h) } Start-Sleep -Milliseconds 300 if ($server.HasExited) { Add-Result $f.Name 'FAIL' '服务端进程已退出' 0; break } if ($ExternalClient -and $client.HasExited) { Add-Result $f.Name 'FAIL' '客户端进程已退出' 0; break } $expectAny = ($f.Expect -eq 'any') $snapshot = { if ($expectAny) { [E2E]::TopWindows($script:server.Id, $script:main) } else { [E2E]::TopDialogs($script:server.Id, $script:main) } } $before = @(& $snapshot | ForEach-Object { $_.ToInt64() }) $sw = [System.Diagnostics.Stopwatch]::StartNew() [E2E]::PostCommand($main, $f.Id) $timeout = if ($f.Timeout) { $f.Timeout } else { $DefaultTimeoutSec } $newDlg = [IntPtr]::Zero $found = Wait-Until { if ($script:server.HasExited) { return $true } $cur = @(& $snapshot) foreach ($h in $cur) { if ($before -notcontains $h.ToInt64()) { $script:newDlg = $h; return $true } } return $false } $timeout $sw.Stop() # 进程存活检查:进程内客户端模式下客户端崩溃会带走服务端 $crashed = $false if ($server.HasExited) { $crashed = $true } if ($ExternalClient -and $client.HasExited) { $crashed = $true } if ($crashed) { Add-Result $f.Name 'FAIL' "进程已退出($($sw.ElapsedMilliseconds)ms)" $sw.ElapsedMilliseconds; break } if ($f.Expect -eq 'none') { if ($found -and $newDlg -ne [IntPtr]::Zero) { $title = [E2E]::Text($newDlg) Add-Result $f.Name 'FAIL' "不应弹窗,却弹出了「$title」" $sw.ElapsedMilliseconds [E2E]::Close($newDlg) } else { Add-Result $f.Name 'PASS' '无弹窗,符合预期' $sw.ElapsedMilliseconds } continue } if ($found -and $newDlg -ne [IntPtr]::Zero) { $title = [E2E]::Text($newDlg) if (-not $title) { $title = '(无标题)' } # 提示/错误框也算"弹了窗",但它不是功能对话框:单独标 WARN 并把文字带出来, # 让报告里能直接看到「该功能不可用」之类的原因,而不是伪装成 PASS。 $suspicious = $title -match '(?i)^(notice|warning|error|提示|警告|错误|失败)$' if ($suspicious) { $child = [E2E]::ChildTexts($newDlg) Add-Result $f.Name 'WARN' "弹出提示框「$title」: $child" $sw.ElapsedMilliseconds } else { Add-Result $f.Name 'PASS' "弹窗「$title」" $sw.ElapsedMilliseconds } [E2E]::Close($newDlg) # 等窗口真正关掉,避免它挡住下一条命令 [void](Wait-Until { return -not [E2E]::IsWindow($newDlg) } 5) } elseif ($f.Optional) { Add-Result $f.Name 'SKIP' "超时未弹窗(${timeout}s);本项可选(视频需要摄像头)" $sw.ElapsedMilliseconds } else { Add-Result $f.Name 'FAIL' "超时 ${timeout}s 未弹出对话框(点击无反应?)" $sw.ElapsedMilliseconds } } } catch { Write-Host "`n[ERROR] $($_.Exception.Message)" -ForegroundColor Red foreach ($h in @([E2E]::TopDialogs($server.Id, $main) | Where-Object { $_ })) { $t = [E2E]::Text($h); if ($t) { Write-Host " 弹窗: $t" -ForegroundColor Red } } Add-Result 'Startup' 'FAIL' $_.Exception.Message 0 } finally { if (-not $KeepOpen) { Write-Host "==> 清理测试实例 ..." -ForegroundColor Cyan try { if ($main -ne [IntPtr]::Zero) { [E2E]::Close($main) } } catch { } Start-Sleep -Seconds 2 try { if (Test-Alive $server) { $server.Kill() } } catch { } try { if ($ExternalClient -and (Test-Alive $client)) { $client.Kill() } } catch { } # 服务端退出后会回写 settings.ini,恢复必须在进程结束之后 Start-Sleep -Milliseconds 300 Restore-Settings } else { Write-Host "==> -KeepOpen:测试实例保持运行(settings.ini 的临时修改请在关闭后手动恢复)" -ForegroundColor Yellow } } # ---------------------------------------------------------------- 阶段 5:汇总 Write-Host "" Write-Host "===== 结果汇总 =====" -ForegroundColor Cyan $results | Format-Table Feature, Status, Detail, Ms -AutoSize | Out-String | Write-Host $failed = @($results | Where-Object { $_.Status -eq 'FAIL' }) $warned = @($results | Where-Object { $_.Status -eq 'WARN' }) $skipped = @($results | Where-Object { $_.Status -eq 'SKIP' }) if ($warned.Count -gt 0) { Write-Host "WARN: $($warned.Count) 项(弹出了提示/错误框,请人工确认)" -ForegroundColor Yellow } if ($skipped.Count -gt 0) { Write-Host "SKIP: $($skipped.Count) 项(环境不具备或可选功能)" -ForegroundColor DarkYellow } if ($failed.Count -gt 0) { Write-Host "FAILED: $($failed.Count) 项" -ForegroundColor Red exit 1 } Write-Host "ALL PASSED" -ForegroundColor Green exit 0