Files
lneto/ipv4/definitions_test.go
T
Pat Whittingslow 1884cfc9b7 Further improvements to httphi API (#173)
* add Exchange.WriteBodyString

* Mux.MaxPathValues and other improvements

* Mux PathValue improvemnt and fixes

* MuxSlice more method muxing improvements

* diagram out interesting approach to form parsing for clanker

* refactor RequestParseForm and achieve greatness in API design

* explicit naming of headerCapacityKV value in kvBuffer.Reset

* fix Mux bug not matching paths correctly; httpraw HTTP V1 naming applied

* rename many examples,use httphi in examples,remove useless maxAwaitingConn field

* add ipv4.String

* add ipv4 UnspecifiedAddr and BroadcastAddr

* add ethernet.String
2026-07-31 12:47:33 -03:00

65 lines
1.7 KiB
Go

package ipv4
import (
"net/netip"
"testing"
)
func TestAppendFormatAddr(t *testing.T) {
tests := []struct {
addr [4]byte
want string
}{
{addr: UnspecifiedAddr(), want: "0.0.0.0"},
{addr: BroadcastAddr(), want: "255.255.255.255"},
{addr: [4]byte{127, 0, 0, 1}, want: "127.0.0.1"},
{addr: [4]byte{192, 168, 1, 1}, want: "192.168.1.1"},
{addr: [4]byte{10, 0, 0, 1}, want: "10.0.0.1"},
{addr: [4]byte{1, 2, 3, 4}, want: "1.2.3.4"},
{addr: [4]byte{100, 99, 9, 0}, want: "100.99.9.0"},
}
for _, tc := range tests {
got := string(AppendFormatAddr(nil, tc.addr))
if got != tc.want {
t.Errorf("AppendFormatAddr(%v): got %q, want %q", tc.addr, got, tc.want)
}
// Cross-check with netip.
want := netip.AddrFrom4(tc.addr).String()
if got != want {
t.Errorf("AppendFormatAddr(%v) disagrees with netip: got %q, want %q", tc.addr, got, want)
}
}
}
func TestIsLinkLocal(t *testing.T) {
tests := []struct {
addr [4]byte
want bool
}{
{addr: [4]byte{169, 254, 0, 0}, want: true},
{addr: [4]byte{169, 254, 1, 1}, want: true},
{addr: [4]byte{169, 254, 254, 255}, want: true},
{addr: [4]byte{169, 254, 255, 255}, want: true},
{addr: [4]byte{169, 253, 1, 1}, want: false},
{addr: [4]byte{169, 255, 1, 1}, want: false},
{addr: [4]byte{192, 168, 1, 1}, want: false},
{addr: [4]byte{0, 0, 0, 0}, want: false},
}
for _, tc := range tests {
if got := IsLinkLocal(tc.addr); got != tc.want {
t.Errorf("IsLinkLocal(%v): got %v, want %v", tc.addr, got, tc.want)
}
}
}
func TestAppendFormatAddr_noAllocs(t *testing.T) {
var buf [24]byte
addr := [4]byte{192, 168, 1, 1}
allocs := testing.AllocsPerRun(100, func() {
_ = AppendFormatAddr(buf[:0], addr)
})
if allocs != 0 {
t.Errorf("expected 0 allocs, got %v", allocs)
}
}