xray_setting_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. )
  7. func TestUnwrapXrayTemplateConfig(t *testing.T) {
  8. real := `{"log":{},"inbounds":[],"outbounds":[],"routing":{}}`
  9. t.Run("passes through a clean config", func(t *testing.T) {
  10. if got := UnwrapXrayTemplateConfig(real); got != real {
  11. t.Fatalf("clean config was modified: %s", got)
  12. }
  13. })
  14. t.Run("passes through invalid JSON unchanged", func(t *testing.T) {
  15. in := "not json at all"
  16. if got := UnwrapXrayTemplateConfig(in); got != in {
  17. t.Fatalf("invalid input was modified: %s", got)
  18. }
  19. })
  20. t.Run("unwraps one layer of response-shaped wrapper", func(t *testing.T) {
  21. wrapper := `{"inboundTags":["tag"],"outboundTestUrl":"x","xraySetting":` + real + `}`
  22. got := UnwrapXrayTemplateConfig(wrapper)
  23. if !equalJSON(t, got, real) {
  24. t.Fatalf("want %s, got %s", real, got)
  25. }
  26. })
  27. t.Run("unwraps multiple stacked layers", func(t *testing.T) {
  28. lvl1 := `{"xraySetting":` + real + `}`
  29. lvl2 := `{"xraySetting":` + lvl1 + `}`
  30. lvl3 := `{"xraySetting":` + lvl2 + `}`
  31. got := UnwrapXrayTemplateConfig(lvl3)
  32. if !equalJSON(t, got, real) {
  33. t.Fatalf("want %s, got %s", real, got)
  34. }
  35. })
  36. t.Run("handles an xraySetting stored as a JSON-encoded string", func(t *testing.T) {
  37. encoded, _ := json.Marshal(real) // becomes a quoted string
  38. wrapper := `{"xraySetting":` + string(encoded) + `}`
  39. got := UnwrapXrayTemplateConfig(wrapper)
  40. if !equalJSON(t, got, real) {
  41. t.Fatalf("want %s, got %s", real, got)
  42. }
  43. })
  44. t.Run("does not unwrap when top level already has real xray keys", func(t *testing.T) {
  45. // Pathological but defensible: if a user's actual config somehow
  46. // has both the real keys and an unrelated `xraySetting` key, we
  47. // must not strip it.
  48. in := `{"inbounds":[],"xraySetting":{"some":"thing"}}`
  49. got := UnwrapXrayTemplateConfig(in)
  50. if got != in {
  51. t.Fatalf("should have left real config alone, got %s", got)
  52. }
  53. })
  54. t.Run("stops at a reasonable depth", func(t *testing.T) {
  55. // Build a deeper-than-maxDepth chain that ends at something
  56. // non-wrapped, and confirm we end up at some valid JSON (we
  57. // don't loop forever and we don't blow the stack).
  58. s := real
  59. for i := 0; i < 16; i++ {
  60. s = `{"xraySetting":` + s + `}`
  61. }
  62. got := UnwrapXrayTemplateConfig(s)
  63. if !strings.Contains(got, `"inbounds"`) && !strings.Contains(got, `"xraySetting"`) {
  64. t.Fatalf("unexpected tail: %s", got)
  65. }
  66. })
  67. }
  68. func equalJSON(t *testing.T, a, b string) bool {
  69. t.Helper()
  70. var va, vb any
  71. if err := json.Unmarshal([]byte(a), &va); err != nil {
  72. return false
  73. }
  74. if err := json.Unmarshal([]byte(b), &vb); err != nil {
  75. return false
  76. }
  77. ja, _ := json.Marshal(va)
  78. jb, _ := json.Marshal(vb)
  79. return string(ja) == string(jb)
  80. }