allowed_client_freeform_test.go 1.1 KB

1234567891011121314151617181920212223242526
  1. package openai
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestMatchClientEntries_WhitelistAND(t *testing.T) {
  7. wl := []AllowedClientEntry{{Originator: "opencode", UAContains: []string{"opencode/"}}}
  8. require.True(t, MatchClientEntries("opencode/1.2 (x)", "opencode", wl))
  9. require.False(t, MatchClientEntries("opencode/1.2 (x)", "other", wl), "originator 不符不放")
  10. require.False(t, MatchClientEntries("curl/8", "opencode", wl), "UA marker 缺失不放")
  11. require.False(t, MatchClientEntries("opencode/1.2", "opencode", []AllowedClientEntry{{Originator: "opencode"}}), "空 UAContains 安全失败")
  12. }
  13. func TestDenyEntries_BlacklistOR(t *testing.T) {
  14. bl := []AllowedClientEntry{
  15. {Originator: "evilbot"},
  16. {UAContains: []string{"badscan/"}},
  17. }
  18. require.True(t, MatchDenyEntries("anything/1", "evilbot", bl), "originator 命中即拒")
  19. require.True(t, MatchDenyEntries("badscan/9 (x)", "whatever", bl), "UA marker 命中即拒")
  20. require.False(t, MatchDenyEntries("codex_cli_rs/0.141.0", "codex_cli_rs", bl), "都不命中不拒")
  21. require.False(t, MatchDenyEntries("x", "y", []AllowedClientEntry{{}}), "全空条目安全忽略")
  22. }