Debian Codegraph @

plugins/ipam/host-local/backend/allocator/range_test.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package allocator
16
17 import (
18 "net"
19
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 "github.com/containernetworking/cni/pkg/types"
24 )
25
26 var _ = Describe("IP ranges", func() {
27 It("should generate sane defaults for ipv4 with a clean prefix", func() {
28 snstr := "192.0.2.0/24"
29 r := Range{Subnet: mustSubnet(snstr)}
30
31 err := r.Canonicalize()
32 Expect(err).NotTo(HaveOccurred())
33
34 Expect(r).To(Equal(Range{
35 Subnet: networkSubnet(snstr),
36 RangeStart: net.IP{192, 0, 2, 1},
37 RangeEnd: net.IP{192, 0, 2, 254},
38 Gateway: net.IP{192, 0, 2, 1},
39 }))
40 })
41 It("should generate sane defaults for a smaller ipv4 subnet", func() {
42 snstr := "192.0.2.0/25"
43 r := Range{Subnet: mustSubnet(snstr)}
44
45 err := r.Canonicalize()
46 Expect(err).NotTo(HaveOccurred())
47
48 Expect(r).To(Equal(Range{
49 Subnet: networkSubnet(snstr),
50 RangeStart: net.IP{192, 0, 2, 1},
51 RangeEnd: net.IP{192, 0, 2, 126},
52 Gateway: net.IP{192, 0, 2, 1},
53 }))
54 })
55 It("should reject ipv4 subnet using a masked address", func() {
56 snstr := "192.0.2.12/24"
57 r := Range{Subnet: mustSubnet(snstr)}
58
59 err := r.Canonicalize()
60 Expect(err).Should(MatchError("Network has host bits set. For a subnet mask of length 24 the network address is 192.0.2.0"))
61 })
62 It("should reject ipv6 subnet using a masked address", func() {
63 snstr := "2001:DB8:1::24:19ff:fee1:c44a/64"
64 r := Range{Subnet: mustSubnet(snstr)}
65
66 err := r.Canonicalize()
67 Expect(err).Should(MatchError("Network has host bits set. For a subnet mask of length 64 the network address is 2001:db8:1::"))
68 })
69 It("should reject ipv6 prefix with host bit set", func() {
70 snstr := "2001:DB8:24:19ff::/63"
71 r := Range{Subnet: mustSubnet(snstr)}
72
73 err := r.Canonicalize()
74 Expect(err).Should(MatchError("Network has host bits set. For a subnet mask of length 63 the network address is 2001:db8:24:19fe::"))
75 })
76 It("should reject ipv4 network with host bit set", func() {
77 snstr := "192.168.127.0/23"
78 r := Range{Subnet: mustSubnet(snstr)}
79
80 err := r.Canonicalize()
81 Expect(err).Should(MatchError("Network has host bits set. For a subnet mask of length 23 the network address is 192.168.126.0"))
82 })
83 It("should generate sane defaults for ipv6 with a clean prefix", func() {
84 snstr := "2001:DB8:1::/64"
85 r := Range{Subnet: mustSubnet(snstr)}
86
87 err := r.Canonicalize()
88 Expect(err).NotTo(HaveOccurred())
89
90 Expect(r).To(Equal(Range{
91 Subnet: networkSubnet(snstr),
92 RangeStart: net.ParseIP("2001:DB8:1::1"),
93 RangeEnd: net.ParseIP("2001:DB8:1::ffff:ffff:ffff:ffff"),
94 Gateway: net.ParseIP("2001:DB8:1::1"),
95 }))
96 })
97
98 It("Should reject a network that's too small", func() {
99 r := Range{Subnet: mustSubnet("192.0.2.0/31")}
100 err := r.Canonicalize()
101 Expect(err).Should(MatchError("Network 192.0.2.0/31 too small to allocate from"))
102 })
103
104 It("should reject invalid RangeStart and RangeEnd specifications", func() {
105 snstr := "192.0.2.0/24"
106 r := Range{Subnet: mustSubnet(snstr), RangeStart: net.ParseIP("192.0.3.0")}
107 err := r.Canonicalize()
108 Expect(err).Should(MatchError("RangeStart 192.0.3.0 not in network 192.0.2.0/24"))
109
110 r = Range{Subnet: mustSubnet(snstr), RangeEnd: net.ParseIP("192.0.4.0")}
111 err = r.Canonicalize()
112 Expect(err).Should(MatchError("RangeEnd 192.0.4.0 not in network 192.0.2.0/24"))
113
114 r = Range{
115 Subnet: networkSubnet(snstr),
116 RangeStart: net.ParseIP("192.0.2.50"),
117 RangeEnd: net.ParseIP("192.0.2.40"),
118 }
119 err = r.Canonicalize()
120 Expect(err).Should(MatchError("RangeStart 192.0.2.50 not in network 192.0.2.0/24"))
121 })
122
123 It("should parse all fields correctly", func() {
124 snstr := "192.0.2.0/24"
125 r := Range{
126 Subnet: mustSubnet(snstr),
127 RangeStart: net.ParseIP("192.0.2.40"),
128 RangeEnd: net.ParseIP("192.0.2.50"),
129 Gateway: net.ParseIP("192.0.2.254"),
130 }
131 err := r.Canonicalize()
132 Expect(err).NotTo(HaveOccurred())
133
134 Expect(r).To(Equal(Range{
135 Subnet: networkSubnet(snstr),
136 RangeStart: net.IP{192, 0, 2, 40},
137 RangeEnd: net.IP{192, 0, 2, 50},
138 Gateway: net.IP{192, 0, 2, 254},
139 }))
140 })
141
142 It("should accept v4 IPs in range and reject IPs out of range", func() {
143 r := Range{
144 Subnet: mustSubnet("192.0.2.0/24"),
145 RangeStart: net.ParseIP("192.0.2.40"),
146 RangeEnd: net.ParseIP("192.0.2.50"),
147 Gateway: net.ParseIP("192.0.2.254"),
148 }
149 err := r.Canonicalize()
150 Expect(err).NotTo(HaveOccurred())
151
152 Expect(r.Contains(net.ParseIP("192.0.3.0"))).Should(BeFalse())
153
154 Expect(r.Contains(net.ParseIP("192.0.2.39"))).Should(BeFalse())
155 Expect(r.Contains(net.ParseIP("192.0.2.40"))).Should(BeTrue())
156 Expect(r.Contains(net.ParseIP("192.0.2.50"))).Should(BeTrue())
157 Expect(r.Contains(net.ParseIP("192.0.2.51"))).Should(BeFalse())
158 })
159
160 It("should accept v6 IPs in range and reject IPs out of range", func() {
161 r := Range{
162 Subnet: mustSubnet("2001:DB8:1::/64"),
163 RangeStart: net.ParseIP("2001:db8:1::40"),
164 RangeEnd: net.ParseIP("2001:db8:1::50"),
165 }
166 err := r.Canonicalize()
167 Expect(err).NotTo(HaveOccurred())
168 Expect(r.Contains(net.ParseIP("2001:db8:2::"))).Should(BeFalse())
169
170 Expect(r.Contains(net.ParseIP("2001:db8:1::39"))).Should(BeFalse())
171 Expect(r.Contains(net.ParseIP("2001:db8:1::40"))).Should(BeTrue())
172 Expect(r.Contains(net.ParseIP("2001:db8:1::50"))).Should(BeTrue())
173 Expect(r.Contains(net.ParseIP("2001:db8:1::51"))).Should(BeFalse())
174 })
175
176 DescribeTable("Detecting overlap",
177 func(r1 Range, r2 Range, expected bool) {
178 r1.Canonicalize()
179 r2.Canonicalize()
180
181 // operation should be commutative
182 Expect(r1.Overlaps(&r2)).To(Equal(expected))
183 Expect(r2.Overlaps(&r1)).To(Equal(expected))
184 },
185 Entry("non-overlapping",
186 Range{Subnet: mustSubnet("10.0.0.0/24")},
187 Range{Subnet: mustSubnet("10.0.1.0/24")},
188 false),
189 Entry("different families",
190 // Note that the bits overlap
191 Range{Subnet: mustSubnet("0.0.0.0/24")},
192 Range{Subnet: mustSubnet("::/24")},
193 false),
194 Entry("Identical",
195 Range{Subnet: mustSubnet("10.0.0.0/24")},
196 Range{Subnet: mustSubnet("10.0.0.0/24")},
197 true),
198 Entry("Containing",
199 Range{Subnet: mustSubnet("10.0.0.0/20")},
200 Range{Subnet: mustSubnet("10.0.1.0/24")},
201 true),
202 Entry("same subnet, non overlapping start + end",
203 Range{
204 Subnet: mustSubnet("10.0.0.0/24"),
205 RangeEnd: net.ParseIP("10.0.0.127"),
206 },
207 Range{
208 Subnet: mustSubnet("10.0.0.0/24"),
209 RangeStart: net.ParseIP("10.0.0.128"),
210 },
211 false),
212 Entry("same subnet, overlapping start + end",
213 Range{
214 Subnet: mustSubnet("10.0.0.0/24"),
215 RangeEnd: net.ParseIP("10.0.0.127"),
216 },
217 Range{
218 Subnet: mustSubnet("10.0.0.0/24"),
219 RangeStart: net.ParseIP("10.0.0.127"),
220 },
221 true),
222 )
223 })
224
225 func mustSubnet(s string) types.IPNet {
226 n, err := types.ParseCIDR(s)
227 if err != nil {
228 Fail(err.Error())
229 }
230 canonicalizeIP(&n.IP)
231 return types.IPNet(*n)
232 }
233
234 func networkSubnet(s string) types.IPNet {
235 net := mustSubnet(s)
236 net.IP = net.IP.Mask(net.Mask)
237 return net
238 }