request_identity_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package openai
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestPairCodexClientIdentity(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. ua string
  11. wantOriginator string
  12. wantUA string
  13. wantOK bool
  14. }{
  15. {
  16. name: "cli 首段直接配对",
  17. ua: "codex_cli_rs/0.144.1 (Ubuntu 22.4.0; x86_64) xterm-256color",
  18. wantOriginator: "codex_cli_rs",
  19. wantUA: "codex_cli_rs/0.144.1 (Ubuntu 22.4.0; x86_64) xterm-256color",
  20. wantOK: true,
  21. },
  22. {
  23. name: "tui 首段直接配对",
  24. ua: "codex-tui/0.140.2 (Mac OS X 14.0; arm64) iTerm (codex-tui; 0.140.2)",
  25. wantOriginator: "codex-tui",
  26. wantUA: "codex-tui/0.140.2 (Mac OS X 14.0; arm64) iTerm (codex-tui; 0.140.2)",
  27. wantOK: true,
  28. },
  29. {
  30. name: "Codex 家族前缀配对保留原大小写",
  31. ua: "Codex Desktop/1.2.3",
  32. wantOriginator: "Codex Desktop",
  33. wantUA: "Codex Desktop/1.2.3",
  34. wantOK: true,
  35. },
  36. {
  37. name: "originator override 用尾部 name 重写首段",
  38. ua: "cccc/0.142.0 (Ubuntu 22.4.0; x86_64) screen (codex-tui; 0.142.0)",
  39. wantOriginator: "codex-tui",
  40. wantUA: "codex-tui/0.142.0 (Ubuntu 22.4.0; x86_64) screen (codex-tui; 0.142.0)",
  41. wantOK: true,
  42. },
  43. {
  44. name: "override 尾部恢复保留 Codex 家族真实大小写",
  45. ua: "cccc/1.2.3 (Ubuntu 22.4.0; x86_64) term (Codex Desktop; 1.2.3)",
  46. wantOriginator: "Codex Desktop",
  47. wantUA: "Codex Desktop/1.2.3 (Ubuntu 22.4.0; x86_64) term (Codex Desktop; 1.2.3)",
  48. wantOK: true,
  49. },
  50. {name: "含斜杠的尾部 name 拒绝配对(防自不一致身份)", ua: "foo/1.0 (Codex Desktop/2; 1.0)", wantOK: false},
  51. {
  52. name: "精确集合大小写变体归一为规范小写",
  53. ua: "CODEX_CLI_RS/1.0.0",
  54. wantOriginator: "codex_cli_rs",
  55. wantUA: "codex_cli_rs/1.0.0",
  56. wantOK: true,
  57. },
  58. {
  59. name: "首段尾随空格重建为规范 UA",
  60. ua: "codex-tui /1.0.0",
  61. wantOriginator: "codex-tui",
  62. wantUA: "codex-tui/1.0.0",
  63. wantOK: true,
  64. },
  65. {name: "家族前缀夹带不可打印字节拒绝", ua: "Codex \x01evil/1.0.0", wantOK: false},
  66. {name: "家族前缀夹带非 ASCII 字节拒绝", ua: "Codex \xc3\xa9vil/1.0.0", wantOK: false},
  67. {name: "超长首段拒绝", ua: "Codex " + strings.Repeat("a", 80) + "/1.0.0", wantOK: false},
  68. {name: "第三方 UA 不可配对", ua: "luna/1.0.0", wantOK: false},
  69. {name: "伪造前缀不可配对", ua: "codex_cli_rs_evil/1.0.0", wantOK: false},
  70. {name: "浏览器 UA 不可配对", ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36", wantOK: false},
  71. {name: "无斜杠不可配对", ua: "curl", wantOK: false},
  72. {name: "空 UA 不可配对", ua: "", wantOK: false},
  73. }
  74. for _, tt := range tests {
  75. t.Run(tt.name, func(t *testing.T) {
  76. originator, pairedUA, ok := PairCodexClientIdentity(tt.ua)
  77. require.Equal(t, tt.wantOK, ok)
  78. require.Equal(t, tt.wantOriginator, originator)
  79. require.Equal(t, tt.wantUA, pairedUA)
  80. })
  81. }
  82. }