github.com/containerd/nerdctl@v1.7.7/pkg/portutil/procnet/procnetd_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package procnet
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"gotest.tools/v3/assert"
    24  )
    25  
    26  // All the code in this file is copied from the iima project in https://github.com/lima-vm/lima/blob/v0.8.3/pkg/guestagent/procnettcp/procnettcp_test.go.
    27  // and is licensed under the Apache License, Version 2.0.
    28  
    29  func TestParseTCP(t *testing.T) {
    30  	procNetTCP := []string{
    31  		"0: 0100007F:8AEF 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 28152 1 0000000000000000 100 0 0 10 0",
    32  		"1: 0103000A:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 31474 1 0000000000000000 100 0 0 10 5",
    33  		"2: 3500007F:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000   102        0 30955 1 0000000000000000 100 0 0 10 0",
    34  		"3: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 32910 1 0000000000000000 100 0 0 10 0",
    35  		"4: 0100007F:053A 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 31430 1 0000000000000000 100 0 0 10 0",
    36  		"5: 0B3CA8C0:0016 690AA8C0:F705 01 00000000:00000000 02:00028D8B 00000000     0        0 32989 4 0000000000000000 20 4 31 10 19"}
    37  	entries := Parse(procNetTCP)
    38  	t.Log(entries)
    39  
    40  	assert.Check(t, net.ParseIP("127.0.0.1").Equal(entries[0].LocalIP))
    41  	assert.Equal(t, uint64(35567), entries[0].LocalPort)
    42  
    43  	assert.Check(t, net.ParseIP("192.168.60.11").Equal(entries[5].LocalIP))
    44  	assert.Equal(t, uint64(22), entries[5].LocalPort)
    45  }
    46  
    47  func TestParseTCP6(t *testing.T) {
    48  	procNetTCP := []string{
    49  		"0: 000080FE00000000FF57A6705DC771FE:0050 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 850222 1 0000000000000000 100 0 0 10 0",
    50  	}
    51  	entries := Parse(procNetTCP)
    52  	t.Log(entries)
    53  
    54  	assert.Check(t, net.ParseIP("fe80::70a6:57ff:fe71:c75d").Equal(entries[0].LocalIP))
    55  	assert.Equal(t, uint64(80), entries[0].LocalPort)
    56  }
    57  
    58  func TestParseTCP6Zero(t *testing.T) {
    59  	procNetTCP := []string{
    60  		"0: 00000000000000000000000000000000:0016 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 33825 1 0000000000000000 100 0 0 10 0",
    61  		"1: 00000000000000000000000000000000:006F 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 26772 1 0000000000000000 100 0 0 10 0",
    62  		"2: 00000000000000000000000000000000:0050 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 1210901 1 0000000000000000 100 0 0 10 0"}
    63  
    64  	entries := Parse(procNetTCP)
    65  	t.Log(entries)
    66  
    67  	assert.Check(t, net.IPv6zero.Equal(entries[0].LocalIP))
    68  	assert.Equal(t, uint64(22), entries[0].LocalPort)
    69  }