-
Notifications
You must be signed in to change notification settings - Fork 0
/
tragon_test.go
115 lines (95 loc) · 2 KB
/
tragon_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package tragon
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"log"
"net"
"os"
"sync"
"testing"
)
const (
testTimeout = 1
delta = 1
)
func newConnMock(session []byte) connMock {
buf := bytes.NewBuffer(session)
r := bufio.NewReader(buf)
w := bufio.NewWriter(buf)
return connMock{r: r, w: w}
}
type connMock struct {
net.Conn
r io.Reader
w io.Writer
}
func (cm connMock) Read(b []byte) (int, error) {
return cm.r.Read(b)
}
func (cm connMock) Write(p []byte) (int, error) {
return cm.w.Write(p)
}
func (cm connMock) Close() error {
return nil
}
type testCase struct {
Name string
SessionFile string
MessageFile string
}
var testSuite = []testCase{
testCase{
Name: "basic",
SessionFile: "test/sessions/basic",
MessageFile: "test/messages/basic",
},
testCase{
Name: "headers",
SessionFile: "test/sessions/headers",
MessageFile: "test/messages/headers",
},
testCase{
Name: "incomplete",
SessionFile: "test/sessions/incomplete",
MessageFile: "test/messages/incomplete",
},
}
func TestHandleClient(t *testing.T) {
logger := log.New(os.Stderr, "tragon: ", log.Llongfile)
for _, tc := range testSuite {
log.Printf("running \"%v\" test", tc.Name)
var message []byte
var wg sync.WaitGroup
wg.Add(1)
handlers := Handlers{
ConnectionHandler: func(conn net.Conn) {
logger.Printf("Recieved connection")
},
MessageHandler: func(msg []byte) {
logger.Printf("Got message of length: %v", len(msg))
message = msg
wg.Done()
},
ErrorHandler: func(err error) {
logger.Printf("Got error: %v", err)
},
}
s := New(":0", testTimeout, DefaultReplies, handlers)
session, err := ioutil.ReadFile(tc.SessionFile)
if err != nil {
t.Fatal(err)
}
conn := newConnMock(session)
s.handleClient(conn)
wg.Wait()
wantMessage, err := ioutil.ReadFile(tc.MessageFile)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(message, wantMessage) != 0 {
t.Fatalf("expected \"%v\", got \"%v\"", wantMessage, message)
}
}
}