Debian Codegraph @

network/lan_test.go

raw symbols SCIP
1 package network
2
3 import (
4 "testing"
5
6 "github.com/evilsocket/islazy/data"
7 )
8
9 func buildExampleLAN() *LAN {
10 iface, _ := FindInterface("")
11 gateway, _ := FindGateway(iface)
12 exNewCallback := func(e *Endpoint) {}
13 exLostCallback := func(e *Endpoint) {}
14 aliases := &data.UnsortedKV{}
15 return NewLAN(iface, gateway, aliases, exNewCallback, exLostCallback)
16 }
17
18 func buildExampleEndpoint() *Endpoint {
19 iface, _ := FindInterface("")
20 return iface
21 }
22
23 func TestNewLAN(t *testing.T) {
24 iface, err := FindInterface("")
25 if err != nil {
26 t.Error("no iface found", err)
27 }
28
29 gateway, err := FindGateway(iface)
30 if err != nil {
31 t.Error("no gateway found", err)
32 }
33 exNewCallback := func(e *Endpoint) {}
34 exLostCallback := func(e *Endpoint) {}
35 aliases := &data.UnsortedKV{}
36 lan := NewLAN(iface, gateway, aliases, exNewCallback, exLostCallback)
37 if lan.iface != iface {
38 t.Fatalf("expected '%v', got '%v'", iface, lan.iface)
39 }
40 if lan.gateway != gateway {
41 t.Fatalf("expected '%v', got '%v'", gateway, lan.gateway)
42 }
43 if len(lan.hosts) != 0 {
44 t.Fatalf("expected '%v', got '%v'", 0, len(lan.hosts))
45 }
46 // FIXME: update this to current code base
47 // if !(len(lan.aliases.data) >= 0) {
48 // t.Fatalf("expected '%v', got '%v'", 0, len(lan.aliases.data))
49 // }
50 }
51
52 func TestMarshalJSON(t *testing.T) {
53 iface, err := FindInterface("")
54 if err != nil {
55 t.Error("no iface found", err)
56 }
57 gateway, err := FindGateway(iface)
58 if err != nil {
59 t.Error("no gateway found", err)
60 }
61 exNewCallback := func(e *Endpoint) {}
62 exLostCallback := func(e *Endpoint) {}
63 aliases := &data.UnsortedKV{}
64 lan := NewLAN(iface, gateway, aliases, exNewCallback, exLostCallback)
65 _, err = lan.MarshalJSON()
66 if err != nil {
67 t.Error(err)
68 }
69 }
70
71 // FIXME: update this to current code base
72 // func TestSetAliasFor(t *testing.T) {
73 // exampleAlias := "picat"
74 // exampleLAN := buildExampleLAN()
75 // exampleEndpoint := buildExampleEndpoint()
76 // exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
77 // if !exampleLAN.SetAliasFor(exampleEndpoint.HwAddress, exampleAlias) {
78 // t.Error("unable to set alias for a given mac address")
79 // }
80 // }
81
82 func TestGet(t *testing.T) {
83 exampleLAN := buildExampleLAN()
84 exampleEndpoint := buildExampleEndpoint()
85 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
86 foundEndpoint, foundBool := exampleLAN.Get(exampleEndpoint.HwAddress)
87 if foundEndpoint.String() != exampleEndpoint.String() {
88 t.Fatalf("expected '%v', got '%v'", foundEndpoint, exampleEndpoint)
89 }
90 if !foundBool {
91 t.Error("unable to get known endpoint via mac address from LAN struct")
92 }
93 }
94
95 func TestList(t *testing.T) {
96 exampleLAN := buildExampleLAN()
97 exampleEndpoint := buildExampleEndpoint()
98 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
99 foundList := exampleLAN.List()
100 if len(foundList) != 1 {
101 t.Fatalf("expected '%d', got '%d'", 1, len(foundList))
102 }
103 exp := 1
104 got := len(exampleLAN.List())
105 if got != exp {
106 t.Fatalf("expected '%d', got '%d'", exp, got)
107 }
108 }
109
110 // FIXME: update this to current code base
111 // func TestAliases(t *testing.T) {
112 // exampleAlias := "picat"
113 // exampleLAN := buildExampleLAN()
114 // exampleEndpoint := buildExampleEndpoint()
115 // exampleLAN.hosts["pi:ca:tw:as:he:re"] = exampleEndpoint
116 // exp := exampleAlias
117 // got := exampleLAN.Aliases().Get("pi:ca:tw:as:he:re")
118 // if got != exp {
119 // t.Fatalf("expected '%v', got '%v'", exp, got)
120 // }
121 // }
122
123 func TestWasMissed(t *testing.T) {
124 exampleLAN := buildExampleLAN()
125 exampleEndpoint := buildExampleEndpoint()
126 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
127 exp := false
128 got := exampleLAN.WasMissed(exampleEndpoint.HwAddress)
129 if got != exp {
130 t.Fatalf("expected '%v', got '%v'", exp, got)
131 }
132 }
133
134 // TODO Add TestRemove after removing unnecessary ip argument
135 // func TestRemove(t *testing.T) {
136 // }
137
138 func TestHas(t *testing.T) {
139 exampleLAN := buildExampleLAN()
140 exampleEndpoint := buildExampleEndpoint()
141 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
142 if !exampleLAN.Has(exampleEndpoint.IpAddress) {
143 t.Error("unable find a known IP address in LAN struct")
144 }
145 }
146
147 func TestEachHost(t *testing.T) {
148 exampleBuffer := []string{}
149 exampleLAN := buildExampleLAN()
150 exampleEndpoint := buildExampleEndpoint()
151 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
152 exampleCB := func(mac string, e *Endpoint) {
153 exampleBuffer = append(exampleBuffer, exampleEndpoint.HwAddress)
154 }
155 exampleLAN.EachHost(exampleCB)
156 exp := 1
157 got := len(exampleBuffer)
158 if got != exp {
159 t.Fatalf("expected '%d', got '%d'", exp, got)
160 }
161 }
162
163 func TestGetByIp(t *testing.T) {
164 exampleLAN := buildExampleLAN()
165 exampleEndpoint := buildExampleEndpoint()
166 exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
167
168 exp := exampleEndpoint
169 got := exampleLAN.GetByIp(exampleEndpoint.IpAddress)
170 if got.String() != exp.String() {
171 t.Fatalf("expected '%v', got '%v'", exp, got)
172 }
173 }
174
175 func TestAddIfNew(t *testing.T) {
176 exampleLAN := buildExampleLAN()
177 iface, _ := FindInterface("")
178 // won't add our own IP address
179 if exampleLAN.AddIfNew(iface.IpAddress, iface.HwAddress) != nil {
180 t.Error("added address that should've been ignored ( your own )")
181 }
182 }
183
184 // FIXME: update this to current code base
185 // func TestGetAlias(t *testing.T) {
186 // exampleAlias := "picat"
187 // exampleLAN := buildExampleLAN()
188 // exampleEndpoint := buildExampleEndpoint()
189 // exampleLAN.hosts[exampleEndpoint.HwAddress] = exampleEndpoint
190 // exp := exampleAlias
191 // got := exampleLAN.GetAlias(exampleEndpoint.HwAddress)
192 // if got != exp {
193 // t.Fatalf("expected '%v', got '%v'", exp, got)
194 // }
195 // }
196
197 func TestShouldIgnore(t *testing.T) {
198 exampleLAN := buildExampleLAN()
199 iface, _ := FindInterface("")
200 gateway, _ := FindGateway(iface)
201 exp := true
202 got := exampleLAN.shouldIgnore(iface.IpAddress, iface.HwAddress)
203 if got != exp {
204 t.Fatalf("expected '%v', got '%v'", exp, got)
205 }
206 got = exampleLAN.shouldIgnore(gateway.IpAddress, gateway.HwAddress)
207 if got != exp {
208 t.Fatalf("expected '%v', got '%v'", exp, got)
209 }
210 }