github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/values/ip_range_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package values_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/waf/values"
     7  	"github.com/iwind/TeaGo/assert"
     8  	"github.com/iwind/TeaGo/logs"
     9  	"testing"
    10  )
    11  
    12  func TestIPRange_ParseIPRangeList(t *testing.T) {
    13  	{
    14  		var r = values.ParseIPRangeList("")
    15  		logs.PrintAsJSON(r, t)
    16  	}
    17  	{
    18  		var r = values.ParseIPRangeList("192.168.2.1")
    19  		logs.PrintAsJSON(r, t)
    20  	}
    21  	{
    22  		var r = values.ParseIPRangeList(`192.168.2.1  
    23  192.168.1.100/24
    24  192.168.1.1-192.168.2.100
    25  192.168.1.2,192.168.2.200
    26  192.168.2.200 - 192.168.2.100
    27  # 以下是错误的
    28  192.168
    29  192.168.100.1-1`)
    30  		logs.PrintAsJSON(r, t)
    31  	}
    32  }
    33  
    34  func TestIPRange_Contains(t *testing.T) {
    35  	{
    36  		var a = assert.NewAssertion(t)
    37  		var r = values.ParseIPRangeList(`192.168.2.1  
    38  192.168.1.100/24
    39  192.168.1.1-192.168.2.100
    40  192.168.2.2,192.168.2.200
    41  192.168.3.200 - 192.168.3.100
    42  192.168.4.100
    43  192.168.5.1/26`)
    44  		a.IsTrue(r.Contains("192.168.1.102"))
    45  		a.IsTrue(r.Contains("192.168.2.101"))
    46  		a.IsTrue(r.Contains("192.168.1.1"))
    47  		a.IsTrue(r.Contains("192.168.2.100"))
    48  		a.IsFalse(r.Contains("192.168.2.201"))
    49  		a.IsTrue(r.Contains("192.168.3.101"))
    50  		a.IsTrue(r.Contains("192.168.4.100"))
    51  		a.IsTrue(r.Contains("192.168.5.63"))
    52  		a.IsFalse(r.Contains("192.168.5.128"))
    53  	}
    54  }