3x-ui/util/json_util/json_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

76 lines
1.8 KiB
Go

package json_util
import (
"bytes"
"encoding/json"
"testing"
)
func TestRawMessage_MarshalEmptyIsNull(t *testing.T) {
var m RawMessage
out, err := m.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON on empty returned error: %v", err)
}
if !bytes.Equal(out, []byte("null")) {
t.Fatalf("empty RawMessage marshaled to %q, want %q", out, "null")
}
}
func TestRawMessage_MarshalPassthrough(t *testing.T) {
payload := []byte(`{"a":1}`)
m := RawMessage(payload)
out, err := m.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON returned error: %v", err)
}
if !bytes.Equal(out, payload) {
t.Fatalf("MarshalJSON = %q, want %q", out, payload)
}
}
func TestRawMessage_UnmarshalCopiesData(t *testing.T) {
var m RawMessage
src := []byte(`{"k":"v"}`)
if err := m.UnmarshalJSON(src); err != nil {
t.Fatalf("UnmarshalJSON returned error: %v", err)
}
if !bytes.Equal(m, src) {
t.Fatalf("UnmarshalJSON stored %q, want %q", []byte(m), src)
}
src[0] = 'X'
if m[0] == 'X' {
t.Fatal("UnmarshalJSON kept a reference to the caller's buffer; expected a copy")
}
}
func TestRawMessage_UnmarshalNilReceiverErrors(t *testing.T) {
var m *RawMessage
if err := m.UnmarshalJSON([]byte("123")); err == nil {
t.Fatal("expected error for nil receiver")
}
}
func TestRawMessage_RoundTripInsideStruct(t *testing.T) {
type wrapper struct {
Body RawMessage `json:"body"`
}
in := wrapper{Body: RawMessage(`{"x":42}`)}
encoded, err := json.Marshal(in)
if err != nil {
t.Fatalf("json.Marshal returned error: %v", err)
}
want := `{"body":{"x":42}}`
if string(encoded) != want {
t.Fatalf("Marshal = %s, want %s", encoded, want)
}
var out wrapper
if err := json.Unmarshal(encoded, &out); err != nil {
t.Fatalf("json.Unmarshal returned error: %v", err)
}
if string(out.Body) != `{"x":42}` {
t.Fatalf("round-trip Body = %s, want %s", out.Body, `{"x":42}`)
}
}