Implement fmt.Stringer for NDPRoutePreference

PiperOrigin-RevId: 382427879
This commit is contained in:
Ghanan Gowripalan
2021-06-30 18:33:49 -07:00
committed by gVisor bot
parent 6ef2684096
commit 07ffecef83
2 changed files with 48 additions and 0 deletions
+19
View File
@@ -16,9 +16,12 @@ package header
import (
"encoding/binary"
"fmt"
"time"
)
var _ fmt.Stringer = NDPRoutePreference(0)
// NDPRoutePreference is the preference values for default routers or
// more-specific routes.
//
@@ -64,6 +67,22 @@ const (
ReservedRoutePreference = 0b10
)
// String implements fmt.Stringer.
func (p NDPRoutePreference) String() string {
switch p {
case HighRoutePreference:
return "HighRoutePreference"
case MediumRoutePreference:
return "MediumRoutePreference"
case LowRoutePreference:
return "LowRoutePreference"
case ReservedRoutePreference:
return "ReservedRoutePreference"
default:
return fmt.Sprintf("NDPRoutePreference(%d)", p)
}
}
// NDPRouterAdvert is an NDP Router Advertisement message. It will only contain
// the body of an ICMPv6 packet.
//
+29
View File
@@ -1717,3 +1717,32 @@ func TestNDPOptionsIter(t *testing.T) {
t.Errorf("got Next = (%x, _, _), want = (nil, _, _)", next)
}
}
func TestNDPRoutePreferenceStringer(t *testing.T) {
p := NDPRoutePreference(0)
for {
var wantStr string
switch p {
case 0b01:
wantStr = "HighRoutePreference"
case 0b00:
wantStr = "MediumRoutePreference"
case 0b11:
wantStr = "LowRoutePreference"
case 0b10:
wantStr = "ReservedRoutePreference"
default:
wantStr = fmt.Sprintf("NDPRoutePreference(%d)", p)
}
if gotStr := p.String(); gotStr != wantStr {
t.Errorf("got NDPRoutePreference(%d).String() = %s, want = %s", p, gotStr, wantStr)
}
p++
if p == 0 {
// Overflowed, we hit all values.
break
}
}
}