-
Notifications
You must be signed in to change notification settings - Fork 0
/
yama_windows_test.go
107 lines (98 loc) · 2.46 KB
/
yama_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
/*
* Copyright (c) 2019-20 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package yama_test
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestDeath(t *testing.T) {
Convey("Validate death happens cleanly on windows with ctrl-c event", t, func() {
// create source file
const source = `
package main
import (
"syscall"
"l7e.io/yama"
)
func main() {
watcher, _ := yama.NewWatcher(yama.WatchingSignals(syscall.SIGINT))
watcher.Wait()
}
`
tmp, err := ioutil.TempDir("", "TestCtrlBreak")
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmp)
// write ctrlbreak.go
name := filepath.Join(tmp, "ctlbreak")
src := name + ".go"
f, err := os.Create(src)
if err != nil {
t.Fatalf("Failed to create %v: %v", src, err)
}
defer f.Close()
_, _ = f.Write([]byte(source))
// compile it
exe := name + ".exe"
defer os.Remove(exe)
o, err := exec.Command("go", "build", "-o", exe, src).CombinedOutput()
if err != nil {
t.Fatalf("Failed to compile: %v\n%v", err, string(o))
}
// run it
cmd := exec.Command(exe)
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
err = cmd.Start()
if err != nil {
t.Fatalf("Start failed: %v", err)
}
go func() {
time.Sleep(1 * time.Second)
sendCtrlBreak(t, cmd.Process.Pid)
}()
err = cmd.Wait()
if err != nil {
t.Fatalf("Program exited with error: %v\n%v", err, b.String())
}
})
}
func sendCtrlBreak(t *testing.T, pid int) {
d, e := syscall.LoadDLL("kernel32.dll")
if e != nil {
t.Fatalf("LoadDLL: %v\n", e)
}
p, e := d.FindProc("GenerateConsoleCtrlEvent")
if e != nil {
t.Fatalf("FindProc: %v\n", e)
}
r, _, e := p.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid))
if r == 0 {
t.Fatalf("GenerateConsoleCtrlEvent: %v\n", e)
}
}