+ dhcpd, clients: add more tests

This commit is contained in:
Simon Zolin 2019-06-26 17:53:05 +03:00
parent f1e6a30931
commit efaaeb58eb
2 changed files with 41 additions and 4 deletions

View File

@ -38,12 +38,19 @@ func TestDHCP(t *testing.T) {
p = make(dhcp4.Packet, 241)
// Reserve an IP
// Discover and reserve an IP
hw = []byte{3, 2, 3, 4, 5, 6}
p.SetCHAddr(hw)
lease, _ = s.reserveLease(p)
check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr")
check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP")
p.SetCIAddr([]byte{0, 0, 0, 0})
opt = make(dhcp4.Options, 10)
p2 = s.handleDiscover(p, opt)
opt = p2.ParseOptions()
check(t, bytes.Equal(opt[dhcp4.OptionDHCPMessageType], []byte{byte(dhcp4.Offer)}), "dhcp4.Offer")
check(t, bytes.Equal(p2.YIAddr(), []byte{1, 1, 1, 1}), "p2.YIAddr")
check(t, bytes.Equal(p2.CHAddr(), hw), "p2.CHAddr")
check(t, bytes.Equal(opt[dhcp4.OptionIPAddressLeaseTime], dhcp4.OptionsLeaseTime(5*time.Second)), "OptionIPAddressLeaseTime")
check(t, bytes.Equal(opt[dhcp4.OptionServerIdentifier], s.ipnet.IP), "OptionServerIdentifier")
lease = s.findLease(p)
check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr")
check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP")
@ -88,6 +95,8 @@ func TestDHCP(t *testing.T) {
check(t, bytes.Equal(opt[dhcp4.OptionIPAddressLeaseTime], dhcp4.OptionsLeaseTime(5*time.Second)), "OptionIPAddressLeaseTime")
check(t, bytes.Equal(opt[dhcp4.OptionServerIdentifier], s.ipnet.IP), "OptionServerIdentifier")
check(t, bytes.Equal(s.FindIPbyMAC(hw), []byte{1, 1, 1, 1}), "FindIPbyMAC")
// Commit the previously reserved lease #2
hw = []byte{2, 2, 3, 4, 5, 6}
p.SetCHAddr(hw)
@ -103,10 +112,28 @@ func TestDHCP(t *testing.T) {
lease, _ = s.reserveLease(p)
check(t, lease == nil, "lease == nil")
s.reset()
testStaticLeases(t, &s)
s.reset()
misc(t, &s)
}
func testStaticLeases(t *testing.T, s *Server) {
var err error
var l Lease
l.IP = []byte{1, 1, 1, 1}
l.HWAddr = []byte{2, 2, 3, 4, 5, 6}
err = s.AddStaticLease(l)
check(t, err == nil, "AddStaticLease")
ll := s.StaticLeases()
check(t, len(ll) != 0 && bytes.Equal(ll[0].IP, []byte{1, 1, 1, 1}), "StaticLeases")
err = s.RemoveStaticLease(l)
check(t, err == nil, "RemoveStaticLease")
}
// Small tests that don't require a static server's state
func misc(t *testing.T, s *Server) {
var p, p2 dhcp4.Packet

View File

@ -29,6 +29,16 @@ func TestClients(t *testing.T) {
t.Fatalf("clientAdd #2")
}
c, b = clientFind("1.1.1.1")
if !b || c.Name != "client1" {
t.Fatalf("clientFind #1")
}
c, b = clientFind("2.2.2.2")
if !b || c.Name != "client2" {
t.Fatalf("clientFind #2")
}
// failed add - name in use
c = Client{
IP: "1.2.3.5",