From e87114a6e449d7a2e458b5529923e5668dfa3a11 Mon Sep 17 00:00:00 2001 From: Christopher Koch Date: Fri, 28 Dec 2018 20:19:14 -0800 Subject: dhcpv4: simplify option parsing. option's codes and lengths were being parsed twice: once in ParseOption and once in each option type's Parse implementation. Consolidate such that it only happens once. Additionally, only pass data to options that they should parse -- we know the length before the Parse function is called, so the option only gets to see the data it needs to see. Also, use uio.Lexer to simplify parsing code in general. Easier to read and reason about. --- dhcpv4/option_ntp_servers_test.go | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'dhcpv4/option_ntp_servers_test.go') diff --git a/dhcpv4/option_ntp_servers_test.go b/dhcpv4/option_ntp_servers_test.go index e7bcefd..4d321ff 100644 --- a/dhcpv4/option_ntp_servers_test.go +++ b/dhcpv4/option_ntp_servers_test.go @@ -25,37 +25,23 @@ func TestParseOptNTPServers(t *testing.T) { 192, 168, 0, 10, // NTP server #1 192, 168, 0, 20, // NTP server #2 } - o, err := ParseOptNTPServers(data) + o, err := ParseOptNTPServers(data[2:]) require.NoError(t, err) ntpServers := []net.IP{ - net.IPv4(192, 168, 0, 10), - net.IPv4(192, 168, 0, 20), + net.IP{192, 168, 0, 10}, + net.IP{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} + data = []byte{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) + _, err := ParseOptNTPServers([]byte{}) require.Error(t, err) } -- cgit v1.2.3