| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "github.com/google/gopacket" |
| 5 | "github.com/google/gopacket/layers" |
| 6 | "net" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestDot11Vars(t *testing.T) { |
| 12 | var units = []struct { |
| 13 | got interface{} |
| 14 | exp interface{} |
| 15 | }{ |
| 16 | {openFlags, 1057}, |
| 17 | {wpaFlags, 1041}, |
| 18 | {fakeApRates, []byte{0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, 0x03, 0x01}}, |
| 19 | {fakeApWpaRSN, []byte{ |
| 20 | 0x01, 0x00, // RSN Version 1 |
| 21 | 0x00, 0x0f, 0xac, 0x02, // Group Cipher Suite : 00-0f-ac TKIP |
| 22 | 0x02, 0x00, // 2 Pairwise Cipher Suites (next two lines) |
| 23 | 0x00, 0x0f, 0xac, 0x04, // AES Cipher / CCMP |
| 24 | 0x00, 0x0f, 0xac, 0x02, // TKIP Cipher |
| 25 | 0x01, 0x00, // 1 Authentication Key Management Suite (line below) |
| 26 | 0x00, 0x0f, 0xac, 0x02, // Pre-Shared Key |
| 27 | 0x00, 0x00, |
| 28 | }}, |
| 29 | {wpaSignatureBytes, []byte{0, 0x50, 0xf2, 1}}, |
| 30 | } |
| 31 | for _, u := range units { |
| 32 | if !reflect.DeepEqual(u.exp, u.got) { |
| 33 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func BuildDot11ApConfig() Dot11ApConfig { |
| 39 | ssid := "I still love Ruby, don't worry!" |
| 40 | bssid, _ := net.ParseMAC("pi:ca:tw:as:he:re") |
| 41 | channel := 1 |
| 42 | encryption := false |
| 43 | |
| 44 | config := Dot11ApConfig{ |
| 45 | SSID: ssid, |
| 46 | BSSID: bssid, |
| 47 | Channel: channel, |
| 48 | Encryption: encryption, |
| 49 | } |
| 50 | |
| 51 | return config |
| 52 | } |
| 53 | |
| 54 | func TestDot11ApConfig(t *testing.T) { |
| 55 | ssid := "I still love Ruby, don't worry!" |
| 56 | bssid, _ := net.ParseMAC("pi:ca:tw:as:he:re") |
| 57 | channel := 1 |
| 58 | encryption := false |
| 59 | |
| 60 | config := Dot11ApConfig{ |
| 61 | SSID: ssid, |
| 62 | BSSID: bssid, |
| 63 | Channel: channel, |
| 64 | Encryption: encryption, |
| 65 | } |
| 66 | |
| 67 | var units = []struct { |
| 68 | got interface{} |
| 69 | exp interface{} |
| 70 | }{ |
| 71 | {config.SSID, ssid}, |
| 72 | {config.BSSID, bssid}, |
| 73 | {config.Channel, channel}, |
| 74 | {config.Encryption, encryption}, |
| 75 | } |
| 76 | |
| 77 | for _, u := range units { |
| 78 | if !reflect.DeepEqual(u.exp, u.got) { |
| 79 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestDot11Info(t *testing.T) { |
| 85 | id := layers.Dot11InformationElementIDSSID |
| 86 | info := []byte{} |
| 87 | |
| 88 | dot11InfoElement := Dot11Info(id, info) |
| 89 | |
| 90 | var units = []struct { |
| 91 | got interface{} |
| 92 | exp interface{} |
| 93 | }{ |
| 94 | {dot11InfoElement.ID, id}, |
| 95 | {dot11InfoElement.Length, uint8(len(info))}, |
| 96 | {dot11InfoElement.Info, info}, |
| 97 | } |
| 98 | |
| 99 | for _, u := range units { |
| 100 | if !reflect.DeepEqual(u.exp, u.got) { |
| 101 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestNewDot11Beacon(t *testing.T) { |
| 107 | conf := BuildDot11ApConfig() |
| 108 | seq := uint16(0) |
| 109 | |
| 110 | err, bytes := NewDot11Beacon(conf, seq) |
| 111 | |
| 112 | if err != nil { |
| 113 | t.Error(err) |
| 114 | } |
| 115 | |
| 116 | if len(bytes) <= 0 { |
| 117 | t.Error("unable to create new dot11 beacon") |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestNewDot11Deauth(t *testing.T) { |
| 122 | mac, _ := net.ParseMAC("00:00:00:00:00:00") |
| 123 | seq := uint16(0) |
| 124 | |
| 125 | err, bytes := NewDot11Deauth(mac, mac, mac, seq) |
| 126 | |
| 127 | if err != nil { |
| 128 | t.Error(err) |
| 129 | } |
| 130 | |
| 131 | if len(bytes) <= 0 { |
| 132 | t.Error("unable to create new dot11 beacon") |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func BuildDot11Packet() gopacket.Packet { |
| 137 | mac, _ := net.ParseMAC("00:00:00:00:00:00") |
| 138 | seq := uint16(0) |
| 139 | _, bytes := Serialize( |
| 140 | &layers.RadioTap{}, |
| 141 | &layers.Dot11{ |
| 142 | Address1: mac, |
| 143 | Address2: mac, |
| 144 | Address3: mac, |
| 145 | Type: layers.Dot11TypeMgmtDeauthentication, |
| 146 | SequenceNumber: seq, |
| 147 | }, |
| 148 | &layers.Dot11MgmtDeauthentication{ |
| 149 | Reason: layers.Dot11ReasonClass2FromNonAuth, |
| 150 | }, |
| 151 | ) |
| 152 | |
| 153 | return gopacket.NewPacket(bytes, layers.LayerTypeRadioTap, gopacket.Default) |
| 154 | } |
| 155 | |
| 156 | func TestDot11Parse(t *testing.T) { |
| 157 | packet := BuildDot11Packet() |
| 158 | |
| 159 | ok, radiotap, dot11 := Dot11Parse(packet) |
| 160 | |
| 161 | var units = []struct { |
| 162 | got interface{} |
| 163 | exp interface{} |
| 164 | }{ |
| 165 | // testing for the known bad cases |
| 166 | {ok, false}, |
| 167 | {radiotap, nil}, |
| 168 | {dot11, nil}, |
| 169 | } |
| 170 | |
| 171 | for _, u := range units { |
| 172 | if reflect.DeepEqual(u.exp, u.got) { |
| 173 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestDot11ParseIDSSID(t *testing.T) { |
| 179 | conf := BuildDot11ApConfig() |
| 180 | seq := uint16(0) |
| 181 | |
| 182 | err, bytes := NewDot11Beacon(conf, seq) |
| 183 | |
| 184 | if err != nil { |
| 185 | t.Error(err) |
| 186 | } |
| 187 | |
| 188 | packet := gopacket.NewPacket(bytes, layers.LayerTypeRadioTap, gopacket.Default) |
| 189 | |
| 190 | ok, id := Dot11ParseIDSSID(packet) |
| 191 | |
| 192 | var units = []struct { |
| 193 | got interface{} |
| 194 | exp interface{} |
| 195 | }{ |
| 196 | {ok, true}, |
| 197 | {id, "I still love Ruby, don't worry!"}, |
| 198 | } |
| 199 | |
| 200 | for _, u := range units { |
| 201 | if !reflect.DeepEqual(u.exp, u.got) { |
| 202 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestDot11ParseEncryption(t *testing.T) { |
| 208 | ssid := "I still love Ruby, don't worry!" |
| 209 | bssid, _ := net.ParseMAC("pi:ca:tw:as:he:re") |
| 210 | channel := 1 |
| 211 | encryption := true |
| 212 | |
| 213 | config := Dot11ApConfig{ |
| 214 | SSID: ssid, |
| 215 | BSSID: bssid, |
| 216 | Channel: channel, |
| 217 | Encryption: encryption, |
| 218 | } |
| 219 | |
| 220 | seq := uint16(0) |
| 221 | |
| 222 | err, bytes := NewDot11Beacon(config, seq) |
| 223 | |
| 224 | if err != nil { |
| 225 | t.Error(err) |
| 226 | } |
| 227 | |
| 228 | packet := gopacket.NewPacket(bytes, layers.LayerTypeRadioTap, gopacket.Default) |
| 229 | _, _, dot11 := Dot11Parse(packet) |
| 230 | |
| 231 | found, enc, cipher, auth := Dot11ParseEncryption(packet, dot11) |
| 232 | |
| 233 | var units = []struct { |
| 234 | got interface{} |
| 235 | exp interface{} |
| 236 | }{ |
| 237 | {found, true}, |
| 238 | {enc, "WPA2"}, |
| 239 | {cipher, "TKIP"}, |
| 240 | {auth, "PSK"}, |
| 241 | } |
| 242 | |
| 243 | for _, u := range units { |
| 244 | if !reflect.DeepEqual(u.exp, u.got) { |
| 245 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestDot11IsDataFor(t *testing.T) { |
| 251 | mac, _ := net.ParseMAC("00:00:00:00:00:00") |
| 252 | seq := uint16(0) |
| 253 | _, bytes := Serialize( |
| 254 | &layers.RadioTap{}, |
| 255 | &layers.Dot11{ |
| 256 | Address1: mac, |
| 257 | Address2: mac, |
| 258 | Address3: mac, |
| 259 | Type: layers.Dot11TypeData, |
| 260 | SequenceNumber: seq, |
| 261 | }, |
| 262 | ) |
| 263 | packet := gopacket.NewPacket(bytes, layers.LayerTypeRadioTap, gopacket.Default) |
| 264 | station, _ := net.ParseMAC("00:00:00:00:00:00") |
| 265 | _, _, dot11 := Dot11Parse(packet) |
| 266 | if !Dot11IsDataFor(dot11, station) { |
| 267 | t.Error("unable to determine dot11 packet is for a given station") |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // TODO: add Dot11ParseDSSet test. Not sure how to build proper |
| 272 | // example packet to complete this test, for now. <3 |
| 273 | //func TestDot11ParseDSSet(t *testing.T) { |
| 274 | //} |