forked from awnumar/memguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_test.go
85 lines (71 loc) · 2.06 KB
/
signals_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
//go:build !windows
// +build !windows
package memguard
import (
"net"
"os"
"os/exec"
"testing"
)
func TestCatchSignal(t *testing.T) {
// If we're within the testing subprocess, run test.
if os.Getenv("WITHIN_SUBPROCESS") == "1" {
// Start a listener object
listener, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {
SafePanic(err)
}
defer listener.Close()
// Spawn a handler to catch interrupts
CatchSignal(func(s os.Signal) {
listener.Close()
})
// Grab a handle on the running process
process, err := os.FindProcess(os.Getpid())
if err != nil {
t.Error(err)
}
// Send it an interrupt signal
if err := process.Signal(os.Interrupt); err != nil {
t.Error(err)
}
}
// Construct the subprocess with its initial state
cmd := exec.Command(os.Args[0], "-test.run=TestCatchSignal")
cmd.Env = append(os.Environ(), "WITHIN_SUBPROCESS=1")
// Execute the subprocess and inspect its exit code
err := cmd.Run().(*exec.ExitError)
if err.ExitCode() != 1 {
// if exit code is -1 it was likely killed by the signal
t.Error("Wanted exit code 1, got", err.ExitCode(), "err:", err)
}
// Todo: catch this violation (segfault)
//
// b := NewBuffer(32)
// bA := (*[64]byte)(unsafe.Pointer(&b.Bytes()[0]))
// bA[42] = 0x69 // write to guard page region
}
func TestCatchInterrupt(t *testing.T) {
if os.Getenv("WITHIN_SUBPROCESS") == "1" {
// Start the interrupt handler
CatchInterrupt()
// Grab a handle on the running process
process, err := os.FindProcess(os.Getpid())
if err != nil {
t.Error(err)
}
// Send it an interrupt signal
if err := process.Signal(os.Interrupt); err != nil {
t.Error(err)
}
}
// Construct the subprocess with its initial state
cmd := exec.Command(os.Args[0], "-test.run=TestCatchInterrupt")
cmd.Env = append(os.Environ(), "WITHIN_SUBPROCESS=1")
// Execute the subprocess and inspect its exit code
err := cmd.Run().(*exec.ExitError)
if err.ExitCode() != 1 {
// if exit code is -1 it was likely killed by the signal
t.Error("Wanted exit code 1, got", err.ExitCode(), "err:", err)
}
}