1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
// This file tests some of the functionality provided in the ip4.go
package layers
import (
"bytes"
"encoding/hex"
"net"
"reflect"
"testing"
"github.com/gopacket/gopacket"
)
// Test the function getIPv4OptionSize when the ipv4 has no options
func TestGetIPOptLengthNoOpt(t *testing.T) {
ip := IPv4{}
length := ip.getIPv4OptionSize()
if length != 0 {
t.Fatalf("Empty option list should have 0 length. Actual %d", length)
}
}
// Test the function getIPv4OptionSize when the ipv4 has end of list option
func TestGetIPOptLengthEndOfList(t *testing.T) {
ip := IPv4{}
ip.Options = append(ip.Options, IPv4Option{OptionType: 0, OptionLength: 1})
length := ip.getIPv4OptionSize()
if length != 4 {
t.Fatalf("After padding, the list should have 4 length. Actual %d", length)
}
}
// Test the function getIPv4OptionSize when the ipv4 has padding and end of list option
func TestGetIPOptLengthPaddingEndOfList(t *testing.T) {
ip := IPv4{}
ip.Options = append(ip.Options, IPv4Option{OptionType: 1, OptionLength: 1})
ip.Options = append(ip.Options, IPv4Option{OptionType: 0, OptionLength: 1})
length := ip.getIPv4OptionSize()
if length != 4 {
t.Fatalf("After padding, the list should have 4 length. Actual %d", length)
}
}
// Test the function getIPv4OptionSize when the ipv4 has some non-trivial option and end of list option
func TestGetIPOptLengthOptionEndOfList(t *testing.T) {
ip := IPv4{}
someByte := make([]byte, 8)
ip.Options = append(ip.Options, IPv4Option{OptionType: 2, OptionLength: 10, OptionData: someByte})
ip.Options = append(ip.Options, IPv4Option{OptionType: 0, OptionLength: 1})
length := ip.getIPv4OptionSize()
if length != 12 {
t.Fatalf("The list should have 12 length. Actual %d", length)
}
}
// Tests that the Options slice is properly reset before parsing new data
func TestIPOptResetDuringDecoding(t *testing.T) {
ip := &IPv4{
Options: []IPv4Option{{OptionType: 42, OptionLength: 4, OptionData: make([]byte, 2)}},
}
ipWithoutOptions := &IPv4{
SrcIP: net.IPv4(192, 168, 1, 1),
DstIP: net.IPv4(192, 168, 1, 1),
Protocol: IPProtocolTCP,
}
ipBytes, err := serialize(ipWithoutOptions)
if err != nil {
t.Fatalf("Failed to serialize ip layer: %v", err)
}
err = ip.DecodeFromBytes(ipBytes, gopacket.NilDecodeFeedback)
if err != nil {
t.Fatalf("Failed to deserialize ip layer: %v", err)
}
if len(ip.Options) > 0 {
t.Fatalf("Options slice has stale data from previous packet")
}
}
func serialize(ip *IPv4) ([]byte, error) {
buffer := gopacket.NewSerializeBuffer()
err := ip.SerializeTo(buffer, gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
})
return buffer.Bytes(), err
}
func TestIPv4InvalidOptionLength(t *testing.T) {
// ip4 Packet with option 136 length set to zero
b, err := hex.DecodeString("460000705f5b0000ff114e02af2db00295ab7e0f88001234")
if err != nil {
t.Fatalf("Failed to Decode header: %v", err)
}
var ip4 IPv4
err = ip4.DecodeFromBytes(b, gopacket.NilDecodeFeedback)
if err == nil {
t.Fatal("Expected 'invalid IP option length' error, but got none.")
}
}
func TestIPv4Options(t *testing.T) {
var ip4 IPv4 // reuse ip4 to test reset
for _, test := range []struct {
packet string
options []IPv4Option
padding []byte
}{
{
packet: "4800002803040000fe01c1e0af2db00095ab7e0b820b00000000000000000000",
options: []IPv4Option{
{
OptionType: 130,
OptionData: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0},
OptionLength: 11,
},
{
OptionType: 0,
OptionLength: 1,
},
},
},
{
packet: "4900002803040000fe01c1e0af2db00095ab7e0b01820b00000000000000000000010203",
options: []IPv4Option{
{
OptionType: 1,
OptionLength: 1,
},
{
OptionType: 130,
OptionData: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0},
OptionLength: 11,
},
{
OptionType: 0,
OptionLength: 1,
},
},
padding: []byte{1, 2, 3},
},
{
packet: "4800002803040000fe01c1e0af2db00095ab7e0b820c00000000000000000000",
options: []IPv4Option{
{
OptionType: 130,
OptionData: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
OptionLength: 12,
},
},
},
{
packet: "4900002803040000fe01c1e0af2db00095ab7e0b00820b00000000000000000000010203",
options: []IPv4Option{
{
OptionType: 0,
OptionLength: 1,
},
},
padding: []byte{0x82, 0x0b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3},
},
} {
b, err := hex.DecodeString(test.packet)
if err != nil {
t.Fatalf("Failed to Decode header: %v", err)
}
err = ip4.DecodeFromBytes(b, gopacket.NilDecodeFeedback)
if err != nil {
t.Fatal("Unexpected error during decoding:", err)
}
if !reflect.DeepEqual(ip4.Options, test.options) {
t.Fatalf("Options mismatch.\nGot:\n%#v\nExpected:\n%#v\n", ip4.Options, test.options)
}
if !bytes.Equal(ip4.Padding, test.padding) {
t.Fatalf("Padding mismatch.\nGot:\n%#v\nExpected:\n%#v\n", ip4.Padding, test.padding)
}
}
}
|