-
Notifications
You must be signed in to change notification settings - Fork 0
/
netdrop_test.go
58 lines (50 loc) · 1.91 KB
/
netdrop_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
package main
import (
"bytes"
"context"
"net"
"testing"
"github.com/stretchr/testify/require"
)
func TestSendAndReceive(t *testing.T) {
listener, err := net.Listen("tcp", listenAddr)
require.NoError(t, err, "net.Listen")
tcpListener, ok := listener.(*net.TCPListener)
require.NoError(t, err, "listener not a TCPListener")
defer tcpListener.Close()
go func() {
conn, err := tcpListener.AcceptTCP()
require.NoError(t, err, "listener.Accept")
defer conn.Close()
err = handleSend(conn, bytes.NewBufferString("my-test-string"), "test-password")
require.NoError(t, err, "handleSend")
}()
outBuf, errBuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
addr, ok := tcpListener.Addr().(*net.TCPAddr)
require.True(t, ok, "listener addr not a TCP Addr")
err = receiveFrom(context.TODO(), addr.String(), "test-password", outBuf, errBuf)
require.NoError(t, err, "receiveFrom")
require.Equal(t, "my-test-string", outBuf.String(), "outBuf")
require.Empty(t, errBuf.String(), "errBuf")
}
func TestWrongPassword(t *testing.T) {
listener, err := net.Listen("tcp", listenAddr)
require.NoError(t, err, "net.Listen")
tcpListener, ok := listener.(*net.TCPListener)
require.NoError(t, err, "listener not a TCPListener")
defer tcpListener.Close()
go func() {
conn, err := tcpListener.AcceptTCP()
require.NoError(t, err, "listener.Accept")
defer conn.Close()
err = handleSend(conn, bytes.NewBufferString("my-test-string"), "test-password")
require.EqualError(t, err, ErrWrongPassword.Error(), "handleSend")
}()
outBuf, errBuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
addr, ok := tcpListener.Addr().(*net.TCPAddr)
require.True(t, ok, "listener addr not a TCP Addr")
err = receiveFrom(context.TODO(), addr.String(), "not-the-test-password", outBuf, errBuf)
require.EqualError(t, err, ErrWrongPassword.Error(), "handleSend")
require.Empty(t, outBuf.String(), "outBuf")
require.Empty(t, errBuf.String(), "errBuf")
}