func adapt(fn Func) nltest.Func {
	return func(reqs []netlink.Message) ([]netlink.Message, error) {
		var req netlink.Message
		l := len(reqs)
		switch l {
		case 0:
		case 1:
			req = reqs[0]
		default:
			return nil, fmt.Errorf("genltest: expected zero or one request, but got: %d", l)
		}
		var gm genetlink.Message
		if len(req.Data) > 0 {
			if err := gm.UnmarshalBinary(req.Data); err != nil {
				return nil, err
			}
		}
		gmsgs, err := fn(gm, req)
		if err != nil {
			nerr, ok := err.(*errnoError)
			if !ok {
				return nil, err
			}
			return nltest.Error(nerr.number, reqs)
		}
		nmsgs := make([]netlink.Message, 0, len(gmsgs))
		for _, msg := range gmsgs {
			b, err := msg.MarshalBinary()
			if err != nil {
				return nil, err
			}
			nmsgs = append(nmsgs, netlink.Message{
				Header: netlink.Header{
					Sequence: req.Header.Sequence,
					PID:      req.Header.PID,
				},
				Data: b,
			})
		}
		return nmsgs, nil
	}
}
adapt is an adapter function for a Func to be used as a nltest.Func.  adapt
handles marshaling and unmarshaling of generic netlink messages.
package genltest
import (
	"fmt"
	"github.com/mdlayher/genetlink"
	"github.com/mdlayher/netlink"
	"github.com/mdlayher/netlink/nltest"
)
func Error(number int) error {
	return &errnoError{number: number}
}
type errnoError struct {
	number int
}
func (err *errnoError) Error() string {
	return fmt.Sprintf("genltest errno: %d", err.number)
}
type Func func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error)
func Dial(fn Func) *genetlink.Conn {
	return genetlink.NewConn(nltest.Dial(adapt(fn)))
}
func ServeFamily(f genetlink.Family, fn Func) Func {
	return serveFamily(f, fn)
}
func CheckRequest(family uint16, command uint8, flags netlink.HeaderFlags, fn Func) Func {
	base := nltest.CheckRequest(
		[]netlink.HeaderType{netlink.HeaderType(family)},
		[]netlink.HeaderFlags{flags},
		nltest.Func(func(_ []netlink.Message) ([]netlink.Message, error) {
			return nil, nil
		}),
	)
	return func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) {
		if _, err := base([]netlink.Message{nreq}); err != nil {
			return nil, fmt.Errorf("genltest: netlink header validation failed: %v", err)
		}
		if want, got := command, greq.Header.Command; command != 0 && want != got {
			return nil, fmt.Errorf("genltest: unexpected generic netlink header command: %d, want: %d", got, want)
		}
		return fn(greq, nreq)
	}
}
var _ nltest.Func = adapt(nil)