3x-ui/util/common/format_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

28 lines
678 B
Go

package common
import "testing"
func TestFormatTraffic(t *testing.T) {
cases := []struct {
name string
bytes int64
want string
}{
{"zero", 0, "0.00B"},
{"under_one_kb", 512, "512.00B"},
{"exactly_one_kb", 1024, "1.00KB"},
{"one_and_a_half_kb", 1536, "1.50KB"},
{"one_mb", 1024 * 1024, "1.00MB"},
{"one_gb", 1024 * 1024 * 1024, "1.00GB"},
{"one_tb", 1024 * 1024 * 1024 * 1024, "1.00TB"},
{"one_pb", 1024 * 1024 * 1024 * 1024 * 1024, "1.00PB"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := FormatTraffic(c.bytes)
if got != c.want {
t.Fatalf("FormatTraffic(%d) = %q, want %q", c.bytes, got, c.want)
}
})
}
}