Files
s3/weed/util/outbound_dial_test.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

130 lines
3.9 KiB
Go

package util
import (
"testing"
)
// resetOutbound clears the process-global outbound source address so each test
// starts from a clean slate (SetOutboundLocalIP is otherwise first-write-wins).
func resetOutbound() {
outboundLocalAddrSet.Store(false)
outboundLocalAddr.Store(nil)
}
func TestSetOutboundLocalIP(t *testing.T) {
t.Cleanup(resetOutbound)
cases := []struct {
name string
ip string
wantIP string // empty means no binding
}{
{"ipv4", "10.0.0.5", "10.0.0.5"},
{"ipv6", "fe80::1", "fe80::1"},
{"empty", "", ""},
{"wildcard v4", "0.0.0.0", ""},
{"wildcard v6", "::", ""},
{"garbage", "not-an-ip", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetOutbound()
SetOutboundLocalIP(tc.ip)
got := OutboundLocalAddr()
if tc.wantIP == "" {
if got != nil {
t.Fatalf("expected no bound address, got %v", got)
}
return
}
if got == nil {
t.Fatalf("expected bound address %s, got nil", tc.wantIP)
}
if got.IP.String() != tc.wantIP {
t.Fatalf("bound address = %s, want %s", got.IP.String(), tc.wantIP)
}
if got.Port != 0 {
t.Fatalf("bound port = %d, want 0 (ephemeral)", got.Port)
}
})
}
}
// TestSetOutboundLocalIPFirstWins guards the behavior that keeps `weed server`
// from letting a component's own bind setting clobber the process-wide address.
func TestSetOutboundLocalIPFirstWins(t *testing.T) {
t.Cleanup(resetOutbound)
resetOutbound()
SetOutboundLocalIP("10.0.0.5") // server-level bind, applied first
SetOutboundLocalIP("10.0.0.9") // a component's own bind: must be ignored
SetOutboundLocalIP("") // a component clearing: must not unbind
got := OutboundLocalAddr()
if got == nil || got.IP.String() != "10.0.0.5" {
t.Fatalf("first call should win, got %v", got)
}
}
func TestOutboundLocalAddrForDial(t *testing.T) {
t.Cleanup(resetOutbound)
// No bind configured: never binds a source address.
resetOutbound()
if got := outboundLocalAddrForDial("tcp", "10.0.0.9:8080"); got != nil {
t.Fatalf("unconfigured dial should not bind, got %v", got)
}
resetOutbound()
SetOutboundLocalIP("10.0.0.5")
cases := []struct {
name string
network string
address string
wantBind bool
}{
{"remote tcp binds", "tcp", "10.0.0.9:8080", true},
{"tcp4 binds", "tcp4", "example.com:443", true},
{"hostname binds", "tcp", "filer.internal:8888", true},
{"loopback ip skipped", "tcp", "127.0.0.1:9333", false},
{"loopback name skipped", "tcp", "localhost:9333", false},
{"ipv6 loopback skipped", "tcp", "[::1]:9333", false},
{"ipv6 literal target skipped", "tcp", "[2001:db8::1]:8080", false},
{"unix network skipped", "unix", "/tmp/x.sock", false},
{"udp network skipped", "udp", "10.0.0.9:53", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := outboundLocalAddrForDial(tc.network, tc.address)
if tc.wantBind && got == nil {
t.Fatalf("%s/%s: expected source binding, got nil", tc.network, tc.address)
}
if !tc.wantBind && got != nil {
t.Fatalf("%s/%s: expected no source binding, got %v", tc.network, tc.address, got)
}
})
}
}
func TestOutboundLocalAddrFamilyMismatch(t *testing.T) {
t.Cleanup(resetOutbound)
resetOutbound()
SetOutboundLocalIP("10.0.0.5") // IPv4 source
if got := outboundLocalAddrForDial("tcp", "[2001:db8::1]:8080"); got != nil {
t.Fatalf("IPv4 source to IPv6 literal target should skip binding, got %v", got)
}
if got := outboundLocalAddrForDial("tcp", "10.0.0.9:8080"); got == nil {
t.Fatalf("IPv4 source to IPv4 literal target should bind")
}
resetOutbound()
SetOutboundLocalIP("2001:db8::5") // IPv6 source
if got := outboundLocalAddrForDial("tcp", "10.0.0.9:8080"); got != nil {
t.Fatalf("IPv6 source to IPv4 literal target should skip binding, got %v", got)
}
if got := outboundLocalAddrForDial("tcp", "[2001:db8::1]:8080"); got == nil {
t.Fatalf("IPv6 source to IPv6 literal target should bind")
}
}