1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
package ipstack
import (
"fmt"
"net/netip"
"testing"
)
//func TestInitialize(t *testing.T) {
// lnxFilePath := "../../doc-example/r2.lnx"
// err := Initialize(lnxFilePath)
// if err != nil {
// t.Error(err)
// }
// fmt.Printf("Interfaces:\n%s\n\n", SprintInterfaces())
// fmt.Printf("Neighbors:\n%s\n", SprintNeighbors())
// fmt.Printf("RoutingTable:\n%s\n", SprintRoutingTable())
//
// fmt.Println("TestInitialize successful")
// t.Cleanup(func() { CleanUp() })
//}
//
//func TestInterfaceUpThenDown(t *testing.T) {
// lnxFilePath := "../../doc-example/r2.lnx"
// err := Initialize(lnxFilePath)
// if err != nil {
// t.Error(err)
// }
//
// iface, err := GetInterfaceByName("if0")
// if err != nil {
// t.Error(err)
// }
//
// InterfaceUp(iface)
// if iface.State == false {
// t.Error("iface state should be true")
// }
//
// fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())
//
// time.Sleep(5 * time.Millisecond) // allow time to print
//
// InterfaceDown(iface)
// if iface.State == true {
// t.Error("iface state should be false")
// }
//
// time.Sleep(5 * time.Millisecond) // allow time to print
//
// fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())
//
// fmt.Println("TestInterfaceUpThenDown successful")
// t.Cleanup(func() { CleanUp() })
//}
//
//func TestInterfaceUpThenDownTwice(t *testing.T) {
// lnxFilePath := "../../doc-example/r2.lnx"
// err := Initialize(lnxFilePath)
// if err != nil {
// t.Error(err)
// }
//
// iface, err := GetInterfaceByName("if0")
// if err != nil {
// t.Error(err)
// }
//
// InterfaceUp(iface)
// if iface.State == false {
// t.Error("iface state should be true")
// }
//
// fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())
//
// time.Sleep(5 * time.Millisecond) // allow time to print
//
// fmt.Println("putting interface down")
// InterfaceDown(iface)
// if iface.State == true {
// t.Error("iface state should be false")
// }
//
// time.Sleep(3 * time.Millisecond)
//
// fmt.Println("putting interface back up for 3 iterations")
// InterfaceUp(iface)
// if iface.State == false {
// t.Error("iface state should be true")
// }
// time.Sleep(3 * time.Millisecond) // allow time to print
//
// fmt.Println("putting interface down")
// InterfaceDown(iface)
// if iface.State == true {
// t.Error("iface state should be false")
// }
//
// time.Sleep(5 * time.Millisecond) // allow time to print
//
// fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())
//
// fmt.Println("TestInterfaceUpThenDownTwice successful")
// t.Cleanup(func() { CleanUp() })
//}
//
//func TestSendIPToNeighbor(t *testing.T) {
// lnxFilePath := "../../doc-example/r2.lnx"
// err := Initialize(lnxFilePath)
// if err != nil {
// t.Error(err)
// }
//
// // get the first neighbor of this interface
// iface, err := GetInterfaceByName("if0")
// if err != nil {
// t.Error(err)
// }
// neighbors, err := GetNeighborsToInterface("if0")
// if err != nil {
// t.Error(err)
// }
//
// // setup a neighbor listener socket
// testNeighbor := neighbors[0]
// // close the socket so we can listen on it
// err = testNeighbor.SendSocket.Close()
// if err != nil {
// t.Error(err)
// }
//
// fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())
// fmt.Printf("Neighbors:\n%s\n", SprintNeighbors())
//
// listenString := testNeighbor.UdpAddr.String()
// fmt.Println("listening on " + listenString)
// listenAddr, err := net.ResolveUDPAddr("udp4", listenString)
// if err != nil {
// t.Error(err)
// }
// recvSocket, err := net.ListenUDP("udp4", listenAddr)
// if err != nil {
// t.Error(err)
// }
// testNeighbor.SendSocket = *recvSocket
//
// sent := false
// go func() {
// buffer := make([]byte, MAX_IP_PACKET_SIZE)
// fmt.Println("wating to read from UDP socket")
// _, sourceAddr, err := recvSocket.ReadFromUDP(buffer)
// if err != nil {
// t.Error(err)
// }
// fmt.Println("read from UDP socket")
// hdr, err := ipv4header.ParseHeader(buffer)
// if err != nil {
// t.Error(err)
// }
// headerSize := hdr.Len
// headerBytes := buffer[:headerSize]
// checksumFromHeader := uint16(hdr.Checksum)
// computedChecksum := ValidateChecksum(headerBytes, checksumFromHeader)
//
// var checksumState string
// if computedChecksum == checksumFromHeader {
// checksumState = "OK"
// } else {
// checksumState = "FAIL"
// }
// message := buffer[headerSize:]
// fmt.Printf("Received IP packet from %s\nHeader: %v\nChecksum: %s\nMessage: %s\n",
// sourceAddr.String(), hdr, checksumState, string(message))
// if err != nil {
// t.Error(err)
// }
//
// sent = true
// }()
//
// time.Sleep(10 * time.Millisecond)
//
// // send a message to the neighbor
// fmt.Printf("sending message to neighbor\t%t\n", sent)
// err = SendIP(*iface, *testNeighbor, 0, []byte("You are my firest neighbor!"))
// if err != nil {
// t.Error(err)
// }
//
// fmt.Printf("SENT message to neighbor\t%t\n", sent)
// // give a little time for the message to be sent
// time.Sleep(1000 * time.Millisecond)
// if !sent {
// t.Error("Message not sent")
// t.Fail()
// }
//
// fmt.Println("TestSendIPToNeighbor successful")
// t.Cleanup(func() { CleanUp() })
//}
//
//func TestRecvIP(t *testing.T) {
// lnxFilePath := "../../doc-example/r2.lnx"
// err := Initialize(lnxFilePath)
// if err != nil {
// t.Error(err)
// }
//
// // get the first neighbor of this interface to RecvIP from
// iface, err := GetInterfaceByName("if0")
// if err != nil {
// t.Error(err)
// }
// InterfaceUp(iface)
//
// // setup a random socket to send an ip packet from
// listenAddr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:6969")
// sendSocket, err := net.ListenUDP("udp4", listenAddr)
//
// // send a message to the neighbor
// ifaceAsNeighbor := Neighbor{
// VipAddr: iface.IpPrefix.Addr(),
// UdpAddr: iface.UdpAddr,
// SendSocket: iface.RecvSocket,
// SocketChannel: iface.SocketChannel,
// }
// fakeIface := Interface{
// Name: "if69",
// IpPrefix: netip.MustParsePrefix("10.69.0.1/24"),
// UdpAddr: netip.MustParseAddrPort("127.0.0.1:6969"),
// RecvSocket: net.UDPConn{},
// SocketChannel: nil,
// State: true,
// }
// err = SendIP(fakeIface, ifaceAsNeighbor, 0, []byte("hello"))
// if err != nil {
// return
// }
//
// time.Sleep(10 * time.Millisecond)
//
// // TODO: potenially make this a channel, so it actually checks values.
// // For now, you must read the message from the console.
//
// err = sendSocket.Close()
// if err != nil {
// t.Error(err)
// }
// t.Cleanup(func() { CleanUp() })
//}
func TestIntersect(t *testing.T) {
net1 := netip.MustParsePrefix("1.1.1.1/24")
net2 := netip.MustParsePrefix("1.1.1.2/24")
net3 := netip.MustParsePrefix("1.0.0.1/24")
net4 := netip.MustParsePrefix("0.0.0.0/0") // default route
net5 := netip.MustParsePrefix("1.1.1.1/32")
res00 := intersect(net1, net5)
if !res00 {
t.Error("net1 and net2 should intersect")
t.Fail()
}
res01 := intersect(net5, net1)
if res01 {
t.Error("net1 and net2 should not intersect")
t.Fail()
}
res1 := intersect(net1, net2)
if !res1 {
t.Error("net1 and net2 should intersect")
t.Fail()
}
res0 := intersect(net2, net1)
if !res0 {
t.Error("net1 and net2 should intersect")
t.Fail()
}
res6 := intersect(net1, net3)
if res6 {
t.Error("net1 and net3 should not intersect")
t.Fail()
}
res2 := intersect(net2, net3)
if res2 {
t.Error("net2 and net3 should not intersect")
t.Fail()
}
res3 := intersect(net1, net4)
if !res3 {
t.Error("net1 and net4 should intersect")
t.Fail()
}
res4 := intersect(net2, net4)
if !res4 {
t.Error("net2 and net4 should intersect")
t.Fail()
}
res5 := intersect(net3, net4)
if !res5 {
t.Error("net3 and net4 should intersect")
t.Fail()
}
fmt.Println("TestIntersect successful")
}
|