summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/options_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv4/options_test.go')
-rw-r--r--dhcpv4/options_test.go105
1 files changed, 31 insertions, 74 deletions
diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go
index 07be9bf..ca860cf 100644
--- a/dhcpv4/options_test.go
+++ b/dhcpv4/options_test.go
@@ -1,52 +1,37 @@
package dhcpv4
import (
- "bytes"
"testing"
+
+ "github.com/stretchr/testify/require"
)
func TestParseOption(t *testing.T) {
option := []byte{5, 4, 192, 168, 1, 254} // DNS option
opt, err := ParseOption(option)
- if err != nil {
- t.Fatal(err)
- }
- if opt.Code != OptionNameServer {
- t.Fatalf("Invalid option code. Expected 5, got %v", opt.Code)
- }
- if !bytes.Equal(opt.Data, option[2:]) {
- t.Fatalf("Invalid option data. Expected %v, got %v", option[2:], opt.Data)
- }
+ require.NoError(t, err, "should not get error from parsing option")
+ require.Equal(t, OptionNameServer, opt.Code, "opt should have the same opcode")
+ require.Equal(t, option[2:], opt.Data, "opt should have the same data")
}
func TestParseOptionPad(t *testing.T) {
option := []byte{0}
opt, err := ParseOption(option)
- if err != nil {
- t.Fatal(err)
- }
- if opt.Code != OptionPad {
- t.Fatalf("Invalid option code. Expected %v, got %v", OptionPad, opt.Code)
- }
- if len(opt.Data) != 0 {
- t.Fatalf("Invalid option data. Expected empty slice, got %v", opt.Data)
- }
+ require.NoError(t, err, "should not get error from parsing option")
+ require.Equal(t, OptionPad, opt.Code, "should get pad option code")
+ require.Empty(t, opt.Data, "should get empty data with pad option")
}
func TestParseOptionZeroLength(t *testing.T) {
option := []byte{}
_, err := ParseOption(option)
- if err == nil {
- t.Fatal("Expected an error, got none")
- }
+ require.Error(t, err, "should get error from zero-length options")
}
func TestParseOptionShortOption(t *testing.T) {
option := []byte{53, 1}
_, err := ParseOption(option)
- if err == nil {
- t.Fatal(err)
- }
+ require.Error(t, err, "should get error from short options")
}
func TestOptionsFromBytes(t *testing.T) {
@@ -57,41 +42,29 @@ func TestOptionsFromBytes(t *testing.T) {
0, 0, 0, //padding
}
opts, err := OptionsFromBytes(options)
- if err != nil {
- t.Fatal(err)
- }
- // each padding byte counts as an option. Magic Cookie doesn't add up
- if len(opts) != 5 {
- t.Fatal("Invalid options length. Expected 5, got %v", len(opts))
- }
- if opts[0].Code != OptionNameServer {
- t.Fatal("Invalid option code. Expected %v, got %v", OptionNameServer, opts[0].Code)
- }
- if !bytes.Equal(opts[0].Data, options[6:10]) {
- t.Fatal("Invalid option data. Expected %v, got %v", options[6:10], opts[0].Data)
- }
- if opts[1].Code != OptionEnd {
- t.Fatalf("Invalid option code. Expected %v, got %v", OptionEnd, opts[1].Code)
- }
- if opts[2].Code != OptionPad {
- t.Fatalf("Invalid option code. Expected %v, got %v", OptionPad, opts[2].Code)
- }
+ require.NoError(t, err)
+ require.Equal(t, []Option{
+ Option{
+ Code: OptionNameServer,
+ Data: []byte{192, 168, 1, 1},
+ },
+ Option{Code: OptionEnd, Data: []byte{}},
+ Option{Code: OptionPad, Data: []byte{}},
+ Option{Code: OptionPad, Data: []byte{}},
+ Option{Code: OptionPad, Data: []byte{}},
+ }, opts)
}
func TestOptionsFromBytesZeroLength(t *testing.T) {
options := []byte{}
_, err := OptionsFromBytes(options)
- if err == nil {
- t.Fatal("Expected an error, got none")
- }
+ require.Error(t, err)
}
func TestOptionsFromBytesBadMagicCookie(t *testing.T) {
options := []byte{1, 2, 3, 4}
_, err := OptionsFromBytes(options)
- if err == nil {
- t.Fatal("Expected an error, got none")
- }
+ require.Error(t, err)
}
func TestOptionsToBytes(t *testing.T) {
@@ -102,47 +75,31 @@ func TestOptionsToBytes(t *testing.T) {
0, 0, 0, //padding
}
options, err := OptionsFromBytes(originalOptions)
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
finalOptions := OptionsToBytes(options)
- if !bytes.Equal(originalOptions, finalOptions) {
- t.Fatalf("Invalid options. Expected %v, got %v", originalOptions, finalOptions)
- }
+ require.Equal(t, originalOptions, finalOptions)
}
func TestOptionsToBytesEmpty(t *testing.T) {
originalOptions := []byte{99, 130, 83, 99}
options, err := OptionsFromBytes(originalOptions)
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
finalOptions := OptionsToBytes(options)
- if !bytes.Equal(originalOptions, finalOptions) {
- t.Fatalf("Invalid options. Expected %v, got %v", originalOptions, finalOptions)
- }
+ require.Equal(t, originalOptions, finalOptions)
}
func TestOptionsToStringPad(t *testing.T) {
option := []byte{0}
opt, err := ParseOption(option)
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
stropt := opt.String()
- if stropt != "Pad -> []" {
- t.Fatalf("Invalid string representation: %v", stropt)
- }
+ require.Equal(t, "Pad -> []", stropt)
}
func TestOptionsToStringDHCPMessageType(t *testing.T) {
option := []byte{53, 1, 5}
opt, err := ParseOption(option)
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
stropt := opt.String()
- if stropt != "DHCP Message Type -> [5]" {
- t.Fatalf("Invalid string representation: %v", stropt)
- }
+ require.Equal(t, "DHCP Message Type -> [5]", stropt)
}