1
0

phone_service_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //go:build !service
  2. // phone_service_test.go — 「手机」工具纯函数单元测试。
  3. package main
  4. import (
  5. "encoding/base64"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "reflect"
  10. "strings"
  11. "testing"
  12. )
  13. // TestParseADBDevices 验证 adb devices 输出的解析:
  14. // 跳过 daemon 启动行、表头与空行,正确提取序列号与状态。
  15. func TestParseADBDevices(t *testing.T) {
  16. out := "* daemon not running; starting now at tcp:5037\n" +
  17. "* daemon started successfully\n" +
  18. "List of devices attached\n" +
  19. "0123456789ABCDEF\tdevice\n" +
  20. "emulator-5554\tdevice\n" +
  21. "ABCDEF\tunauthorized\n"
  22. got := parseADBDevices(out)
  23. want := []PhoneDevice{
  24. {Serial: "0123456789ABCDEF", State: "device"},
  25. {Serial: "emulator-5554", State: "device"},
  26. {Serial: "ABCDEF", State: "unauthorized"},
  27. }
  28. if !reflect.DeepEqual(got, want) {
  29. t.Fatalf("parseADBDevices() = %+v, want %+v", got, want)
  30. }
  31. }
  32. // TestParseADBDevicesEmpty 验证空输出/无设备时返回空列表。
  33. func TestParseADBDevicesEmpty(t *testing.T) {
  34. for _, out := range []string{
  35. "",
  36. "List of devices attached\n",
  37. "\n\n",
  38. "* daemon started successfully\n",
  39. } {
  40. if got := parseADBDevices(out); len(got) != 0 {
  41. t.Fatalf("parseADBDevices(%q) = %+v, want empty", out, got)
  42. }
  43. }
  44. }
  45. // TestParseADBVersion 验证从 adb version 输出提取版本行。
  46. func TestParseADBVersion(t *testing.T) {
  47. out := "Android Debug Bridge version 1.0.41\nVersion 35.0.2-12742879\nInstalled as C:\\adb\\adb.exe\n"
  48. got := parseADBVersion(out)
  49. want := "Android Debug Bridge version 1.0.41"
  50. if got != want {
  51. t.Fatalf("parseADBVersion() = %q, want %q", got, want)
  52. }
  53. }
  54. // TestParseADBVersionFallback 验证无版本行时返回整体 trim 后输出。
  55. func TestParseADBVersionFallback(t *testing.T) {
  56. out := " some output \n"
  57. if got := parseADBVersion(out); got != "some output" {
  58. t.Fatalf("parseADBVersion() = %q, want %q", got, "some output")
  59. }
  60. }
  61. // TestScreenshotDataURI 验证 PNG 字节编码为 base64 data URI。
  62. func TestScreenshotDataURI(t *testing.T) {
  63. png := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A}
  64. want := "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
  65. if got := screenshotDataURI(png); got != want {
  66. t.Fatalf("screenshotDataURI() = %q, want %q", got, want)
  67. }
  68. }
  69. // TestDecodeScreenshotPNG 验证截图 data URI 解析:合法输入还原字节,
  70. // 非法前缀/非法 base64/空内容均返回错误。
  71. func TestDecodeScreenshotPNG(t *testing.T) {
  72. png := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A}
  73. uri := screenshotDataURI(png)
  74. got, err := decodeScreenshotPNG(uri)
  75. if err != nil {
  76. t.Fatalf("decodeScreenshotPNG(%q) 错误: %v", uri, err)
  77. }
  78. if !reflect.DeepEqual(got, png) {
  79. t.Fatalf("decodeScreenshotPNG() = %v, want %v", got, png)
  80. }
  81. // 非法前缀
  82. if _, err := decodeScreenshotPNG("data:image/jpeg;base64,AAAA"); err == nil {
  83. t.Fatal("非 PNG data URI 应返回错误")
  84. }
  85. // 非法 base64
  86. if _, err := decodeScreenshotPNG("data:image/png;base64,@@@"); err == nil {
  87. t.Fatal("非法 base64 应返回错误")
  88. }
  89. // 空内容
  90. if _, err := decodeScreenshotPNG("data:image/png;base64,"); err == nil {
  91. t.Fatal("空内容应返回错误")
  92. }
  93. }
  94. // TestParseClipboardServiceOutput 验证 `service call clipboard 2` 输出解析:
  95. // 拼接单引号内可打印 ASCII 片段(忽略不可打印字节/引号外文本)。
  96. func TestParseClipboardServiceOutput(t *testing.T) {
  97. out := "Result: Parcel(00000000 00000000 00000000 00000000 ' h e l l o' 00000000)\n" +
  98. "00000012 00000000 ' w o r l d '"
  99. got := parseClipboardServiceOutput(out)
  100. // 片段内所有可打印字符被拼接(含空格),非字母数字也保留
  101. if got != " h e l l o w o r l d " {
  102. t.Fatalf("parseClipboardServiceOutput() = %q, want %q", got, " h e l l o w o r l d ")
  103. }
  104. // 无单引号(空剪贴板)返回空串
  105. if v := parseClipboardServiceOutput("Result: Parcel(00000000)"); v != "" {
  106. t.Fatalf("空输出应返回空串,got %q", v)
  107. }
  108. }
  109. // TestClipboardServiceString 验证剪贴板写入字符串编码:转义内部单引号/反斜杠/双引号,
  110. // 并截断超长文本(binder 事务安全上限 1000 字符)。
  111. func TestClipboardServiceString(t *testing.T) {
  112. s := clipboardServiceString("it's a \"test\" \\ ok")
  113. want := `'it\'s a \"test\" \\ ok'`
  114. if s != want {
  115. t.Fatalf("clipboardServiceString() = %q, want %q", s, want)
  116. }
  117. // 超长截断
  118. long := strings.Repeat("a", 1500)
  119. if got := clipboardServiceString(long); len(got) != 1002 { // 1000 + 2 个引号
  120. t.Fatalf("超长文本未截断到 1000 字符,长度 = %d", len(got))
  121. }
  122. }
  123. // TestFingerprintDevices 验证设备集合指纹:空集为空串,非空集稳定且区分状态。
  124. func TestFingerprintDevices(t *testing.T) {
  125. if got := fingerprintDevices(nil); got != "" {
  126. t.Fatalf("fingerprintDevices(nil) = %q, want empty", got)
  127. }
  128. devs := []PhoneDevice{{Serial: "A", State: "device"}, {Serial: "B", State: "unauthorized"}}
  129. fp := fingerprintDevices(devs)
  130. if fp != "A=device;B=unauthorized;" {
  131. t.Fatalf("fingerprintDevices() = %q, want %q", fp, "A=device;B=unauthorized;")
  132. }
  133. // 状态变化应产生不同指纹(去抖依赖此差异)
  134. changed := []PhoneDevice{{Serial: "A", State: "device"}, {Serial: "B", State: "device"}}
  135. if fingerprintDevices(changed) == fp {
  136. t.Fatalf("fingerprintDevices() 应区分不同状态")
  137. }
  138. }
  139. // TestProbeInstalledADBSubdir 模拟 zip 解压出 platform-tools/ 子目录(含 adb.exe),
  140. // 验证 probeInstalled 能识别 adb 特判(候选名与顶层 .exe 扫描都无法命中该子目录)。
  141. func TestProbeInstalledADBSubdir(t *testing.T) {
  142. dir := t.TempDir()
  143. sub := filepath.Join(dir, "platform-tools")
  144. if err := os.MkdirAll(sub, 0o755); err != nil {
  145. t.Fatal(err)
  146. }
  147. if err := os.WriteFile(filepath.Join(sub, "adb.exe"), []byte("MZ"), 0o755); err != nil {
  148. t.Fatal(err)
  149. }
  150. // 构造 adb 资源条目(与 defaultResources 中一致)
  151. var item ResourceItem
  152. for _, it := range defaultResources() {
  153. if it.Name == "adb" {
  154. item = it
  155. break
  156. }
  157. }
  158. if item.Name != "adb" {
  159. t.Fatal("defaultResources 中未找到 adb 条目")
  160. }
  161. if got := resourceSubdir(&item); got != "platform-tools" {
  162. t.Fatalf("resourceSubdir(adb) = %q, want platform-tools", got)
  163. }
  164. installed, ver, path := probeInstalled(item, dir)
  165. if !installed {
  166. t.Fatal("probeInstalled 未识别 platform-tools 子目录为已安装")
  167. }
  168. if ver != item.Version {
  169. t.Fatalf("probeInstalled version = %q, want %q", ver, item.Version)
  170. }
  171. if path != sub {
  172. t.Fatalf("probeInstalled path = %q, want %q", path, sub)
  173. }
  174. }
  175. // TestFindADBPathFallback 验证方案2:进程 PATH 不含 adb(模拟旧进程快照)时,
  176. // findADBPath 兜底从注册表用户级+系统级 PATH 命中 adb,Available() 因此可用。
  177. // 这是「PowerShell 能跑 adb 但程序搜不到」的根因回归测试。
  178. func TestFindADBPathFallback(t *testing.T) {
  179. orig := os.Getenv("Path")
  180. defer os.Setenv("Path", orig)
  181. // 模拟进程启动早于 adb 加入 PATH:进程 PATH 无任何 adb 目录
  182. os.Setenv("Path", `C:\Windows\System32;C:\Windows`)
  183. // 注册表 PATH 也无 adb 时跳过(无 adb 的机器上不误报)
  184. if _, err := exec.LookPath("adb"); err == nil {
  185. t.Fatal("干净 PATH 下 LookPath 不应命中(测试前提失效)")
  186. }
  187. path := findADBPath()
  188. if path == "" {
  189. t.Skip("注册表用户/系统 PATH 无 adb,跳过兜底断言")
  190. }
  191. t.Logf("兜底命中:%s", path)
  192. info := NewPhoneService().Available()
  193. if !info.Available {
  194. t.Fatalf("进程 PATH 无 adb 时 Available() 应兜底可用,得到 %+v", info)
  195. }
  196. if info.Path != path {
  197. t.Fatalf("Available() 路径 = %q, want %q", info.Path, path)
  198. }
  199. t.Logf("Available() 兜底成功:path=%s version=%q", info.Path, info.Version)
  200. }