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