LEFT | RIGHT |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package ipv4_test | 5 package ipv4_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "code.google.com/p/go.net/ipv4" | 9 "code.google.com/p/go.net/ipv4" |
10 "net" | 10 "net" |
11 "reflect" | 11 "reflect" |
12 "runtime" | 12 "runtime" |
13 "testing" | 13 "testing" |
14 ) | 14 ) |
15 | 15 |
16 var ( | 16 var ( |
17 » incomingWireHeader = [ipv4.HeaderLen]byte{ | 17 » wireHeaderFromKernel = [ipv4.HeaderLen]byte{ |
18 » » 0x45, 0x01, 0xbe, 0xef, 0xca, 0xfe, 0x05, 0xdc, 0xff, 0x01, | 18 » » 0x45, 0x01, 0xbe, 0xef, |
19 » » 0xde, 0xad, 172, 16, 254, 254, 192, 168, 0, 1, | 19 » » 0xca, 0xfe, 0x05, 0xdc, |
| 20 » » 0xff, 0x01, 0xde, 0xad, |
| 21 » » 172, 16, 254, 254, |
| 22 » » 192, 168, 0, 1, |
20 } | 23 } |
21 » outgoingWireHeader = [ipv4.HeaderLen]byte{ | 24 » wireHeaderToKernel = [ipv4.HeaderLen]byte{ |
22 » » 0x45, 0x01, 0xbe, 0xef, 0xca, 0xfe, 0x05, 0xdc, 0xff, 0x01, | 25 » » 0x45, 0x01, 0xbe, 0xef, |
23 » » 0xde, 0xad, 172, 16, 254, 254, 192, 168, 0, 1, | 26 » » 0xca, 0xfe, 0x05, 0xdc, |
| 27 » » 0xff, 0x01, 0xde, 0xad, |
| 28 » » 172, 16, 254, 254, |
| 29 » » 192, 168, 0, 1, |
24 } | 30 } |
25 » incomingTradBSDWireHeader = [ipv4.HeaderLen]byte{ | 31 » wireHeaderFromTradBSDKernel = [ipv4.HeaderLen]byte{ |
26 » » 0x45, 0x01, 0xdb, 0xbe, 0xca, 0xfe, 0xdc, 0x05, 0xff, 0x01, | 32 » » 0x45, 0x01, 0xdb, 0xbe, |
27 » » 0xde, 0xad, 172, 16, 254, 254, 192, 168, 0, 1, | 33 » » 0xca, 0xfe, 0xdc, 0x05, |
| 34 » » 0xff, 0x01, 0xde, 0xad, |
| 35 » » 172, 16, 254, 254, |
| 36 » » 192, 168, 0, 1, |
28 } | 37 } |
29 » outgoingTradBSDWireHeader = [ipv4.HeaderLen]byte{ | 38 » wireHeaderToTradBSDKernel = [ipv4.HeaderLen]byte{ |
30 » » 0x45, 0x01, 0xef, 0xbe, 0xca, 0xfe, 0xdc, 0x05, 0xff, 0x01, | 39 » » 0x45, 0x01, 0xef, 0xbe, |
31 » » 0xde, 0xad, 172, 16, 254, 254, 192, 168, 0, 1, | 40 » » 0xca, 0xfe, 0xdc, 0x05, |
| 41 » » 0xff, 0x01, 0xde, 0xad, |
| 42 » » 172, 16, 254, 254, |
| 43 » » 192, 168, 0, 1, |
32 } | 44 } |
| 45 // TODO(mikio): Add platform dependent wire header formats when |
| 46 // we support new platforms. |
33 ) | 47 ) |
34 | 48 |
35 func testHeader() *ipv4.Header { | 49 func testHeader() *ipv4.Header { |
36 h := &ipv4.Header{} | 50 h := &ipv4.Header{} |
37 » h.Version = ipv4.ProtocolVersion | 51 » h.Version = ipv4.Version |
38 h.Len = ipv4.HeaderLen | 52 h.Len = ipv4.HeaderLen |
39 h.TOS = 1 | 53 h.TOS = 1 |
40 h.TotalLen = 0xbeef | 54 h.TotalLen = 0xbeef |
41 h.ID = 0xcafe | 55 h.ID = 0xcafe |
42 h.FragOff = 1500 | 56 h.FragOff = 1500 |
43 h.TTL = 255 | 57 h.TTL = 255 |
44 h.Protocol = 1 | 58 h.Protocol = 1 |
45 h.Checksum = 0xdead | 59 h.Checksum = 0xdead |
46 h.Src = net.IPv4(172, 16, 254, 254) | 60 h.Src = net.IPv4(172, 16, 254, 254) |
47 h.Dst = net.IPv4(192, 168, 0, 1) | 61 h.Dst = net.IPv4(192, 168, 0, 1) |
48 return h | 62 return h |
49 } | 63 } |
50 | 64 |
51 func TestMarshalHeader(t *testing.T) { | 65 func TestMarshalHeader(t *testing.T) { |
52 th := testHeader() | 66 th := testHeader() |
53 b, err := th.Marshal() | 67 b, err := th.Marshal() |
54 if err != nil { | 68 if err != nil { |
55 » » t.Fatalf("ipv4.MarshalHeader failed: %v", err) | 69 » » t.Fatalf("ipv4.Header.Marshal failed: %v", err) |
56 } | 70 } |
57 var wh []byte | 71 var wh []byte |
58 switch runtime.GOOS { | 72 switch runtime.GOOS { |
59 case "linux", "openbsd": | 73 case "linux", "openbsd": |
60 » » wh = outgoingWireHeader[:] | 74 » » wh = wireHeaderToKernel[:] |
61 default: | 75 default: |
62 » » wh = outgoingTradBSDWireHeader[:] | 76 » » wh = wireHeaderToTradBSDKernel[:] |
63 } | 77 } |
64 if !bytes.Equal(b, wh) { | 78 if !bytes.Equal(b, wh) { |
65 t.Fatalf("ipv4.Header.Marshal failed: %#v not equal %#v", b, wh) | 79 t.Fatalf("ipv4.Header.Marshal failed: %#v not equal %#v", b, wh) |
66 } | 80 } |
67 } | 81 } |
68 | 82 |
69 func TestParseHeader(t *testing.T) { | 83 func TestParseHeader(t *testing.T) { |
70 var wh []byte | 84 var wh []byte |
71 switch runtime.GOOS { | 85 switch runtime.GOOS { |
72 case "linux", "openbsd": | 86 case "linux", "openbsd": |
73 » » wh = incomingWireHeader[:] | 87 » » wh = wireHeaderFromKernel[:] |
74 default: | 88 default: |
75 » » wh = incomingTradBSDWireHeader[:] | 89 » » wh = wireHeaderFromTradBSDKernel[:] |
76 } | 90 } |
77 h, err := ipv4.ParseHeader(wh) | 91 h, err := ipv4.ParseHeader(wh) |
78 if err != nil { | 92 if err != nil { |
79 t.Fatalf("ipv4.ParseHeader failed: %v", err) | 93 t.Fatalf("ipv4.ParseHeader failed: %v", err) |
80 } | 94 } |
81 th := testHeader() | 95 th := testHeader() |
82 if !reflect.DeepEqual(h, th) { | 96 if !reflect.DeepEqual(h, th) { |
83 t.Fatalf("ipv4.ParseHeader failed: %#v not equal %#v", h, th) | 97 t.Fatalf("ipv4.ParseHeader failed: %#v not equal %#v", h, th) |
84 } | 98 } |
85 } | 99 } |
LEFT | RIGHT |