2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2017-02-02 23:07:22 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os/user"
|
|
|
|
"strconv"
|
2020-04-13 14:32:22 +00:00
|
|
|
"syscall"
|
2017-02-02 23:07:22 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSocketGet(t *testing.T) {
|
2017-11-28 00:10:03 +00:00
|
|
|
defer setUpNetlinkTestWithLoopback(t)()
|
|
|
|
|
2017-02-02 23:07:22 +00:00
|
|
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
conn, err := net.Dial(l.Addr().Network(), l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
localAddr := conn.LocalAddr().(*net.TCPAddr)
|
|
|
|
remoteAddr := conn.RemoteAddr().(*net.TCPAddr)
|
|
|
|
socket, err := SocketGet(localAddr, remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := socket.ID.Source, localAddr.IP; !got.Equal(want) {
|
|
|
|
t.Fatalf("local ip = %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := socket.ID.Destination, remoteAddr.IP; !got.Equal(want) {
|
|
|
|
t.Fatalf("remote ip = %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := int(socket.ID.SourcePort), localAddr.Port; got != want {
|
|
|
|
t.Fatalf("local port = %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := int(socket.ID.DestinationPort), remoteAddr.Port; got != want {
|
|
|
|
t.Fatalf("remote port = %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got, want := strconv.Itoa(int(socket.UID)), u.Uid; got != want {
|
|
|
|
t.Fatalf("UID = %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 14:32:22 +00:00
|
|
|
|
|
|
|
func TestSocketDiagTCPInfo(t *testing.T) {
|
|
|
|
Family4 := uint8(syscall.AF_INET)
|
|
|
|
Family6 := uint8(syscall.AF_INET6)
|
|
|
|
families := []uint8{Family4, Family6}
|
|
|
|
for _, wantFamily := range families {
|
|
|
|
res, err := SocketDiagTCPInfo(wantFamily)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, i := range res {
|
|
|
|
gotFamily := i.InetDiagMsg.Family
|
|
|
|
if gotFamily != wantFamily {
|
|
|
|
t.Fatalf("Socket family = %d, want %d", gotFamily, wantFamily)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|