Skip to content

Commit a3a4c1f

Browse files
michaelharohugelgupf
authored andcommitted
Escape agent options containing binary when stringifying
Printing strings that contain binary messes up my terminal. Using %q instead of %s will result in the binary being escaped. Signed-off-by: Michael Haro <[email protected]>
1 parent c3c2d84 commit a3a4c1f

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

dhcpv4/option_relay_agent_information.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type raiSubOptionValue struct {
5050
}
5151

5252
func (rv raiSubOptionValue) String() string {
53-
return fmt.Sprintf("%s (%v)", string(rv.val), rv.val)
53+
return fmt.Sprintf("%q (%v)", string(rv.val), rv.val)
5454
}
5555

5656
type raiSubOptionCode uint8

dhcpv4/option_relay_agent_information_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ func TestOptRelayAgentInfo(t *testing.T) {
3636
opt := OptRelayAgentInfo(
3737
OptGeneric(GenericOptionCode(1), []byte("linux")),
3838
OptGeneric(GenericOptionCode(2), []byte("boot")),
39+
OptGeneric(GenericOptionCode(3), []byte{2, 30, 99}),
3940
OptGeneric(GenericOptionCode(LinkSelectionSubOption), []byte{192, 0, 2, 1}),
4041
)
4142
wantBytes := []byte{
4243
1, 5, 'l', 'i', 'n', 'u', 'x',
4344
2, 4, 'b', 'o', 'o', 't',
45+
3, 3, 2, 30, 99,
4446
5, 4, 192, 0, 2, 1,
4547
}
46-
wantString := "Relay Agent Information:\n\n Agent Circuit ID Sub-option: linux ([108 105 110 117 120])\n Agent Remote ID Sub-option: boot ([98 111 111 116])\n Link Selection Sub-option: 192.0.2.1\n"
48+
wantString := "Relay Agent Information:\n\n Agent Circuit ID Sub-option: \"linux\" ([108 105 110 117 120])\n Agent Remote ID Sub-option: \"boot\" ([98 111 111 116])\n unknown (3): \"\\x02\\x1ec\" ([2 30 99])\n Link Selection Sub-option: 192.0.2.1\n"
4749
require.Equal(t, wantBytes, opt.Value.ToBytes())
4850
require.Equal(t, OptionRelayAgentInformation, opt.Code)
4951
require.Equal(t, wantString, opt.String())

dhcpv4/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestParseOption(t *testing.T) {
119119
{
120120
code: OptionRelayAgentInformation,
121121
value: []byte{1, 12, 99, 105, 114, 99, 117, 105, 116, 45, 105, 100, 45, 49},
122-
want: "\n Agent Circuit ID Sub-option: circuit-id-1 ([99 105 114 99 117 105 116 45 105 100 45 49])\n",
122+
want: "\n Agent Circuit ID Sub-option: \"circuit-id-1\" ([99 105 114 99 117 105 116 45 105 100 45 49])\n",
123123
},
124124
{
125125
code: OptionClientSystemArchitectureType,

0 commit comments

Comments
 (0)