diff options
-rw-r--r-- | dhcpv4/option_bootfile_name.go | 30 | ||||
-rw-r--r-- | dhcpv4/option_bootfile_name_test.go | 37 | ||||
-rw-r--r-- | dhcpv4/option_class_identifier.go | 32 | ||||
-rw-r--r-- | dhcpv4/option_class_identifier_test.go | 25 | ||||
-rw-r--r-- | dhcpv4/option_domain_name.go | 30 | ||||
-rw-r--r-- | dhcpv4/option_domain_name_test.go | 25 | ||||
-rw-r--r-- | dhcpv4/option_host_name.go | 30 | ||||
-rw-r--r-- | dhcpv4/option_host_name_test.go | 25 | ||||
-rw-r--r-- | dhcpv4/option_root_path.go | 32 | ||||
-rw-r--r-- | dhcpv4/option_root_path_test.go | 28 | ||||
-rw-r--r-- | dhcpv4/option_string.go | 163 | ||||
-rw-r--r-- | dhcpv4/option_string_test.go | 102 | ||||
-rw-r--r-- | dhcpv4/option_tftp_server_name.go | 30 | ||||
-rw-r--r-- | dhcpv4/option_tftp_server_name_test.go | 37 |
14 files changed, 265 insertions, 361 deletions
diff --git a/dhcpv4/option_bootfile_name.go b/dhcpv4/option_bootfile_name.go deleted file mode 100644 index da0d873..0000000 --- a/dhcpv4/option_bootfile_name.go +++ /dev/null @@ -1,30 +0,0 @@ -package dhcpv4 - -import ( - "fmt" -) - -// OptBootfileName implements the bootfile name option described in RFC 2132, -// Section 9.5. -type OptBootfileName struct { - BootfileName string -} - -// Code returns the option code -func (op *OptBootfileName) Code() OptionCode { - return OptionBootfileName -} - -// ToBytes serializes the option and returns it as a sequence of bytes -func (op *OptBootfileName) ToBytes() []byte { - return []byte(op.BootfileName) -} - -func (op *OptBootfileName) String() string { - return fmt.Sprintf("Bootfile Name -> %s", op.BootfileName) -} - -// ParseOptBootfileName returns a new OptBootfile from a byte stream or error if any -func ParseOptBootfileName(data []byte) (*OptBootfileName, error) { - return &OptBootfileName{BootfileName: string(data)}, nil -} diff --git a/dhcpv4/option_bootfile_name_test.go b/dhcpv4/option_bootfile_name_test.go deleted file mode 100644 index 38a200d..0000000 --- a/dhcpv4/option_bootfile_name_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptBootfileNameCode(t *testing.T) { - opt := OptBootfileName{} - require.Equal(t, OptionBootfileName, opt.Code()) -} - -func TestOptBootfileNameToBytes(t *testing.T) { - opt := OptBootfileName{ - BootfileName: "linuxboot", - } - data := opt.ToBytes() - expected := []byte{ - 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', - } - require.Equal(t, expected, data) -} - -func TestParseOptBootfileName(t *testing.T) { - expected := []byte{ - 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', - } - opt, err := ParseOptBootfileName(expected) - require.NoError(t, err) - require.Equal(t, "linuxboot", opt.BootfileName) -} - -func TestOptBootfileNameString(t *testing.T) { - o := OptBootfileName{BootfileName: "testy test"} - require.Equal(t, "Bootfile Name -> testy test", o.String()) -} diff --git a/dhcpv4/option_class_identifier.go b/dhcpv4/option_class_identifier.go deleted file mode 100644 index 286cb59..0000000 --- a/dhcpv4/option_class_identifier.go +++ /dev/null @@ -1,32 +0,0 @@ -package dhcpv4 - -import ( - "fmt" -) - -// OptClassIdentifier implements the vendor class identifier option described -// in RFC 2132, Section 9.13. -type OptClassIdentifier struct { - Identifier string -} - -// ParseOptClassIdentifier constructs an OptClassIdentifier struct from a sequence of -// bytes and returns it, or an error. -func ParseOptClassIdentifier(data []byte) (*OptClassIdentifier, error) { - return &OptClassIdentifier{Identifier: string(data)}, nil -} - -// Code returns the option code. -func (o *OptClassIdentifier) Code() OptionCode { - return OptionClassIdentifier -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptClassIdentifier) ToBytes() []byte { - return []byte(o.Identifier) -} - -// String returns a human-readable string for this option. -func (o *OptClassIdentifier) String() string { - return fmt.Sprintf("Class Identifier -> %v", o.Identifier) -} diff --git a/dhcpv4/option_class_identifier_test.go b/dhcpv4/option_class_identifier_test.go deleted file mode 100644 index 780cf1d..0000000 --- a/dhcpv4/option_class_identifier_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptClassIdentifierInterfaceMethods(t *testing.T) { - o := OptClassIdentifier{Identifier: "foo"} - require.Equal(t, OptionClassIdentifier, o.Code(), "Code") - require.Equal(t, []byte{'f', 'o', 'o'}, o.ToBytes(), "ToBytes") -} - -func TestParseOptClassIdentifier(t *testing.T) { - data := []byte{'t', 'e', 's', 't'} - o, err := ParseOptClassIdentifier(data) - require.NoError(t, err) - require.Equal(t, &OptClassIdentifier{Identifier: "test"}, o) -} - -func TestOptClassIdentifierString(t *testing.T) { - o := OptClassIdentifier{Identifier: "testy test"} - require.Equal(t, "Class Identifier -> testy test", o.String()) -} diff --git a/dhcpv4/option_domain_name.go b/dhcpv4/option_domain_name.go deleted file mode 100644 index 932a1a4..0000000 --- a/dhcpv4/option_domain_name.go +++ /dev/null @@ -1,30 +0,0 @@ -package dhcpv4 - -import "fmt" - -// OptDomainName implements the domain name option described in RFC 2132, -// Section 3.17. -type OptDomainName struct { - DomainName string -} - -// ParseOptDomainName returns a new OptDomainName from a byte stream, or error -// if any. -func ParseOptDomainName(data []byte) (*OptDomainName, error) { - return &OptDomainName{DomainName: string(data)}, nil -} - -// Code returns the option code. -func (o *OptDomainName) Code() OptionCode { - return OptionDomainName -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptDomainName) ToBytes() []byte { - return []byte(o.DomainName) -} - -// String returns a human-readable string. -func (o *OptDomainName) String() string { - return fmt.Sprintf("Domain Name -> %v", o.DomainName) -} diff --git a/dhcpv4/option_domain_name_test.go b/dhcpv4/option_domain_name_test.go deleted file mode 100644 index 8c259d5..0000000 --- a/dhcpv4/option_domain_name_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptDomainNameInterfaceMethods(t *testing.T) { - o := OptDomainName{DomainName: "foo"} - require.Equal(t, OptionDomainName, o.Code(), "Code") - require.Equal(t, []byte{'f', 'o', 'o'}, o.ToBytes(), "ToBytes") -} - -func TestParseOptDomainName(t *testing.T) { - data := []byte{'t', 'e', 's', 't'} - o, err := ParseOptDomainName(data) - require.NoError(t, err) - require.Equal(t, &OptDomainName{DomainName: "test"}, o) -} - -func TestOptDomainNameString(t *testing.T) { - o := OptDomainName{DomainName: "testy test"} - require.Equal(t, "Domain Name -> testy test", o.String()) -} diff --git a/dhcpv4/option_host_name.go b/dhcpv4/option_host_name.go deleted file mode 100644 index 242ea22..0000000 --- a/dhcpv4/option_host_name.go +++ /dev/null @@ -1,30 +0,0 @@ -package dhcpv4 - -import "fmt" - -// OptHostName implements the host name option described by RFC 2132, Section -// 3.14. -type OptHostName struct { - HostName string -} - -// ParseOptHostName returns a new OptHostName from a byte stream, or error if -// any. -func ParseOptHostName(data []byte) (*OptHostName, error) { - return &OptHostName{HostName: string(data)}, nil -} - -// Code returns the option code. -func (o *OptHostName) Code() OptionCode { - return OptionHostName -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptHostName) ToBytes() []byte { - return []byte(o.HostName) -} - -// String returns a human-readable string. -func (o *OptHostName) String() string { - return fmt.Sprintf("Host Name -> %v", o.HostName) -} diff --git a/dhcpv4/option_host_name_test.go b/dhcpv4/option_host_name_test.go deleted file mode 100644 index 3761ada..0000000 --- a/dhcpv4/option_host_name_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptHostNameInterfaceMethods(t *testing.T) { - o := OptHostName{HostName: "foo"} - require.Equal(t, OptionHostName, o.Code(), "Code") - require.Equal(t, []byte{'f', 'o', 'o'}, o.ToBytes(), "ToBytes") -} - -func TestParseOptHostName(t *testing.T) { - data := []byte{'t', 'e', 's', 't'} - o, err := ParseOptHostName(data) - require.NoError(t, err) - require.Equal(t, &OptHostName{HostName: "test"}, o) -} - -func TestOptHostNameString(t *testing.T) { - o := OptHostName{HostName: "testy test"} - require.Equal(t, "Host Name -> testy test", o.String()) -} diff --git a/dhcpv4/option_root_path.go b/dhcpv4/option_root_path.go deleted file mode 100644 index 66a55a7..0000000 --- a/dhcpv4/option_root_path.go +++ /dev/null @@ -1,32 +0,0 @@ -package dhcpv4 - -import ( - "fmt" -) - -// OptRootPath implements the root path option described by RFC 2132, Section -// 3.19. -type OptRootPath struct { - Path string -} - -// ParseOptRootPath constructs an OptRootPath struct from a sequence of bytes -// and returns it, or an error. -func ParseOptRootPath(data []byte) (*OptRootPath, error) { - return &OptRootPath{Path: string(data)}, nil -} - -// Code returns the option code. -func (o *OptRootPath) Code() OptionCode { - return OptionRootPath -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptRootPath) ToBytes() []byte { - return []byte(o.Path) -} - -// String returns a human-readable string for this option. -func (o *OptRootPath) String() string { - return fmt.Sprintf("Root Path -> %v", o.Path) -} diff --git a/dhcpv4/option_root_path_test.go b/dhcpv4/option_root_path_test.go deleted file mode 100644 index e05dd0e..0000000 --- a/dhcpv4/option_root_path_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptRootPathInterfaceMethods(t *testing.T) { - o := OptRootPath{Path: "/foo/bar/baz"} - require.Equal(t, OptionRootPath, o.Code(), "Code") - wantBytes := []byte{ - '/', 'f', 'o', 'o', '/', 'b', 'a', 'r', '/', 'b', 'a', 'z', - } - require.Equal(t, wantBytes, o.ToBytes(), "ToBytes") -} - -func TestParseOptRootPath(t *testing.T) { - data := []byte{byte(OptionRootPath), 4, '/', 'f', 'o', 'o'} - o, err := ParseOptRootPath(data[2:]) - require.NoError(t, err) - require.Equal(t, &OptRootPath{Path: "/foo"}, o) -} - -func TestOptRootPathString(t *testing.T) { - o := OptRootPath{Path: "/foo/bar/baz"} - require.Equal(t, "Root Path -> /foo/bar/baz", o.String()) -} diff --git a/dhcpv4/option_string.go b/dhcpv4/option_string.go new file mode 100644 index 0000000..76a2db2 --- /dev/null +++ b/dhcpv4/option_string.go @@ -0,0 +1,163 @@ +package dhcpv4 + +import ( + "fmt" +) + +// OptDomainName implements the domain name option described in RFC 2132, +// Section 3.17. +type OptDomainName struct { + DomainName string +} + +// ParseOptDomainName returns a new OptDomainName from a byte stream, or error +// if any. +func ParseOptDomainName(data []byte) (*OptDomainName, error) { + return &OptDomainName{DomainName: string(data)}, nil +} + +// Code returns the option code. +func (o *OptDomainName) Code() OptionCode { + return OptionDomainName +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptDomainName) ToBytes() []byte { + return []byte(o.DomainName) +} + +// String returns a human-readable string. +func (o *OptDomainName) String() string { + return fmt.Sprintf("Domain Name -> %v", o.DomainName) +} + +// OptHostName implements the host name option described by RFC 2132, Section +// 3.14. +type OptHostName struct { + HostName string +} + +// ParseOptHostName returns a new OptHostName from a byte stream, or error if +// any. +func ParseOptHostName(data []byte) (*OptHostName, error) { + return &OptHostName{HostName: string(data)}, nil +} + +// Code returns the option code. +func (o *OptHostName) Code() OptionCode { + return OptionHostName +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptHostName) ToBytes() []byte { + return []byte(o.HostName) +} + +// String returns a human-readable string. +func (o *OptHostName) String() string { + return fmt.Sprintf("Host Name -> %v", o.HostName) +} + +// OptRootPath implements the root path option described by RFC 2132, Section +// 3.19. +type OptRootPath struct { + Path string +} + +// ParseOptRootPath constructs an OptRootPath struct from a sequence of bytes +// and returns it, or an error. +func ParseOptRootPath(data []byte) (*OptRootPath, error) { + return &OptRootPath{Path: string(data)}, nil +} + +// Code returns the option code. +func (o *OptRootPath) Code() OptionCode { + return OptionRootPath +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptRootPath) ToBytes() []byte { + return []byte(o.Path) +} + +// String returns a human-readable string for this option. +func (o *OptRootPath) String() string { + return fmt.Sprintf("Root Path -> %v", o.Path) +} + +// OptBootfileName implements the bootfile name option described in RFC 2132, +// Section 9.5. +type OptBootfileName struct { + BootfileName string +} + +// Code returns the option code +func (op *OptBootfileName) Code() OptionCode { + return OptionBootfileName +} + +// ToBytes serializes the option and returns it as a sequence of bytes +func (op *OptBootfileName) ToBytes() []byte { + return []byte(op.BootfileName) +} + +func (op *OptBootfileName) String() string { + return fmt.Sprintf("Bootfile Name -> %s", op.BootfileName) +} + +// ParseOptBootfileName returns a new OptBootfile from a byte stream or error if any +func ParseOptBootfileName(data []byte) (*OptBootfileName, error) { + return &OptBootfileName{BootfileName: string(data)}, nil +} + +// OptTFTPServerName implements the TFTP server name option described by RFC +// 2132, Section 9.4. +type OptTFTPServerName struct { + TFTPServerName string +} + +// Code returns the option code +func (op *OptTFTPServerName) Code() OptionCode { + return OptionTFTPServerName +} + +// ToBytes serializes the option and returns it as a sequence of bytes +func (op *OptTFTPServerName) ToBytes() []byte { + return []byte(op.TFTPServerName) +} + +func (op *OptTFTPServerName) String() string { + return fmt.Sprintf("TFTP Server Name -> %s", op.TFTPServerName) +} + +// ParseOptTFTPServerName returns a new OptTFTPServerName from a byte stream or error if any +func ParseOptTFTPServerName(data []byte) (*OptTFTPServerName, error) { + return &OptTFTPServerName{TFTPServerName: string(data)}, nil +} + +// OptClassIdentifier implements the vendor class identifier option described +// in RFC 2132, Section 9.13. +type OptClassIdentifier struct { + Identifier string +} + +// ParseOptClassIdentifier constructs an OptClassIdentifier struct from a sequence of +// bytes and returns it, or an error. +func ParseOptClassIdentifier(data []byte) (*OptClassIdentifier, error) { + return &OptClassIdentifier{Identifier: string(data)}, nil +} + +// Code returns the option code. +func (o *OptClassIdentifier) Code() OptionCode { + return OptionClassIdentifier +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptClassIdentifier) ToBytes() []byte { + return []byte(o.Identifier) +} + +// String returns a human-readable string for this option. +func (o *OptClassIdentifier) String() string { + return fmt.Sprintf("Class Identifier -> %v", o.Identifier) +} diff --git a/dhcpv4/option_string_test.go b/dhcpv4/option_string_test.go new file mode 100644 index 0000000..0704f31 --- /dev/null +++ b/dhcpv4/option_string_test.go @@ -0,0 +1,102 @@ +package dhcpv4 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptDomainName(t *testing.T) { + o := OptDomainName{DomainName: "foo"} + require.Equal(t, OptionDomainName, o.Code(), "Code") + require.Equal(t, []byte{'f', 'o', 'o'}, o.ToBytes(), "ToBytes") + require.Equal(t, "Domain Name -> foo", o.String()) +} + +func TestParseOptDomainName(t *testing.T) { + data := []byte{'t', 'e', 's', 't'} + o, err := ParseOptDomainName(data) + require.NoError(t, err) + require.Equal(t, &OptDomainName{DomainName: "test"}, o) +} + +func TestOptHostName(t *testing.T) { + o := OptHostName{HostName: "foo"} + require.Equal(t, OptionHostName, o.Code(), "Code") + require.Equal(t, []byte{'f', 'o', 'o'}, o.ToBytes(), "ToBytes") + require.Equal(t, "Host Name -> foo", o.String()) +} + +func TestParseOptHostName(t *testing.T) { + data := []byte{'t', 'e', 's', 't'} + o, err := ParseOptHostName(data) + require.NoError(t, err) + require.Equal(t, &OptHostName{HostName: "test"}, o) +} + +func TestOptRootPath(t *testing.T) { + o := OptRootPath{Path: "/foo/bar/baz"} + require.Equal(t, OptionRootPath, o.Code(), "Code") + wantBytes := []byte{ + '/', 'f', 'o', 'o', '/', 'b', 'a', 'r', '/', 'b', 'a', 'z', + } + require.Equal(t, wantBytes, o.ToBytes(), "ToBytes") + require.Equal(t, "Root Path -> /foo/bar/baz", o.String()) +} + +func TestParseOptRootPath(t *testing.T) { + data := []byte{byte(OptionRootPath), 4, '/', 'f', 'o', 'o'} + o, err := ParseOptRootPath(data[2:]) + require.NoError(t, err) + require.Equal(t, &OptRootPath{Path: "/foo"}, o) +} + +func TestOptBootfileName(t *testing.T) { + opt := OptBootfileName{ + BootfileName: "linuxboot", + } + require.Equal(t, OptionBootfileName, opt.Code()) + require.Equal(t, []byte{'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't'}, opt.ToBytes()) + require.Equal(t, "Bootfile Name -> linuxboot", opt.String()) +} + +func TestParseOptBootfileName(t *testing.T) { + expected := []byte{ + 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', + } + opt, err := ParseOptBootfileName(expected) + require.NoError(t, err) + require.Equal(t, "linuxboot", opt.BootfileName) +} + +func TestOptTFTPServer(t *testing.T) { + opt := OptTFTPServerName{ + TFTPServerName: "linuxboot", + } + require.Equal(t, OptionTFTPServerName, opt.Code()) + require.Equal(t, []byte("linuxboot"), opt.ToBytes()) + require.Equal(t, "TFTP Server Name -> linuxboot", opt.String()) +} + +func TestParseOptTFTPServerName(t *testing.T) { + expected := []byte{ + 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', + } + opt, err := ParseOptTFTPServerName(expected) + require.NoError(t, err) + require.Equal(t, "linuxboot", string(opt.TFTPServerName)) +} + +func TestOptClassIdentifier(t *testing.T) { + o := OptClassIdentifier{Identifier: "foo"} + require.Equal(t, OptionClassIdentifier, o.Code(), "Code") + require.Equal(t, []byte("foo"), o.ToBytes(), "ToBytes") + require.Equal(t, "Class Identifier -> foo", o.String()) +} + +func TestParseOptClassIdentifier(t *testing.T) { + data := []byte("test") + o, err := ParseOptClassIdentifier(data) + require.NoError(t, err) + require.Equal(t, &OptClassIdentifier{Identifier: "test"}, o) +} diff --git a/dhcpv4/option_tftp_server_name.go b/dhcpv4/option_tftp_server_name.go deleted file mode 100644 index 52c638d..0000000 --- a/dhcpv4/option_tftp_server_name.go +++ /dev/null @@ -1,30 +0,0 @@ -package dhcpv4 - -import ( - "fmt" -) - -// OptTFTPServerName implements the TFTP server name option described by RFC -// 2132, Section 9.4. -type OptTFTPServerName struct { - TFTPServerName string -} - -// Code returns the option code -func (op *OptTFTPServerName) Code() OptionCode { - return OptionTFTPServerName -} - -// ToBytes serializes the option and returns it as a sequence of bytes -func (op *OptTFTPServerName) ToBytes() []byte { - return []byte(op.TFTPServerName) -} - -func (op *OptTFTPServerName) String() string { - return fmt.Sprintf("TFTP Server Name -> %s", op.TFTPServerName) -} - -// ParseOptTFTPServerName returns a new OptTFTPServerName from a byte stream or error if any -func ParseOptTFTPServerName(data []byte) (*OptTFTPServerName, error) { - return &OptTFTPServerName{TFTPServerName: string(data)}, nil -} diff --git a/dhcpv4/option_tftp_server_name_test.go b/dhcpv4/option_tftp_server_name_test.go deleted file mode 100644 index c46b5db..0000000 --- a/dhcpv4/option_tftp_server_name_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package dhcpv4 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptTFTPServerNameCode(t *testing.T) { - opt := OptTFTPServerName{} - require.Equal(t, OptionTFTPServerName, opt.Code()) -} - -func TestOptTFTPServerNameToBytes(t *testing.T) { - opt := OptTFTPServerName{ - TFTPServerName: "linuxboot", - } - data := opt.ToBytes() - expected := []byte{ - 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', - } - require.Equal(t, expected, data) -} - -func TestParseOptTFTPServerName(t *testing.T) { - expected := []byte{ - 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', - } - opt, err := ParseOptTFTPServerName(expected) - require.NoError(t, err) - require.Equal(t, "linuxboot", string(opt.TFTPServerName)) -} - -func TestOptTFTPServerNameString(t *testing.T) { - o := OptTFTPServerName{TFTPServerName: "testy test"} - require.Equal(t, "TFTP Server Name -> testy test", o.String()) -} |