-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_test.go
99 lines (88 loc) · 1.83 KB
/
linux_test.go
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
//+build !windows,!darwin
package networkchangenotifier
import (
"os"
"os/exec"
"testing"
"time"
)
func init() {
os.Setenv("GODEBUG", "networkchange=1")
}
func TestCallbackAddDelIP(t *testing.T) {
ncn := new(NetworkChangeNotifier)
err := ncn.Init()
if err != nil {
t.Log(err)
t.Fail()
}
ncn.OnNetworkChanged(func(dataNLMSG uint64) {
t.Log("OnNetworkChanged", dataNLMSG)
})
go func() {
time.Sleep(1 * time.Second)
testAddDelIP()
time.Sleep(1 * time.Second)
testAddDelIP()
}()
time.Sleep(3 * time.Second)
err = ncn.Cleanup()
if err != nil {
t.Log(err)
t.Fail()
}
}
func TestCallbackAddDelRoute(t *testing.T) {
ncn := new(NetworkChangeNotifier)
err := ncn.Init()
if err != nil {
t.Log(err)
t.Fail()
}
ncn.OnNetworkChanged(func(dataNLMSG uint64) {
t.Log("OnNetworkChanged", dataNLMSG)
})
go func() {
time.Sleep(1 * time.Second)
testAddDelRoute()
time.Sleep(1 * time.Second)
testAddDelRoute()
}()
time.Sleep(3 * time.Second)
err = ncn.Cleanup()
if err != nil {
t.Log(err)
t.Fail()
}
}
func TestUnReg(t *testing.T) {
ncn := new(NetworkChangeNotifier)
err := ncn.Init()
if err != nil {
t.Log(err)
t.Fail()
}
ncn.OnNetworkChanged(func(dataNLMSG uint64) {
t.Log("OnNetworkChanged", dataNLMSG)
t.Fail()
})
ncn.UnregisterCallback()
go func() {
time.Sleep(1 * time.Second)
testAddDelIP()
time.Sleep(1 * time.Second)
testAddDelIP()
}()
time.Sleep(3 * time.Second)
err = ncn.Cleanup()
if err != nil {
t.Log(err)
t.Fail()
}
}
func testAddDelIP() {
exec.Command("/bin/bash", "-c", "source /etc/profile && sudo ip addr add 127.0.0.2/8 dev lo && sudo ip addr del 127.0.0.2/8 dev lo").Run()
}
func testAddDelRoute() {
exec.Command("/bin/bash", "-c", "source /etc/profile && sudo ip route add 127.0.0.2/32 via 127.0.0.1 && sudo ip route del 127.0.0.2/32 via 127.0.0.1").Run()
}