| 985 | } |
| 986 | |
| 987 | func TestCIDRCost(t *testing.T) { |
| 988 | ipv4 := "cidr('192.168.0.0/16')" |
| 989 | ipv4BaseEstimatedCost := checker.CostEstimate{Min: 2, Max: 2} |
| 990 | ipv4BaseRuntimeCost := uint64(2) |
| 991 | |
| 992 | ipv6 := "cidr('2001:db8::/32')" |
| 993 | ipv6BaseEstimatedCost := checker.CostEstimate{Min: 2, Max: 2} |
| 994 | ipv6BaseRuntimeCost := uint64(2) |
| 995 | |
| 996 | type testCase struct { |
| 997 | ops []string |
| 998 | expectEsimatedCost func(checker.CostEstimate) checker.CostEstimate |
| 999 | expectRuntimeCost func(uint64) uint64 |
| 1000 | } |
| 1001 | |
| 1002 | cases := []testCase{ |
| 1003 | { |
| 1004 | // For just parsing the IP, the cost is expected to be the base. |
| 1005 | ops: []string{""}, |
| 1006 | expectEsimatedCost: func(c checker.CostEstimate) checker.CostEstimate { return c }, |
| 1007 | expectRuntimeCost: func(c uint64) uint64 { return c }, |
| 1008 | }, |
| 1009 | { |
| 1010 | ops: []string{".ip()", ".prefixLength()", ".masked()"}, |
| 1011 | // For most other operations, the cost is expected to be the base + 1. |
| 1012 | expectEsimatedCost: func(c checker.CostEstimate) checker.CostEstimate { |
| 1013 | return checker.CostEstimate{Min: c.Min + 1, Max: c.Max + 1} |
| 1014 | }, |
| 1015 | expectRuntimeCost: func(c uint64) uint64 { return c + 1 }, |
| 1016 | }, |
| 1017 | { |
| 1018 | ops: []string{" == cidr('2001:db8::/32')"}, |
| 1019 | // For most other operations, the cost is expected to be the base + 1. |
| 1020 | expectEsimatedCost: func(c checker.CostEstimate) checker.CostEstimate { |
| 1021 | return c.Add(ipv6BaseEstimatedCost).Add(checker.CostEstimate{Min: 1, Max: 2}) |
| 1022 | }, |
| 1023 | expectRuntimeCost: func(c uint64) uint64 { return c + ipv6BaseRuntimeCost + 1 }, |
| 1024 | }, |
| 1025 | } |
| 1026 | |
| 1027 | //nolint:gocritic |
| 1028 | ipv4Cases := append(cases, []testCase{ |
| 1029 | { |
| 1030 | ops: []string{".containsCIDR(cidr('192.0.0.0/30'))"}, |
| 1031 | expectEsimatedCost: func(c checker.CostEstimate) checker.CostEstimate { |
| 1032 | return checker.CostEstimate{Min: c.Min + 5, Max: c.Max + 9} |
| 1033 | }, |
| 1034 | expectRuntimeCost: func(c uint64) uint64 { return c + 5 }, |
| 1035 | }, |
| 1036 | { |
| 1037 | ops: []string{".containsCIDR(cidr('192.168.0.0/16'))"}, |
| 1038 | expectEsimatedCost: func(c checker.CostEstimate) checker.CostEstimate { |
| 1039 | return checker.CostEstimate{Min: c.Min + 5, Max: c.Max + 9} |
| 1040 | }, |
| 1041 | expectRuntimeCost: func(c uint64) uint64 { return c + 5 }, |
| 1042 | }, |
| 1043 | { |
| 1044 | ops: []string{".containsCIDR('192.0.0.0/30')"}, |