3x-ui/util/random/random_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

63 lines
1.4 KiB
Go

package random
import (
"encoding/base64"
"testing"
)
func TestSeq_LengthAndAlphabet(t *testing.T) {
for _, n := range []int{0, 1, 8, 64, 256} {
s := Seq(n)
if len(s) != n {
t.Fatalf("Seq(%d) returned length %d", n, len(s))
}
for i, r := range s {
isDigit := r >= '0' && r <= '9'
isLower := r >= 'a' && r <= 'z'
isUpper := r >= 'A' && r <= 'Z'
if !(isDigit || isLower || isUpper) {
t.Fatalf("Seq(%d) byte %d = %q is not alphanumeric", n, i, r)
}
}
}
}
func TestSeq_NotConstant(t *testing.T) {
a := Seq(32)
b := Seq(32)
if a == b {
t.Fatalf("two consecutive Seq(32) calls produced identical output: %q", a)
}
}
func TestNum_InRange(t *testing.T) {
for _, upper := range []int{1, 2, 10, 1000} {
for i := 0; i < 200; i++ {
v := Num(upper)
if v < 0 || v >= upper {
t.Fatalf("Num(%d) returned %d, out of [0, %d)", upper, v, upper)
}
}
}
}
func TestBase64Bytes_DecodesToRequestedSize(t *testing.T) {
for _, n := range []int{1, 16, 32, 64} {
out := Base64Bytes(n)
decoded, err := base64.StdEncoding.DecodeString(out)
if err != nil {
t.Fatalf("Base64Bytes(%d) produced invalid base64 %q: %v", n, out, err)
}
if len(decoded) != n {
t.Fatalf("Base64Bytes(%d) decoded to %d bytes", n, len(decoded))
}
}
}
func TestBase64Bytes_Random(t *testing.T) {
a := Base64Bytes(32)
b := Base64Bytes(32)
if a == b {
t.Fatalf("two consecutive Base64Bytes(32) calls produced identical output: %q", a)
}
}