github.com/samuelkuklis/utils@v1.0.0/net/net_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes 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 net
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestParseCIDRs(t *testing.T) {
    24  	testCases := []struct {
    25  		cidrs         []string
    26  		errString     string
    27  		errorExpected bool
    28  	}{
    29  		{
    30  			cidrs:         []string{},
    31  			errString:     "should not return an error for an empty slice",
    32  			errorExpected: false,
    33  		},
    34  		{
    35  			cidrs:         []string{"10.0.0.0/8", "not-a-valid-cidr", "2000::/10"},
    36  			errString:     "should return error for bad cidr",
    37  			errorExpected: true,
    38  		},
    39  		{
    40  			cidrs:         []string{"10.0.0.0/8", "2000::/10"},
    41  			errString:     "should not return error for good  cidrs",
    42  			errorExpected: false,
    43  		},
    44  	}
    45  
    46  	for _, tc := range testCases {
    47  		cidrs, err := ParseCIDRs(tc.cidrs)
    48  		if tc.errorExpected {
    49  			if err == nil {
    50  				t.Errorf("%v", tc.errString)
    51  			}
    52  			continue
    53  		}
    54  		if err != nil {
    55  			t.Errorf("%v error:%v", tc.errString, err)
    56  		}
    57  
    58  		// validate lengths
    59  		if len(cidrs) != len(tc.cidrs) {
    60  			t.Errorf("cidrs should be of the same lengths %v != %v", len(cidrs), len(tc.cidrs))
    61  		}
    62  
    63  	}
    64  }
    65  
    66  func TestParsePort(t *testing.T) {
    67  	var tests = []struct {
    68  		name          string
    69  		port          string
    70  		allowZero     bool
    71  		expectedPort  int
    72  		expectedError bool
    73  	}{
    74  		{
    75  			name:         "valid port: 1",
    76  			port:         "1",
    77  			expectedPort: 1,
    78  		},
    79  		{
    80  			name:         "valid port: 1234",
    81  			port:         "1234",
    82  			expectedPort: 1234,
    83  		},
    84  		{
    85  			name:         "valid port: 65535",
    86  			port:         "65535",
    87  			expectedPort: 65535,
    88  		},
    89  		{
    90  			name:          "invalid port: not a number",
    91  			port:          "a",
    92  			expectedError: true,
    93  			allowZero:     false,
    94  		},
    95  		{
    96  			name:          "invalid port: too small",
    97  			port:          "0",
    98  			expectedError: true,
    99  		},
   100  		{
   101  			name:          "invalid port: negative",
   102  			port:          "-10",
   103  			expectedError: true,
   104  		},
   105  		{
   106  			name:          "invalid port: too big",
   107  			port:          "65536",
   108  			expectedError: true,
   109  		},
   110  		{
   111  			name:      "zero port: allowed",
   112  			port:      "0",
   113  			allowZero: true,
   114  		},
   115  		{
   116  			name:          "zero port: not allowed",
   117  			port:          "0",
   118  			expectedError: true,
   119  		},
   120  	}
   121  
   122  	for _, rt := range tests {
   123  		t.Run(rt.name, func(t *testing.T) {
   124  			actualPort, actualError := ParsePort(rt.port, rt.allowZero)
   125  
   126  			if actualError != nil && !rt.expectedError {
   127  				t.Errorf("%s unexpected failure: %v", rt.name, actualError)
   128  				return
   129  			}
   130  			if actualError == nil && rt.expectedError {
   131  				t.Errorf("%s passed when expected to fail", rt.name)
   132  				return
   133  			}
   134  			if actualPort != rt.expectedPort {
   135  				t.Errorf("%s returned wrong port: got %d, expected %d", rt.name, actualPort, rt.expectedPort)
   136  			}
   137  		})
   138  	}
   139  }
   140  
   141  func TestRangeSize(t *testing.T) {
   142  	testCases := []struct {
   143  		name  string
   144  		cidr  string
   145  		addrs int64
   146  	}{
   147  		{
   148  			name:  "supported IPv4 cidr",
   149  			cidr:  "192.168.1.0/24",
   150  			addrs: 256,
   151  		},
   152  		{
   153  			name:  "unsupported IPv4 cidr",
   154  			cidr:  "192.168.1.0/1",
   155  			addrs: 0,
   156  		},
   157  		{
   158  			name:  "unsupported IPv6 mask",
   159  			cidr:  "2001:db8::/1",
   160  			addrs: 0,
   161  		},
   162  	}
   163  
   164  	for _, tc := range testCases {
   165  		_, cidr, err := ParseCIDRSloppy(tc.cidr)
   166  		if err != nil {
   167  			t.Errorf("failed to parse cidr for test %s, unexpected error: '%s'", tc.name, err)
   168  		}
   169  		if size := RangeSize(cidr); size != tc.addrs {
   170  			t.Errorf("test %s failed. %s should have a range size of %d, got %d",
   171  				tc.name, tc.cidr, tc.addrs, size)
   172  		}
   173  	}
   174  }
   175  
   176  func TestGetIndexedIP(t *testing.T) {
   177  	testCases := []struct {
   178  		cidr        string
   179  		index       int
   180  		expectError bool
   181  		expectedIP  string
   182  	}{
   183  		{
   184  			cidr:        "192.168.1.0/24",
   185  			index:       20,
   186  			expectError: false,
   187  			expectedIP:  "192.168.1.20",
   188  		},
   189  		{
   190  			cidr:        "192.168.1.0/30",
   191  			index:       10,
   192  			expectError: true,
   193  		},
   194  		{
   195  			cidr:        "192.168.1.0/24",
   196  			index:       255,
   197  			expectError: false,
   198  			expectedIP:  "192.168.1.255",
   199  		},
   200  		{
   201  			cidr:        "255.255.255.0/24",
   202  			index:       256,
   203  			expectError: true,
   204  		},
   205  		{
   206  			cidr:        "fd:11:b2:be::/120",
   207  			index:       20,
   208  			expectError: false,
   209  			expectedIP:  "fd:11:b2:be::14",
   210  		},
   211  		{
   212  			cidr:        "fd:11:b2:be::/126",
   213  			index:       10,
   214  			expectError: true,
   215  		},
   216  		{
   217  			cidr:        "fd:11:b2:be::/120",
   218  			index:       255,
   219  			expectError: false,
   220  			expectedIP:  "fd:11:b2:be::ff",
   221  		},
   222  		{
   223  			cidr:        "00:00:00:be::/120",
   224  			index:       255,
   225  			expectError: false,
   226  			expectedIP:  "::be:0:0:0:ff",
   227  		},
   228  	}
   229  
   230  	for _, tc := range testCases {
   231  		_, subnet, err := ParseCIDRSloppy(tc.cidr)
   232  		if err != nil {
   233  			t.Errorf("failed to parse cidr %s, unexpected error: '%s'", tc.cidr, err)
   234  		}
   235  
   236  		ip, err := GetIndexedIP(subnet, tc.index)
   237  		if err == nil && tc.expectError || err != nil && !tc.expectError {
   238  			t.Errorf("expectedError is %v and err is %s", tc.expectError, err)
   239  			continue
   240  		}
   241  
   242  		if err == nil {
   243  			ipString := ip.String()
   244  			if ipString != tc.expectedIP {
   245  				t.Errorf("expected %s but instead got %s", tc.expectedIP, ipString)
   246  			}
   247  		}
   248  
   249  	}
   250  }