github.com/crowdsecurity/crowdsec@v1.6.1/pkg/types/ip_test.go (about)

     1  package types
     2  
     3  import (
     4  	"math"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestIP2Int(t *testing.T) {
    11  
    12  	tEmpty := net.IP{}
    13  	_, _, _, err := IP2Ints(tEmpty)
    14  	if !strings.Contains(err.Error(), "unexpected len 0 for <nil>") {
    15  		t.Fatalf("unexpected: %s", err)
    16  	}
    17  }
    18  func TestRange2Int(t *testing.T) {
    19  	tEmpty := net.IPNet{}
    20  	//empty item
    21  	_, _, _, _, _, err := Range2Ints(tEmpty)
    22  	if !strings.Contains(err.Error(), "converting first ip in range") {
    23  		t.Fatalf("unexpected: %s", err)
    24  	}
    25  
    26  }
    27  
    28  func TestAdd2Int(t *testing.T) {
    29  	tests := []struct {
    30  		in_addr       string
    31  		exp_sz        int
    32  		exp_start_ip  int64
    33  		exp_start_sfx int64
    34  		exp_end_ip    int64
    35  		exp_end_sfx   int64
    36  		exp_error     string
    37  	}{
    38  		{
    39  			in_addr: "7FFF:FFFF:FFFF:FFFF:aaaa:aaaa:aaaa:fff7",
    40  
    41  			exp_sz:        16,
    42  			exp_start_ip:  -math.MaxInt64 + 0x7FFFFFFFFFFFFFFF,
    43  			exp_start_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
    44  			exp_end_ip:    -math.MaxInt64 + 0x7FFFFFFFFFFFFFFF,
    45  			exp_end_sfx:   -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
    46  		},
    47  		{
    48  			in_addr: "aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:fff7",
    49  
    50  			exp_sz:        16,
    51  			exp_start_ip:  -math.MaxInt64 + 0xaaaaaaaaaaaaaaaa,
    52  			exp_start_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
    53  			exp_end_ip:    -math.MaxInt64 + 0xaaaaaaaaaaaaaaaa,
    54  			exp_end_sfx:   -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
    55  		},
    56  		{
    57  			in_addr: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff7",
    58  			/*ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
    59  
    60  			exp_sz:        16,
    61  			exp_start_ip:  math.MaxInt64,
    62  			exp_start_sfx: -math.MaxInt64 + 0xfffffffffffffff7,
    63  			exp_end_ip:    math.MaxInt64,
    64  			exp_end_sfx:   -math.MaxInt64 + 0xfffffffffffffff7,
    65  		},
    66  		{
    67  			in_addr: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
    68  			/*ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
    69  
    70  			exp_sz:        16,
    71  			exp_start_ip:  math.MaxInt64,
    72  			exp_start_sfx: math.MaxInt64,
    73  			exp_end_ip:    math.MaxInt64,
    74  			exp_end_sfx:   math.MaxInt64,
    75  		},
    76  		{
    77  			in_addr: "::",
    78  			/*::*/
    79  
    80  			exp_sz:        16,
    81  			exp_start_ip:  -math.MaxInt64,
    82  			exp_start_sfx: -math.MaxInt64,
    83  			exp_end_ip:    -math.MaxInt64,
    84  			exp_end_sfx:   -math.MaxInt64,
    85  		},
    86  		{
    87  			in_addr: "2001:db8::",
    88  			/*2001:db8:: -> 2001:db8::*/
    89  			exp_sz:        16,
    90  			exp_start_ip:  -math.MaxInt64 + 0x20010DB800000000,
    91  			exp_start_sfx: -math.MaxInt64,
    92  			exp_end_ip:    -math.MaxInt64 + 0x20010DB800000000,
    93  			exp_end_sfx:   -math.MaxInt64,
    94  		},
    95  		{
    96  			in_addr: "2001:db8:0000:0000:0000:0000:0000:00ff",
    97  			/*2001:db8:0000:0000:0000:0000:0000:00ff*/
    98  			exp_sz:        16,
    99  			exp_start_ip:  -math.MaxInt64 + 0x20010DB800000000,
   100  			exp_start_sfx: -math.MaxInt64 + 0xFF,
   101  			exp_end_ip:    -math.MaxInt64 + 0x20010DB800000000,
   102  			exp_end_sfx:   -math.MaxInt64 + 0xFF,
   103  		},
   104  		{
   105  			in_addr: "1.2.3.4",
   106  			/*1.2.3.4*/
   107  			exp_sz:        4,
   108  			exp_start_ip:  -math.MaxInt64 + 0x01020304,
   109  			exp_start_sfx: 0,
   110  			exp_end_ip:    -math.MaxInt64 + 0x01020304,
   111  			exp_end_sfx:   0,
   112  		},
   113  		{
   114  			in_addr: "::/0",
   115  			/*:: -> ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
   116  
   117  			exp_sz:        16,
   118  			exp_start_ip:  -math.MaxInt64,
   119  			exp_start_sfx: -math.MaxInt64,
   120  			exp_end_ip:    math.MaxInt64,
   121  			exp_end_sfx:   math.MaxInt64,
   122  		},
   123  		{
   124  			in_addr: "::/64",
   125  			/*:: -> 0000:0000:0000:0000:ffff:ffff:ffff:ffff*/
   126  			exp_sz:        16,
   127  			exp_start_ip:  -math.MaxInt64,
   128  			exp_start_sfx: -math.MaxInt64,
   129  			exp_end_ip:    -math.MaxInt64,
   130  			exp_end_sfx:   math.MaxInt64,
   131  		},
   132  		{
   133  			in_addr: "2001:db8::/109",
   134  			/*2001:db8:: -> 2001:db8:0000:0000:0000:0000:0007:ffff*/
   135  			exp_sz:        16,
   136  			exp_start_ip:  -math.MaxInt64 + 0x20010DB800000000,
   137  			exp_start_sfx: -math.MaxInt64,
   138  			exp_end_ip:    -math.MaxInt64 + 0x20010DB800000000,
   139  			exp_end_sfx:   -math.MaxInt64 + 0x7FFFF,
   140  		},
   141  		{
   142  			in_addr: "0.0.0.0/0",
   143  			/*0.0.0.0 -> 255.255.255.255*/
   144  			exp_sz:        4,
   145  			exp_start_ip:  -math.MaxInt64,
   146  			exp_start_sfx: 0,
   147  			exp_end_ip:    -math.MaxInt64 + 0xFFFFFFFF,
   148  			exp_end_sfx:   0,
   149  		},
   150  		{
   151  			in_addr: "0.0.0.0/16",
   152  			/*0.0.0.0 -> 0.0.255.255*/
   153  			exp_sz:        4,
   154  			exp_start_ip:  -math.MaxInt64,
   155  			exp_start_sfx: 0,
   156  			exp_end_ip:    -math.MaxInt64 + 0x0000FFFF,
   157  			exp_end_sfx:   0,
   158  		},
   159  		{
   160  			in_addr: "255.255.0.0/16",
   161  			/*255.255.0.0 -> 255.255.255.255*/
   162  			exp_sz:        4,
   163  			exp_start_ip:  -math.MaxInt64 + 0xFFFF0000,
   164  			exp_start_sfx: 0,
   165  			exp_end_ip:    -math.MaxInt64 + 0xFFFFFFFF,
   166  			exp_end_sfx:   0,
   167  		},
   168  		{
   169  			in_addr: "1.2.3.0/24",
   170  			/*1.2.3.0 -> 1.2.3.255*/
   171  			exp_sz:        4,
   172  			exp_start_ip:  -math.MaxInt64 + 0x01020300,
   173  			exp_start_sfx: 0,
   174  			exp_end_ip:    -math.MaxInt64 + 0x010203FF,
   175  			exp_end_sfx:   0,
   176  		},
   177  		/*errors*/
   178  		{
   179  			in_addr:   "xxx/24",
   180  			exp_error: "invalid CIDR address",
   181  		},
   182  		{
   183  			in_addr:   "xxx2",
   184  			exp_error: "invalid address",
   185  		},
   186  	}
   187  
   188  	for idx, test := range tests {
   189  		sz, start_ip, start_sfx, end_ip, end_sfx, err := Addr2Ints(test.in_addr)
   190  		if err != nil && test.exp_error == "" {
   191  			t.Fatalf("%d unexpected error : %s", idx, err)
   192  		}
   193  		if test.exp_error != "" {
   194  			if !strings.Contains(err.Error(), test.exp_error) {
   195  				t.Fatalf("%d unmatched error : %s != %s", idx, err, test.exp_error)
   196  			}
   197  			continue //we can skip this one
   198  		}
   199  		if sz != test.exp_sz {
   200  			t.Fatalf("%d unexpected size %d != %d", idx, sz, test.exp_sz)
   201  		}
   202  		if start_ip != test.exp_start_ip {
   203  			t.Fatalf("%d unexpected start_ip %d != %d", idx, start_ip, test.exp_start_ip)
   204  		}
   205  		if sz == 16 {
   206  			if start_sfx != test.exp_start_sfx {
   207  				t.Fatalf("%d unexpected start sfx %d != %d", idx, start_sfx, test.exp_start_sfx)
   208  			}
   209  		}
   210  		if end_ip != test.exp_end_ip {
   211  			t.Fatalf("%d unexpected end ip %d != %d", idx, end_ip, test.exp_end_ip)
   212  		}
   213  		if sz == 16 {
   214  			if end_sfx != test.exp_end_sfx {
   215  				t.Fatalf("%d unexpected end sfx %d != %d", idx, end_sfx, test.exp_end_sfx)
   216  			}
   217  		}
   218  
   219  	}
   220  }