-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarwin_test.go
93 lines (82 loc) · 1.86 KB
/
darwin_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
//+build 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(dataCFPropertyListRef uint64) {
t.Log("OnNetworkChanged", dataCFPropertyListRef)
})
go func() {
time.Sleep(1 * time.Second)
testAddDelIP()
}()
time.Sleep(2 * 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(dataCFPropertyListRef uint64) {
// t.Log("OnNetworkChanged", dataCFPropertyListRef)
// })
// go func() {
// time.Sleep(1 * time.Second)
// testAddDelRoute()
// }()
// time.Sleep(2 * 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(dataCFPropertyListRef uint64) {
t.Log("OnNetworkChanged", dataCFPropertyListRef)
t.Fail()
})
ncn.UnregisterCallback()
go func() {
time.Sleep(1 * time.Second)
testAddDelIP()
}()
time.Sleep(2 * time.Second)
err = ncn.Cleanup()
if err != nil {
t.Log(err)
t.Fail()
}
}
func testAddDelIP() {
exec.Command("/bin/bash", "-c", "source /etc/profile && sudo ifconfig lo0 add 127.0.0.2/8 && sudo ifconfig lo0 delete 127.0.0.2").Run()
}
// func testAddDelRoute() {
// exec.Command("/bin/bash", "-c", "source /etc/profile && sudo route -n add -net 127.0.0.2 -netmask 255.255.255.255 127.0.0.1 && sudo route -n delete -net 127.0.0.2 -netmask 255.255.255.255 127.0.0.1").Run()
// }