Repository navigation
Expand file tree
/
Copy pathspoof_test.go
More file actions
86 lines (77 loc) · 2.49 KB
/
Copy pathspoof_test.go
File metadata and controls
86 lines (77 loc) · 2.49 KB
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
package arp
import (
"context"
"sync"
"testing"
"time"
marp "github.com/mdlayher/arp"
)
func TestHandler_ForceIPChange(t *testing.T) {
//Debug = true
// log.SetLevel(log.DebugLevel)
h, conn := testHandler(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
go func() {
wg.Add(1)
h.ListenAndServe(ctx)
wg.Done()
}()
time.Sleep(time.Millisecond * 20) // time for ListenAndServe to start
e2, _ := h.table.upsert(StateNormal, mac2, ip2)
e2.Online = true
h.table.updateIP(e2, ip3)
h.table.updateIP(e2, ip4)
h.ForceIPChange(e2.MAC, true)
if e := h.table.findByMAC(mac2); e == nil || e.State != StateHunt || !e.Online {
t.Fatalf("Test_ForceIPChange entry2 state=%s, online=%v", e.State, e.Online)
}
tests := []struct {
name string
packet *marp.Packet
wantErr error
wantLen int
wantIPs int
}{
{"request3", newPacket(marp.OperationRequest, mac2, ip4, zeroMAC, ip4), nil, 4, 3},
{"request4", newPacket(marp.OperationRequest, mac2, ip4, zeroMAC, ip4), nil, 4, 3},
{"request5", newPacket(marp.OperationRequest, mac2, ip5, zeroMAC, ip5), nil, 4, 4},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := conn.WriteTo(tt.packet, nil); err != tt.wantErr {
t.Errorf("TestHandler_ForceIPChange:%s error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
time.Sleep(time.Millisecond * 10)
if len(h.table.macTable) != tt.wantLen {
h.PrintTable()
t.Errorf("TestHandler_ForceIPChange:%s table len = %v, wantLen %v", tt.name, len(h.table.macTable), tt.wantLen)
}
if tt.wantIPs != 0 {
e := h.table.findByMAC(tt.packet.SenderHardwareAddr)
if e == nil || len(e.IPs()) != tt.wantIPs {
t.Errorf("TestHandler_ForceIPChange:%s table IP entry=%+v, wantLen %v", tt.name, e, tt.wantLen)
}
}
})
}
if entry := h.table.findVirtualIP(ip2); entry == nil {
t.Errorf("TestHandler_ForceIPChange invalid virtual ip2")
}
if entry := h.table.findVirtualIP(ip3); entry == nil {
t.Errorf("TestHandler_ForceIPChange invalid virtual ip3")
}
if entry := h.table.findVirtualIP(ip4); entry == nil {
t.Errorf("TestHandler_ForceIPChange invalid virtual ip4")
}
if entry := h.table.findVirtualIP(ip5); entry != nil {
t.Errorf("TestHandler_ForceIPChange invalid virtual ip5")
}
if entry := h.table.findByIP(ip5); entry == nil || entry.State != StateNormal || len(entry.IPs()) != 4 {
h.PrintTable()
t.Errorf("TestHandler_ForceIPChange invalid virtual ip52 entry=%+v", entry)
}
cancel()
wg.Wait()
}