github.com/rootless-containers/rootlesskit/v2@v2.3.4/pkg/network/iputils/iputils_test.go (about)

     1  package iputils
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  )
     7  
     8  func TestAddIPInt(t *testing.T) {
     9  	type testCase struct {
    10  		s        string
    11  		i        int
    12  		expected string
    13  	}
    14  	testCases := []testCase{
    15  		{
    16  			"10.0.2.0",
    17  			100,
    18  			"10.0.2.100",
    19  		},
    20  		{
    21  			"255.255.255.100",
    22  			155,
    23  			"255.255.255.255",
    24  		},
    25  		{
    26  			"255.255.255.100",
    27  			156,
    28  			"",
    29  		},
    30  	}
    31  	for i, tc := range testCases {
    32  		ip := net.ParseIP(tc.s)
    33  		if ip == nil {
    34  			t.Fatalf("invalid IP: %q", tc.s)
    35  		}
    36  		gotIP, err := AddIPInt(ip, tc.i)
    37  		if tc.expected == "" {
    38  			if err == nil {
    39  				t.Fatalf("#%d: expected error, got no error", i)
    40  			}
    41  		} else {
    42  			if err != nil {
    43  				t.Fatalf("#%d: expected no error, got %q", i, err)
    44  			}
    45  			got := gotIP.String()
    46  			if got != tc.expected {
    47  				t.Fatalf("#%d: expected %q, got %q", i, tc.expected, got)
    48  			}
    49  		}
    50  	}
    51  }