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