testutil

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package testutil provides helpers and utilities to make testing easier and less tedious.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ECDSACert

func ECDSACert(t *testing.T, tmpl *x509.Certificate) []byte

ECDSACert generates a self-signed ECDSA P-256 certificate from tmpl and returns its PEM encoding. The generated key is discarded; callers that need a matching key should use GenerateSelfSignedCert or GenerateMTLSCerts.

func GenerateMTLSCerts

func GenerateMTLSCerts(t *testing.T) (caFile, certFile, keyFile string)

GenerateMTLSCerts writes a self-signed ECDSA P-256 CA certificate plus an RSA-2048 leaf certificate signed by that CA (with its matching key) to a fresh testing.T.TempDir and returns the three paths. The leaf is RSA because credential validation checks the leaf's key type against an RSA-only cipher suite allowlist; an ECDSA leaf fails that check even though it verifies fine against the CA. The leaf advertises CN "localhost" with DNSNames=["localhost"]; both certificates are valid for one hour. Use this when a test needs the leaf to verify against the CA.

func GenerateRSACert

func GenerateRSACert(t *testing.T) (certFile, keyFile string)

GenerateRSACert writes a self-signed RSA-2048 certificate and its matching PKCS#1 private key to a fresh testing.T.TempDir and returns the paths. It is the RSA counterpart to GenerateSelfSignedCert: the key is retained and written out, so the pair loads via crypto/tls.LoadX509KeyPair as a real TLS identity. The certificate advertises CN "localhost" with DNSNames=["localhost"] and both server- and client-auth extended key usages, so it serves as either a server certificate or a client identity. It is valid for one hour.

func GenerateSelfSignedCert

func GenerateSelfSignedCert(t *testing.T) (certFile, keyFile string)

GenerateSelfSignedCert writes a self-signed ECDSA P-256 certificate and its matching PKCS#1-style EC private key to a fresh testing.T.TempDir and returns the paths. The certificate is valid for one hour, advertises CN "localhost" with DNSNames=["localhost"], and is suitable for loading via crypto/tls.LoadX509KeyPair in server-auth scenarios.

func RSACert

func RSACert(t *testing.T, tmpl *x509.Certificate) []byte

RSACert generates a self-signed RSA-2048 certificate from tmpl and returns its PEM encoding. The generated key is discarded; callers that need a matching key should use GenerateSelfSignedCert or GenerateMTLSCerts.

func WriteFile

func WriteFile(t *testing.T, dir, name string, contents []byte) string

WriteFile writes contents to dir/name with mode 0600 and returns the full path. Any IO failure fails the test immediately. Callers typically pass testing.T.TempDir as dir so cleanup is automatic.

Types

type ClientConn added in v0.5.0

type ClientConn struct {
	InvokeErr error
	StreamErr error
	Stream    grpc.ClientStream
	Header    metadata.MD
	Trailer   metadata.MD
}

ClientConn is a google.golang.org/grpc.ClientConnInterface that succeeds or fails as configured, so a test can drive an outbound call's failure modes without standing up an upstream. The zero value succeeds and yields no response metadata.

func (*ClientConn) Invoke added in v0.5.0

func (c *ClientConn) Invoke(_ context.Context, _ string, _, _ any, opts ...grpc.CallOption) error

Invoke reports InvokeErr, having first filled the caller's header and trailer the way a real upstream would, so code that relays response metadata has something to pass along. Call options other than google.golang.org/grpc.Header and google.golang.org/grpc.Trailer are ignored.

func (*ClientConn) NewStream added in v0.5.0

NewStream reports StreamErr when set, and otherwise returns the configured Stream, which may be nil for a caller that never reads it.

type ClientStream added in v0.5.0

type ClientStream struct {
	RecvErr   error
	SendErr   error
	HeaderErr error
	BlockRecv chan struct{}
}

ClientStream is a google.golang.org/grpc.ClientStream whose steps fail as configured. When BlockRecv is non-nil, RecvMsg waits on it and then reports io.EOF, which lets a test park the stream and control which of two concurrent readers reports first.

func (*ClientStream) CloseSend added in v0.5.0

func (*ClientStream) CloseSend() error

CloseSend always succeeds.

func (*ClientStream) Context added in v0.5.0

func (*ClientStream) Context() context.Context

Context returns a background context.

func (*ClientStream) Header added in v0.5.0

func (s *ClientStream) Header() (metadata.MD, error)

Header reports HeaderErr and never returns metadata.

func (*ClientStream) RecvMsg added in v0.5.0

func (s *ClientStream) RecvMsg(any) error

RecvMsg waits on BlockRecv and reports io.EOF when one is configured, and otherwise reports RecvErr immediately.

func (*ClientStream) SendMsg added in v0.5.0

func (s *ClientStream) SendMsg(any) error

SendMsg reports SendErr.

func (*ClientStream) Trailer added in v0.5.0

func (*ClientStream) Trailer() metadata.MD

Trailer returns no metadata.

type ServerStream added in v0.5.0

type ServerStream struct {
	Ctx       context.Context
	RecvErr   error
	SendErr   error
	HeaderErr error
}

ServerStream is a google.golang.org/grpc.ServerStream whose steps fail as configured. A nil RecvErr means a request message arrived.

func (ServerStream) Context added in v0.5.0

func (s ServerStream) Context() context.Context

Context returns Ctx.

func (ServerStream) RecvMsg added in v0.5.0

func (s ServerStream) RecvMsg(any) error

RecvMsg reports RecvErr.

func (ServerStream) SendHeader added in v0.5.0

func (s ServerStream) SendHeader(metadata.MD) error

SendHeader reports HeaderErr.

func (ServerStream) SendMsg added in v0.5.0

func (s ServerStream) SendMsg(any) error

SendMsg reports SendErr.

func (ServerStream) SetHeader added in v0.5.0

func (s ServerStream) SetHeader(metadata.MD) error

SetHeader reports HeaderErr.

func (ServerStream) SetTrailer added in v0.5.0

func (ServerStream) SetTrailer(metadata.MD)

SetTrailer discards the trailer.

type ServerTransportStream added in v0.5.0

type ServerTransportStream struct {
	FullMethodName string
}

ServerTransportStream carries the full method name a handler reads from its request context, for installing via google.golang.org/grpc.NewContextWithServerTransportStream.

func (ServerTransportStream) Method added in v0.5.0

func (s ServerTransportStream) Method() string

Method returns FullMethodName.

func (ServerTransportStream) SendHeader added in v0.5.0

func (ServerTransportStream) SendHeader(metadata.MD) error

SendHeader always succeeds.

func (ServerTransportStream) SetHeader added in v0.5.0

SetHeader always succeeds.

func (ServerTransportStream) SetTrailer added in v0.5.0

func (ServerTransportStream) SetTrailer(metadata.MD) error

SetTrailer always succeeds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL