-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
95 lines (83 loc) · 1.47 KB
/
main_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
package main
import (
"io/ioutil"
"net"
"os"
"os/exec"
"testing"
)
// Tests need to be run with:
//
// go test -exec ./leakcheck .
func TestFileLeak(t *testing.T) {
file, err := ioutil.TempFile("", "leak-*.txt")
if err != nil {
t.Fatal(err)
}
_ = file
}
func TestDeleteOpenFile(t *testing.T) {
file, err := ioutil.TempFile("", "leak-*.txt")
if err != nil {
t.Fatal(err)
}
_ = os.Remove(file.Name())
_ = file.Close()
}
func TestOpenFile(t *testing.T) {
{
// opening/reading allowed
file, err := os.Open("main.go")
if err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
}
{
file, err := os.Create("testfile~")
if err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
os.Remove("testfile~")
}
}
func TestServerLeak(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
_ = ln
}
func TestConnLeak(t *testing.T) {
conn, err := net.Dial("tcp", "loov.io:80")
if err != nil {
t.Fatal(err)
}
_ = conn
}
func TestExecNormal(t *testing.T) {
cmd := exec.Command("sleep", "0")
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}
func TestExecLeak(t *testing.T) {
cmd := exec.Command("sleep", "5")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
}
func TestExecKill(t *testing.T) {
cmd := exec.Command("sleep", "5")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
}