2023-03-23 16:52:01 +03:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_handleDHCPStatus(t *testing.T) {
|
2023-04-10 11:17:05 +03:00
|
|
|
const (
|
|
|
|
staticName = "static-client"
|
|
|
|
staticMAC = "aa:aa:aa:aa:aa:aa"
|
|
|
|
)
|
2023-03-23 16:52:01 +03:00
|
|
|
|
|
|
|
staticIP := netip.MustParseAddr("192.168.10.10")
|
|
|
|
|
2023-04-10 11:17:05 +03:00
|
|
|
staticLease := &leaseStatic{
|
2023-03-23 16:52:01 +03:00
|
|
|
HWAddr: staticMAC,
|
|
|
|
IP: staticIP,
|
2023-04-10 11:17:05 +03:00
|
|
|
Hostname: staticName,
|
2023-03-23 16:52:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Create(&ServerConfig{
|
|
|
|
Enabled: true,
|
|
|
|
Conf4: *defaultV4ServerConf(),
|
2023-04-18 15:12:11 +03:00
|
|
|
DataDir: t.TempDir(),
|
2023-03-23 16:52:01 +03:00
|
|
|
ConfigModified: func() {},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// checkStatus is a helper that asserts the response of
|
|
|
|
// [*server.handleDHCPStatus].
|
|
|
|
checkStatus := func(t *testing.T, want *dhcpStatusResponse) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&want)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s.handleDHCPStatus(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
assert.JSONEq(t, b.String(), w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultResponse is a helper that returs the response with default
|
|
|
|
// configuration.
|
|
|
|
defaultResponse := func() *dhcpStatusResponse {
|
|
|
|
conf4 := defaultV4ServerConf()
|
|
|
|
conf4.LeaseDuration = 86400
|
|
|
|
|
|
|
|
resp := &dhcpStatusResponse{
|
|
|
|
V4: *conf4,
|
|
|
|
V6: V6ServerConf{},
|
2023-04-10 11:17:05 +03:00
|
|
|
Leases: []*leaseDynamic{},
|
|
|
|
StaticLeases: []*leaseStatic{},
|
2023-03-23 16:52:01 +03:00
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := t.Run("status", func(t *testing.T) {
|
|
|
|
resp := defaultResponse()
|
|
|
|
|
|
|
|
checkStatus(t, resp)
|
|
|
|
})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ok = t.Run("add_static_lease", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(staticLease)
|
|
|
|
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.StatusOK, w.Code)
|
|
|
|
|
|
|
|
resp := defaultResponse()
|
2023-04-10 11:17:05 +03:00
|
|
|
resp.StaticLeases = []*leaseStatic{staticLease}
|
2023-03-23 16:52:01 +03:00
|
|
|
|
|
|
|
checkStatus(t, resp)
|
|
|
|
})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ok = t.Run("add_invalid_lease", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
2023-04-10 11:17:05 +03:00
|
|
|
err = json.NewEncoder(b).Encode(&leaseStatic{})
|
2023-03-23 16:52:01 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ok = t.Run("remove_static_lease", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(staticLease)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var r *http.Request
|
|
|
|
r, err = http.NewRequest(http.MethodPost, "", b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s.handleDHCPRemoveStaticLease(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
resp := defaultResponse()
|
|
|
|
|
|
|
|
checkStatus(t, resp)
|
|
|
|
})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ok = t.Run("set_config", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
resp := defaultResponse()
|
|
|
|
resp.Enabled = false
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var r *http.Request
|
|
|
|
r, err = http.NewRequest(http.MethodPost, "", b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s.handleDHCPSetConfig(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
checkStatus(t, resp)
|
|
|
|
})
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|