From 726100f8eb88e8ea97ef5770b47a271276a8b741 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 1 Feb 2021 10:25:40 -0800 Subject: Assert expected size before unmarshalling ...to prevent panicking in case of failure. PiperOrigin-RevId: 354970257 --- test/packetimpact/tests/tcp_info_test.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test') diff --git a/test/packetimpact/tests/tcp_info_test.go b/test/packetimpact/tests/tcp_info_test.go index b66e8f609..69275e54b 100644 --- a/test/packetimpact/tests/tcp_info_test.go +++ b/test/packetimpact/tests/tcp_info_test.go @@ -55,6 +55,9 @@ func TestTCPInfo(t *testing.T) { info := linux.TCPInfo{} infoBytes := dut.GetSockOpt(t, acceptFD, unix.SOL_TCP, unix.TCP_INFO, int32(linux.SizeOfTCPInfo)) + if got, want := len(infoBytes), linux.SizeOfTCPInfo; got != want { + t.Fatalf("expected %T, got %d bytes want %d bytes", info, got, want) + } binary.Unmarshal(infoBytes, usermem.ByteOrder, &info) rtt := time.Duration(info.RTT) * time.Microsecond @@ -93,6 +96,9 @@ func TestTCPInfo(t *testing.T) { info = linux.TCPInfo{} infoBytes = dut.GetSockOpt(t, acceptFD, unix.SOL_TCP, unix.TCP_INFO, int32(linux.SizeOfTCPInfo)) + if got, want := len(infoBytes), linux.SizeOfTCPInfo; got != want { + t.Fatalf("expected %T, got %d bytes want %d bytes", info, got, want) + } binary.Unmarshal(infoBytes, usermem.ByteOrder, &info) if info.CaState != linux.TCP_CA_Loss { t.Errorf("expected the connection to be in loss recovery, got: %v want: %v", info.CaState, linux.TCP_CA_Loss) -- cgit v1.2.3