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
|
// 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 (
"encoding/binary"
"encoding/hex"
"net"
"testing"
"github.com/google/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
}
// Test the function checksum
func TestChecksum(t *testing.T) {
testData := []struct {
name string
header string
want string
}{{
name: "sum has two carries",
header: "4540005800000000ff11ffff0aeb1d070aed8877",
want: "fffe",
}, {
name: "wikipedia case",
header: "45000073000040004011b861c0a80001c0a800c7",
want: "b861",
}}
for _, test := range testData {
bytes, err := hex.DecodeString(test.header)
if err != nil {
t.Fatalf("Failed to Decode header: %v", err)
}
wantBytes, err := hex.DecodeString(test.want)
if err != nil {
t.Fatalf("Failed to decode want checksum: %v", err)
}
if got, want := checksum(bytes), binary.BigEndian.Uint16(wantBytes); got != want {
t.Errorf("In test %q, got incorrect checksum: got(%x), want(%x)", test.name, got, want)
}
}
}
|