96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package ts3
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestEscapeRoundTrip(t *testing.T) {
|
|
values := []string{"abc", "a b|c", "slash/and\\backslash", "ąęść", "x\ny\tz"}
|
|
for _, v := range values {
|
|
if got := unescape(escape(v)); got != v {
|
|
t.Fatalf("roundtrip %q -> %q", v, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseFields(t *testing.T) {
|
|
f := parseFields(`clid=12 client_nickname=Jan\sKowalski connection_client_ip=1.2.3.4`)
|
|
if f["client_nickname"] != "Jan Kowalski" || f["clid"] != "12" {
|
|
t.Fatalf("bad fields: %#v", f)
|
|
}
|
|
}
|
|
|
|
func TestCreateChannelUsesDatabaseIDAndRefreshesClientID(t *testing.T) {
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
|
|
commands := make(chan []string, 1)
|
|
go func() {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer c.Close()
|
|
_, _ = io.WriteString(c, "TS3\nWelcome to the TeamSpeak 3 ServerQuery interface\n")
|
|
r := bufio.NewReader(c)
|
|
var got []string
|
|
for {
|
|
line, err := r.ReadString('\n')
|
|
if err != nil {
|
|
break
|
|
}
|
|
line = strings.TrimSpace(line)
|
|
if line == "" {
|
|
continue
|
|
}
|
|
got = append(got, line)
|
|
switch {
|
|
case strings.HasPrefix(line, "login "):
|
|
_, _ = io.WriteString(c, "error id=0 msg=ok\n")
|
|
case strings.HasPrefix(line, "use "):
|
|
_, _ = io.WriteString(c, "error id=0 msg=ok\n")
|
|
case line == "clientlist -ip -uid":
|
|
_, _ = io.WriteString(c, "clid=8 cid=1 client_database_id=42 client_nickname=Jan\\sKowalski client_type=0 client_unique_identifier=abc connection_client_ip=1.2.3.4\nerror id=0 msg=ok\n")
|
|
case strings.HasPrefix(line, "channelcreate "):
|
|
_, _ = io.WriteString(c, "cid=321\nerror id=0 msg=ok\n")
|
|
case strings.HasPrefix(line, "setclientchannelgroup "):
|
|
_, _ = io.WriteString(c, "error id=0 msg=ok\n")
|
|
case strings.HasPrefix(line, "clientmove "):
|
|
_, _ = io.WriteString(c, "error id=0 msg=ok\n")
|
|
case line == "quit":
|
|
commands <- got
|
|
return
|
|
}
|
|
}
|
|
commands <- got
|
|
}()
|
|
|
|
q := &Query{Addr: ln.Addr().String(), User: "u", Password: "p", VoicePort: 9987, Timeout: 2 * time.Second}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
cid, err := q.CreateChannelForClient(ctx, Client{ID: 7, DatabaseID: 42, Nick: "old", UID: "abc", IP: "1.2.3.4"}, 5, "Moj kanal", "temat", "haslo")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cid != 321 {
|
|
t.Fatalf("cid=%d", cid)
|
|
}
|
|
got := <-commands
|
|
joined := strings.Join(got, "\n")
|
|
if !strings.Contains(joined, "setclientchannelgroup cgid=5 cid=321 cldbid=42") {
|
|
t.Fatalf("missing channel group command:\n%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "clientmove clid=8 cid=321") {
|
|
t.Fatalf("expected refreshed clid:\n%s", joined)
|
|
}
|
|
}
|