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
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestAbstractPluginDefaults(t *testing.T) {
|
|
var plugin AbstractPlugin
|
|
|
|
if err := plugin.Init(nil); err != nil {
|
|
t.Fatalf("Init() error = %v", err)
|
|
}
|
|
if err := plugin.Destroy(); err != nil {
|
|
t.Fatalf("Destroy() error = %v", err)
|
|
}
|
|
if err := plugin.ReloadConfig(struct{}{}); err != nil {
|
|
t.Fatalf("ReloadConfig() error = %v", err)
|
|
}
|
|
if cfg := plugin.NewConfigObj(); cfg == nil {
|
|
t.Fatal("NewConfigObj() returned nil")
|
|
}
|
|
}
|
|
|
|
func TestHookTypesAndHandlers(t *testing.T) {
|
|
if got := HookUpstream.Key(); got != "upstream" {
|
|
t.Fatalf("HookUpstream.Key() = %q, want upstream", got)
|
|
}
|
|
if got := HookUpstream.AsAny().Key(); got != HookUpstream.Key() {
|
|
t.Fatalf("AsAny().Key() = %q, want %q", got, HookUpstream.Key())
|
|
}
|
|
|
|
acceptor := func(net.Conn, string) bool { return true }
|
|
handler := func(net.Conn, string) (net.Conn, error) { return nil, nil }
|
|
hookHandler := HookHandler[
|
|
func(net.Conn, string) bool,
|
|
func(net.Conn, string) (net.Conn, error),
|
|
]{acceptor: acceptor, handler: handler}
|
|
|
|
if hookHandler.Acceptor() == nil {
|
|
t.Fatal("Acceptor() returned nil")
|
|
}
|
|
if hookHandler.Handler() == nil {
|
|
t.Fatal("Handler() returned nil")
|
|
}
|
|
}
|
|
|
|
func TestRegisterHookHandler(t *testing.T) {
|
|
gateway := &recordingGateway{hooks: make(map[string]any)}
|
|
acceptor := func(net.Conn, string) bool { return true }
|
|
handler := func(net.Conn, string) (net.Conn, error) { return nil, nil }
|
|
|
|
if err := RegisterHookHandler(gateway, HookUpstream, acceptor, handler); err != nil {
|
|
t.Fatalf("RegisterHookHandler() error = %v", err)
|
|
}
|
|
|
|
rawHandler, ok := gateway.hooks[HookUpstream.Key()]
|
|
if !ok {
|
|
t.Fatalf("hook %q was not registered", HookUpstream.Key())
|
|
}
|
|
registered, ok := rawHandler.(HookHandler[
|
|
func(net.Conn, string) bool,
|
|
func(net.Conn, string) (net.Conn, error),
|
|
])
|
|
if !ok {
|
|
t.Fatalf("registered hook type = %T", rawHandler)
|
|
}
|
|
if registered.Acceptor() == nil {
|
|
t.Fatal("registered acceptor is nil")
|
|
}
|
|
if registered.Handler() == nil {
|
|
t.Fatal("registered handler is nil")
|
|
}
|
|
}
|
|
|
|
type recordingGateway struct {
|
|
hooks map[string]any
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
func (g *recordingGateway) HandleConn(net.Conn) {}
|
|
|
|
func (g *recordingGateway) ExitWaitGroup() *sync.WaitGroup {
|
|
return &g.wg
|
|
}
|
|
|
|
func (g *recordingGateway) Hook(hook string, handler any) error {
|
|
g.hooks[hook] = handler
|
|
return nil
|
|
}
|