sub_json_observatory_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package sub
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  5. )
  6. // Bad observatory settings (malformed JSON, non-URL destination, bad duration)
  7. // must not leak into the emitted burstObservatory — each falls back to the
  8. // built-in defaults instead of poisoning the client config.
  9. func TestSubJson_ObservatoryConfigInvalidValuesFallBack(t *testing.T) {
  10. seedSubDB(t)
  11. inb := seedSubInbound(t, "s1", "lp", 4821, 1, wsTLSStream)
  12. seedSubBalancer(t, &model.SubBalancer{
  13. Remark: "pinger", Strategy: "leastPing", InboundIds: []int{inb.Id}, SortOrder: 1, Enabled: true,
  14. })
  15. def := defaultSubBalancerObservatoryConfig()
  16. cases := []struct {
  17. name string
  18. cfg string
  19. }{
  20. {"bad json", `{not-json`},
  21. {"bad destination", `{"destination":"not-a-url"}`},
  22. {"bad interval", `{"interval":"xyz"}`},
  23. {"bad timeout", `{"timeout":"5x"}`},
  24. {"bad connectivity", `{"connectivity":"ftp://bad"}`},
  25. }
  26. for _, tc := range cases {
  27. t.Run(tc.name, func(t *testing.T) {
  28. js := NewSubJsonService("", "", "", NewSubService(""))
  29. js.SetObservatoryConfig(tc.cfg)
  30. out, _, err := js.GetJson("s1", "req.example.com", true)
  31. if err != nil {
  32. t.Fatalf("GetJson: %v", err)
  33. }
  34. ping := observatoryPingConfig(t, parseSubJsonDocs(t, out), "pinger")
  35. if ping["destination"] != def.Destination {
  36. t.Fatalf("destination = %v, want default %q (cfg=%s)", ping["destination"], def.Destination, tc.cfg)
  37. }
  38. if ping["interval"] != def.Interval {
  39. t.Fatalf("interval = %v, want default %q (cfg=%s)", ping["interval"], def.Interval, tc.cfg)
  40. }
  41. if ping["timeout"] != def.Timeout {
  42. t.Fatalf("timeout = %v, want default %q (cfg=%s)", ping["timeout"], def.Timeout, tc.cfg)
  43. }
  44. if ping["connectivity"] != def.Connectivity {
  45. t.Fatalf("connectivity = %v, want default %q (cfg=%s)", ping["connectivity"], def.Connectivity, tc.cfg)
  46. }
  47. })
  48. }
  49. }