probe_testability_gate_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package outbound
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. // A direct, DNS, loopback or blackhole outbound is not a tunnel, so the probe
  10. // rejects it whatever the spelling: the core resolves "Freedom" to freedom.
  11. func TestTestOutboundsRejectsCaseVariantUntestableIDs(t *testing.T) {
  12. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. w.WriteHeader(http.StatusNoContent)
  14. }))
  15. defer srv.Close()
  16. withStubProcess(t, func(cfg *xray.Config, configPath string) batchProcess {
  17. return &stubProcess{cfg: cfg, serveSocks: true}
  18. })
  19. withEgressTraceProbe(t, func(*url.URL) *TestEgressResult {
  20. return &TestEgressResult{IPv4: "198.51.100.2", Country: "ZZ", Warp: "off"}
  21. })
  22. tests := []struct {
  23. name string
  24. protocol string
  25. wantErr string
  26. }{
  27. {"freedom", "freedom", "Direct/DNS outbound cannot be tested"},
  28. {"Freedom", "Freedom", "Direct/DNS outbound cannot be tested"},
  29. {"FREEDOM", "FREEDOM", "Direct/DNS outbound cannot be tested"},
  30. {"DNS", "DNS", "Direct/DNS outbound cannot be tested"},
  31. {"Blackhole", "Blackhole", "Blocked/blackhole outbound cannot be tested"},
  32. {"Loopback", "Loopback", "Loopback outbound cannot be tested"},
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. batch := mustJSON(t, []any{map[string]any{"tag": "probe", "protocol": tt.protocol}})
  37. results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "http")
  38. if err != nil {
  39. t.Fatalf("TestOutbounds: %v", err)
  40. }
  41. r := results[0]
  42. if r.Success || r.Error != tt.wantErr {
  43. t.Errorf("%q = success=%v err=%q egress=%+v, want the rejection %q",
  44. tt.protocol, r.Success, r.Error, r.Egress, tt.wantErr)
  45. }
  46. })
  47. }
  48. }