clash_yaml_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package sub
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/goccy/go-yaml"
  6. )
  7. // The encoder's own parser reads every one of these back as a string, so a
  8. // round-trip through it proves nothing. What matters is the emitted text: a
  9. // plain scalar that the YAML resolution rules turn into a number, bool, null
  10. // or date is what breaks a Clash core, so those must carry quotes.
  11. func TestAmbiguousScalarsAreQuoted(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. value string
  15. mustQuote bool
  16. }{
  17. {"reality short-id read as a float", "2351e1", true},
  18. {"short exponent form", "0e1", true},
  19. {"long exponent form", "12e34", true},
  20. {"all digits", "123456", true},
  21. {"leading zeros read as octal", "0177", true},
  22. {"hex form", "0x1f", true},
  23. {"decimal point", "1.5", true},
  24. {"boolean word", "true", true},
  25. {"single letter bool", "y", true},
  26. {"null word", "null", true},
  27. {"tilde", "~", true},
  28. {"date", "2026-07-27", true},
  29. {"sexagesimal", "12:30", true},
  30. {"plain hex with letters", "6ba7b8", false},
  31. {"letters only", "abcdef", false},
  32. {"hostname", "example.com", false},
  33. {"dotted quad", "1.2.3.4", false},
  34. {"uuid", "6ba7b810-9dad-11d1-80b4-00c04fd430c8", false},
  35. {"alpn token", "h2", false},
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. out, err := marshalClashYAML(map[string]any{
  40. "reality-opts": map[string]any{"short-id": tt.value},
  41. })
  42. if err != nil {
  43. t.Fatalf("marshalClashYAML: %v", err)
  44. }
  45. got := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(string(out)), "reality-opts:"))
  46. quoted := strings.Contains(got, "'"+tt.value+"'") || strings.Contains(got, `"`+tt.value+`"`)
  47. if tt.mustQuote && !quoted {
  48. t.Errorf("%q must be emitted quoted, got %q", tt.value, got)
  49. }
  50. if !tt.mustQuote && quoted {
  51. t.Errorf("%q needs no quoting, got %q", tt.value, got)
  52. }
  53. // Whatever the quoting decision, the document must still parse.
  54. var decoded map[string]any
  55. if err := yaml.Unmarshal(out, &decoded); err != nil {
  56. t.Fatalf("output must stay parseable: %v\n%s", err, out)
  57. }
  58. })
  59. }
  60. }
  61. // The value in the report: unquoted, YAML reads 2351e1 as 23510, mihomo hex-
  62. // decodes an odd-length "23510" and the provider drops to zero nodes (#6104).
  63. func TestClashShortIDIsQuotedInOutput(t *testing.T) {
  64. out, err := marshalClashYAML(map[string]any{
  65. "proxies": []any{map[string]any{
  66. "name": "de-1",
  67. "reality-opts": map[string]any{"short-id": "2351e1"},
  68. }},
  69. })
  70. if err != nil {
  71. t.Fatalf("marshalClashYAML: %v", err)
  72. }
  73. if !strings.Contains(string(out), `'2351e1'`) {
  74. t.Errorf("short-id must be emitted as a quoted scalar, got:\n%s", out)
  75. }
  76. }
  77. // A password or pre-shared key of the same shape reaches the output the same
  78. // way, so the guard is not specific to short-id.
  79. func TestAmbiguousPasswordIsQuoted(t *testing.T) {
  80. out, err := marshalClashYAML(map[string]any{
  81. "proxies": []any{map[string]any{
  82. "name": "de-1",
  83. "password": "80e12",
  84. "obfs-password": "12345",
  85. "pre-shared-key": "9e9",
  86. }},
  87. })
  88. if err != nil {
  89. t.Fatalf("marshalClashYAML: %v", err)
  90. }
  91. for _, want := range []string{`'80e12'`, `'12345'`, `'9e9'`} {
  92. if !strings.Contains(string(out), want) {
  93. t.Errorf("expected %s in output:\n%s", want, out)
  94. }
  95. }
  96. }
  97. // Values that are unambiguous must not pick up noise quoting, so the emitted
  98. // document stays byte-identical to what the encoder produced before.
  99. func TestUnambiguousScalarsAreUnchanged(t *testing.T) {
  100. config := map[string]any{
  101. "port": 7890,
  102. "mode": "rule",
  103. "proxies": []any{map[string]any{"name": "de-1", "udp": true, "port": 443}},
  104. "rules": []string{"MATCH,PROXY"},
  105. "alpn": []string{"h2", "http/1.1"},
  106. "nonempty": "example.com",
  107. }
  108. quoted, err := marshalClashYAML(config)
  109. if err != nil {
  110. t.Fatalf("marshalClashYAML: %v", err)
  111. }
  112. plain, err := yaml.Marshal(config)
  113. if err != nil {
  114. t.Fatalf("yaml.Marshal: %v", err)
  115. }
  116. if string(quoted) != string(plain) {
  117. t.Errorf("unambiguous document changed:\n--- got ---\n%s\n--- want ---\n%s", quoted, plain)
  118. }
  119. }
  120. // A quote inside the value must not terminate the scalar.
  121. func TestQuotedScalarEscapesQuotes(t *testing.T) {
  122. out, err := marshalClashYAML(map[string]any{"password": "1e2'3"})
  123. if err != nil {
  124. t.Fatalf("marshalClashYAML: %v", err)
  125. }
  126. var decoded map[string]any
  127. if err := yaml.Unmarshal(out, &decoded); err != nil {
  128. t.Fatalf("output must stay parseable: %v\n%s", err, out)
  129. }
  130. if got := decoded["password"]; got != any("1e2'3") {
  131. t.Errorf("password round-tripped to %#v\n%s", got, out)
  132. }
  133. }