|
| 1 | +package protocols |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestARPPacketFromBytes(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + raw []byte |
| 13 | + expectedPacket *ARPPacket |
| 14 | + expectedErr error |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "Valid ARP Request Packet", |
| 18 | + raw: []byte{ |
| 19 | + // Ethernet Frame Header |
| 20 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Destination MAC |
| 21 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Source MAC |
| 22 | + 0x08, 0x06, // EtherType (ARP) |
| 23 | + |
| 24 | + // ARP Packet |
| 25 | + 0x00, 0x01, // Hardware Type (Ethernet) |
| 26 | + 0x08, 0x00, // Protocol Type (IPv4) |
| 27 | + 0x06, // Hardware Address Length (6 bytes) |
| 28 | + 0x04, // Protocol Address Length (4 bytes) |
| 29 | + 0x00, 0x01, // Operation (ARP Request) |
| 30 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Sender HW Address |
| 31 | + 0xc0, 0xa8, 0x01, 0x01, // Sender Protocol Address (192.168.1.1) |
| 32 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Target HW Address |
| 33 | + 0xc0, 0xa8, 0x01, 0x02, // Target Protocol Address (192.168.1.2) |
| 34 | + }, |
| 35 | + expectedPacket: &ARPPacket{ |
| 36 | + EthFrame: EthernetFrame{ |
| 37 | + DestinationMAC: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e}, |
| 38 | + SourceMAC: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f}, |
| 39 | + EtherType: 0x0806, |
| 40 | + Payload: []byte{ |
| 41 | + 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, |
| 42 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, 0xc0, 0xa8, |
| 43 | + 0x01, 0x01, 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, |
| 44 | + 0xc0, 0xa8, 0x01, 0x02, |
| 45 | + }, |
| 46 | + }, |
| 47 | + HardwareType: 0x0001, |
| 48 | + ProtocolType: 0x0800, |
| 49 | + HardwareAddrLen: 6, |
| 50 | + ProtocolAddrLen: 4, |
| 51 | + Operation: 0x0001, |
| 52 | + SenderHWAddr: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e}, |
| 53 | + SenderProtoAddr: net.IP{0xc0, 0xa8, 0x01, 0x01}, |
| 54 | + TargetHWAddr: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f}, |
| 55 | + TargetProtoAddr: net.IP{0xc0, 0xa8, 0x01, 0x02}, |
| 56 | + }, |
| 57 | + expectedErr: nil, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Valid ARP Reply Packet", |
| 61 | + raw: []byte{ |
| 62 | + // Ethernet Frame Header |
| 63 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Destination MAC |
| 64 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Source MAC |
| 65 | + 0x08, 0x06, // EtherType (ARP) |
| 66 | + |
| 67 | + // ARP Packet |
| 68 | + 0x00, 0x01, // Hardware Type (Ethernet) |
| 69 | + 0x08, 0x00, // Protocol Type (IPv4) |
| 70 | + 0x06, // Hardware Address Length (6 bytes) |
| 71 | + 0x04, // Protocol Address Length (4 bytes) |
| 72 | + 0x00, 0x02, // Operation (ARP Reply) |
| 73 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Sender HW Address |
| 74 | + 0xc0, 0xa8, 0x01, 0x02, // Sender Protocol Address (192.168.1.2) |
| 75 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Target HW Address |
| 76 | + 0xc0, 0xa8, 0x01, 0x01, // Target Protocol Address (192.168.1.1) |
| 77 | + }, |
| 78 | + expectedPacket: &ARPPacket{ |
| 79 | + EthFrame: EthernetFrame{ |
| 80 | + DestinationMAC: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f}, |
| 81 | + SourceMAC: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e}, |
| 82 | + EtherType: 0x0806, |
| 83 | + Payload: []byte{ |
| 84 | + 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02, |
| 85 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, 0xc0, 0xa8, |
| 86 | + 0x01, 0x02, 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, |
| 87 | + 0xc0, 0xa8, 0x01, 0x01, |
| 88 | + }, |
| 89 | + }, |
| 90 | + HardwareType: 0x0001, |
| 91 | + ProtocolType: 0x0800, |
| 92 | + HardwareAddrLen: 6, |
| 93 | + ProtocolAddrLen: 4, |
| 94 | + Operation: 0x0002, |
| 95 | + SenderHWAddr: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f}, |
| 96 | + SenderProtoAddr: net.IP{0xc0, 0xa8, 0x01, 0x02}, |
| 97 | + TargetHWAddr: net.HardwareAddr{0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e}, |
| 98 | + TargetProtoAddr: net.IP{0xc0, 0xa8, 0x01, 0x01}, |
| 99 | + }, |
| 100 | + expectedErr: nil, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Invalid ARP Packet (Too Short)", |
| 104 | + raw: []byte{ |
| 105 | + // Ethernet Frame Header |
| 106 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Destination MAC |
| 107 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Source MAC |
| 108 | + 0x08, 0x06, // EtherType (ARP) |
| 109 | + |
| 110 | + // ARP Packet (too short) |
| 111 | + 0x00, 0x01, |
| 112 | + 0x08, 0x00, |
| 113 | + 0x06, |
| 114 | + 0x04, |
| 115 | + }, |
| 116 | + expectedPacket: nil, |
| 117 | + expectedErr: errInvalidARPPacket, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "Invalid ARP Packet (Malformed)", |
| 121 | + raw: []byte{ |
| 122 | + // Ethernet Frame Header |
| 123 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Destination MAC |
| 124 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Source MAC |
| 125 | + 0x08, 0x06, // EtherType (ARP) |
| 126 | + |
| 127 | + // ARP Packet (malformed) |
| 128 | + 0x00, 0x01, // Hardware Type |
| 129 | + 0x08, 0x00, // Protocol Type |
| 130 | + 0x06, // Hardware Address Length |
| 131 | + 0x04, // Protocol Address Length |
| 132 | + 0x00, 0x01, // Operation |
| 133 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, // Sender HW Address |
| 134 | + 0xc0, 0xa8, 0x01, 0x01, // Sender Protocol Address |
| 135 | + 0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5f, // Target HW Address |
| 136 | + // Missing Target Protocol Address |
| 137 | + }, |
| 138 | + expectedPacket: nil, |
| 139 | + expectedErr: errInvalidARPPacket, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + p, err := ARPPacketFromBytes(tt.raw) |
| 146 | + if tt.expectedErr != err { |
| 147 | + t.Errorf("expected error to be: %v - got: %v", tt.expectedErr, err) |
| 148 | + } |
| 149 | + |
| 150 | + if !reflect.DeepEqual(tt.expectedPacket, p) { |
| 151 | + t.Errorf("expected packet to be %v - got %v", tt.expectedPacket, p) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments