Files
zmq/security/plain/plain_cxx_test.go
zeekayandHanzo Dev cde37cf581 Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-16 19:37:50 -07:00

256 lines
5.3 KiB
Go

// Copyright 2018 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build cgo
package plain_test
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"time"
czmq4 "github.com/luxfi/czmq/v4"
"github.com/luxfi/zmq/v4"
"github.com/luxfi/zmq/v4/security/plain"
"golang.org/x/sync/errgroup"
)
var (
repQuit = zmq4.NewMsgString("bye")
)
func TestMain(m *testing.M) {
auth := czmq4.NewAuth()
err := auth.Allow("127.0.0.1")
if err != nil {
auth.Destroy()
panic(err)
}
err = auth.Plain("./testdata/password.txt")
if err != nil {
auth.Destroy()
panic(err)
}
// call flag.Parse() here if TestMain uses flags
exit := m.Run()
auth.Destroy()
os.Exit(exit)
}
func TestHandshakeReqCRep(t *testing.T) {
t.Skipf("%s", "REQ-CREP")
sec := plain.Security("user", "secret")
if got, want := sec.Type(), zmq4.PlainSecurity; got != want {
t.Fatalf("got=%v, want=%v", got, want)
}
ctx, timeout := context.WithTimeout(context.Background(), 10*time.Second)
defer timeout()
ep := must(EndPoint("tcp"))
req := zmq4.NewReq(ctx, zmq4.WithSecurity(sec))
defer req.Close()
rep := zmq4.NewCRep(ctx, czmq4.SockSetZapDomain("global"), czmq4.SockSetPlainServer(1))
defer rep.Close()
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := rep.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
msg, err := rep.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, reqQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
err = rep.Send(repQuit)
if err != nil {
return fmt.Errorf("could not send REP message: %w", err)
}
return nil
})
grp.Go(func() error {
err := req.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
err = req.Send(reqQuit)
if err != nil {
return fmt.Errorf("could not send REQ message: %w", err)
}
msg, err := req.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, repQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
return nil
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
}
func TestHandshakeCReqRep(t *testing.T) {
t.Skipf("%s", "CREQ-REP")
sec := plain.Security("user", "secret")
if got, want := sec.Type(), zmq4.PlainSecurity; got != want {
t.Fatalf("got=%v, want=%v", got, want)
}
ctx, timeout := context.WithTimeout(context.Background(), 10*time.Second)
defer timeout()
ep := must(EndPoint("tcp"))
req := zmq4.NewCReq(ctx, czmq4.SockSetPlainUsername("user"), czmq4.SockSetPlainPassword("secret"))
defer req.Close()
rep := zmq4.NewRep(ctx, zmq4.WithSecurity(sec))
defer rep.Close()
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := rep.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
msg, err := rep.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, reqQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
err = rep.Send(repQuit)
if err != nil {
return fmt.Errorf("could not send REP message: %w", err)
}
return nil
})
grp.Go(func() error {
err := req.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
err = req.Send(reqQuit)
if err != nil {
return fmt.Errorf("could not send REQ message: %w", err)
}
msg, err := req.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, repQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
return nil
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
}
func TestHandshakeCReqCRep(t *testing.T) {
t.Skipf("%s", "CREQ-CREP")
sec := plain.Security("user", "secret")
if got, want := sec.Type(), zmq4.PlainSecurity; got != want {
t.Fatalf("got=%v, want=%v", got, want)
}
ctx, timeout := context.WithTimeout(context.Background(), 10*time.Second)
defer timeout()
ep := must(EndPoint("tcp"))
req := zmq4.NewCReq(ctx, czmq4.SockSetPlainUsername("user"), czmq4.SockSetPlainPassword("secret"))
defer req.Close()
rep := zmq4.NewCRep(ctx, czmq4.SockSetZapDomain("global"), czmq4.SockSetPlainServer(1))
defer rep.Close()
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := rep.Listen(ep)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
msg, err := rep.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, reqQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
err = rep.Send(repQuit)
if err != nil {
return fmt.Errorf("could not send REP message: %w", err)
}
return nil
})
grp.Go(func() error {
err := req.Dial(ep)
if err != nil {
return fmt.Errorf("could not dial: %w", err)
}
err = req.Send(reqQuit)
if err != nil {
return fmt.Errorf("could not send REQ message: %w", err)
}
msg, err := req.Recv()
if err != nil {
return fmt.Errorf("could not recv REQ message: %w", err)
}
if !reflect.DeepEqual(msg, repQuit) {
return fmt.Errorf("got = %v, want = %v", msg, repQuit)
}
return nil
})
if err := grp.Wait(); err != nil {
t.Fatalf("error: %+v", err)
}
}