...

Source file src/edge-infra.dev/pkg/lib/kernel/udev/sockets/sockets_test.go

Documentation: edge-infra.dev/pkg/lib/kernel/udev/sockets

     1  //go:build linux
     2  
     3  package sockets
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type ipParseTest = struct {
    12  	description  string
    13  	input        string
    14  	expectedPort int
    15  	expectedIP   [4]byte
    16  	expectedErr  error
    17  }
    18  
    19  var ipParsingTests = []ipParseTest{
    20  	{
    21  		description:  "valid ip and port bind to all ipv4 addresses",
    22  		input:        "0.0.0.0:9997",
    23  		expectedPort: 9997,
    24  		expectedIP:   [4]byte{0, 0, 0, 0},
    25  		expectedErr:  nil,
    26  	},
    27  	{
    28  		description:  "valid ip and port to defined address",
    29  		input:        "192.168.1.1:9997",
    30  		expectedPort: 9997,
    31  		expectedIP:   [4]byte{0xc0, 0xa8, 0x1, 0x1},
    32  		expectedErr:  nil,
    33  	},
    34  	{
    35  		description: "invalid ip",
    36  		input:       "192.1.1:9997",
    37  		expectedErr: ErrInvalidIP,
    38  	},
    39  	{
    40  		description: "invalid port",
    41  		input:       "192.1.1.1:three-thousand",
    42  		expectedErr: ErrInvalidPort,
    43  	},
    44  	{
    45  		description: "invalid socket address format",
    46  		input:       "192.1.1.1.9997",
    47  		expectedErr: ErrInvalidSocketAddress,
    48  	},
    49  }
    50  
    51  func TestIPParsing(t *testing.T) {
    52  	for _, test := range ipParsingTests {
    53  		t.Log("TEST:", test.description)
    54  		port, ip, err := parseAddress(test.input)
    55  		if test.expectedErr != nil {
    56  			require.ErrorIs(t, test.expectedErr, err)
    57  			continue
    58  		}
    59  		require.NoError(t, err)
    60  		require.Equal(t, test.expectedPort, port)
    61  		require.Equal(t, test.expectedIP, ip)
    62  	}
    63  }
    64  

View as plain text