github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/common/randomip/randomip_test.go (about)

     1  package randomip
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestGetRandomIp(t *testing.T) {
    11  	tests := []struct {
    12  		name     string
    13  		cidr     []string
    14  		errorMsg string
    15  		valid    bool
    16  	}{
    17  		{
    18  			name:  "Valid C class",
    19  			cidr:  []string{"193.6.32.110/24"},
    20  			valid: true,
    21  		},
    22  		{
    23  			name:  "Valid B class",
    24  			cidr:  []string{"128.34.33.29/16"},
    25  			valid: true,
    26  		},
    27  		{
    28  			name:  "Valid A class",
    29  			cidr:  []string{"10.1.2.3/8"},
    30  			valid: true,
    31  		},
    32  		{
    33  			name:  "Valid classless zero based network",
    34  			cidr:  []string{"205.102.139.2/30"},
    35  			valid: true,
    36  		},
    37  		{
    38  			name:  "Valid classless non-zero based network",
    39  			cidr:  []string{"205.102.139.49/29"},
    40  			valid: true,
    41  		},
    42  		{
    43  			name:  "Multiple CIDRs",
    44  			cidr:  []string{"1.2.3.4/15", "230.149.150.22/28"},
    45  			valid: true,
    46  		},
    47  		{
    48  			name:     "Negative CIDR length",
    49  			cidr:     []string{"10.11.12.13/-1"},
    50  			valid:    false,
    51  			errorMsg: "10.11.12.13/-1 is not a valid CIDR",
    52  		},
    53  		{
    54  			name:     "Large CIDR length",
    55  			cidr:     []string{"10.11.12.13/33"},
    56  			valid:    false,
    57  			errorMsg: "10.11.12.13/33 is not a valid CIDR",
    58  		},
    59  		{
    60  			name:     "No CIDR provided",
    61  			cidr:     []string{},
    62  			valid:    false,
    63  			errorMsg: "must specify at least one cidr",
    64  		},
    65  		{
    66  			name:  "Valid but crazy",
    67  			cidr:  []string{"0.0.0.0/0"},
    68  			valid: true,
    69  		},
    70  		{
    71  			name:  "Valid but unlikely",
    72  			cidr:  []string{"193.6.32.109/32"},
    73  			valid: true,
    74  		},
    75  		{
    76  			name:  "Valid IPv6",
    77  			cidr:  []string{"2607:fb91:1294:85fa:3cbf:491:cd46:2625/120"},
    78  			valid: true,
    79  		},
    80  		{
    81  			name:  "Classless IPv4 starting with a non-zero base",
    82  			cidr:  []string{"129.47.78.253/30"},
    83  			valid: true,
    84  		},
    85  		{
    86  			name:  "IPv6 and IPv4",
    87  			cidr:  []string{"2603:8080:4400:d070:913:dee4:6c0c:9ae8/96", "212.78.146.240/25"},
    88  			valid: true,
    89  		},
    90  		{
    91  			name:     "Negative CIDR length IPv6",
    92  			cidr:     []string{"2600:1700:27c:70:44eb:2d78:86b3:e905/-1"},
    93  			valid:    false,
    94  			errorMsg: "2600:1700:27c:70:44eb:2d78:86b3:e905/-1 is not a valid CIDR",
    95  		},
    96  		{
    97  			name:     "Large CIDR length IPv6",
    98  			cidr:     []string{"2607:fb91:bd02:127c:d736:abcf:5c77:e7fd/129"},
    99  			valid:    false,
   100  			errorMsg: "2607:fb91:bd02:127c:d736:abcf:5c77:e7fd/129 is not a valid CIDR",
   101  		},
   102  		{
   103  			name:  "Valid but unlikely IPv6",
   104  			cidr:  []string{"2607:fb91:bd02:127c:d736:abcf:5c77:e7fd/128"},
   105  			valid: true,
   106  		},
   107  	}
   108  
   109  	for _, test := range tests {
   110  		t.Run(test.name, func(t *testing.T) {
   111  			ip, err := GetRandomIPWithCidr(test.cidr...)
   112  			if test.valid {
   113  				assert.NoError(t, err)
   114  				anyInRange := false
   115  				for _, cidr := range test.cidr {
   116  					_, network, _ := net.ParseCIDR(cidr)
   117  					anyInRange = anyInRange || network.Contains(ip)
   118  				}
   119  				assert.Truef(t, anyInRange, "the IP address returned %v is not in range of the provided CIDRs", ip)
   120  			} else {
   121  				assert.Error(t, err, test.errorMsg)
   122  			}
   123  		})
   124  	}
   125  }