github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/service/internal/sameuser/sameuser_linux_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package sameuser
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  )
    10  
    11  func TestSameUserForRemoteAddr(t *testing.T) {
    12  	uid = 149098
    13  	var proc string
    14  	readFile = func(string) ([]byte, error) {
    15  		return []byte(proc), nil
    16  	}
    17  	for _, tt := range []struct {
    18  		name                  string
    19  		proc                  string
    20  		localAddr, remoteAddr *net.TCPAddr
    21  		want                  bool
    22  	}{
    23  		{
    24  			name: "ipv4-same",
    25  			proc: `  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    26    21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149098        0 8420541 2 0000000000000000 20 0 0 10 -1                  `,
    27  			localAddr:  &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
    28  			remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 59010},
    29  			want:       true,
    30  		},
    31  
    32  		{
    33  			name: "ipv4-not-found",
    34  			proc: `  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    35    21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149098        0 8420541 2 0000000000000000 20 0 0 10 -1                  `,
    36  			localAddr:  &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
    37  			remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 2342},
    38  			want:       false,
    39  		},
    40  
    41  		{
    42  			name: "ipv4-different-uid",
    43  			proc: `  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    44    21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149097        0 8420541 2 0000000000000000 20 0 0 10 -1                  `,
    45  			localAddr:  &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
    46  			remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 59010},
    47  			want:       false,
    48  		},
    49  
    50  		{
    51  			name: "ipv6-same",
    52  			proc: `  sl  local_address                         remote_address                        st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    53     5: 00000000000000000000000001000000:D3E4 00000000000000000000000001000000:0FC8 01 00000000:00000000 00:00000000 00000000 149098        0 8425526 2 0000000000000000 20 0 0 10 -1
    54     6: 00000000000000000000000001000000:0FC8 00000000000000000000000001000000:D3E4 01 00000000:00000000 00:00000000 00000000 149098        0 8424744 1 0000000000000000 20 0 0 10 -1`,
    55  			localAddr:  &net.TCPAddr{IP: net.ParseIP("::1"), Port: 4040},
    56  			remoteAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 54244},
    57  			want:       true,
    58  		},
    59  	} {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			proc = tt.proc
    62  			// The returned error is for reporting only.
    63  			same, _ := sameUserForRemoteAddr(tt.localAddr, tt.remoteAddr)
    64  			if got, want := same, tt.want; got != want {
    65  				t.Errorf("sameUserForRemoteAddr(%v, %v) = %v, want %v", tt.localAddr, tt.remoteAddr, got, want)
    66  			}
    67  		})
    68  	}
    69  }