3x-ui/sub/links_test.go
MHSanaei 106adca414
test: cover crypto, random, netsafe, sub helpers, xray equals, websocket hub, node service
Adds ~110 unit tests across previously untested packages. Focus on
pure-logic and concurrency surfaces where regressions would silently
affect users:

- util/crypto, util/random: password hashing round-trip, ss2022 key
  generation, alphabet/length invariants.
- util/netsafe: IsBlockedIP edge cases, NormalizeHost validation,
  SSRF guard with AllowPrivate context bypass.
- util/common, util/json_util: traffic formatter, Combine nil-skip,
  RawMessage empty-as-null and copy-on-unmarshal.
- sub: splitLinkLines, searchKey/searchHost, kcp share fields,
  finalmask normalization, buildVmessLink round-trip.
- xray: Config.Equals and InboundConfig.Equals field-by-field,
  getRequiredUserString/getOptionalUserString type checks.
- web/websocket: hub registration, throttling, slow-client eviction,
  nil-receiver safety, concurrent register/unregister.
- web/service: NodeService.normalize validation, normalizeBasePath,
  HeartbeatPatch.ToUI mapping.
- web/job: atomicBool concurrent set/takeAndReset semantics.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 10:00:09 +02:00

40 lines
1.1 KiB
Go

package sub
import (
"reflect"
"testing"
)
func TestSplitLinkLines(t *testing.T) {
cases := []struct {
name string
in string
want []string
}{
{"single_line", "vless://abc", []string{"vless://abc"}},
{"two_lines", "vless://abc\nvmess://xyz", []string{"vless://abc", "vmess://xyz"}},
{"trims_each_line", " vless://abc \n\tvmess://xyz\t", []string{"vless://abc", "vmess://xyz"}},
{"skips_blank_lines", "vless://abc\n\n\nvmess://xyz\n", []string{"vless://abc", "vmess://xyz"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := splitLinkLines(c.in)
if !reflect.DeepEqual(got, c.want) {
t.Fatalf("splitLinkLines(%q) = %#v, want %#v", c.in, got, c.want)
}
})
}
}
func TestSplitLinkLines_EmptyInputIsNil(t *testing.T) {
if got := splitLinkLines(""); got != nil {
t.Fatalf("splitLinkLines(\"\") = %#v, want nil", got)
}
}
func TestSplitLinkLines_WhitespaceOnlyHasNoEntries(t *testing.T) {
got := splitLinkLines(" \n\t \n")
if len(got) != 0 {
t.Fatalf("splitLinkLines(whitespace) = %#v, want empty slice", got)
}
}