(t *testing.T, icmpPacket *ICMP)
| 110 | } |
| 111 | |
| 112 | func assertICMPChecksum(t *testing.T, icmpPacket *ICMP) { |
| 113 | buf := gopacket.NewSerializeBuffer() |
| 114 | if icmpPacket.Protocol == layers.IPProtocolICMPv4 { |
| 115 | icmpv4 := layers.ICMPv4{ |
| 116 | TypeCode: layers.CreateICMPv4TypeCode(uint8(icmpPacket.Type.(ipv4.ICMPType)), uint8(icmpPacket.Code)), |
| 117 | } |
| 118 | switch body := icmpPacket.Body.(type) { |
| 119 | case *icmp.Echo: |
| 120 | icmpv4.Id = uint16(body.ID) |
| 121 | icmpv4.Seq = uint16(body.Seq) |
| 122 | payload := gopacket.Payload(body.Data) |
| 123 | require.NoError(t, payload.SerializeTo(buf, serializeOpts)) |
| 124 | default: |
| 125 | require.NoError(t, serializeICMPAsPayload(icmpPacket.Message, buf)) |
| 126 | } |
| 127 | // SerializeTo sets the checksum in icmpv4 |
| 128 | require.NoError(t, icmpv4.SerializeTo(buf, serializeOpts)) |
| 129 | require.Equal(t, icmpv4.Checksum, uint16(icmpPacket.Checksum)) |
| 130 | } else { |
| 131 | switch body := icmpPacket.Body.(type) { |
| 132 | case *icmp.Echo: |
| 133 | payload := gopacket.Payload(body.Data) |
| 134 | require.NoError(t, payload.SerializeTo(buf, serializeOpts)) |
| 135 | echo := layers.ICMPv6Echo{ |
| 136 | Identifier: uint16(body.ID), |
| 137 | SeqNumber: uint16(body.Seq), |
| 138 | } |
| 139 | require.NoError(t, echo.SerializeTo(buf, serializeOpts)) |
| 140 | default: |
| 141 | require.NoError(t, serializeICMPAsPayload(icmpPacket.Message, buf)) |
| 142 | } |
| 143 | |
| 144 | icmpv6 := layers.ICMPv6{ |
| 145 | TypeCode: layers.CreateICMPv6TypeCode(uint8(icmpPacket.Type.(ipv6.ICMPType)), uint8(icmpPacket.Code)), |
| 146 | } |
| 147 | ipLayer := layers.IPv6{ |
| 148 | Version: 6, |
| 149 | SrcIP: icmpPacket.Src.AsSlice(), |
| 150 | DstIP: icmpPacket.Dst.AsSlice(), |
| 151 | NextHeader: icmpPacket.Protocol, |
| 152 | HopLimit: icmpPacket.TTL, |
| 153 | } |
| 154 | require.NoError(t, icmpv6.SetNetworkLayerForChecksum(&ipLayer)) |
| 155 | |
| 156 | // SerializeTo sets the checksum in icmpv4 |
| 157 | require.NoError(t, icmpv6.SerializeTo(buf, serializeOpts)) |
| 158 | require.Equal(t, icmpv6.Checksum, uint16(icmpPacket.Checksum)) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func serializeICMPAsPayload(message *icmp.Message, buf gopacket.SerializeBuffer) error { |
| 163 | serializedBody, err := message.Body.Marshal(message.Type.Protocol()) |
no test coverage detected