| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestNewARPTo(t *testing.T) { |
| 10 | from := net.IP{0, 0, 0, 0} |
| 11 | from_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 12 | to := net.IP{0, 0, 0, 0} |
| 13 | to_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 14 | req := uint16(0) |
| 15 | |
| 16 | eth, arp := NewARPTo(from, from_hw, to, to_hw, req) |
| 17 | |
| 18 | if !reflect.DeepEqual(eth.SrcMAC, from_hw) { |
| 19 | t.Fatalf("expected '%s', got '%s'", eth.SrcMAC, from_hw) |
| 20 | } |
| 21 | |
| 22 | if !reflect.DeepEqual(eth.DstMAC, to_hw) { |
| 23 | t.Fatalf("expected '%s', got '%s'", eth.DstMAC, to_hw) |
| 24 | } |
| 25 | |
| 26 | if !reflect.DeepEqual(arp.Operation, req) { |
| 27 | t.Fatalf("expected '%d', got '%d'", arp.Operation, req) |
| 28 | } |
| 29 | |
| 30 | if !reflect.DeepEqual(arp.SourceHwAddress, []byte(from_hw)) { |
| 31 | t.Fatalf("expected '%v', got '%v'", arp.SourceHwAddress, []byte(from_hw)) |
| 32 | } |
| 33 | |
| 34 | if !reflect.DeepEqual(arp.DstHwAddress, []byte(to_hw)) { |
| 35 | t.Fatalf("expected '%v', got '%v'", arp.DstHwAddress, []byte(to_hw)) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestNewARP(t *testing.T) { |
| 40 | from := net.IP{0, 0, 0, 0} |
| 41 | from_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 42 | to_hw, _ := net.ParseMAC("00:00:00:00:00:00") |
| 43 | to := net.IP{0, 0, 0, 0} |
| 44 | req := uint16(0) |
| 45 | |
| 46 | eth, arp := NewARP(from, from_hw, to, req) |
| 47 | |
| 48 | if !reflect.DeepEqual(eth.SrcMAC, from_hw) { |
| 49 | t.Fatalf("expected '%s', got '%s'", eth.SrcMAC, from_hw) |
| 50 | } |
| 51 | |
| 52 | if !reflect.DeepEqual(eth.DstMAC, to_hw) { |
| 53 | t.Fatalf("expected '%s', got '%s'", eth.DstMAC, to_hw) |
| 54 | } |
| 55 | |
| 56 | if !reflect.DeepEqual(arp.Operation, req) { |
| 57 | t.Fatalf("expected '%d', got '%d'", arp.Operation, req) |
| 58 | } |
| 59 | |
| 60 | if !reflect.DeepEqual(arp.SourceHwAddress, []byte(from_hw)) { |
| 61 | t.Fatalf("expected '%v', got '%v'", arp.SourceHwAddress, []byte(from_hw)) |
| 62 | } |
| 63 | |
| 64 | if !reflect.DeepEqual(arp.DstHwAddress, []byte{0, 0, 0, 0, 0, 0}) { |
| 65 | t.Fatalf("expected '%v', got '%v'", arp.DstHwAddress, []byte{0, 0, 0, 0, 0, 0}) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestNewARPRequest(t *testing.T) { |
| 70 | from := net.IP{0, 0, 0, 0} |
| 71 | from_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 72 | to := net.IP{0, 0, 0, 0} |
| 73 | |
| 74 | err, bytes := NewARPRequest(from, from_hw, to) |
| 75 | if err != nil { |
| 76 | t.Error(err) |
| 77 | } |
| 78 | |
| 79 | if len(bytes) <= 0 { |
| 80 | t.Error("unable to serialize new arp request packet") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestNewARPReply(t *testing.T) { |
| 85 | from := net.IP{0, 0, 0, 0} |
| 86 | from_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 87 | to := net.IP{0, 0, 0, 0} |
| 88 | to_hw, _ := net.ParseMAC("01:23:45:67:89:ab") |
| 89 | |
| 90 | err, bytes := NewARPReply(from, from_hw, to, to_hw) |
| 91 | if err != nil { |
| 92 | t.Error(err) |
| 93 | } |
| 94 | |
| 95 | if len(bytes) <= 0 { |
| 96 | t.Error("unable to serialize new arp request packet") |
| 97 | } |
| 98 | } |