summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_vivc_test.go
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2018-12-28 20:19:14 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2019-01-10 23:01:22 +0000
commite87114a6e449d7a2e458b5529923e5668dfa3a11 (patch)
tree7c5ec350c8eb859400a159b881081ffe0dbd4ef0 /dhcpv4/option_vivc_test.go
parent108ed92e1c9901936541020bc3214533acce77bb (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_vivc_test.go')
-rw-r--r--dhcpv4/option_vivc_test.go28
1 files changed, 6 insertions, 22 deletions
diff --git a/dhcpv4/option_vivc_test.go b/dhcpv4/option_vivc_test.go
index b290da0..0b4ab7c 100644
--- a/dhcpv4/option_vivc_test.go
+++ b/dhcpv4/option_vivc_test.go
@@ -31,36 +31,20 @@ func TestOptVIVCInterfaceMethods(t *testing.T) {
}
func TestParseOptVICO(t *testing.T) {
- o, err := ParseOptVIVC(sampleVIVCOptRaw)
+ o, err := ParseOptVIVC(sampleVIVCOptRaw[2:])
require.NoError(t, err)
require.Equal(t, &sampleVIVCOpt, o)
- // Short byte stream
- data := []byte{byte(OptionVendorIdentifyingVendorClass)}
- _, err = ParseOptVIVC(data)
- require.Error(t, err, "should get error from short byte stream")
-
- // Wrong code
- data = []byte{54, 2, 1, 1}
- _, err = ParseOptVIVC(data)
- require.Error(t, err, "should get error from wrong code")
-
- // Bad length
- data = []byte{byte(OptionVendorIdentifyingVendorClass), 6, 1, 1, 1}
- _, err = ParseOptVIVC(data)
- require.Error(t, err, "should get error from bad length")
-
// Identifier len too long
- data = make([]byte, len(sampleVIVCOptRaw))
- copy(data, sampleVIVCOptRaw)
- data[6] = 40
+ data := make([]byte, len(sampleVIVCOptRaw[2:]))
+ copy(data, sampleVIVCOptRaw[2:])
+ data[4] = 40
_, err = ParseOptVIVC(data)
require.Error(t, err, "should get error from bad length")
// Longer than length
- data[1] = 10
- data[6] = 5
- o, err = ParseOptVIVC(data)
+ data[4] = 5
+ o, err = ParseOptVIVC(data[:10])
require.NoError(t, err)
require.Equal(t, o.Identifiers[0].Data, []byte("Cisco"))
}