mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-02 07:07:33 -04:00
unix: test anyToSockaddr without creating actual socket
Test_anyToSockaddr on linux needs to create a socket with the given domain, type and protocol in order to test anyToSockaddr. Depending on kernel version, permission and other factors, a given combination might not be available on the system that runs the test, as is e.g. the case for AF_CAN/SOCK_DGRAM/CAN_J1939 on several builders after CL 272767. The only reason to create the socket is to be able to get the socket protocol in anyToSockaddr using GetsockoptInt(..., SO_PROTOCOL). Move this implementation into a wrapper func which can be overriden in tests to with a func unconditionally returning the protocol under test. This makes the test less dependent on the system it runs on and should fix the builders broken by CL 272767. While at it also removed the unused SocketSpec type in syscall_internal_bsd_test.go and remove an unnecessary error var declaration. Change-Id: Ie8754cf795fa96980b29ae43777f698cd86ae863 Reviewed-on: https://go-review.googlesource.com/c/sys/+/274046 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
committed by
Tobias Klauser
parent
111129e158
commit
760e229fe7
@@ -13,20 +13,12 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// as per socket(2)
|
||||
type SocketSpec struct {
|
||||
domain int
|
||||
typ int
|
||||
protocol int
|
||||
}
|
||||
|
||||
func Test_anyToSockaddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rsa *RawSockaddrAny
|
||||
sa Sockaddr
|
||||
err error
|
||||
skt SocketSpec
|
||||
}{
|
||||
{
|
||||
name: "AF_UNIX zero length",
|
||||
@@ -80,19 +72,6 @@ func Test_anyToSockaddr(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fd := int(0)
|
||||
var err error
|
||||
if tt.skt.domain != 0 {
|
||||
fd, err = Socket(tt.skt.domain, tt.skt.typ, tt.skt.protocol)
|
||||
// Some sockaddr types need specific kernel modules running: if these
|
||||
// are not present we'll get EPROTONOSUPPORT back when trying to create
|
||||
// the socket. Skip the test in this situation.
|
||||
if err == EPROTONOSUPPORT {
|
||||
t.Skip("socket family/protocol not supported by kernel")
|
||||
} else if err != nil {
|
||||
t.Fatalf("socket(%v): %v", tt.skt, err)
|
||||
}
|
||||
defer Close(fd)
|
||||
}
|
||||
sa, err := anyToSockaddr(fd, tt.rsa)
|
||||
if err != tt.err {
|
||||
t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
|
||||
|
||||
Reference in New Issue
Block a user