-
Notifications
You must be signed in to change notification settings - Fork 7
/
relp_test.go
85 lines (73 loc) · 1.88 KB
/
relp_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
package relp
import (
"bufio"
"fmt"
"io"
"net"
"strconv"
"strings"
"testing"
"time"
)
func TestNewServer(t *testing.T) {
_, err := NewServer("127.0.0.1", 3333, true)
if err != nil {
t.Error("Error setting up listener:", err)
return
}
conn, err := net.DialTimeout("tcp", "127.0.0.1:3333", time.Second)
if err != nil {
t.Error("Error connecting to socket:", err)
return
}
defer conn.Close()
reader := bufio.NewReader(conn)
offerBytes := []byte("relp_version=0\nrelp_software=gorelp,0.2.0,https://github.com/stith/gorelp\ncommands=syslog")
outString := fmt.Sprintf("1 open %d %s\n", len(offerBytes), offerBytes)
_, err = conn.Write([]byte(outString))
if err != nil {
t.Error("Error sending offer", err)
return
}
txn, err := reader.ReadString(' ')
if err != nil {
t.Error("Error reading offer response", err)
return
}
txn = strings.TrimSpace(txn)
if txn != "1" {
t.Error("Offer response txn != 1", txn)
return
}
cmd, err := reader.ReadString(' ')
if err != nil {
t.Error("Error reading offer response command", err)
return
}
cmd = strings.TrimSpace(cmd)
if cmd != "rsp" {
t.Error("Offer response command != rsp", cmd)
return
}
dataLenS, err := reader.ReadString(' ')
if err != nil {
t.Error("Error reading dataLen:", err)
return
}
dataLen, err := strconv.Atoi(strings.TrimSpace(dataLenS))
if err != nil {
t.Error("Error converting dataLenS to int:", err)
return
}
dataBytes := make([]byte, dataLen)
_, err = io.ReadFull(reader, dataBytes)
if err != nil {
t.Error("Error reading message:", err)
return
}
dataString := string(dataBytes[:dataLen])
if dataString != "200 OK\nrelp_version=0\nrelp_software=gorelp,0.2.0,https://github.com/stith/gorelp\ncommands=syslog" {
t.Error("Offer response != 200 OK\\nrelp_version=0\\nrelp_software=gorelp,0.2.0,https://github.com/stith/gorelp\\ncommands=syslog\n", dataString)
return
}
}