How to use getIPBlock method of types Package

Best K6 code snippet using types.getIPBlock

ipblock_test.go

Source:ipblock_test.go Github

copy

Full Screen

...57 for name, data := range testdata {58 name, data := name, data59 t.Run(name, func(t *testing.T) {60 t.Parallel()61 b, err := getIPBlock(name)62 require.NoError(t, err)63 assert.Equal(t, data.count, b.count)64 pb := ipPoolBlock{firstIP: b.firstIP}65 idx := big.NewInt(0)66 assert.Equal(t, data.firstIP.To16(), pb.getIP(idx).To16())67 idx.Sub(idx.Add(idx, b.count), big.NewInt(1))68 assert.Equal(t, data.lastIP.To16(), pb.getIP(idx).To16())69 })70 }71}72func TestIPPool(t *testing.T) {73 t.Parallel()74 testdata := map[string]struct {75 count *big.Int76 queries map[uint64]net.IP77 }{78 "192.168.0.101": {79 count: new(big.Int).SetInt64(1),80 queries: map[uint64]net.IP{0: net.ParseIP("192.168.0.101"), 12: net.ParseIP("192.168.0.101")},81 },82 "192.168.0.101,192.168.0.102": {83 count: new(big.Int).SetInt64(2),84 queries: map[uint64]net.IP{85 0: net.ParseIP("192.168.0.101"),86 1: net.ParseIP("192.168.0.102"),87 12: net.ParseIP("192.168.0.101"),88 13: net.ParseIP("192.168.0.102"),89 },90 },91 "192.168.0.101-192.168.0.105,fd00::2/112": {92 count: new(big.Int).SetInt64(65541),93 queries: map[uint64]net.IP{94 0: net.ParseIP("192.168.0.101"),95 1: net.ParseIP("192.168.0.102"),96 5: net.ParseIP("fd00::0"),97 6: net.ParseIP("fd00::1"),98 65541: net.ParseIP("192.168.0.101"),99 },100 },101 "192.168.0.101,192.168.0.102,192.168.0.103,192.168.0.104,192.168.0.105-192.168.0.105,fd00::2/112": {102 count: new(big.Int).SetInt64(65541),103 queries: map[uint64]net.IP{104 0: net.ParseIP("192.168.0.101"),105 1: net.ParseIP("192.168.0.102"),106 2: net.ParseIP("192.168.0.103"),107 3: net.ParseIP("192.168.0.104"),108 4: net.ParseIP("192.168.0.105"),109 5: net.ParseIP("fd00::0"),110 6: net.ParseIP("fd00::1"),111 65541: net.ParseIP("192.168.0.101"),112 },113 },114 }115 for name, data := range testdata {116 name, data := name, data117 t.Run(name, func(t *testing.T) {118 t.Parallel()119 p, err := NewIPPool(name)120 require.NoError(t, err)121 assert.Equal(t, data.count, p.count)122 for q, a := range data.queries {123 assert.Equal(t, a.To16(), p.GetIP(q).To16(), "index %d", q)124 }125 })126 }127}128func TestIpBlockError(t *testing.T) {129 t.Parallel()130 testdata := map[string]string{131 "whatever": "not a valid IP",132 "192.168.0.1012": "not a valid IP",133 "192.168.0.10/244": "invalid CIDR",134 "fd00::0/244": "invalid CIDR",135 "192.168.0.101-192.168.0.102/32": "wrong IP range format",136 "192.168.0.101-fd00::1": "mixed IP range format",137 "fd00::1-192.168.0.101": "mixed IP range format",138 "192.168.0.100-192.168.0.2": "negative IP range",139 "fd00:1:1:0::0-fd00:1:0:ff::3ff": "negative IP range",140 }141 for name, data := range testdata {142 name, data := name, data143 t.Run(name, func(t *testing.T) {144 t.Parallel()145 _, err := getIPBlock(name)146 require.Error(t, err)147 require.Contains(t, err.Error(), data)148 })149 }150}151func TestNullIPPoolMarshalText(t *testing.T) {152 t.Parallel()153 rangeInput := "192.168.20.12-192.168.20.15,192.168.10.0/27"154 p, err := NewIPPool(rangeInput)155 require.NoError(t, err)156 nullpool := NullIPPool{157 Pool: p,158 Valid: true,159 raw: []byte(rangeInput),...

Full Screen

Full Screen

ipblock.go

Source:ipblock.go Github

copy

Full Screen

...39type IPPool struct {40 list []ipPoolBlock41 count *big.Int42}43func getIPBlock(s string) (*ipBlock, error) {44 switch {45 case strings.Contains(s, "-"):46 return ipBlockFromRange(s)47 case strings.Contains(s, "/"):48 return ipBlockFromCIDR(s)49 default:50 if net.ParseIP(s) == nil {51 return nil, fmt.Errorf("%s is not a valid IP, IP range or CIDR", s)52 }53 return ipBlockFromRange(s + "-" + s)54 }55}56func ipBlockFromRange(s string) (*ipBlock, error) {57 ss := strings.SplitN(s, "-", 2)58 ip0, ip1 := net.ParseIP(ss[0]), net.ParseIP(ss[1])59 if ip0 == nil || ip1 == nil {60 return nil, errors.New("wrong IP range format: " + s)61 }62 if (ip0.To4() == nil) != (ip1.To4() == nil) { // XOR63 return nil, errors.New("mixed IP range format: " + s)64 }65 block := ipBlockFromTwoIPs(ip0, ip1)66 if block.count.Sign() <= 0 {67 return nil, errors.New("negative IP range: " + s)68 }69 return block, nil70}71func ipBlockFromTwoIPs(ip0, ip1 net.IP) *ipBlock {72 // This code doesn't do any checks on the validity of the arguments, that should be73 // done before and/or after it is called74 var block ipBlock75 block.firstIP = new(big.Int)76 block.count = new(big.Int)77 block.ipv6 = ip0.To4() == nil78 if block.ipv6 {79 block.firstIP.SetBytes(ip0.To16())80 block.count.SetBytes(ip1.To16())81 } else {82 block.firstIP.SetBytes(ip0.To4())83 block.count.SetBytes(ip1.To4())84 }85 block.count.Sub(block.count, block.firstIP)86 block.count.Add(block.count, big.NewInt(1))87 return &block88}89func ipBlockFromCIDR(s string) (*ipBlock, error) {90 _, pnet, err := net.ParseCIDR(s)91 if err != nil {92 return nil, fmt.Errorf("parseCIDR() failed parsing %s: %w", s, err)93 }94 ip0 := pnet.IP95 // TODO: this is just to copy it, it will probably be better to copy the bytes ...96 ip1 := net.ParseIP(ip0.String())97 if ip1.To4() == nil {98 ip1 = ip1.To16()99 } else {100 ip1 = ip1.To4()101 }102 for i := range ip1 {103 ip1[i] |= (255 ^ pnet.Mask[i])104 }105 block := ipBlockFromTwoIPs(ip0, ip1)106 // in the case of ipv4 if the network is bigger than 31 the first and last IP are reserved so we107 // need to reduce the addresses by 2 and increment the first ip108 if !block.ipv6 && big.NewInt(2).Cmp(block.count) < 0 {109 block.count.Sub(block.count, big.NewInt(2))110 block.firstIP.Add(block.firstIP, big.NewInt(1))111 }112 return block, nil113}114func (b ipPoolBlock) getIP(index *big.Int) net.IP {115 // TODO implement walking ipv6 networks first116 // that will probably require more math ... including knowing which is the next network and ...117 // thinking about it - it looks like it's going to be kind of hard or badly defined118 i := new(big.Int)119 i.Add(b.firstIP, index)120 // TODO use big.Int.FillBytes when golang 1.14 is no longer supported121 return net.IP(i.Bytes())122}123// NewIPPool returns an IPPool slice from the provided string representation that should be comma124// separated list of IPs, IP ranges(ip1-ip2) and CIDRs125func NewIPPool(ranges string) (*IPPool, error) {126 ss := strings.Split(ranges, ",")127 pool := &IPPool{}128 pool.list = make([]ipPoolBlock, len(ss))129 pool.count = new(big.Int)130 for i, bs := range ss {131 r, err := getIPBlock(bs)132 if err != nil {133 return nil, err134 }135 pool.list[i] = ipPoolBlock{136 firstIP: r.firstIP,137 startIndex: new(big.Int).Set(pool.count), // this is how many there are until now138 }139 pool.count.Add(pool.count, r.count)140 }141 // The list gets reversed here as later it is searched based on when the index we are looking is142 // bigger than startIndex but it will be true always for the first block which is with143 // startIndex 0. This can also be fixed by iterating in reverse but this seems better144 for i := 0; i < len(pool.list)/2; i++ {145 pool.list[i], pool.list[len(pool.list)-1-i] = pool.list[len(pool.list)-1-i], pool.list[i]...

Full Screen

Full Screen

getIPBlock

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var types = require('./types.js');6 var ipBlock = types.getIPBlock('

Full Screen

Full Screen

getIPBlock

Using AI Code Generation

copy

Full Screen

1ipBlock := types.GetIPBlock()2ipBlock := types.GetIPBlock()3ipBlock := types.GetIPBlock()4ipBlock := types.GetIPBlock()5ipBlock := types.GetIPBlock()6ipBlock := types.GetIPBlock()7ipBlock := types.GetIPBlock()8ipBlock := types.GetIPBlock()9ipBlock := types.GetIPBlock()10ipBlock := types.GetIPBlock()11ipBlock := types.GetIPBlock()12ipBlock := types.GetIPBlock()13ipBlock := types.GetIPBlock()

Full Screen

Full Screen

getIPBlock

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xlFile, err := xlsx.OpenFile("1.xlsx")4 if err != nil {5 log.Printf("Error: %s6 os.Exit(1)7 }8 cell := sheet.Cell(0, 0)9 fmt.Println(cell.Value)10 ipBlock := cell.GetIPBlock()11 fmt.Printf("IP block: %s12", ipBlock.String())13}14import (15func main() {16 xlFile := xlsx.NewFile()17 sheet, err := xlFile.AddSheet("Sheet1")18 if err != nil {19 log.Printf("Error: %s20 os.Exit(1)21 }22 row := sheet.AddRow()23 cell := row.AddCell()24 ipBlock, err := types.NewIPBlock("

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful