1
0

remote_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package runtime
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/database/model"
  6. )
  7. // cacheGetTag must resolve a remote inbound id even when the n<id>- prefix
  8. // sits on only one side: the node may store the bare tag while the central
  9. // panel pushes the prefixed form, or vice versa. Without this a mismatch makes
  10. // the push create a duplicate inbound on the node.
  11. func TestCacheGetTag_PrefixAgnostic(t *testing.T) {
  12. cases := []struct {
  13. name string
  14. cacheTag string
  15. lookup string
  16. wantID int
  17. wantFound bool
  18. }{
  19. {"exact", "n1-in-443-tcp", "n1-in-443-tcp", 7, true},
  20. {"node bare, lookup prefixed", "in-443-tcp", "n1-in-443-tcp", 7, true},
  21. {"node prefixed, lookup bare", "n1-in-443-tcp", "in-443-tcp", 7, true},
  22. {"unrelated tag", "in-443-tcp", "in-999-tcp", 0, false},
  23. }
  24. for _, c := range cases {
  25. t.Run(c.name, func(t *testing.T) {
  26. r := NewRemote(&model.Node{Id: 1, Name: "n1"})
  27. r.cacheSet(c.cacheTag, 7)
  28. id, ok := r.cacheGetTag(c.lookup)
  29. if ok != c.wantFound || id != c.wantID {
  30. t.Fatalf("cacheGetTag(%q) = (%d, %v), want (%d, %v)", c.lookup, id, ok, c.wantID, c.wantFound)
  31. }
  32. })
  33. }
  34. }
  35. func TestSanitizeStreamSettingsForRemote(t *testing.T) {
  36. tests := []struct {
  37. name string
  38. input string
  39. // wantCertFile / wantKeyFile: expected presence after sanitize
  40. wantCertFile bool
  41. wantKeyFile bool
  42. }{
  43. {
  44. name: "file paths only — kept intact (remote node paths)",
  45. input: `{
  46. "tlsSettings": {
  47. "certificates": [{
  48. "certificateFile": "/etc/ssl/cert.crt",
  49. "keyFile": "/etc/ssl/key.key"
  50. }]
  51. }
  52. }`,
  53. wantCertFile: true,
  54. wantKeyFile: true,
  55. },
  56. {
  57. name: "inline content only — unchanged",
  58. input: `{
  59. "tlsSettings": {
  60. "certificates": [{
  61. "certificate": ["-----BEGIN CERTIFICATE-----"],
  62. "key": ["-----BEGIN PRIVATE KEY-----"]
  63. }]
  64. }
  65. }`,
  66. wantCertFile: false,
  67. wantKeyFile: false,
  68. },
  69. {
  70. name: "both file paths and inline content — file paths stripped (redundant)",
  71. input: `{
  72. "tlsSettings": {
  73. "certificates": [{
  74. "certificateFile": "/etc/ssl/cert.crt",
  75. "keyFile": "/etc/ssl/key.key",
  76. "certificate": ["-----BEGIN CERTIFICATE-----"],
  77. "key": ["-----BEGIN PRIVATE KEY-----"]
  78. }]
  79. }
  80. }`,
  81. wantCertFile: false,
  82. wantKeyFile: false,
  83. },
  84. {
  85. name: "empty stream settings",
  86. input: "",
  87. // empty input returns empty, nothing to check
  88. },
  89. }
  90. for _, tc := range tests {
  91. t.Run(tc.name, func(t *testing.T) {
  92. if tc.input == "" {
  93. if got := sanitizeStreamSettingsForRemote(tc.input); got != "" {
  94. t.Errorf("expected empty string, got %q", got)
  95. }
  96. return
  97. }
  98. got := sanitizeStreamSettingsForRemote(tc.input)
  99. var out map[string]any
  100. if err := json.Unmarshal([]byte(got), &out); err != nil {
  101. t.Fatalf("output is not valid JSON: %v\noutput: %s", err, got)
  102. }
  103. tls, _ := out["tlsSettings"].(map[string]any)
  104. certs, _ := tls["certificates"].([]any)
  105. if len(certs) == 0 {
  106. t.Fatal("certificates array missing in output")
  107. }
  108. cert, _ := certs[0].(map[string]any)
  109. _, hasCertFile := cert["certificateFile"]
  110. _, hasKeyFile := cert["keyFile"]
  111. if hasCertFile != tc.wantCertFile {
  112. t.Errorf("certificateFile present=%v, want %v", hasCertFile, tc.wantCertFile)
  113. }
  114. if hasKeyFile != tc.wantKeyFile {
  115. t.Errorf("keyFile present=%v, want %v", hasKeyFile, tc.wantKeyFile)
  116. }
  117. })
  118. }
  119. }