Debian Codegraph @

network/wifi_test.go

raw symbols SCIP
1 package network
2
3 import (
4 "testing"
5
6 "github.com/evilsocket/islazy/data"
7 )
8
9 // Define test data for dot11 frequency <-> channel tests
10 type dot11pair struct {
11 frequency int
12 channel int
13 }
14
15 var dot11TestVector = []dot11pair{
16 {2472, 13},
17 {2484, 14},
18 {5825, 165},
19 {5885, 177},
20 }
21
22 func buildExampleWiFi() *WiFi {
23 aliases := &data.UnsortedKV{}
24 return NewWiFi(buildExampleEndpoint(), aliases, func(ap *AccessPoint) {}, func(ap *AccessPoint) {})
25 }
26
27 func TestDot11Freq2Chan(t *testing.T) {
28 for _, entry := range dot11TestVector {
29 gotChannel := Dot11Freq2Chan(entry.frequency)
30 if gotChannel != entry.channel {
31 t.Fatalf("expected '%v', got '%v'", entry.channel, gotChannel)
32 }
33 }
34 }
35
36 func TestDot11Chan2Freq(t *testing.T) {
37 for _, entry := range dot11TestVector {
38 gotFrequency := Dot11Chan2Freq(entry.channel)
39 if gotFrequency != entry.frequency {
40 t.Fatalf("expected '%v', got '%v'", entry.frequency, gotFrequency)
41 }
42 }
43 }
44
45 func TestNewWiFi(t *testing.T) {
46 aliases := &data.UnsortedKV{}
47 exampleWiFi := NewWiFi(buildExampleEndpoint(), aliases, func(ap *AccessPoint) {}, func(ap *AccessPoint) {})
48 if exampleWiFi == nil {
49 t.Error("unable to build net wifi struct")
50 }
51 }
52
53 func TestWiFiMarshalJSON(t *testing.T) {
54 exampleWiFi := buildExampleWiFi()
55 json, err := exampleWiFi.MarshalJSON()
56 if err != nil {
57 t.Error(err)
58 }
59 if len(json) <= 0 {
60 t.Error("unable to marshal JSON from WiFi struct")
61 }
62 }
63
64 func TestEachAccessPoint(t *testing.T) {
65 exampleWiFi := buildExampleWiFi()
66 aliases := &data.UnsortedKV{}
67 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
68 exampleWiFi.aps["ff:ff:ff:ff:ff:f1"] = exampleAP
69 exampleWiFi.aps["ff:ff:ff:ff:ff:f2"] = exampleAP
70 count := 0
71 exampleCB := func(mac string, ap *AccessPoint) {
72 count++
73 }
74 exampleWiFi.EachAccessPoint(exampleCB)
75 if count != 2 {
76 t.Error("unable to perform callback function for each access point")
77 }
78 }
79
80 func TestStations(t *testing.T) {
81 exampleWiFi := buildExampleWiFi()
82 aliases := &data.UnsortedKV{}
83 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
84 exampleWiFi.aps["ff:ff:ff:ff:ff:f1"] = exampleAP
85 exampleWiFi.aps["ff:ff:ff:ff:ff:f2"] = exampleAP
86 exp := 2
87 got := len(exampleWiFi.Stations())
88 if got != exp {
89 t.Fatalf("expected '%v', got '%v'", exp, got)
90 }
91 }
92
93 func TestWiFiList(t *testing.T) {
94 exampleWiFi := buildExampleWiFi()
95 aliases := &data.UnsortedKV{}
96 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
97 exampleWiFi.aps["ff:ff:ff:ff:ff:f1"] = exampleAP
98 exampleWiFi.aps["ff:ff:ff:ff:ff:f2"] = exampleAP
99 exp := 2
100 got := len(exampleWiFi.List())
101 if got != exp {
102 t.Fatalf("expected '%v', got '%v'", exp, got)
103 }
104 }
105
106 func TestWiFiRemove(t *testing.T) {
107 exampleWiFi := buildExampleWiFi()
108 aliases := &data.UnsortedKV{}
109 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
110 exampleWiFi.aps["ff:ff:ff:ff:ff:f1"] = exampleAP
111 exampleWiFi.aps["ff:ff:ff:ff:ff:f2"] = exampleAP
112 exampleWiFi.Remove("ff:ff:ff:ff:ff:f1")
113 exp := 1
114 got := len(exampleWiFi.List())
115 if got != exp {
116 t.Fatalf("expected '%v', got '%v'", exp, got)
117 }
118 }
119
120 func TestWiFiAddIfNew(t *testing.T) {
121 exampleWiFi := buildExampleWiFi()
122 aliases := &data.UnsortedKV{}
123 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
124 exampleWiFi.aps["ff:ff:ff:ff:ff:f1"] = exampleAP
125 exampleWiFi.aps["ff:ff:ff:ff:ff:f2"] = exampleAP
126 exampleWiFi.AddIfNew("my_wifi2", "ff:ff:ff:ff:ff:f3", 2472, int8(0))
127 exp := 3
128 got := len(exampleWiFi.List())
129 if got != exp {
130 t.Fatalf("expected '%v', got '%v'", exp, got)
131 }
132 }
133
134 func TestWiFiGet(t *testing.T) {
135 exampleWiFi := buildExampleWiFi()
136 aliases := &data.UnsortedKV{}
137 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
138 exampleWiFi.aps["ff:ff:ff:ff:ff:ff"] = exampleAP
139 _, found := exampleWiFi.Get("ff:ff:ff:ff:ff:ff")
140 if !found {
141 t.Error("unable to get access point from wifi struct with mac address")
142 }
143 }
144
145 func TestWiFiGetClient(t *testing.T) {
146 exampleWiFi := buildExampleWiFi()
147 aliases := &data.UnsortedKV{}
148 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
149 exampleClient := NewStation("my_wifi", "ff:ff:ff:ff:ff:xx", 2472, int8(0))
150 exampleAP.clients["ff:ff:ff:ff:ff:xx"] = exampleClient
151 exampleWiFi.aps["ff:ff:ff:ff:ff:ff"] = exampleAP
152 _, found := exampleWiFi.GetClient("ff:ff:ff:ff:ff:xx")
153 if !found {
154 t.Error("unable to get client from wifi struct with mac address")
155 }
156 }
157
158 func TestWiFiClear(t *testing.T) {
159 exampleWiFi := buildExampleWiFi()
160 aliases := &data.UnsortedKV{}
161 exampleAP := NewAccessPoint("my_wifi", "ff:ff:ff:ff:ff:ff", 2472, int8(0), aliases)
162 exampleWiFi.aps["ff:ff:ff:ff:ff:ff"] = exampleAP
163 exampleWiFi.Clear()
164 if len(exampleWiFi.aps) != 0 {
165 t.Error("unable to clear known access point for wifi struct")
166 }
167 }