File: socket_test.go

package info (click to toggle)
golang-github-vishvananda-netlink 1.0.0+git20181030.023a6da-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 928 kB
  • sloc: makefile: 25
file content (58 lines) | stat: -rw-r--r-- 1,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// +build linux

package netlink

import (
	"log"
	"net"
	"os/user"
	"strconv"
	"testing"
)

func TestSocketGet(t *testing.T) {
	defer setUpNetlinkTestWithLoopback(t)()

	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)
	}
}