60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package mc
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestExtractSlug(t *testing.T) {
|
|
if got := ExtractSlug("abc.play.example.net", "play.example.net"); got != "abc" {
|
|
t.Fatalf("got %q want abc", got)
|
|
}
|
|
if got := ExtractSlug("other.net", "play.example.net"); got != "" {
|
|
t.Fatalf("expected empty slug")
|
|
}
|
|
}
|
|
|
|
func TestReadHandshake(t *testing.T) {
|
|
var payload bytes.Buffer
|
|
writeVarInt(&payload, 0)
|
|
writeVarInt(&payload, 763)
|
|
writeString(&payload, "demo.play.example.net")
|
|
payload.WriteByte(0x63)
|
|
payload.WriteByte(0xdd)
|
|
writeVarInt(&payload, 2)
|
|
|
|
var packet bytes.Buffer
|
|
writeVarInt(&packet, int32(payload.Len()))
|
|
packet.Write(payload.Bytes())
|
|
|
|
hs, err := ReadHandshake(&packet)
|
|
if err != nil {
|
|
t.Fatalf("read handshake: %v", err)
|
|
}
|
|
if hs.ServerAddress != "demo.play.example.net" {
|
|
t.Fatalf("address %q", hs.ServerAddress)
|
|
}
|
|
if hs.ServerPort != 25565 {
|
|
t.Fatalf("port %d", hs.ServerPort)
|
|
}
|
|
}
|
|
|
|
func writeVarInt(buf *bytes.Buffer, value int32) {
|
|
for {
|
|
temp := byte(value & 0x7F)
|
|
value >>= 7
|
|
if value != 0 {
|
|
temp |= 0x80
|
|
}
|
|
buf.WriteByte(temp)
|
|
if value == 0 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func writeString(buf *bytes.Buffer, value string) {
|
|
writeVarInt(buf, int32(len(value)))
|
|
buf.WriteString(value)
|
|
}
|