-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_test.go
83 lines (74 loc) · 1.48 KB
/
tcp_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
package http
import (
"bytes"
"io/ioutil"
"syscall"
"testing"
"time"
)
func TestTCPClient(t *testing.T) {
ch := make(chan struct{})
go func() {
socket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(socket)
sa := syscall.SockaddrInet4{
Addr: [4]byte{127, 0, 0, 1},
Port: 11111,
}
if err := syscall.Bind(socket, &sa); err != nil {
t.Fatal(err)
}
if err := syscall.Listen(socket, 5); err != nil {
t.Fatal(err)
}
socket1, _, err := syscall.Accept(socket)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(socket1)
n, err := syscall.Write(socket1, []byte("hello"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatal(n)
}
buf := make([]byte, 5)
if _, err := syscall.Read(socket1, buf); err != nil {
t.Fatal(err)
}
expected := []byte("world")
if bytes.Compare(buf, expected) != 0 {
t.Errorf("expected: '%v', actual: '%v'\n", expected, buf)
}
ch <- struct{}{}
}()
time.Sleep(10 * time.Millisecond)
ip := [4]byte{127, 0, 0, 1}
port := 11111
c := NewTCPClient()
conn, err := c.Connect(ip, port)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
b, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
expected := []byte("hello")
if bytes.Compare(b, expected) != 0 {
t.Errorf("expected: '%v', actual: '%v'\n", expected, b)
}
n, err := conn.Write([]byte("world"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatal(n)
}
<-ch
}