github.com/netdata/go.d.plugin@v0.58.1/pkg/iprange/pool_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package iprange
     4  
     5  import (
     6  	"fmt"
     7  	"math/big"
     8  	"net"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestPool_String(t *testing.T) {
    16  	tests := map[string]struct {
    17  		input      string
    18  		wantString string
    19  	}{
    20  		"singe": {
    21  			input:      "192.0.2.0-192.0.2.10",
    22  			wantString: "192.0.2.0-192.0.2.10",
    23  		},
    24  		"multiple": {
    25  			input:      "192.0.2.0-192.0.2.10 2001:db8::-2001:db8::10",
    26  			wantString: "192.0.2.0-192.0.2.10 2001:db8::-2001:db8::10",
    27  		},
    28  	}
    29  
    30  	for name, test := range tests {
    31  		t.Run(name, func(t *testing.T) {
    32  			rs, err := ParseRanges(test.input)
    33  			require.NoError(t, err)
    34  			p := Pool(rs)
    35  
    36  			assert.Equal(t, test.wantString, p.String())
    37  		})
    38  	}
    39  }
    40  
    41  func TestPool_Size(t *testing.T) {
    42  	tests := map[string]struct {
    43  		input    string
    44  		wantSize *big.Int
    45  	}{
    46  		"singe": {
    47  			input:    "192.0.2.0-192.0.2.10",
    48  			wantSize: big.NewInt(11),
    49  		},
    50  		"multiple": {
    51  			input:    "192.0.2.0-192.0.2.10 2001:db8::-2001:db8::10",
    52  			wantSize: big.NewInt(11 + 17),
    53  		},
    54  	}
    55  
    56  	for name, test := range tests {
    57  		t.Run(name, func(t *testing.T) {
    58  			rs, err := ParseRanges(test.input)
    59  			require.NoError(t, err)
    60  			p := Pool(rs)
    61  
    62  			assert.Equal(t, test.wantSize, p.Size())
    63  		})
    64  	}
    65  }
    66  
    67  func TestPool_Contains(t *testing.T) {
    68  	tests := map[string]struct {
    69  		input    string
    70  		ip       string
    71  		wantFail bool
    72  	}{
    73  		"inside first": {
    74  			input: "192.0.2.0-192.0.2.10 192.0.2.20-192.0.2.30 2001:db8::-2001:db8::10",
    75  			ip:    "192.0.2.5",
    76  		},
    77  		"inside last": {
    78  			input: "192.0.2.0-192.0.2.10 192.0.2.20-192.0.2.30 2001:db8::-2001:db8::10",
    79  			ip:    "2001:db8::5",
    80  		},
    81  		"outside": {
    82  			input:    "192.0.2.0-192.0.2.10 192.0.2.20-192.0.2.30 2001:db8::-2001:db8::10",
    83  			ip:       "192.0.2.100",
    84  			wantFail: true,
    85  		},
    86  	}
    87  
    88  	for name, test := range tests {
    89  		name = fmt.Sprintf("%s (range: %s, ip: %s)", name, test.input, test.ip)
    90  		t.Run(name, func(t *testing.T) {
    91  			rs, err := ParseRanges(test.input)
    92  			require.NoError(t, err)
    93  			ip := net.ParseIP(test.ip)
    94  			require.NotNil(t, ip)
    95  			p := Pool(rs)
    96  
    97  			if test.wantFail {
    98  				assert.False(t, p.Contains(ip))
    99  			} else {
   100  				assert.True(t, p.Contains(ip))
   101  			}
   102  		})
   103  	}
   104  }