github.com/cilium/cilium@v1.16.2/operator/pkg/lbipam/range_store_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package lbipam 5 6 import ( 7 "net/netip" 8 "testing" 9 ) 10 11 func TestIntersects(t *testing.T) { 12 type test struct { 13 name string 14 from1, to1 string 15 from2, to2 string 16 expected bool 17 } 18 tests := []test{ 19 { 20 name: "no overlap right", 21 from1: "10.0.0.0", to1: "10.0.0.10", 22 from2: "10.0.0.11", to2: "10.0.0.20", 23 expected: false, 24 }, 25 { 26 name: "right edge overlap", 27 from1: "10.0.0.0", to1: "10.0.0.10", 28 from2: "10.0.0.10", to2: "10.0.0.20", 29 expected: true, 30 }, 31 { 32 name: "right overlap", 33 from1: "10.0.0.0", to1: "10.0.0.10", 34 from2: "10.0.0.5", to2: "10.0.0.20", 35 expected: true, 36 }, 37 { 38 name: "full overlap", 39 from1: "10.0.0.0", to1: "10.0.0.10", 40 from2: "10.0.0.0", to2: "10.0.0.10", 41 expected: true, 42 }, 43 { 44 name: "left overlap", 45 from1: "10.0.0.5", to1: "10.0.0.20", 46 from2: "10.0.0.0", to2: "10.0.0.10", 47 expected: true, 48 }, 49 { 50 name: "left edge overlap", 51 from1: "10.0.0.10", to1: "10.0.0.20", 52 from2: "10.0.0.0", to2: "10.0.0.10", 53 expected: true, 54 }, 55 { 56 name: "no overlap left", 57 from1: "10.0.0.11", to1: "10.0.0.20", 58 from2: "10.0.0.0", to2: "10.0.0.10", 59 expected: false, 60 }, 61 } 62 63 for _, subT := range tests { 64 t.Run(subT.name, func(tt *testing.T) { 65 from1 := netip.MustParseAddr(subT.from1) 66 from2 := netip.MustParseAddr(subT.from2) 67 to1 := netip.MustParseAddr(subT.to1) 68 to2 := netip.MustParseAddr(subT.to2) 69 70 got := intersect(from1, to1, from2, to2) 71 if got != subT.expected { 72 tt.Fatalf("%s, %s-%s / %s-%s, got: %v, expected %v", subT.name, from1, to1, from2, to2, got, subT.expected) 73 } 74 }) 75 } 76 }