github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/cidr/cidr_test.go (about)

     1  package cidr
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPublicDetection(t *testing.T) {
    12  
    13  	var tests = []struct {
    14  		input  string
    15  		public bool
    16  	}{
    17  		{
    18  			input:  "1.2.3.4",
    19  			public: true,
    20  		},
    21  		{
    22  			input:  "127.0.0.1",
    23  			public: false,
    24  		},
    25  		{
    26  			input:  "192.168.0.0",
    27  			public: false,
    28  		},
    29  		{
    30  			input:  "192.168.0.1/16",
    31  			public: false,
    32  		},
    33  		{
    34  			input:  "192.168.0.1/24",
    35  			public: false,
    36  		},
    37  		{
    38  			input:  "192.168.0.1/8",
    39  			public: true,
    40  		},
    41  		{
    42  			input:  "10.0.0.0",
    43  			public: false,
    44  		},
    45  		{
    46  			input:  "10.0.0.0/8",
    47  			public: false,
    48  		},
    49  		{
    50  			input:  "10.0.0.0/7",
    51  			public: true,
    52  		},
    53  		{
    54  			input:  "169.254.0.0/16",
    55  			public: false,
    56  		},
    57  		{
    58  			input:  "172.16.0.0/12",
    59  			public: false,
    60  		},
    61  		{
    62  			input:  "172.16.0.0/2",
    63  			public: true,
    64  		},
    65  		{
    66  			input:  "0.0.0.0/0",
    67  			public: true,
    68  		},
    69  		{
    70  			input:  "127.0.0.1/0",
    71  			public: true,
    72  		},
    73  		{
    74  			input:  "0000:0000:0000:0000:0000:0000:0000:0001",
    75  			public: false,
    76  		},
    77  		{
    78  			input:  "::1/128",
    79  			public: false,
    80  		},
    81  		{
    82  			input:  "fe80::/10",
    83  			public: false,
    84  		},
    85  		{
    86  			input:  "fc00::/7",
    87  			public: false,
    88  		},
    89  		{
    90  			input:  "fd00:1234:1234:1234:1234:1234:1234:1234",
    91  			public: false,
    92  		},
    93  		{
    94  			input:  "6666:6666:6666:6666:6666:6666:6666:6666",
    95  			public: true,
    96  		},
    97  		{
    98  			input:  "nonsense",
    99  			public: false,
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		t.Run(test.input, func(t *testing.T) {
   105  			assert.Equal(t, test.public, IsPublic(test.input))
   106  		})
   107  	}
   108  
   109  }
   110  
   111  func TestCountCIDRAddresses(t *testing.T) {
   112  	tests := []struct {
   113  		cidr     string
   114  		expected uint64
   115  	}{
   116  		{
   117  			cidr:     "127.0.0.1",
   118  			expected: 1,
   119  		},
   120  		{
   121  			cidr:     "192.168.0.1/16",
   122  			expected: 0x10000,
   123  		},
   124  		{
   125  			cidr:     "1.2.3.4/0",
   126  			expected: 0x100000000,
   127  		},
   128  		{
   129  			cidr:     "::0/0",
   130  			expected: math.MaxUint64,
   131  		},
   132  	}
   133  	for _, test := range tests {
   134  		t.Run(
   135  			fmt.Sprintf("%s => %d", test.cidr, test.expected),
   136  			func(t *testing.T) {
   137  				actual := CountAddresses(test.cidr)
   138  				assert.Equal(t, test.expected, actual)
   139  			},
   140  		)
   141  	}
   142  }