summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header/ipversion_test.go
diff options
context:
space:
mode:
authorGoogler <noreply@google.com>2018-04-27 10:37:02 -0700
committerAdin Scannell <ascannell@google.com>2018-04-28 01:44:26 -0400
commitd02b74a5dcfed4bfc8f2f8e545bca4d2afabb296 (patch)
tree54f95eef73aee6bacbfc736fffc631be2605ed53 /pkg/tcpip/header/ipversion_test.go
parentf70210e742919f40aa2f0934a22f1c9ba6dada62 (diff)
Check in gVisor.
PiperOrigin-RevId: 194583126 Change-Id: Ica1d8821a90f74e7e745962d71801c598c652463
Diffstat (limited to 'pkg/tcpip/header/ipversion_test.go')
-rw-r--r--pkg/tcpip/header/ipversion_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/pkg/tcpip/header/ipversion_test.go b/pkg/tcpip/header/ipversion_test.go
new file mode 100644
index 000000000..5f3956160
--- /dev/null
+++ b/pkg/tcpip/header/ipversion_test.go
@@ -0,0 +1,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)
+ }
+}