stream_network_alias_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. // TestCanonicalizeStreamNetworkKey covers xray-core v26.7.11's "method" alias
  7. // for streamSettings "network": a config keyed on "method" (from an imported
  8. // or API-authored inbound) must be folded back to the panel-canonical
  9. // "network" key that every downstream reader — link generation, port-conflict
  10. // detection, flow eligibility — depends on.
  11. func TestCanonicalizeStreamNetworkKey(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. in string
  15. wantNetwork string
  16. wantMethod bool
  17. }{
  18. {
  19. name: "method alias becomes network",
  20. in: `{"method": "ws", "security": "tls"}`,
  21. wantNetwork: "ws",
  22. },
  23. {
  24. name: "method wins when both present",
  25. in: `{"method": "grpc", "network": "tcp"}`,
  26. wantNetwork: "grpc",
  27. },
  28. {
  29. name: "plain network untouched",
  30. in: `{"network": "tcp"}`,
  31. wantNetwork: "tcp",
  32. },
  33. }
  34. for _, tc := range tests {
  35. t.Run(tc.name, func(t *testing.T) {
  36. got := canonicalizeStreamNetworkKey(tc.in)
  37. var stream map[string]any
  38. if err := json.Unmarshal([]byte(got), &stream); err != nil {
  39. t.Fatalf("result is not valid JSON: %v", err)
  40. }
  41. if stream["network"] != tc.wantNetwork {
  42. t.Fatalf("network = %v, want %q", stream["network"], tc.wantNetwork)
  43. }
  44. if _, hasMethod := stream["method"]; hasMethod {
  45. t.Fatalf("method key must be removed, got %s", got)
  46. }
  47. })
  48. }
  49. }
  50. func TestCanonicalizeStreamNetworkKey_EmptyPassthrough(t *testing.T) {
  51. if got := canonicalizeStreamNetworkKey(""); got != "" {
  52. t.Fatalf("empty stream must round-trip, got %q", got)
  53. }
  54. }