@@ -1542,6 +1542,129 @@ func TestIPv4Validation(t *testing.T) {
15421542 }
15431543}
15441544
1545+ func TestCIDRValidation (t * testing.T ) {
1546+ tests := []struct {
1547+ param string
1548+ expected bool
1549+ }{
1550+ {"10.0.0.0/0" , true },
1551+ {"10.0.0.1/8" , true },
1552+ {"172.16.0.1/16" , true },
1553+ {"192.168.0.1/24" , true },
1554+ {"192.168.255.254/24" , true },
1555+ {"192.168.255.254/48" , false },
1556+ {"192.168.255.256/24" , false },
1557+ {"172.16.255.254/16" , true },
1558+ {"172.16.256.255/16" , false },
1559+ {"2001:cdba:0000:0000:0000:0000:3257:9652/64" , true },
1560+ {"2001:cdba:0000:0000:0000:0000:3257:9652/256" , false },
1561+ {"2001:cdba:0:0:0:0:3257:9652/32" , true },
1562+ {"2001:cdba::3257:9652/16" , true },
1563+ }
1564+
1565+ for i , test := range tests {
1566+
1567+ errs := validate .Field (test .param , "cidr" )
1568+
1569+ if test .expected == true {
1570+ if ! IsEqual (errs , nil ) {
1571+ t .Fatalf ("Index: %d cidr failed Error: %s" , i , errs )
1572+ }
1573+ } else {
1574+ if IsEqual (errs , nil ) {
1575+ t .Fatalf ("Index: %d cidr failed Error: %s" , i , errs )
1576+ } else {
1577+ val := errs .(ValidationErrors )["" ]
1578+ if val .Tag != "cidr" {
1579+ t .Fatalf ("Index: %d cidr failed Error: %s" , i , errs )
1580+ }
1581+ }
1582+ }
1583+ }
1584+ }
1585+
1586+ func TestCIDRv6Validation (t * testing.T ) {
1587+ tests := []struct {
1588+ param string
1589+ expected bool
1590+ }{
1591+ {"10.0.0.0/0" , false },
1592+ {"10.0.0.1/8" , false },
1593+ {"172.16.0.1/16" , false },
1594+ {"192.168.0.1/24" , false },
1595+ {"192.168.255.254/24" , false },
1596+ {"192.168.255.254/48" , false },
1597+ {"192.168.255.256/24" , false },
1598+ {"172.16.255.254/16" , false },
1599+ {"172.16.256.255/16" , false },
1600+ {"2001:cdba:0000:0000:0000:0000:3257:9652/64" , true },
1601+ {"2001:cdba:0000:0000:0000:0000:3257:9652/256" , false },
1602+ {"2001:cdba:0:0:0:0:3257:9652/32" , true },
1603+ {"2001:cdba::3257:9652/16" , true },
1604+ }
1605+
1606+ for i , test := range tests {
1607+
1608+ errs := validate .Field (test .param , "cidrv6" )
1609+
1610+ if test .expected == true {
1611+ if ! IsEqual (errs , nil ) {
1612+ t .Fatalf ("Index: %d cidrv6 failed Error: %s" , i , errs )
1613+ }
1614+ } else {
1615+ if IsEqual (errs , nil ) {
1616+ t .Fatalf ("Index: %d cidrv6 failed Error: %s" , i , errs )
1617+ } else {
1618+ val := errs .(ValidationErrors )["" ]
1619+ if val .Tag != "cidrv6" {
1620+ t .Fatalf ("Index: %d cidrv6 failed Error: %s" , i , errs )
1621+ }
1622+ }
1623+ }
1624+ }
1625+ }
1626+
1627+ func TestCIDRv4Validation (t * testing.T ) {
1628+ tests := []struct {
1629+ param string
1630+ expected bool
1631+ }{
1632+ {"10.0.0.0/0" , true },
1633+ {"10.0.0.1/8" , true },
1634+ {"172.16.0.1/16" , true },
1635+ {"192.168.0.1/24" , true },
1636+ {"192.168.255.254/24" , true },
1637+ {"192.168.255.254/48" , false },
1638+ {"192.168.255.256/24" , false },
1639+ {"172.16.255.254/16" , true },
1640+ {"172.16.256.255/16" , false },
1641+ {"2001:cdba:0000:0000:0000:0000:3257:9652/64" , false },
1642+ {"2001:cdba:0000:0000:0000:0000:3257:9652/256" , false },
1643+ {"2001:cdba:0:0:0:0:3257:9652/32" , false },
1644+ {"2001:cdba::3257:9652/16" , false },
1645+ }
1646+
1647+ for i , test := range tests {
1648+
1649+ errs := validate .Field (test .param , "cidrv4" )
1650+
1651+ if test .expected == true {
1652+ if ! IsEqual (errs , nil ) {
1653+ t .Fatalf ("Index: %d cidrv4 failed Error: %s" , i , errs )
1654+ }
1655+ } else {
1656+ if IsEqual (errs , nil ) {
1657+ t .Fatalf ("Index: %d cidrv4 failed Error: %s" , i , errs )
1658+ } else {
1659+ val := errs .(ValidationErrors )["" ]
1660+ if val .Tag != "cidrv4" {
1661+ t .Fatalf ("Index: %d cidrv4 failed Error: %s" , i , errs )
1662+ }
1663+ }
1664+ }
1665+ }
1666+ }
1667+
15451668func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation (t * testing.T ) {
15461669
15471670 var m map [string ]string
0 commit comments