Dereference pointers in Layer's Stringer impl

Dereference any fields which are pointers before string formatting so that the
value pointed to ends up in the string representation.

Tested:
  Added TestLayerStringFormat to
  //third_party/gvisor/test/packetimpact/testbench:testbench_test
PiperOrigin-RevId: 305627821
This commit is contained in:
gVisor bot
2020-04-08 23:30:09 -07:00
parent a10389e783
commit 21e438d257
3 changed files with 97 additions and 1 deletions
+1
View File
@@ -36,4 +36,5 @@ go_test(
size = "small",
srcs = ["layers_test.go"],
library = ":testbench",
deps = ["//pkg/tcpip"],
)
+1 -1
View File
@@ -118,7 +118,7 @@ func stringLayer(l Layer) string {
if v.IsNil() {
continue
}
ret = append(ret, fmt.Sprintf("%s:%v", t.Name, v))
ret = append(ret, fmt.Sprintf("%s:%v", t.Name, reflect.Indirect(v)))
}
return fmt.Sprintf("&%s{%s}", t, strings.Join(ret, " "))
}
@@ -16,6 +16,8 @@ package testbench
import "testing"
import "gvisor.dev/gvisor/pkg/tcpip"
func TestLayerMatch(t *testing.T) {
var nilPayload *Payload
noPayload := &Payload{}
@@ -47,3 +49,96 @@ func TestLayerMatch(t *testing.T) {
}
}
}
func TestLayerStringFormat(t *testing.T) {
for _, tt := range []struct {
name string
l Layer
want string
}{
{
name: "TCP",
l: &TCP{
SrcPort: Uint16(34785),
DstPort: Uint16(47767),
SeqNum: Uint32(3452155723),
AckNum: Uint32(2596996163),
DataOffset: Uint8(5),
Flags: Uint8(20),
WindowSize: Uint16(64240),
Checksum: Uint16(0x2e2b),
},
want: "&testbench.TCP{" +
"SrcPort:34785 " +
"DstPort:47767 " +
"SeqNum:3452155723 " +
"AckNum:2596996163 " +
"DataOffset:5 " +
"Flags:20 " +
"WindowSize:64240 " +
"Checksum:11819" +
"}",
},
{
name: "UDP",
l: &UDP{
SrcPort: Uint16(34785),
DstPort: Uint16(47767),
Length: Uint16(12),
},
want: "&testbench.UDP{" +
"SrcPort:34785 " +
"DstPort:47767 " +
"Length:12" +
"}",
},
{
name: "IPv4",
l: &IPv4{
IHL: Uint8(5),
TOS: Uint8(0),
TotalLength: Uint16(44),
ID: Uint16(0),
Flags: Uint8(2),
FragmentOffset: Uint16(0),
TTL: Uint8(64),
Protocol: Uint8(6),
Checksum: Uint16(0x2e2b),
SrcAddr: Address(tcpip.Address([]byte{197, 34, 63, 10})),
DstAddr: Address(tcpip.Address([]byte{197, 34, 63, 20})),
},
want: "&testbench.IPv4{" +
"IHL:5 " +
"TOS:0 " +
"TotalLength:44 " +
"ID:0 " +
"Flags:2 " +
"FragmentOffset:0 " +
"TTL:64 " +
"Protocol:6 " +
"Checksum:11819 " +
"SrcAddr:197.34.63.10 " +
"DstAddr:197.34.63.20" +
"}",
},
{
name: "Ether",
l: &Ether{
SrcAddr: LinkAddress(tcpip.LinkAddress([]byte{0x02, 0x42, 0xc5, 0x22, 0x3f, 0x0a})),
DstAddr: LinkAddress(tcpip.LinkAddress([]byte{0x02, 0x42, 0xc5, 0x22, 0x3f, 0x14})),
Type: NetworkProtocolNumber(4),
},
want: "&testbench.Ether{" +
"SrcAddr:02:42:c5:22:3f:0a " +
"DstAddr:02:42:c5:22:3f:14 " +
"Type:4" +
"}",
},
} {
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.String(); got != tt.want {
t.Errorf("%s.String() = %s, want: %s", tt.name, got, tt.want)
}
})
}
}