Repository navigation
Expand file tree
/
Copy pathfd_test.go
More file actions
84 lines (70 loc) · 1.37 KB
/
Copy pathfd_test.go
File metadata and controls
84 lines (70 loc) · 1.37 KB
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
package fdpassing_test
import (
"bytes"
"fdpassing"
"io"
"net"
"os"
"testing"
)
func TestFd(t *testing.T) {
data := []byte("helloworld")
// run server
ready := make(chan struct{})
go func(ready chan<- struct{}) {
ln, err := net.Listen("unix", "@fdpassing-testfd")
if err != nil {
panic(err)
}
defer ln.Close()
close(ready)
conn, err := ln.Accept()
if err != nil {
panic(err)
}
defer conn.Close()
// new file
file, err := os.Create("/tmp/fdpassing-testfd.txt")
if err != nil {
panic(err)
}
defer file.Close()
// write data
file.Write(data)
file.Sync()
file.Seek(0, io.SeekStart)
// passing fd
unixConn := conn.(*net.UnixConn)
fileFd := int(file.Fd())
fdp := fdpassing.NewFd(unixConn)
if err := fdp.Send(fileFd); err != nil {
panic(err)
}
t.Logf("[S] fd send (%d)", fileFd)
}(ready)
// wait
<-ready
conn, err := net.Dial("unix", "@fdpassing-testfd")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// recv fd
unixConn := conn.(*net.UnixConn)
fdp := fdpassing.NewFd(unixConn)
fd, err := fdp.Recv()
if err != nil {
t.Fatal(err)
}
t.Logf("[C] fd recv (%d)", fd)
file := os.NewFile(uintptr(fd), "recv-file")
defer file.Close()
// read data
content, err := io.ReadAll(file)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, data) {
t.Errorf("[want]: %s, [got]: %s", data, content)
}
}