-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_test.go
110 lines (100 loc) · 2.27 KB
/
windows_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
100
101
102
103
104
105
106
107
108
109
110
//+build windows
package networkchangenotifier
import (
"fmt"
"net"
"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(HandleUIntPtr uint64) {
t.Log("OnNetworkChanged", Row)
})
go 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(HandleUIntPtr uint64) {
t.Log("OnNetworkChanged", Row)
})
go 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(HandleUIntPtr uint64) {
t.Log("OnNetworkChanged", Row)
t.Fail()
})
ncn.UnregisterCallback()
go testAddDelIP()
time.Sleep(2 * time.Second)
err = ncn.Cleanup()
if err != nil {
t.Log(err)
t.Fail()
}
}
// run as Administrator Please
func testAddDelIP() {
cmd1 := exec.Command(`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "add", "address", "1", "127.0.0.2", "255.0.0.0")
cmd1.Stderr = os.Stderr
cmd1.Stdout = os.Stdout
fmt.Println(cmd1.Run())
cmd2 := exec.Command(`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "delete", "address", "1", "127.0.0.2")
cmd2.Stderr = os.Stderr
cmd2.Stdout = os.Stdout
fmt.Println(cmd2.Run())
}
// run as Administrator Please
func testAddDelRoute() {
var cip string
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
if ip != nil && ip.IsGlobalUnicast() && ip.To4() != nil {
cip = ip.String()
break
}
}
cmd1 := exec.Command(`C:\Windows\System32\route.exe`, "add", "127.0.0.2", "mask", "255.255.255.255", cip)
cmd1.Stderr = os.Stderr
cmd1.Stdout = os.Stdout
fmt.Println(cmd1.Run())
cmd2 := exec.Command(`C:\Windows\System32\route.exe`, "delete", "127.0.0.2", "mask", "255.255.255.255")
cmd2.Stderr = os.Stderr
cmd2.Stdout = os.Stdout
fmt.Println(cmd2.Run())
}