From 1b658907958d9fcf246e6b089f9c0c944345c9c9 Mon Sep 17 00:00:00 2001 From: Tang Le Date: Fri, 10 Mar 2017 10:22:25 +0800 Subject: [PATCH] Validate rangeStart and rangeEnd specified in conf Signed-off-by: Tang Le --- .../host-local/backend/allocator/allocator.go | 4 +- .../backend/allocator/allocator_test.go | 66 ++++++++++++------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/plugins/ipam/host-local/backend/allocator/allocator.go b/plugins/ipam/host-local/backend/allocator/allocator.go index 5f934240..c93a27ca 100644 --- a/plugins/ipam/host-local/backend/allocator/allocator.go +++ b/plugins/ipam/host-local/backend/allocator/allocator.go @@ -56,13 +56,13 @@ func NewIPAllocator(conf *IPAMConfig, store backend.Store) (*IPAllocator, error) start = ip.NextIP(start) if conf.RangeStart != nil { - if err := validateRangeIP(conf.RangeStart, (*net.IPNet)(&conf.Subnet), nil, nil); err != nil { + if err := validateRangeIP(conf.RangeStart, (*net.IPNet)(&conf.Subnet), start, end); err != nil { return nil, err } start = conf.RangeStart } if conf.RangeEnd != nil { - if err := validateRangeIP(conf.RangeEnd, (*net.IPNet)(&conf.Subnet), start, nil); err != nil { + if err := validateRangeIP(conf.RangeEnd, (*net.IPNet)(&conf.Subnet), start, end); err != nil { return nil, err } end = conf.RangeEnd diff --git a/plugins/ipam/host-local/backend/allocator/allocator_test.go b/plugins/ipam/host-local/backend/allocator/allocator_test.go index 147fe259..894f5022 100644 --- a/plugins/ipam/host-local/backend/allocator/allocator_test.go +++ b/plugins/ipam/host-local/backend/allocator/allocator_test.go @@ -263,33 +263,55 @@ var _ = Describe("host-local ip allocator", func() { }) It("RangeStart must be in the given subnet", func() { - subnet, err := types.ParseCIDR("192.168.1.0/24") - Expect(err).ToNot(HaveOccurred()) - - conf := IPAMConfig{ - Name: "test", - Type: "host-local", - Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask}, - RangeStart: net.ParseIP("10.0.0.1"), + testcases := []struct { + name string + ipnet string + start string + }{ + {"outside-subnet", "192.168.1.0/24", "10.0.0.1"}, + {"zero-ip", "10.1.0.0/16", "10.1.0.0"}, + } + + for _, tc := range testcases { + subnet, err := types.ParseCIDR(tc.ipnet) + Expect(err).ToNot(HaveOccurred()) + + conf := IPAMConfig{ + Name: tc.name, + Type: "host-local", + Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask}, + RangeStart: net.ParseIP(tc.start), + } + store := fakestore.NewFakeStore(map[string]string{}, net.ParseIP("")) + _, err = NewIPAllocator(&conf, store) + Expect(err).To(HaveOccurred()) } - store := fakestore.NewFakeStore(map[string]string{}, net.ParseIP("")) - _, err = NewIPAllocator(&conf, store) - Expect(err).To(HaveOccurred()) }) It("RangeEnd must be in the given subnet", func() { - subnet, err := types.ParseCIDR("192.168.1.0/24") - Expect(err).ToNot(HaveOccurred()) - - conf := IPAMConfig{ - Name: "test", - Type: "host-local", - Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask}, - RangeEnd: net.ParseIP("10.0.0.1"), + testcases := []struct { + name string + ipnet string + end string + }{ + {"outside-subnet", "192.168.1.0/24", "10.0.0.1"}, + {"broadcast-ip", "10.1.0.0/16", "10.1.255.255"}, + } + + for _, tc := range testcases { + subnet, err := types.ParseCIDR(tc.ipnet) + Expect(err).ToNot(HaveOccurred()) + + conf := IPAMConfig{ + Name: tc.name, + Type: "host-local", + Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask}, + RangeEnd: net.ParseIP(tc.end), + } + store := fakestore.NewFakeStore(map[string]string{}, net.ParseIP("")) + _, err = NewIPAllocator(&conf, store) + Expect(err).To(HaveOccurred()) } - store := fakestore.NewFakeStore(map[string]string{}, net.ParseIP("")) - _, err = NewIPAllocator(&conf, store) - Expect(err).To(HaveOccurred()) }) It("RangeEnd must be after RangeStart in the given subnet", func() {