diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-28 20:19:14 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-10 23:01:22 +0000 |
commit | e87114a6e449d7a2e458b5529923e5668dfa3a11 (patch) | |
tree | 7c5ec350c8eb859400a159b881081ffe0dbd4ef0 /dhcpv4/option_bootfile_name_test.go | |
parent | 108ed92e1c9901936541020bc3214533acce77bb (diff) |
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.
Diffstat (limited to 'dhcpv4/option_bootfile_name_test.go')
-rw-r--r-- | dhcpv4/option_bootfile_name_test.go | 33 |
1 files changed, 4 insertions, 29 deletions
diff --git a/dhcpv4/option_bootfile_name_test.go b/dhcpv4/option_bootfile_name_test.go index 0c7c200..2671ac5 100644 --- a/dhcpv4/option_bootfile_name_test.go +++ b/dhcpv4/option_bootfile_name_test.go @@ -13,7 +13,7 @@ func TestOptBootfileNameCode(t *testing.T) { func TestOptBootfileNameToBytes(t *testing.T) { opt := OptBootfileName{ - BootfileName: []byte("linuxboot"), + BootfileName: "linuxboot", } data := opt.ToBytes() expected := []byte{ @@ -26,40 +26,15 @@ func TestOptBootfileNameToBytes(t *testing.T) { func TestParseOptBootfileName(t *testing.T) { expected := []byte{ - 67, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', + 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', } opt, err := ParseOptBootfileName(expected) require.NoError(t, err) require.Equal(t, 9, opt.Length()) - require.Equal(t, "linuxboot", string(opt.BootfileName)) -} - -func TestParseOptBootfileNameZeroLength(t *testing.T) { - expected := []byte{ - 67, 0, - } - _, err := ParseOptBootfileName(expected) - require.Error(t, err) -} - -func TestParseOptBootfileNameInvalidLength(t *testing.T) { - expected := []byte{ - 67, 9, 'l', 'i', 'n', 'u', 'x', 'b', - } - _, err := ParseOptBootfileName(expected) - require.Error(t, err) -} - -func TestParseOptBootfileNameShortLength(t *testing.T) { - expected := []byte{ - 67, 4, 'l', 'i', 'n', 'u', 'x', - } - opt, err := ParseOptBootfileName(expected) - require.NoError(t, err) - require.Equal(t, []byte("linu"), opt.BootfileName) + require.Equal(t, "linuxboot", opt.BootfileName) } func TestOptBootfileNameString(t *testing.T) { - o := OptBootfileName{BootfileName: []byte("testy test")} + o := OptBootfileName{BootfileName: "testy test"} require.Equal(t, "Bootfile Name -> testy test", o.String()) } |