3x-ui/xray/api_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

82 lines
2.2 KiB
Go

package xray
import (
"strings"
"testing"
)
func TestGetRequiredUserString_Present(t *testing.T) {
user := map[string]any{"email": "alice@example.com"}
got, err := getRequiredUserString(user, "email")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "alice@example.com" {
t.Fatalf("got %q, want %q", got, "alice@example.com")
}
}
func TestGetRequiredUserString_Missing(t *testing.T) {
user := map[string]any{}
if _, err := getRequiredUserString(user, "email"); err == nil {
t.Fatal("expected error for missing key")
}
}
func TestGetRequiredUserString_NilValue(t *testing.T) {
user := map[string]any{"email": nil}
if _, err := getRequiredUserString(user, "email"); err == nil {
t.Fatal("expected error for nil value")
}
}
func TestGetRequiredUserString_WrongType(t *testing.T) {
user := map[string]any{"email": 42}
_, err := getRequiredUserString(user, "email")
if err == nil {
t.Fatal("expected error for non-string value")
}
if !strings.Contains(err.Error(), "invalid type") {
t.Fatalf("expected %q in error, got: %v", "invalid type", err)
}
}
func TestGetOptionalUserString_Present(t *testing.T) {
user := map[string]any{"flow": "xtls-rprx-vision"}
got, err := getOptionalUserString(user, "flow")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "xtls-rprx-vision" {
t.Fatalf("got %q, want %q", got, "xtls-rprx-vision")
}
}
func TestGetOptionalUserString_MissingReturnsEmptyNoError(t *testing.T) {
user := map[string]any{}
got, err := getOptionalUserString(user, "flow")
if err != nil {
t.Fatalf("unexpected error for missing optional field: %v", err)
}
if got != "" {
t.Fatalf("got %q, want empty string", got)
}
}
func TestGetOptionalUserString_NilReturnsEmptyNoError(t *testing.T) {
user := map[string]any{"flow": nil}
got, err := getOptionalUserString(user, "flow")
if err != nil {
t.Fatalf("unexpected error for nil optional field: %v", err)
}
if got != "" {
t.Fatalf("got %q, want empty string", got)
}
}
func TestGetOptionalUserString_WrongTypeErrors(t *testing.T) {
user := map[string]any{"flow": []string{"a", "b"}}
if _, err := getOptionalUserString(user, "flow"); err == nil {
t.Fatal("expected error for non-string optional value")
}
}