summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header/ipversion_test.go
blob: 5f3956160d0091381fc8b0cff2c3411c3a1bf9e1 (plain)
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
// Copyright 2016 The Netstack Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package header_test

import (
	"testing"

	"gvisor.googlesource.com/gvisor/pkg/tcpip/header"
)

func TestIPv4(t *testing.T) {
	b := header.IPv4(make([]byte, header.IPv4MinimumSize))
	b.Encode(&header.IPv4Fields{})

	const want = header.IPv4Version
	if v := header.IPVersion(b); v != want {
		t.Fatalf("Bad version, want %v, got %v", want, v)
	}
}

func TestIPv6(t *testing.T) {
	b := header.IPv6(make([]byte, header.IPv6MinimumSize))
	b.Encode(&header.IPv6Fields{})

	const want = header.IPv6Version
	if v := header.IPVersion(b); v != want {
		t.Fatalf("Bad version, want %v, got %v", want, v)
	}
}

func TestOtherVersion(t *testing.T) {
	const want = header.IPv4Version + header.IPv6Version
	b := make([]byte, 1)
	b[0] = want << 4

	if v := header.IPVersion(b); v != want {
		t.Fatalf("Bad version, want %v, got %v", want, v)
	}
}

func TestTooShort(t *testing.T) {
	b := make([]byte, 1)
	b[0] = (header.IPv4Version + header.IPv6Version) << 4

	// Get the version of a zero-length slice.
	const want = -1
	if v := header.IPVersion(b[:0]); v != want {
		t.Fatalf("Bad version, want %v, got %v", want, v)
	}

	// Get the version of a nil slice.
	if v := header.IPVersion(nil); v != want {
		t.Fatalf("Bad version, want %v, got %v", want, v)
	}
}