Skip to content

Commit

Permalink
[fix] Fixed subnet division rule validation when master subnet is empty
Browse files Browse the repository at this point in the history
#866

Fixes #866

---------

Co-authored-by: Federico Capoano <[email protected]>
  • Loading branch information
praptisharma28 and nemesifier authored Sep 26, 2024
1 parent 448dd6b commit bab9db1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 7 additions & 4 deletions openwisp_controller/subnet_division/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def rule_class(self):
def clean(self):
super().clean()
self._validate_label()
self._validate_master_subnet_validity()
self._validate_master_subnet_consistency()
self._validate_ip_address_consistency()
if not self._state.adding:
Expand All @@ -86,6 +87,10 @@ def _validate_label(self):
}
)

def _validate_master_subnet_validity(self):
if not self.master_subnet.subnet:
raise ValidationError({'master_subnet': _('Invalid master subnet.')})

def _validate_existing_fields(self):
db_instance = self._meta.model.objects.get(id=self.id)
# The size field should not be changed
Expand All @@ -104,10 +109,10 @@ def _validate_existing_fields(self):

def _validate_master_subnet_consistency(self):
master_subnet = self.master_subnet.subnet
# Ensure that the size of the generated subnet
# is not greater than size of master subnet
# Try to generate subnets and validate size
try:
next(master_subnet.subnets(new_prefix=self.size))
# if size is not consistent, raise error
except ValueError:
raise ValidationError(
{
Expand All @@ -118,7 +123,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

# Ensure that master subnet can accommodate
# the required number of generated subnets
available = 2 ** (self.size - master_subnet.prefixlen)
Expand All @@ -135,7 +139,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

# Validate organization of master subnet
if (
self.master_subnet.organization is not None
Expand Down
21 changes: 21 additions & 0 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,27 @@ def test_single_subnet_vpn_device_rule(self):
device_rule.number_of_subnets,
)

def test_invalid_master_subnet(self):
# Instantiate a subnet without setting its 'subnet' attribute
invalid_subnet = Subnet(name='Invalid Subnet')
rule = SubnetDivisionRule(
type='openwisp_controller.subnet_division.rule_types.vpn.'
'VpnSubnetDivisionRuleType',
label='TEST',
size=28,
number_of_subnets=2,
number_of_ips=2,
organization=self.org,
master_subnet=invalid_subnet, # Invalid master_subnet
)
with self.assertRaises(ValidationError) as cm:
rule.full_clean()
e = cm.exception
self.assertIn(
'Invalid master subnet.',
e.message_dict.get('master_subnet', []),
)


class TestOpenVPNSubnetDivisionRule(
SubnetDivisionTestMixin,
Expand Down

0 comments on commit bab9db1

Please sign in to comment.