all: add tests
This commit is contained in:
parent
f338086309
commit
c68e85c409
|
@ -16,16 +16,15 @@ import (
|
|||
//
|
||||
// ip must be either an IPv4 or an IPv6.
|
||||
func GenerateHostname(ip netip.Addr) (hostname string) {
|
||||
if ip.Is4() {
|
||||
hostname = ip.String()
|
||||
|
||||
return strings.Replace(hostname, ".", "-", -1)
|
||||
if !ip.IsValid() {
|
||||
// TODO(s.chzhen): Get rid of it.
|
||||
panic("aghnet generate hostname: invalid ip")
|
||||
}
|
||||
|
||||
if ip.Is4In6() {
|
||||
b := ip.As4()
|
||||
addr := netip.AddrFrom4(b)
|
||||
hostname = addr.String()
|
||||
ip = ip.Unmap()
|
||||
|
||||
if ip.Is4() {
|
||||
hostname = ip.String()
|
||||
|
||||
return strings.Replace(hostname, ".", "-", -1)
|
||||
}
|
||||
|
|
|
@ -25,10 +25,6 @@ func TestGenerateHostName(t *testing.T) {
|
|||
name: "4to6",
|
||||
want: "1-2-3-4",
|
||||
ip: netip.MustParseAddr("::ffff:1.2.3.4"),
|
||||
}, {
|
||||
name: "invalid",
|
||||
want: "invalid IP",
|
||||
ip: netip.Addr{},
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
@ -38,4 +34,20 @@ func TestGenerateHostName(t *testing.T) {
|
|||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
ip netip.Addr
|
||||
}{{
|
||||
name: "nil",
|
||||
ip: netip.Addr{},
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert.Panics(t, func() { GenerateHostname(tc.ip) })
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -496,6 +496,12 @@ func (s *server) handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request
|
|||
return
|
||||
}
|
||||
|
||||
if !l.IP.IsValid() {
|
||||
aghhttp.Error(r, w, http.StatusBadRequest, "invalid IP")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var srv DHCPServer
|
||||
if l.IP.Is4() {
|
||||
srv = s.srv4
|
||||
|
|
|
@ -17,15 +17,10 @@ import (
|
|||
)
|
||||
|
||||
func TestServer_handleDHCPStatus(t *testing.T) {
|
||||
const (
|
||||
staticName = "static-client"
|
||||
anotherName = "another-client"
|
||||
)
|
||||
const staticName = "static-client"
|
||||
|
||||
staticIP := netip.MustParseAddr("192.168.10.10")
|
||||
// anotherIP := DefaultRangeStart
|
||||
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
||||
// anotherMAC := net.HardwareAddr{0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB}
|
||||
|
||||
conf4 := defaultV4ServerConf()
|
||||
conf4.LeaseDuration = 86400
|
||||
|
@ -47,7 +42,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
Enabled: true,
|
||||
}
|
||||
|
||||
wantLease := &Lease{
|
||||
wantLease := Lease{
|
||||
Expiry: time.Unix(leaseExpireStatic, 0),
|
||||
Hostname: staticName,
|
||||
HWAddr: staticMAC,
|
||||
|
@ -97,7 +92,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
wantConf := wantConf
|
||||
wantConf.StaticLeases = []*Lease{wantLease}
|
||||
wantConf.StaticLeases = []*Lease{&wantLease}
|
||||
|
||||
err = json.NewEncoder(b).Encode(&wantConf)
|
||||
require.NoError(t, err)
|
||||
|
@ -108,6 +103,27 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
assert.JSONEq(t, b.String(), w.Body.String())
|
||||
})
|
||||
|
||||
t.Run("add_invalid_lease", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
lease := wantLease
|
||||
lease.IP = netip.Addr{}
|
||||
|
||||
err = json.NewEncoder(b).Encode(&lease)
|
||||
require.NoError(t, err)
|
||||
|
||||
var r *http.Request
|
||||
r, err = http.NewRequest(http.MethodPost, "", b)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.handleDHCPAddStaticLease(w, r)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
|
||||
assert.Equal(t, "invalid IP\n", w.Body.String())
|
||||
})
|
||||
|
||||
t.Run("remove_static_lease", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
|
|
@ -1033,8 +1033,7 @@ func (s *v4Server) handle(req, resp *dhcpv4.DHCPv4) int {
|
|||
}
|
||||
|
||||
if l != nil {
|
||||
ip := net.IP(l.IP.AsSlice())
|
||||
resp.YourIPAddr = slices.Clone(ip)
|
||||
resp.YourIPAddr = net.IP(l.IP.AsSlice())
|
||||
}
|
||||
|
||||
s.updateOptions(req, resp)
|
||||
|
|
|
@ -873,3 +873,60 @@ func TestV4Server_Send(t *testing.T) {
|
|||
assert.True(t, resp.IsBroadcast())
|
||||
})
|
||||
}
|
||||
|
||||
func TestV4Server_FindMACbyIP(t *testing.T) {
|
||||
const (
|
||||
staticName = "static-client"
|
||||
anotherName = "another-client"
|
||||
)
|
||||
|
||||
staticIP := netip.MustParseAddr("192.168.10.10")
|
||||
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
||||
|
||||
anotherIP := netip.MustParseAddr("192.168.100.100")
|
||||
anotherMAC := net.HardwareAddr{0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB}
|
||||
|
||||
s := &v4Server{
|
||||
leases: []*Lease{{
|
||||
Expiry: time.Unix(leaseExpireStatic, 0),
|
||||
Hostname: staticName,
|
||||
HWAddr: staticMAC,
|
||||
IP: staticIP,
|
||||
}, {
|
||||
Expiry: time.Unix(10, 0),
|
||||
Hostname: anotherName,
|
||||
HWAddr: anotherMAC,
|
||||
IP: anotherIP,
|
||||
}},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
want net.HardwareAddr
|
||||
ip netip.Addr
|
||||
name string
|
||||
}{{
|
||||
name: "basic",
|
||||
ip: staticIP,
|
||||
want: staticMAC,
|
||||
}, {
|
||||
name: "not_found",
|
||||
ip: netip.MustParseAddr("1.2.3.4"),
|
||||
want: nil,
|
||||
}, {
|
||||
name: "expired",
|
||||
ip: anotherIP,
|
||||
want: nil,
|
||||
}, {
|
||||
name: "v6",
|
||||
ip: netip.MustParseAddr("ffff::1"),
|
||||
want: nil,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mac := s.FindMACbyIP(tc.ip)
|
||||
|
||||
require.Equal(t, tc.want, mac)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/insomniacslk/dhcp/dhcpv6"
|
||||
"github.com/insomniacslk/dhcp/iana"
|
||||
|
@ -311,3 +312,72 @@ func TestIP6InRange(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestV6_FindMACbyIP(t *testing.T) {
|
||||
const (
|
||||
staticName = "static-client"
|
||||
anotherName = "another-client"
|
||||
)
|
||||
|
||||
staticIP := netip.MustParseAddr("2001::1")
|
||||
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
||||
|
||||
anotherIP := netip.MustParseAddr("2001::100")
|
||||
anotherMAC := net.HardwareAddr{0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB}
|
||||
|
||||
s := &v6Server{
|
||||
leases: []*Lease{{
|
||||
Expiry: time.Unix(leaseExpireStatic, 0),
|
||||
Hostname: staticName,
|
||||
HWAddr: staticMAC,
|
||||
IP: staticIP,
|
||||
}, {
|
||||
Expiry: time.Unix(10, 0),
|
||||
Hostname: anotherName,
|
||||
HWAddr: anotherMAC,
|
||||
IP: anotherIP,
|
||||
}},
|
||||
}
|
||||
|
||||
s.leases = []*Lease{{
|
||||
Expiry: time.Unix(leaseExpireStatic, 0),
|
||||
Hostname: staticName,
|
||||
HWAddr: staticMAC,
|
||||
IP: staticIP,
|
||||
}, {
|
||||
Expiry: time.Unix(10, 0),
|
||||
Hostname: anotherName,
|
||||
HWAddr: anotherMAC,
|
||||
IP: anotherIP,
|
||||
}}
|
||||
|
||||
testCases := []struct {
|
||||
want net.HardwareAddr
|
||||
ip netip.Addr
|
||||
name string
|
||||
}{{
|
||||
name: "basic",
|
||||
ip: staticIP,
|
||||
want: staticMAC,
|
||||
}, {
|
||||
name: "not_found",
|
||||
ip: netip.MustParseAddr("ffff::1"),
|
||||
want: nil,
|
||||
}, {
|
||||
name: "expired",
|
||||
ip: anotherIP,
|
||||
want: nil,
|
||||
}, {
|
||||
name: "v4",
|
||||
ip: netip.MustParseAddr("1.2.3.4"),
|
||||
want: nil,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mac := s.FindMACbyIP(tc.ip)
|
||||
|
||||
require.Equal(t, tc.want, mac)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue