-
Notifications
You must be signed in to change notification settings - Fork 1
/
bedrockping_test.go
146 lines (122 loc) · 2.86 KB
/
bedrockping_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package bedrockping
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"reflect"
"strings"
"testing"
"time"
)
func TestWriteUnconnectedPing(t *testing.T) {
buf := new(bytes.Buffer)
if err := WriteUnconnectedPing(buf, 0); err != nil {
t.Error(err)
}
if buf.Len() != 25 {
t.Error("didn't write 25 bytes")
}
var id byte
if err := binary.Read(buf, binary.BigEndian, &id); err != nil {
t.Error(err)
}
if id != 0x01 {
t.Errorf("invalid packet id: %d", id)
}
var timestamp uint64
if err := binary.Read(buf, binary.BigEndian, ×tamp); err != nil {
t.Error(err)
}
if timestamp != 0 {
t.Errorf("invalid timestamp: %d", timestamp)
}
temp := make([]byte, 16)
if _, err := buf.Read(temp); err != nil {
t.Error(err)
}
if !bytes.Equal(offlineMessageDataID, temp) {
t.Errorf("invalid offline message data id: %x", temp)
}
if buf.Len() != 0 {
t.Error("failed to read all bytes")
}
}
func writeUTFString(buf io.Writer, str string) error {
var strLen = uint16(len(str))
if err := binary.Write(buf, binary.BigEndian, &strLen); err != nil {
return err
}
if _, err := buf.Write([]byte(str)); err != nil {
return err
}
return nil
}
func TestReadUTFString(t *testing.T) {
var testString = "This is some test string"
buf := new(bytes.Buffer)
if err := writeUTFString(buf, testString); err != nil {
t.Error(err)
}
str, err := ReadUTFString(buf)
if err != nil {
t.Error(err)
}
if str != testString {
t.Errorf("failed to read string: '%s'", str)
}
}
func TestReadUnconnectedPong(t *testing.T) {
expect := Response{
Timestamp: 0,
ServerID: 0,
GameID: "MCPE",
ServerName: "ServerName",
ProtocolVersion: 0,
MCPEVersion: "0.0.0",
PlayerCount: 0,
MaxPlayers: 0,
Extra: []string{"Extra", "Stuff"},
}
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, byte(0x1c)); err != nil {
t.Error(err)
}
if err := binary.Write(buf, binary.BigEndian, expect.Timestamp); err != nil {
t.Error(err)
}
if err := binary.Write(buf, binary.BigEndian, expect.ServerID); err != nil {
t.Error(err)
}
if _, err := buf.Write(offlineMessageDataID); err != nil {
t.Error(err)
}
payload := fmt.Sprintf("%s;%s;%d;%s;%d;%d",
expect.GameID,
expect.ServerName,
expect.ProtocolVersion,
expect.MCPEVersion,
expect.PlayerCount,
expect.MaxPlayers)
if expect.Extra != nil {
payload = payload + ";" + strings.Join(expect.Extra, ";")
}
if err := writeUTFString(buf, payload); err != nil {
t.Error(err)
}
var resp Response
reader := bufio.NewReader(buf)
if err := ReadUnconnectedPong(reader, &resp); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(expect, resp) {
t.Errorf("incorrect resp: %v", resp)
}
}
func TestQuery(t *testing.T) {
_, err := Query("hivebedrock.network:19132", 5*time.Second, 150*time.Millisecond)
if err != nil {
t.Error(err)
}
}