github.com/igoogolx/clash@v1.19.8/component/process/process_test.go (about)

     1  package process
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func testConn(t *testing.T, network, address string) {
    13  	l, err := net.Listen(network, address)
    14  	if err != nil {
    15  		assert.FailNow(t, "Listen failed", err)
    16  	}
    17  	defer l.Close()
    18  
    19  	conn, err := net.Dial("tcp", l.Addr().String())
    20  	if err != nil {
    21  		assert.FailNow(t, "Dial failed", err)
    22  	}
    23  	defer conn.Close()
    24  
    25  	rConn, err := l.Accept()
    26  	if err != nil {
    27  		assert.FailNow(t, "Accept conn failed", err)
    28  	}
    29  	defer rConn.Close()
    30  
    31  	path, err := FindProcessPath(TCP, conn.LocalAddr().(*net.TCPAddr).AddrPort(), conn.RemoteAddr().(*net.TCPAddr).AddrPort())
    32  	if err != nil {
    33  		assert.FailNow(t, "Find process path failed", err)
    34  	}
    35  
    36  	exePath, err := os.Executable()
    37  	if err != nil {
    38  		assert.FailNow(t, "Get executable failed", err)
    39  	}
    40  
    41  	assert.Equal(t, exePath, path)
    42  }
    43  
    44  func TestFindProcessPathTCP(t *testing.T) {
    45  	t.Run("v4", func(t *testing.T) {
    46  		testConn(t, "tcp4", "127.0.0.1:0")
    47  	})
    48  	t.Run("v6", func(t *testing.T) {
    49  		testConn(t, "tcp6", "[::1]:0")
    50  	})
    51  }
    52  
    53  func testPacketConn(t *testing.T, network, lAddress, rAddress string) {
    54  	lConn, err := net.ListenPacket(network, lAddress)
    55  	if err != nil {
    56  		assert.FailNow(t, "ListenPacket failed", err)
    57  	}
    58  	defer lConn.Close()
    59  
    60  	rConn, err := net.ListenPacket(network, rAddress)
    61  	if err != nil {
    62  		assert.FailNow(t, "ListenPacket failed", err)
    63  	}
    64  	defer rConn.Close()
    65  
    66  	_, err = lConn.WriteTo([]byte{0}, rConn.LocalAddr())
    67  	if err != nil {
    68  		assert.FailNow(t, "Send message failed", err)
    69  	}
    70  
    71  	_, lAddr, err := rConn.ReadFrom([]byte{0})
    72  	if err != nil {
    73  		assert.FailNow(t, "Receive message failed", err)
    74  	}
    75  
    76  	path, err := FindProcessPath(UDP, lAddr.(*net.UDPAddr).AddrPort(), rConn.LocalAddr().(*net.UDPAddr).AddrPort())
    77  	if err != nil {
    78  		assert.FailNow(t, "Find process path", err)
    79  	}
    80  
    81  	exePath, err := os.Executable()
    82  	if err != nil {
    83  		assert.FailNow(t, "Find executable", err)
    84  	}
    85  
    86  	assert.Equal(t, exePath, path)
    87  }
    88  
    89  func TestFindProcessPathUDP(t *testing.T) {
    90  	t.Run("v4", func(t *testing.T) {
    91  		testPacketConn(t, "udp4", "127.0.0.1:0", "127.0.0.1:0")
    92  	})
    93  	t.Run("v6", func(t *testing.T) {
    94  		testPacketConn(t, "udp6", "[::1]:0", "[::1]:0")
    95  	})
    96  	t.Run("v4AnyLocal", func(t *testing.T) {
    97  		testPacketConn(t, "udp4", "0.0.0.0:0", "127.0.0.1:0")
    98  	})
    99  	t.Run("v6AnyLocal", func(t *testing.T) {
   100  		testPacketConn(t, "udp6", "[::]:0", "[::1]:0")
   101  	})
   102  }
   103  
   104  func BenchmarkFindProcessName(b *testing.B) {
   105  	from := netip.MustParseAddrPort("127.0.0.1:11447")
   106  	to := netip.MustParseAddrPort("127.0.0.1:33669")
   107  
   108  	b.ResetTimer()
   109  	for i := 0; i < b.N; i++ {
   110  		FindProcessPath(TCP, from, to)
   111  	}
   112  }