-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsocket_test.go
53 lines (46 loc) · 1.1 KB
/
socket_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
// Copyright 2019 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package lib_test
import (
"syscall"
"testing"
"changkun.de/x/occamy/internal/lib"
)
func TestNewSocket(t *testing.T) {
fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Error("cannot create socketpair")
t.FailNow()
}
sock1, err := lib.NewSocket(fds[0])
if err != nil {
t.Error("create lib socket error: ", err)
t.FailNow()
}
sock2, err := lib.NewSocket(fds[1])
if err != nil {
t.Error("create lib socket error: ", err)
t.FailNow()
}
t.Run("io", func(t *testing.T) {
message := "Wir müssen wissen, wir werden wissen."
err := sock1.Write([]byte(message))
if err != nil {
t.Error("sock1.Write error: ", err)
t.FailNow()
}
buf := make([]byte, len(message))
_, err = sock2.Read(buf)
if err != nil {
t.Error("sock2.Read error: ", err)
t.FailNow()
}
if string(buf) != message {
t.Error("message incorrect, got: ", string(buf))
t.FailNow()
}
})
sock1.Close()
sock2.Close()
}