Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
159 lines
3.7 KiB
Go
159 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadPluginConfig(t *testing.T) {
|
|
defer saveGatewayState(t)()
|
|
|
|
type pluginConfig struct {
|
|
Enable bool `toml:"enable"`
|
|
Name string `toml:"name"`
|
|
Count int `toml:"count"`
|
|
}
|
|
|
|
var got pluginConfig
|
|
err := loadPluginConfig(map[string]any{
|
|
"enable": true,
|
|
"name": "plugin-a",
|
|
"count": 7,
|
|
}, &got)
|
|
if err != nil {
|
|
t.Fatalf("loadPluginConfig() error = %v", err)
|
|
}
|
|
|
|
want := pluginConfig{Enable: true, Name: "plugin-a", Count: 7}
|
|
if got != want {
|
|
t.Fatalf("loadPluginConfig() = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestLoadPluginConfigReturnsDecodeError(t *testing.T) {
|
|
defer saveGatewayState(t)()
|
|
|
|
type pluginConfig struct {
|
|
Count int `toml:"count"`
|
|
}
|
|
|
|
var got pluginConfig
|
|
if err := loadPluginConfig(map[string]any{"count": "not-an-int"}, &got); err == nil {
|
|
t.Fatal("loadPluginConfig() error = nil, want error")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigReadsTomlAndAppliesSideEffects(t *testing.T) {
|
|
defer saveGatewayState(t)()
|
|
|
|
tmpDir := t.TempDir()
|
|
pidPath := filepath.Join(tmpDir, "gateway.pid")
|
|
logPath := filepath.Join(tmpDir, "logs", "gateway.log")
|
|
configFile = filepath.Join(tmpDir, "config.toml")
|
|
|
|
toml := fmt.Sprintf(`
|
|
pid_file = %q
|
|
|
|
[log]
|
|
level = "debug"
|
|
file = %q
|
|
|
|
[tcp]
|
|
enable = true
|
|
port = 25565
|
|
|
|
[quic]
|
|
enable = true
|
|
port = 25566
|
|
application_protocols = ["minecraft", "raw"]
|
|
|
|
[kcp]
|
|
enable = true
|
|
port = 25567
|
|
data_shards = 10
|
|
parity_Shards = 3
|
|
|
|
[websocket]
|
|
enable = true
|
|
port = 25568
|
|
path = "/gateway"
|
|
|
|
[hosts]
|
|
"play.example" = "backend.example:25565"
|
|
default = "fallback.example:25565"
|
|
|
|
[plugin.disabled]
|
|
enable = false
|
|
`, pidPath, logPath)
|
|
|
|
if err := os.WriteFile(configFile, []byte(toml), 0644); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
|
|
if err := loadConfig(); err != nil {
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
}
|
|
|
|
if !config.Tcp.Enable || config.Tcp.Port != 25565 {
|
|
t.Fatalf("tcp config = %+v", config.Tcp)
|
|
}
|
|
if !config.Quic.Enable || config.Quic.Port != 25566 {
|
|
t.Fatalf("quic config = %+v", config.Quic)
|
|
}
|
|
if got := strings.Join(config.Quic.ApplicationProtocols, ","); got != "minecraft,raw" {
|
|
t.Fatalf("application protocols = %q, want minecraft,raw", got)
|
|
}
|
|
if !config.Kcp.Enable || config.Kcp.DataShards != 10 || config.Kcp.ParityShards != 3 {
|
|
t.Fatalf("kcp config = %+v", config.Kcp)
|
|
}
|
|
if !config.WebSocket.Enable || config.WebSocket.Path != "/gateway" {
|
|
t.Fatalf("websocket config = %+v", config.WebSocket)
|
|
}
|
|
if got := config.Hosts["play.example"]; got != "backend.example:25565" {
|
|
t.Fatalf("host route = %q, want backend.example:25565", got)
|
|
}
|
|
if currentPidFile != pidPath {
|
|
t.Fatalf("currentPidFile = %q, want %q", currentPidFile, pidPath)
|
|
}
|
|
if currentLogFile != logPath {
|
|
t.Fatalf("currentLogFile = %q, want %q", currentLogFile, logPath)
|
|
}
|
|
|
|
pidBytes, err := os.ReadFile(pidPath)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(pid) error = %v", err)
|
|
}
|
|
if wantPID := fmt.Sprintf("%d\n", os.Getpid()); string(pidBytes) != wantPID {
|
|
t.Fatalf("pid file = %q, want %q", string(pidBytes), wantPID)
|
|
}
|
|
if _, err := os.Stat(logPath); err != nil {
|
|
t.Fatalf("Stat(log file) error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigReturnsErrors(t *testing.T) {
|
|
t.Run("missing file", func(t *testing.T) {
|
|
defer saveGatewayState(t)()
|
|
|
|
configFile = filepath.Join(t.TempDir(), "missing.toml")
|
|
if err := loadConfig(); err == nil {
|
|
t.Fatal("loadConfig() error = nil, want error")
|
|
}
|
|
})
|
|
|
|
t.Run("invalid toml", func(t *testing.T) {
|
|
defer saveGatewayState(t)()
|
|
|
|
configFile = filepath.Join(t.TempDir(), "config.toml")
|
|
if err := os.WriteFile(configFile, []byte("[tcp\n"), 0644); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
if err := loadConfig(); err == nil {
|
|
t.Fatal("loadConfig() error = nil, want error")
|
|
}
|
|
})
|
|
}
|