From 79aba137cf3ea8e0c1c980cad412cc034e145c0e Mon Sep 17 00:00:00 2001 From: Hu Jun Date: Sat, 5 Sep 2020 19:54:16 -0700 Subject: - restore OptionGeneric.String() - add raiValue in option_relay_agent_information.go to implement fmt.Stringer - change test cases accordingly Signed-off-by: Hu Jun --- dhcpv4/option_generic.go | 2 +- dhcpv4/option_generic_test.go | 4 ++-- dhcpv4/option_relay_agent_information.go | 10 +++++++++- dhcpv4/options_test.go | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dhcpv4/option_generic.go b/dhcpv4/option_generic.go index 6095bc2..a54cdeb 100644 --- a/dhcpv4/option_generic.go +++ b/dhcpv4/option_generic.go @@ -18,7 +18,7 @@ func (o OptionGeneric) ToBytes() []byte { // String returns a human-readable representation of a generic option. func (o OptionGeneric) String() string { - return fmt.Sprintf("%v (%v)", string(o.Data), o.Data) + return fmt.Sprintf("%v", o.Data) } // OptGeneric returns a generic option. diff --git a/dhcpv4/option_generic_test.go b/dhcpv4/option_generic_test.go index 2da2a5d..4c4f2e8 100644 --- a/dhcpv4/option_generic_test.go +++ b/dhcpv4/option_generic_test.go @@ -10,10 +10,10 @@ func TestOptionGenericCode(t *testing.T) { o := OptGeneric(OptionDHCPMessageType, []byte{byte(MessageTypeDiscover)}) require.Equal(t, OptionDHCPMessageType, o.Code) require.Equal(t, []byte{1}, o.Value.ToBytes()) - require.Equal(t, "DHCP Message Type: \x01 ([1])", o.String()) + require.Equal(t, "DHCP Message Type: [1]", o.String()) } func TestOptionGenericStringUnknown(t *testing.T) { o := OptGeneric(optionCode(102), []byte{byte(MessageTypeDiscover)}) - require.Equal(t, "unknown (102): \x01 ([1])", o.String()) + require.Equal(t, "unknown (102): [1]", o.String()) } diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go index a6a2c34..b2e84d8 100644 --- a/dhcpv4/option_relay_agent_information.go +++ b/dhcpv4/option_relay_agent_information.go @@ -4,6 +4,14 @@ import ( "fmt" ) +type raiValue struct { + val []byte +} + +func (rv raiValue) String() string { + return fmt.Sprintf("%s (%v)", string([]byte(rv.val)), rv.val) +} + // RelayOptions is like Options, but stringifies using the Relay Agent Specific // option space. type RelayOptions struct { @@ -12,7 +20,7 @@ type RelayOptions struct { var relayHumanizer = OptionHumanizer{ ValueHumanizer: func(code OptionCode, data []byte) fmt.Stringer { - return OptionGeneric{data} + return raiValue{val: data} }, CodeHumanizer: func(c uint8) OptionCode { return raiSubOptionCode(c) diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go index 7dbc48f..fed2312 100644 --- a/dhcpv4/options_test.go +++ b/dhcpv4/options_test.go @@ -19,7 +19,7 @@ func TestParseOption(t *testing.T) { { code: OptionNameServer, value: []byte{192, 168, 1, 254}, - want: "\xc0\xa8\x01\xfe ([192 168 1 254])", + want: "[192 168 1 254]", }, { code: OptionSubnetMask, @@ -152,7 +152,7 @@ func TestOptionStringUnknown(t *testing.T) { Code: GenericOptionCode(102), // Returend option code. Value: &OptionGeneric{[]byte{byte(MessageTypeDiscover)}}, } - require.Equal(t, "unknown (102): \x01 ([1])", o.String()) + require.Equal(t, "unknown (102): [1]", o.String()) } func TestOptionsMarshal(t *testing.T) { -- cgit v1.2.3