mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-02 15:17:31 -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"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// as per socket(2)
|
|
||||||
type SocketSpec struct {
|
|
||||||
domain int
|
|
||||||
typ int
|
|
||||||
protocol int
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_anyToSockaddr(t *testing.T) {
|
func Test_anyToSockaddr(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
rsa *RawSockaddrAny
|
rsa *RawSockaddrAny
|
||||||
sa Sockaddr
|
sa Sockaddr
|
||||||
err error
|
err error
|
||||||
skt SocketSpec
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "AF_UNIX zero length",
|
name: "AF_UNIX zero length",
|
||||||
@@ -80,19 +72,6 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fd := int(0)
|
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)
|
sa, err := anyToSockaddr(fd, tt.rsa)
|
||||||
if err != tt.err {
|
if err != tt.err {
|
||||||
t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
|
t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ func Test_anyToSockaddr_darwin(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fd := int(0)
|
fd := int(0)
|
||||||
var err error
|
|
||||||
sa, err := anyToSockaddr(fd, tt.rsa)
|
sa, err := anyToSockaddr(fd, tt.rsa)
|
||||||
if err != tt.err {
|
if err != tt.err {
|
||||||
t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
|
t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
|
||||||
|
|||||||
@@ -13,20 +13,17 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// as per socket(2)
|
func makeProto(proto int) *int {
|
||||||
type SocketSpec struct {
|
return &proto
|
||||||
domain int
|
|
||||||
typ int
|
|
||||||
protocol int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_anyToSockaddr(t *testing.T) {
|
func Test_anyToSockaddr(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
rsa *RawSockaddrAny
|
rsa *RawSockaddrAny
|
||||||
sa Sockaddr
|
sa Sockaddr
|
||||||
err error
|
err error
|
||||||
skt SocketSpec
|
proto *int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "AF_TIPC bad addrtype",
|
name: "AF_TIPC bad addrtype",
|
||||||
@@ -109,7 +106,7 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
Addr: [4]byte{0xef, 0x10, 0x5b, 0xa2},
|
Addr: [4]byte{0xef, 0x10, 0x5b, 0xa2},
|
||||||
ConnId: 0x1234abcd,
|
ConnId: 0x1234abcd,
|
||||||
},
|
},
|
||||||
skt: SocketSpec{domain: AF_INET, typ: SOCK_DGRAM, protocol: IPPROTO_L2TP},
|
proto: makeProto(IPPROTO_L2TP),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "AF_INET6 IPPROTO_L2TP",
|
name: "AF_INET6 IPPROTO_L2TP",
|
||||||
@@ -135,7 +132,7 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
ZoneId: 90210,
|
ZoneId: 90210,
|
||||||
ConnId: 0x1234abcd,
|
ConnId: 0x1234abcd,
|
||||||
},
|
},
|
||||||
skt: SocketSpec{domain: AF_INET6, typ: SOCK_DGRAM, protocol: IPPROTO_L2TP},
|
proto: makeProto(IPPROTO_L2TP),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "AF_UNIX unnamed/abstract",
|
name: "AF_UNIX unnamed/abstract",
|
||||||
@@ -185,7 +182,7 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
RxID: 0xAAAAAAAA,
|
RxID: 0xAAAAAAAA,
|
||||||
TxID: 0xBBBBBBBB,
|
TxID: 0xBBBBBBBB,
|
||||||
},
|
},
|
||||||
skt: SocketSpec{domain: AF_CAN, typ: SOCK_RAW, protocol: CAN_RAW},
|
proto: makeProto(CAN_RAW),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "AF_CAN CAN_J1939",
|
name: "AF_CAN CAN_J1939",
|
||||||
@@ -205,7 +202,7 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
PGN: 0xBBBBBBBB,
|
PGN: 0xBBBBBBBB,
|
||||||
Addr: 0xCC,
|
Addr: 0xCC,
|
||||||
},
|
},
|
||||||
skt: SocketSpec{domain: AF_CAN, typ: SOCK_DGRAM, protocol: CAN_J1939},
|
proto: makeProto(CAN_J1939),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "AF_MAX EAFNOSUPPORT",
|
name: "AF_MAX EAFNOSUPPORT",
|
||||||
@@ -219,27 +216,15 @@ func Test_anyToSockaddr(t *testing.T) {
|
|||||||
// TODO: expand to support other families.
|
// TODO: expand to support other families.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
realSocketProtocol := socketProtocol
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fd := int(0)
|
fd := int(0)
|
||||||
var err error
|
if tt.proto != nil {
|
||||||
if tt.skt.domain != 0 {
|
socketProtocol = func(fd int) (int, error) { return *tt.proto, nil }
|
||||||
fd, err = Socket(tt.skt.domain, tt.skt.typ, tt.skt.protocol)
|
} else {
|
||||||
// Some sockaddr types need specific kernel modules running: if these
|
socketProtocol = realSocketProtocol
|
||||||
// are not present we'll get EPROTONOSUPPORT/EAFNOSUPPORT 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 == EAFNOSUPPORT {
|
|
||||||
t.Skip("socket address family not supported by kernel")
|
|
||||||
} else if err == EACCES {
|
|
||||||
// Some platforms might require elevated privileges to perform
|
|
||||||
// actions on sockets. Skip the test in this situation.
|
|
||||||
t.Skip("socket operation not permitted")
|
|
||||||
} else if err != nil {
|
|
||||||
t.Fatalf("socket(%v): %v", tt.skt, err)
|
|
||||||
}
|
|
||||||
defer Close(fd)
|
|
||||||
}
|
}
|
||||||
sa, err := anyToSockaddr(fd, tt.rsa)
|
sa, err := anyToSockaddr(fd, tt.rsa)
|
||||||
if err != tt.err {
|
if err != tt.err {
|
||||||
|
|||||||
@@ -982,6 +982,10 @@ func (sa *SockaddrIUCV) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
|||||||
return unsafe.Pointer(&sa.raw), SizeofSockaddrIUCV, nil
|
return unsafe.Pointer(&sa.raw), SizeofSockaddrIUCV, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var socketProtocol = func(fd int) (int, error) {
|
||||||
|
return GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
|
||||||
|
}
|
||||||
|
|
||||||
func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
||||||
switch rsa.Addr.Family {
|
switch rsa.Addr.Family {
|
||||||
case AF_NETLINK:
|
case AF_NETLINK:
|
||||||
@@ -1032,7 +1036,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
|||||||
return sa, nil
|
return sa, nil
|
||||||
|
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
proto, err := GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
|
proto, err := socketProtocol(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1058,7 +1062,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
proto, err := GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
|
proto, err := socketProtocol(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1093,7 +1097,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
|||||||
}
|
}
|
||||||
return sa, nil
|
return sa, nil
|
||||||
case AF_BLUETOOTH:
|
case AF_BLUETOOTH:
|
||||||
proto, err := GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
|
proto, err := socketProtocol(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1180,7 +1184,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
|
|||||||
return sa, nil
|
return sa, nil
|
||||||
|
|
||||||
case AF_CAN:
|
case AF_CAN:
|
||||||
proto, err := GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
|
proto, err := socketProtocol(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user