summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ntp_servers_test.go
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2018-08-01 17:59:36 +0100
committerinsomniac <insomniacslk@users.noreply.github.com>2018-08-01 17:59:36 +0100
commit1ce17c65bf3f13919fa2fbb1a14ce8306a0eca04 (patch)
treedc5a823e91d82a7d5ebd8d73cabd9dddc2c8545a /dhcpv4/option_ntp_servers_test.go
parent0f1b0b63aa938c56794e19e5c360d54dc02fd62a (diff)
add OptNTPServers (#115)
Diffstat (limited to 'dhcpv4/option_ntp_servers_test.go')
-rw-r--r--dhcpv4/option_ntp_servers_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/dhcpv4/option_ntp_servers_test.go b/dhcpv4/option_ntp_servers_test.go
new file mode 100644
index 0000000..e7bcefd
--- /dev/null
+++ b/dhcpv4/option_ntp_servers_test.go
@@ -0,0 +1,65 @@
+package dhcpv4
+
+import (
+ "net"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestOptNTPServersInterfaceMethods(t *testing.T) {
+ ntpServers := []net.IP{
+ net.IPv4(192, 168, 0, 10),
+ net.IPv4(192, 168, 0, 20),
+ }
+ o := OptNTPServers{NTPServers: ntpServers}
+ require.Equal(t, OptionNTPServers, o.Code(), "Code")
+ require.Equal(t, net.IPv4len*len(ntpServers), o.Length(), "Length")
+ require.Equal(t, ntpServers, o.NTPServers, "NTPServers")
+}
+
+func TestParseOptNTPServers(t *testing.T) {
+ data := []byte{
+ byte(OptionNTPServers),
+ 8, // Length
+ 192, 168, 0, 10, // NTP server #1
+ 192, 168, 0, 20, // NTP server #2
+ }
+ o, err := ParseOptNTPServers(data)
+ require.NoError(t, err)
+ ntpServers := []net.IP{
+ net.IPv4(192, 168, 0, 10),
+ net.IPv4(192, 168, 0, 20),
+ }
+ require.Equal(t, &OptNTPServers{NTPServers: ntpServers}, o)
+
+ // Short byte stream
+ data = []byte{byte(OptionNTPServers)}
+ _, err = ParseOptNTPServers(data)
+ require.Error(t, err, "should get error from short byte stream")
+
+ // Wrong code
+ data = []byte{54, 2, 1, 1}
+ _, err = ParseOptNTPServers(data)
+ require.Error(t, err, "should get error from wrong code")
+
+ // Bad length
+ data = []byte{byte(OptionNTPServers), 6, 1, 1, 1}
+ _, err = ParseOptNTPServers(data)
+ require.Error(t, err, "should get error from bad length")
+}
+
+func TestParseOptNTPserversNoNTPServers(t *testing.T) {
+ // RFC2132 requires that at least one NTP server IP is specified
+ data := []byte{
+ byte(OptionNTPServers),
+ 0, // Length
+ }
+ _, err := ParseOptNTPServers(data)
+ require.Error(t, err)
+}
+
+func TestOptNTPServersString(t *testing.T) {
+ o := OptNTPServers{NTPServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}}
+ require.Equal(t, "NTP Servers -> 192.168.0.1, 192.168.0.10", o.String())
+}