| 24 | ) |
| 25 | |
| 26 | func TestNetwork_Success(t *testing.T) { |
| 27 | // These test cases are ported from kubernetes/staging/src/k8s.io/apiserver/pkg/cel/library |
| 28 | // to ensure 1-to-1 parity with the Kubernetes implementation. |
| 29 | tests := []struct { |
| 30 | name string |
| 31 | expr string |
| 32 | out any |
| 33 | }{ |
| 34 | // CIDR Accessors |
| 35 | { |
| 36 | name: "cidr ip extraction", |
| 37 | expr: "cidr('192.168.0.0/24').ip() == ip('192.168.0.0')", |
| 38 | out: true, |
| 39 | }, |
| 40 | { |
| 41 | name: "cidr ip extraction (host bits set)", |
| 42 | // K8s behavior: cidr('1.2.3.4/24').ip() returns 1.2.3.4, not 1.2.3.0 |
| 43 | expr: "cidr('192.168.1.5/24').ip() == ip('192.168.1.5')", |
| 44 | out: true, |
| 45 | }, |
| 46 | { |
| 47 | name: "cidr masked", |
| 48 | // masked() zeroes out the host bits |
| 49 | expr: "cidr('192.168.1.5/24').masked() == cidr('192.168.1.0/24')", |
| 50 | out: true, |
| 51 | }, |
| 52 | { |
| 53 | name: "cidr masked identity", |
| 54 | expr: "cidr('192.168.1.0/24').masked() == cidr('192.168.1.0/24')", |
| 55 | out: true, |
| 56 | }, |
| 57 | { |
| 58 | name: "cidr prefixLength", |
| 59 | expr: "cidr('192.168.0.0/24').prefixLength()", |
| 60 | out: int64(24), |
| 61 | }, |
| 62 | { |
| 63 | name: "cidr to string IPv4", |
| 64 | expr: "string(cidr('10.0.0.0/8'))", |
| 65 | out: "10.0.0.0/8", |
| 66 | }, |
| 67 | { |
| 68 | name: "cidr to string IPv6", |
| 69 | expr: "string(cidr('::1/128'))", |
| 70 | out: "::1/128", |
| 71 | }, |
| 72 | |
| 73 | // Containment (CIDR in CIDR) |
| 74 | { |
| 75 | name: "containsCIDR different family", |
| 76 | expr: "cidr('10.0.0.0/8').containsCIDR(cidr('::1/128'))", |
| 77 | out: false, |
| 78 | }, |
| 79 | { |
| 80 | name: "containsCIDR disjoint", |
| 81 | expr: "cidr('10.0.0.0/8').containsCIDR(cidr('11.0.0.0/8'))", |
| 82 | out: false, |
| 83 | }, |