This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 2
/
telnet_test.go
76 lines (58 loc) · 1.56 KB
/
telnet_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
package main
import (
"bytes"
"io/ioutil"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestTelnetClient(t *testing.T) {
t.Run("basic", func(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:")
require.NoError(t, err)
defer func() { require.NoError(t, l.Close()) }()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
in := &bytes.Buffer{}
out := &bytes.Buffer{}
timeout, err := time.ParseDuration("10s")
require.NoError(t, err)
client := NewTelnetClient(l.Addr().String(), timeout, ioutil.NopCloser(in), out)
require.NoError(t, client.Connect())
defer func() { require.NoError(t, client.Close()) }()
in.WriteString("hello\n")
err = client.Send()
require.NoError(t, err)
err = client.Receive()
require.NoError(t, err)
require.Equal(t, "world\n", out.String())
}()
go func() {
defer wg.Done()
conn, err := l.Accept()
require.NoError(t, err)
require.NotNil(t, conn)
defer func() { require.NoError(t, conn.Close()) }()
request := make([]byte, 1024)
n, err := conn.Read(request)
require.NoError(t, err)
require.Equal(t, "hello\n", string(request)[:n])
n, err = conn.Write([]byte("world\n"))
require.NoError(t, err)
require.NotEqual(t, 0, n)
}()
wg.Wait()
})
}
func TestWrongAddress(t *testing.T) {
in := &bytes.Buffer{}
out := &bytes.Buffer{}
timeout, err := time.ParseDuration("1s")
require.NoError(t, err)
client := NewTelnetClient("localhoZt:3333", timeout, ioutil.NopCloser(in), out)
require.Error(t, client.Connect())
}