Example #1
0
def test_reservedaddress():
  s1 = Site( name='tsite1', description='test site1' )
  s1.full_clean()
  s1.save()

  nwd = Networked( site=s1, hostname='test' )
  nwd.full_clean()
  nwd.save()

  ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  ab1.full_clean()
  ab1.save()

  ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 )
  ab2.full_clean()
  ab2.save()

  ra = ReservedAddress()
  with pytest.raises( ValidationError ):
    ra.full_clean()

  ra = ReservedAddress( reason='testing' )
  with pytest.raises( ValidationError ):
    ra.full_clean()

  ra = ReservedAddress( address_block=ab1 )
  with pytest.raises( ValidationError ):
    ra.full_clean()

  ra = ReservedAddress( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ra.full_clean()

  ra = ReservedAddress( address_block=ab1, offset=1, reason='testing' )
  ra.full_clean()
  ra.save()

  ba = BaseAddress( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=2 )
  ba.full_clean()
  ba.save()

  ra = ReservedAddress( address_block=ab1, offset=2, reason='testing' )
  with pytest.raises( ValidationError ):
    ra.full_clean()

  ra = BaseAddress.objects.get( address_block=ab1, offset=1 )
  assert ra.type == 'ReservedAddress'
  assert ra.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( address_block=ab1, offset=1 )
  assert ba.type == 'ReservedAddress'
  assert ba.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( address_block=ab1, offset=2 )
  assert ba.type == 'Unknown'
  assert ba.ip_address == '0.0.0.2'
Example #2
0
def ipAddress2Native(ip_address):
    try:
        address_block = AddressBlock.objects.get(subnet__lte=ip_address,
                                                 _max_address__gte=ip_address)
    except AddressBlock.DoesNotExist:
        raise ValueError(
            'ip_address "{0}" does not exist in any existing Address Blocks'.
            format(ip_address))

    return address_block, StrToIp(ip_address) - StrToIp(address_block.subnet)
Example #3
0
def ipAddress2Native(ip_address):
    try:
        address_block = AddressBlock.objects.get(subnet__lte=ip_address,
                                                 _max_address__gte=ip_address)
    except AddressBlock.DoesNotExist:
        raise UtilitiesException(
            'ADDRESS_NOT_FOUND',
            'ip_address "{0}" does not exist in any existing Address Blocks'.
            format(ip_address))

    return address_block, StrToIp(ip_address) - StrToIp(address_block.subnet)
Example #4
0
def test_dynamicaddress():
  s1 = Site( name='tsite1', description='test site1' )
  s1.full_clean()
  s1.save()

  nwd = Networked( site=s1, hostname='test' )
  nwd.full_clean()
  nwd.save()

  ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  ab1.full_clean()
  ab1.save()

  ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 )
  ab2.full_clean()
  ab2.save()

  da = DynamicAddress()
  with pytest.raises( ValidationError ):
    da.full_clean()

  da = DynamicAddress( address_block=ab1 )
  with pytest.raises( ValidationError ):
    da.full_clean()

  da = DynamicAddress( address_block=ab1, offset=1 )  # TODO: test with PXE set
  da.full_clean()
  da.save()

  ba = BaseAddress( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=2 )
  ba.full_clean()
  ba.save()

  da = DynamicAddress( address_block=ab1, offset=2 )
  with pytest.raises( ValidationError ):
    da.full_clean()

  da = DynamicAddress.objects.get( address_block=ab1, offset=1 )
  assert da.type == 'DynamicAddress'
  assert da.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( address_block=ab1, offset=1 )
  assert ba.type == 'DynamicAddress'
  assert ba.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( address_block=ab1, offset=2 )
  assert ba.type == 'Unknown'
  assert ba.ip_address == '0.0.0.2'
Example #5
0
    def lookup(ip_address):
        try:
            address_block = AddressBlock.objects.get(
                subnet__lte=ip_address, _max_address__gte=ip_address)
        except AddressBlock.DoesNotExist:
            return None

        offset = StrToIp(ip_address) - StrToIp(address_block.subnet)
        try:
            return BaseAddress.objects.get(address_block=address_block,
                                           offset=offset)
        except BaseAddress.DoesNotExist:
            return None

        return None
Example #6
0
    def nextAddress(
        self, networked, interface_name, is_primary
    ):  # TODO: wrap this in a transaction, or some other way to unwrap everything if it fails
        address = Address(networked=networked,
                          interface_name=interface_name,
                          is_primary=is_primary)
        if networked.structure.foundation.subclass.__class__.__name__ == 'DockerFoundation':
            # address.pointer = Address.objects.get( networked=structure.foundation.docker_host.members[0], interface_name='eth0' )
            return None  # set map_ports will do the address

        else:  # TODO: either retry till all_offsets is empty, or lock the Address table(s)
            all_offsets = set(
                CIDRNetworkRange(StrToIp(self.subnet), self.prefix, False,
                                 True))
            if self.gateway_offset is not None:
                all_offsets = all_offsets - set([self.gateway_offset])

            if not all_offsets:
                raise UtilitiesException('NO_OFFSETS', 'No Available Offsets')

            used_offsets = set(
                BaseAddress.objects.filter(address_block=self,
                                           offset__isnull=False).values_list(
                                               'offset', flat=True))
            address.address_block = self
            address.offset = random.choice(list(all_offsets - used_offsets))

        address.full_clean()
        address.save()

        return address
Example #7
0
    def clean(self, *args, **kwargs):
        super().clean(*args, **kwargs)
        errors = {}
        try:
            subnet_ip = StrToIp(self.subnet)
            ipv4 = IpIsV4(subnet_ip)
        except ValueError:
            ipv4 = None
            errors['subnet'] = 'Invalid Ip Address'

        if self.prefix is None or self.prefix < 1:
            errors['prefix'] = 'Min Prefix is 1'

        if errors:  # no point in continuing
            raise ValidationError(errors)

        if ipv4 is not None:
            if ipv4:
                if self.prefix > 32:
                    errors['prefix'] = 'Max Prefix for ipv4 is 32'
            else:
                if self.prefix > 128:
                    errors['prefix'] = 'Max Prefix for ipv6 is 128'

            if self.gateway_offset is not None:
                (low, high) = CIDRNetworkBounds(subnet_ip, self.prefix, False,
                                                True)
                if low == high:
                    errors[
                        'gateway_offset'] = 'Gateway not possible in single host subnet'

                if self.gateway_offset < low or self.gateway_offset > high:
                    errors[
                        'gateway_offset'] = 'Must be greater than {0} and less than {1}'.format(
                            low, high)

        if errors:  # no point in continuing
            raise ValidationError(errors)

        (subnet_ip, last_ip) = CIDRNetworkBounds(subnet_ip, self.prefix, True)
        self.subnet = IpToStr(subnet_ip)
        self._max_address = IpToStr(last_ip)
        block_count = AddressBlock.objects.filter(
            subnet__gte=self.subnet, _max_address__lte=self.subnet).count()
        block_count += AddressBlock.objects.filter(
            subnet__gte=self._max_address,
            _max_address__lte=self._max_address).count()
        block_count += AddressBlock.objects.filter(
            _max_address__gte=self.subnet,
            _max_address__lte=self._max_address).count()
        block_count += AddressBlock.objects.filter(
            subnet__gte=self.subnet, subnet__lte=self._max_address).count()
        if block_count > 0:
            errors[
                'subnet'] = 'This subnet/prefix overlaps with an existing Address Block'

        if errors:
            raise ValidationError(errors)
Example #8
0
def test_iptostr():
  assert IpToStr( 0 ) == '::'
  assert IpToStr( 1 ) == '::1'
  assert IpToStr( 10 ) == '::a'
  assert IpToStr( 65535 ) == '::ffff'
  assert IpToStr( 0, False ) == '::'
  assert IpToStr( 1, False ) == '::1'
  assert IpToStr( 10, False ) == '::a'
  assert IpToStr( 65535, False ) == '::ffff'
  assert IpToStr( 0, True ) == '::'
  assert IpToStr( 1, True ) == '::1'
  assert IpToStr( 10, True ) == '::a'
  assert IpToStr( 65535, True ) == '::ffff'
  assert IpToStr( 42540766411282592856903984951653826561 ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217 ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185 ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896 ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528 ) == '2001::'
  assert IpToStr( 42540766411282592856903984951653826561, False ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217, False ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185, False ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896, False ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528, False ) == '2001::'
  assert IpToStr( 42540766411282592856903984951653826561, True ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217, True ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185, True ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896, True ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528, True ) == '2001::'
  assert IpToStr( 281470681743360 ) == '0.0.0.0'
  assert IpToStr( 281470681743361 ) == '0.0.0.1'
  assert IpToStr( 281472812449793 ) == '127.0.0.1'
  assert IpToStr( 281470698652420 ) == '1.2.3.4'
  assert IpToStr( 281470681743360, False ) == '0.0.0.0'
  assert IpToStr( 281470681743361, False ) == '0.0.0.1'
  assert IpToStr( 281472812449793, False ) == '127.0.0.1'
  assert IpToStr( 281470698652420, False ) == '1.2.3.4'
  assert IpToStr( 281470681743360, True ) == ':ffff:0.0.0.0'
  assert IpToStr( 281470681743361, True ) == ':ffff:0.0.0.1'
  assert IpToStr( 281472812449793, True ) == ':ffff:127.0.0.1'
  assert IpToStr( 281470698652420, True ) == ':ffff:1.2.3.4'
  assert IpToStr( StrToIp( '1:1:1:1:2:3:4:5' ) ) == '1:1:1:1:2:3:4:5'
  assert IpToStr( StrToIp( '2:2:2:2:2:3:4:5' ) ) == '2:2:2:2:2:3:4:5'
  assert IpToStr( StrToIp( '0:0:0:0:2:3:4:5' ) ) == '::2:3:4:5'
  assert IpToStr( StrToIp( '1:2:2:2:2:3:4:5' ) ) == '1:2:2:2:2:3:4:5'
  assert IpToStr( StrToIp( '1:0:0:0:0:3:4:5' ) ) == '1::3:4:5'
  assert IpToStr( StrToIp( '1:1:1:1:0:0:4:5' ) ) == '1:1:1:1::4:5'
  assert IpToStr( StrToIp( '1:1:1:1:0:0:0:0' ) ) == '1:1:1:1::'

  with pytest.raises( ValueError ):
    IpToStr( -1 )
  with pytest.raises( ValueError ):
    IpToStr( 0x100000000000000000000000000000000 )
  with pytest.raises( ValueError ):
    IpToStr( '0' )

  assert IpToStr( None ) is None
Example #9
0
    def lookup(ip_address):
        try:
            ip_address_ip = StrToIp(ip_address)
        except ValueError:
            return None

        ip_address = IpToStr(ip_address_ip)  # so it is in a consistant format
        try:
            address_block = AddressBlock.objects.get(
                subnet__lte=ip_address, _max_address__gte=ip_address)
        except AddressBlock.DoesNotExist:
            return None

        offset = ip_address_ip - StrToIp(address_block.subnet)
        try:
            return BaseAddress.objects.get(address_block=address_block,
                                           offset=offset)
        except BaseAddress.DoesNotExist:
            return None

        return None
Example #10
0
def test_cidrnetworksize():
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 32 ) == 1
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 32, True ) == 1
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 32, False ) == 1
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 31 ) == 2
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 31, True ) == 2
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 31, False ) == 2
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 30 ) == 2
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 30, True ) == 4
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 30, False ) == 2
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 29 ) == 6
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 29, True ) == 8
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 29, False ) == 6
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 8 ) == 16777214
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 8, True ) == 16777216
  assert CIDRNetworkSize( StrToIp( '3.2.5.3' ), 8, False ) == 16777214

  assert CIDRNetworkSize( StrToIp( '::3' ), 128 ) == 1
  assert CIDRNetworkSize( StrToIp( '::3' ), 128, True ) == 1
  assert CIDRNetworkSize( StrToIp( '::3' ), 128, False ) == 1
  assert CIDRNetworkSize( StrToIp( '::3' ), 127 ) == 2
  assert CIDRNetworkSize( StrToIp( '::3' ), 127, True ) == 2
  assert CIDRNetworkSize( StrToIp( '::3' ), 127, False ) == 2
  assert CIDRNetworkSize( StrToIp( '::3' ), 126 ) == 2
  assert CIDRNetworkSize( StrToIp( '::3' ), 126, True ) == 4
  assert CIDRNetworkSize( StrToIp( '::3' ), 126, False ) == 2
  assert CIDRNetworkSize( StrToIp( '::3' ), 125 ) == 6
  assert CIDRNetworkSize( StrToIp( '::3' ), 125, True ) == 8
  assert CIDRNetworkSize( StrToIp( '::3' ), 125, False ) == 6
  assert CIDRNetworkSize( StrToIp( '::3' ), 8 ) == 1329227995784915872903807060280344574
  assert CIDRNetworkSize( StrToIp( '::3' ), 8, True ) == 1329227995784915872903807060280344576
  assert CIDRNetworkSize( StrToIp( '::3' ), 8, False ) == 1329227995784915872903807060280344574

  with pytest.raises( ValueError ):
    CIDRNetworkSize( -1, 0 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( 0x100000000000000000000000000000000, 0 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( StrToIp( '255.255.255.255' ), 33 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( StrToIp( '0.0.0.0' ), 33 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), 129 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( StrToIp( '::' ), 129 )
  with pytest.raises( ValueError ):
    CIDRNetworkSize( '::', 129 )
Example #11
0
    def gateway_ip(self):
        if self.gateway_offset is None:
            return None

        return IpToStr(StrToIp(self.subnet) + self.gateway_offset)
Example #12
0
def test_address():
    s1 = Site(name='tsite1', description='test site1')
    s1.full_clean()
    s1.save()

    nwd = Networked(site=s1, hostname='test')
    nwd.full_clean()
    nwd.save()

    ab1 = AddressBlock(site=s1,
                       subnet=StrToIp('0.0.0.0'),
                       prefix=24,
                       name='test1')
    ab1.full_clean()
    ab1.save()

    ab2 = AddressBlock(site=s1,
                       subnet=StrToIp('1.0.0.0'),
                       prefix=31,
                       name='test2')
    ab2.full_clean()
    ab2.save()

    ad = Address()
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad1 = Address(networked=nwd, interface_name='tun0')
    ad1.full_clean()
    ad1.save()
    assert ad1.as_dict == {
        'address': None,
        'netmask': None,
        'prefix': None,
        'subnet': None,
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ad1.address_block = ab2
    ad1.offset = 5
    ad1.save()
    assert ad1.as_dict == {
        'address': '1.0.0.5',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ad = Address(address_block=ab1)
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1, offset=1)
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1, offset=1, networked=nwd)
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1,
                 offset=1,
                 networked=nwd,
                 interface_name='lo')
    ad.full_clean()
    ad.save()
    assert ad.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ad = Address(address_block=ab1,
                 offset=1,
                 pointer=ad1,
                 networked=nwd,
                 interface_name='vpn0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(offset=1, pointer=ad1, networked=nwd, interface_name='vpn0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1,
                 pointer=ad1,
                 networked=nwd,
                 interface_name='vpn0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1,
                 pointer=ad1,
                 networked=nwd,
                 interface_name='vpn0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(offset=1, pointer=ad1, networked=nwd, interface_name='vpn0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(pointer=ad1, networked=nwd, interface_name='vpn0')
    ad.full_clean()
    ad.save()
    assert ad.as_dict == {
        'address': '1.0.0.5',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab1, offset=1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab1, offset=2)
    ba.full_clean()
    ba.save()
    assert ba.as_dict == {
        'address': '0.0.0.2',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ad = Address(address_block=ab1,
                 offset=2,
                 networked=nwd,
                 interface_name='eth0')
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab1,
                 offset=3,
                 networked=nwd,
                 interface_name='lo')
    ad.full_clean()

    ad = Address(address_block=ab2,
                 offset=0,
                 networked=nwd,
                 interface_name='lo',
                 sub_interface=0)
    ad.full_clean()

    ad = Address(address_block=ab2,
                 offset=0,
                 networked=nwd,
                 interface_name='lo',
                 sub_interface=123)
    ad.full_clean()

    ad = Address(address_block=ab2,
                 offset=0,
                 networked=nwd,
                 interface_name='lo',
                 sub_interface=-1)
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab2,
                 offset=0,
                 networked=nwd,
                 interface_name='lo',
                 is_primary=True)
    ad.full_clean()
    ad.save()
    assert ad.as_dict == {
        'address': '1.0.0.0',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': True,
        'auto': True,
        'mtu': 1500
    }

    ad = Address(address_block=ab2,
                 offset=1,
                 networked=nwd,
                 interface_name='lo',
                 is_primary=True)
    with pytest.raises(ValidationError):
        ad.full_clean()

    ad = Address(address_block=ab2,
                 offset=1,
                 networked=nwd,
                 interface_name='lo',
                 is_primary=False)
    ad.full_clean()

    ad = Address.objects.get(address_block=ab1, offset=1)
    assert ad.type == 'Address'
    assert ad.ip_address == '0.0.0.1'
    assert ad.structure is None  # TODO: make a networked with a structure and test that
    assert ad.interface is None  # TODO: dido ^
    assert ad.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(address_block=ab1, offset=1)
    assert ba.type == 'Address'
    assert ba.ip_address == '0.0.0.1'
    assert ba.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(address_block=ab1, offset=2)
    assert ba.type == 'Unknown'
    assert ba.ip_address == '0.0.0.2'
    assert ba.as_dict == {
        'address': '0.0.0.2',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ad = Address.objects.get(networked=nwd, interface_name='vpn0')
    assert ad.type == 'Address'
    assert ad.ip_address == '1.0.0.5'
    assert ad.as_dict == {
        'address': '1.0.0.5',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(pk=ad.baseaddress_ptr.pk)
    assert ba.type == 'Address'
    assert ba.ip_address is None
    assert ba.subclass.ip_address == '1.0.0.5'
    assert ba.as_dict == {
        'address': None,
        'netmask': None,
        'prefix': None,
        'subnet': None,
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }
    assert ba.subclass.as_dict == {
        'address': '1.0.0.5',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'sub_interface': None,
        'primary': False,
        'auto': True,
        'mtu': 1500
    }
Example #13
0
def test_reservedaddress():
    s1 = Site(name='tsite1', description='test site1')
    s1.full_clean()
    s1.save()

    nwd = Networked(site=s1, hostname='test')
    nwd.full_clean()
    nwd.save()

    ab1 = AddressBlock(site=s1,
                       subnet=StrToIp('0.0.0.0'),
                       prefix=24,
                       name='test1')
    ab1.full_clean()
    ab1.save()

    ab2 = AddressBlock(site=s1,
                       subnet=StrToIp('1.0.0.0'),
                       prefix=31,
                       name='test2')
    ab2.full_clean()
    ab2.save()

    ra = ReservedAddress()
    with pytest.raises(ValidationError):
        ra.full_clean()

    ra = ReservedAddress(reason='testing')
    with pytest.raises(ValidationError):
        ra.full_clean()

    ra = ReservedAddress(address_block=ab1)
    with pytest.raises(ValidationError):
        ra.full_clean()

    ra = ReservedAddress(address_block=ab1, offset=1)
    with pytest.raises(ValidationError):
        ra.full_clean()

    ra = ReservedAddress(address_block=ab1, offset=1, reason='testing')
    ra.full_clean()
    ra.save()
    assert ra.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab1, offset=1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab1, offset=2)
    ba.full_clean()
    ba.save()
    assert ba.as_dict == {
        'address': '0.0.0.2',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ra = ReservedAddress(address_block=ab1, offset=2, reason='testing')
    with pytest.raises(ValidationError):
        ra.full_clean()

    ra = BaseAddress.objects.get(address_block=ab1, offset=1)
    assert ra.type == 'ReservedAddress'
    assert ra.ip_address == '0.0.0.1'
    assert ra.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(address_block=ab1, offset=1)
    assert ba.type == 'ReservedAddress'
    assert ba.ip_address == '0.0.0.1'
    assert ba.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(address_block=ab1, offset=2)
    assert ba.type == 'Unknown'
    assert ba.ip_address == '0.0.0.2'
    assert ba.as_dict == {
        'address': '0.0.0.2',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }
Example #14
0
def test_cidrnetworkrange():
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32 ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32, True ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32, False ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31 ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31, True ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31, False ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30 ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30, True ) ) == [ StrToIp( '169.254.1.0' ), StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30, False ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29 ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29, True ) ) == [ StrToIp( '169.254.1.0' ), StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ), StrToIp( '169.254.1.7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29, False ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128 ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128, True ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128, False ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127 ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127, True ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127, False ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126 ) ) == [ StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126, True ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ), StrToIp( '2::7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126, False ) ) == [ StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125 ) ) == [ StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125, True ) ) == [ StrToIp( '2::0' ), StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ), StrToIp( '2::7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125, False ) ) == [ StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ) ]
Example #15
0
def test_baseaddress():
    s1 = Site(name='tsite1', description='test site1')
    s1.full_clean()
    s1.save()

    ab1 = AddressBlock(site=s1,
                       subnet=StrToIp('0.0.0.0'),
                       prefix=24,
                       name='test1')
    ab1.full_clean()
    ab1.save()

    ab2 = AddressBlock(site=s1,
                       subnet=StrToIp('1.0.0.0'),
                       prefix=31,
                       name='test2')
    ab2.full_clean()
    ab2.save()

    ab3 = AddressBlock(site=s1,
                       subnet=StrToIp('2.0.0.0'),
                       prefix=24,
                       gateway_offset=1,
                       name='test3')
    ab3.full_clean()
    ab3.save()

    ba1 = BaseAddress()
    ba1.full_clean()
    ba1.save()
    assert ba1.as_dict == {
        'address': None,
        'netmask': None,
        'prefix': None,
        'subnet': None,
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(offset=0)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(offset=1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab1, offset=0)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab1, offset=1)
    ba.full_clean()
    ba.save()
    assert ba.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab1, offset=1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab1, offset=254)
    ba.full_clean()
    assert ba.as_dict == {
        'address': '0.0.0.254',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab1, offset=255)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab2, offset=0)
    ba.full_clean()
    assert ba.as_dict == {
        'address': '1.0.0.0',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab2, offset=1)
    ba.full_clean()
    assert ba.as_dict == {
        'address': '1.0.0.1',
        'netmask': '255.255.255.254',
        'prefix': 31,
        'subnet': '1.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress(address_block=ab2, offset=2)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab2, offset=-1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab3, offset=0)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab3, offset=1)
    with pytest.raises(ValidationError):
        ba.full_clean()

    ba = BaseAddress(address_block=ab3, offset=2)
    ba.full_clean()
    assert ba.as_dict == {
        'address': '2.0.0.2',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '2.0.0.0',
        'gateway': '2.0.0.1',
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(address_block=ab1, offset=1)
    assert ba.type == 'Unknown'
    assert ba.ip_address == '0.0.0.1'
    assert ba.as_dict == {
        'address': '0.0.0.1',
        'netmask': '255.255.255.0',
        'prefix': 24,
        'subnet': '0.0.0.0',
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }

    ba = BaseAddress.objects.get(pk=ba1.pk)
    assert ba.type == 'Unknown'
    assert ba.ip_address is None
    assert ba.as_dict == {
        'address': None,
        'netmask': None,
        'prefix': None,
        'subnet': None,
        'gateway': None,
        'auto': True,
        'mtu': 1500
    }
Example #16
0
    def ip_address(self):
        if self.address_block is None or self.offset is None:
            return None

        return IpToStr(StrToIp(self.address_block.subnet) + self.offset)
Example #17
0
 def isIpV4(self):
     return IpIsV4(StrToIp(self.subnet))
Example #18
0
def test_address():
  s1 = Site( name='tsite1', description='test site1' )
  s1.full_clean()
  s1.save()

  nwd = Networked( site=s1, hostname='test' )
  nwd.full_clean()
  nwd.save()

  ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  ab1.full_clean()
  ab1.save()

  ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 )
  ab2.full_clean()
  ab2.save()

  ad = Address()
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad1 = Address( networked=nwd, interface_name='tun0' )
  ad1.full_clean()
  ad1.save()

  ad1.address_block = ab2
  ad1.offset = 5
  ad1.save()

  ad = Address( address_block=ab1 )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, offset=1, networked=nwd )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, offset=1, networked=nwd, interface_name='lo' )
  ad.full_clean()
  ad.save()

  ad = Address( address_block=ab1, offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( pointer=ad1, networked=nwd, interface_name='vpn0' )
  ad.full_clean()
  ad.save()

  ba = BaseAddress( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=2 )
  ba.full_clean()
  ba.save()

  ad = Address( address_block=ab1, offset=2, networked=nwd, interface_name='eth0' )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab1, offset=3, networked=nwd, interface_name='lo' )
  ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=0 )
  ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=1 )
  ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=-1 )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=4097 )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=0 )
  ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=123 )
  ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=-1 )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', is_primary=True )
  ad.full_clean()
  ad.save()

  ad = Address( address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=True )
  with pytest.raises( ValidationError ):
    ad.full_clean()

  ad = Address( address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=False )
  ad.full_clean()

  ad = Address.objects.get( address_block=ab1, offset=1 )
  assert ad.type == 'Address'
  assert ad.ip_address == '0.0.0.1'
  assert ad.structure is None   # TODO: make a networked with a structure and test that
  assert ad.interface is None   # TODO: dido ^

  ba = BaseAddress.objects.get( address_block=ab1, offset=1 )
  assert ba.type == 'Address'
  assert ba.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( address_block=ab1, offset=2 )
  assert ba.type == 'Unknown'
  assert ba.ip_address == '0.0.0.2'

  ad = Address.objects.get( networked=nwd, interface_name='vpn0' )
  assert ad.type == 'Address'
  assert ad.ip_address == '1.0.0.5'

  ba = BaseAddress.objects.get( pk=ad.baseaddress_ptr.pk )
  assert ba.type == 'Address'
  assert ba.ip_address is None
  assert ba.subclass.ip_address == '1.0.0.5'
Example #19
0
def test_cidrnetmasktoprefix():
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.0' ) ) == 24
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.128' ) ) == 25
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.192' ) ) == 26
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.224' ) ) == 27
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.254.0' ) ) == 23
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.255' ) ) == 32
  assert CIDRNetmaskToPrefix( StrToIp( '255.255.255.254' ) ) == 31
  assert CIDRNetmaskToPrefix( StrToIp( '255.0.0.0' ) ) == 8
  assert CIDRNetmaskToPrefix( StrToIp( 'ff00::' ) ) == 8
  assert CIDRNetmaskToPrefix( StrToIp( 'ffff::' ) ) == 16
  assert CIDRNetmaskToPrefix( StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ) ) == 128
  assert CIDRNetmaskToPrefix( StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe' ) ) == 127
  assert CIDRNetmaskToPrefix( StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00' ) ) == 120

  assert CIDRNetmaskToPrefix( StrToIp( '10.0.0.3' ) ) == 1  # yea, with CIDR are relying on leading bits being set

  with pytest.raises( ValueError ):
    CIDRNetmaskToPrefix( '127.0.0.1' )
Example #20
0
def validate_ipaddress(value):
    try:
        StrToIp(value)
    except ValueError:
        raise ValidationError('Invalid Ip Address "%(value)s"',
                              params={'value': value[0:100]})
Example #21
0
 def get_prep_value(self, value):
     return StrToIp(value)
Example #22
0
def test_cidrnetworkbounds():
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 8 ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 8, False ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 8, True ) == ( StrToIp( '10.0.0.0' ), StrToIp( '10.255.255.255' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 8 ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 8, False ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 8, True ) == ( StrToIp( '10.0.0.0' ), StrToIp( '10.255.255.255' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 8 ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 8, False ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.255.255.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 8, True ) == ( StrToIp( '10.0.0.0' ), StrToIp( '10.255.255.255' ) )

  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 24 ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.0.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 24, False ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.0.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 24, True ) == ( StrToIp( '10.0.0.0' ), StrToIp( '10.0.0.255' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 24 ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.0.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 24, False ) == ( StrToIp( '10.0.0.1' ), StrToIp( '10.0.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 24, True ) == ( StrToIp( '10.0.0.0' ), StrToIp( '10.0.0.255' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 24 ) == ( StrToIp( '10.3.0.1' ), StrToIp( '10.3.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 24, False ) == ( StrToIp( '10.3.0.1' ), StrToIp( '10.3.0.254' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 24, True ) == ( StrToIp( '10.3.0.0' ), StrToIp( '10.3.0.255' ) )

  assert CIDRNetworkBounds( StrToIp( '2001::' ), 112 ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 112, False ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 112, True ) == ( StrToIp( '2001::' ), StrToIp( '2001::ffff' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 112 ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 112, False ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 112, True ) == ( StrToIp( '2001::' ), StrToIp( '2001::ffff' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 112 ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 112, False ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fffe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 112, True ) == ( StrToIp( '2001::' ), StrToIp( '2001::ffff' ) )

  assert CIDRNetworkBounds( StrToIp( '2001::' ), 120 ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 120, False ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 120, True ) == ( StrToIp( '2001::' ), StrToIp( '2001::ff' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 120 ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 120, False ) == ( StrToIp( '2001::1' ), StrToIp( '2001::fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 120, True ) == ( StrToIp( '2001::' ), StrToIp( '2001::ff' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 120 ) == ( StrToIp( '2001::f001' ), StrToIp( '2001::f0fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 120, False ) == ( StrToIp( '2001::f001' ), StrToIp( '2001::f0fe' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 120, True ) == ( StrToIp( '2001::f000' ), StrToIp( '2001::f0ff' ) )

  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 32 ) == ( StrToIp( '10.3.2.5' ), StrToIp( '10.3.2.5' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 32, False ) == ( StrToIp( '10.3.2.5' ), StrToIp( '10.3.2.5' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 32, True ) == ( StrToIp( '10.3.2.5' ), StrToIp( '10.3.2.5' ) )

  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 31 ) == ( StrToIp( '10.3.2.4' ), StrToIp( '10.3.2.5' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 31, False ) == ( StrToIp( '10.3.2.4' ), StrToIp( '10.3.2.5' ) )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 31, True ) == ( StrToIp( '10.3.2.4' ), StrToIp( '10.3.2.5' ) )

  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 128 ) == ( StrToIp( '2001::f009' ), StrToIp( '2001::f009' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 128, False ) == ( StrToIp( '2001::f009' ), StrToIp( '2001::f009' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 128, True ) == ( StrToIp( '2001::f009' ), StrToIp( '2001::f009' ) )

  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 127 ) == ( StrToIp( '2001::f008' ), StrToIp( '2001::f009' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 127, False ) == ( StrToIp( '2001::f008' ), StrToIp( '2001::f009' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 127, True ) == ( StrToIp( '2001::f008' ), StrToIp( '2001::f009' ) )

  assert CIDRNetworkBounds( StrToIp( '254.0.0.0' ), 8, True ) == ( StrToIp( '254.0.0.0' ), StrToIp( '254.255.255.255' ) )
  assert CIDRNetworkBounds( StrToIp( '255.0.0.0' ), 8, True ) == ( StrToIp( '255.0.0.0' ), StrToIp( '255.255.255.255' ) )
  assert CIDRNetworkBounds( StrToIp( '1.2.3.4' ), 0, True ) == ( StrToIp( '0.0.0.0' ), StrToIp( '255.255.255.255' ) )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 0, True ) == ( StrToIp( '::' ), StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ) )

  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 8, False, True ) == ( 1, 16777214 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 8, True, True ) == ( 0, 16777215 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 8, False, True ) == ( 1, 16777214 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 8, True, True ) == ( 0, 16777215 )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 8, False, True ) == ( 1, 16777214 )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 8, True, True ) == ( 0, 16777215 )

  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 24, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.0' ), 24, True, True ) == ( 0, 255 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 24, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '10.0.0.1' ), 24, True, True ) == ( 0, 255 )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 24, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '10.3.0.0' ), 24, True, True ) == ( 0, 255 )

  assert CIDRNetworkBounds( StrToIp( '2001::' ), 112, False, True ) == ( 1, 65534 )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 112, True, True ) == ( 0, 65535 )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 112, False, True ) == ( 1, 65534 )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 112, True, True ) == ( 0, 65535 )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 112, False, True ) == ( 1, 65534 )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 112, True, True ) == ( 0, 65535 )

  assert CIDRNetworkBounds( StrToIp( '2001::' ), 120, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 120, True, True ) == ( 0, 255 )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 120, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '2001::1' ), 120, True, True ) == ( 0, 255 )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 120, False, True ) == ( 1, 254 )
  assert CIDRNetworkBounds( StrToIp( '2001::f001' ), 120, True, True ) == ( 0, 255 )

  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 32, False, True ) == ( 0, 0 )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 32, True, True ) == ( 0, 0 )

  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 31, False, True ) == ( 0, 1 )
  assert CIDRNetworkBounds( StrToIp( '10.3.2.5' ), 31, True, True ) == ( 0, 1 )

  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 128, False, True ) == ( 0, 0 )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 128, True, True ) == ( 0, 0 )

  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 127, False, True ) == ( 0, 1 )
  assert CIDRNetworkBounds( StrToIp( '2001::f009' ), 127, True, True ) == ( 0, 1 )

  assert CIDRNetworkBounds( StrToIp( '254.0.0.0' ), 8, True, True ) == ( 0, 16777215 )
  assert CIDRNetworkBounds( StrToIp( '255.0.0.0' ), 8, True, True ) == ( 0, 16777215 )
  assert CIDRNetworkBounds( StrToIp( '1.2.3.4' ), 0, True, True ) == ( 0, 4294967295 )
  assert CIDRNetworkBounds( StrToIp( '2001::' ), 0, True, True ) == ( 0, 340282366920938463463374607431768211455 )

  with pytest.raises( ValueError ):
    CIDRNetworkBounds( -1, 0 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( 0x100000000000000000000000000000000, 0 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( StrToIp( '255.255.255.255' ), 33 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( StrToIp( '0.0.0.0' ), 33 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), 129 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( StrToIp( '::' ), 129 )
  with pytest.raises( ValueError ):
    CIDRNetworkBounds( '::', 129 )
Example #23
0
 def size(self):
     return CIDRNetworkSize(StrToIp(self.subnet), self.prefix,
                            not self.isIpV4)
Example #24
0
def test_baseaddress():
  s1 = Site( name='tsite1', description='test site1' )
  s1.full_clean()
  s1.save()

  ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  ab1.full_clean()
  ab1.save()

  ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 )
  ab2.full_clean()
  ab2.save()

  ab3 = AddressBlock( site=s1, subnet=StrToIp( '2.0.0.0' ), prefix=24, gateway_offset=1 )
  ab3.full_clean()
  ab3.save()

  ba1 = BaseAddress()
  ba1.full_clean()
  ba1.save()

  ba = BaseAddress( address_block=ab1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( offset=0 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=0 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=1 )
  ba.full_clean()
  ba.save()

  ba = BaseAddress( address_block=ab1, offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=254 )
  ba.full_clean()

  ba = BaseAddress( address_block=ab1, offset=255 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab2, offset=0 )
  ba.full_clean()

  ba = BaseAddress( address_block=ab2, offset=1 )
  ba.full_clean()

  ba = BaseAddress( address_block=ab2, offset=2 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab2, offset=-1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab3, offset=0 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab3, offset=1 )
  with pytest.raises( ValidationError ):
    ba.full_clean()

  ba = BaseAddress( address_block=ab3, offset=2 )
  ba.full_clean()

  ba = BaseAddress.objects.get( address_block=ab1, offset=1 )
  assert ba.type == 'Unknown'
  assert ba.ip_address == '0.0.0.1'

  ba = BaseAddress.objects.get( pk=ba1.pk )
  assert ba.type == 'Unknown'
  assert ba.ip_address is None
Example #25
0
 def offsetBounds(self):
     return CIDRNetworkBounds(StrToIp(self.subnet),
                              self.prefix,
                              include_unusable=False,
                              as_offsets=True)
Example #26
0
def test_addressblock():
    s1 = Site(name='tsite1', description='test site1')
    s1.full_clean()
    s1.save()

    s2 = Site(name='tsite2', description='test site2')
    s2.full_clean()
    s2.save()

    ab = AddressBlock()
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1)
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'))
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24)
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('0.0.0.0'),
                      prefix=24,
                      name='test')
    ab.full_clean()
    ab.save()

    ab.gateway_offset = 1
    ab.full_clean()
    ab.save()

    ab.name = 'something_else'
    ab.full_clean()
    ab.save()

    ab.name = 'test'
    ab.full_clean()
    ab.save()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('50.0.0.0'),
                      prefix=24,
                      name='test')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s2,
                      subnet=StrToIp('51.0.0.0'),
                      prefix=24,
                      name='test')
    ab.full_clean()
    ab.save()

    ab.site = s1
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab.site = s2
    ab.full_clean()
    ab.save()

    ab = AddressBlock.objects.get(site=s1, name='test')
    ab.gateway_offset = None
    ab.full_clean()
    ab.save()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('0.0.0.0'),
                      prefix=24,
                      name='test2')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s2,
                      subnet=StrToIp('0.0.0.0'),
                      prefix=24,
                      name='test2')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=24,
                      name='test3')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=-1,
                      name='test4')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=33,
                      name='test5')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('255.0.0.0'),
                      prefix=1,
                      name='test6')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=32,
                      name='test7')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('2.0.0.0'),
                      prefix=8,
                      gateway_offset=1,
                      name='test8')
    ab.full_clean()
    ab.save()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=24,
                      gateway_offset=1,
                      name='test9')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=32,
                      gateway_offset=0,
                      name='test10')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=32,
                      gateway_offset=1,
                      name='test11')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=32,
                      gateway_offset=2,
                      name='test12')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=31,
                      gateway_offset=0,
                      name='test13')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=31,
                      gateway_offset=1,
                      name='test14')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=31,
                      gateway_offset=2,
                      name='test15')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=30,
                      gateway_offset=0,
                      name='test16')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=30,
                      gateway_offset=1,
                      name='test17')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=30,
                      gateway_offset=2,
                      name='test18')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=30,
                      gateway_offset=3,
                      name='test19')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=29,
                      gateway_offset=0,
                      name='test20')
    with pytest.raises(ValidationError):
        ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=29,
                      gateway_offset=1,
                      name='test21')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=29,
                      gateway_offset=2,
                      name='test22')
    ab.full_clean()

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('1.0.0.0'),
                      prefix=29,
                      gateway_offset=3,
                      name='test23')
    ab.full_clean()

    ab = AddressBlock.objects.get(site=s1, name='test')
    assert ab.subnet == '0.0.0.0'
    assert ab._max_address == '0.0.0.255'
    assert ab.gateway is None
    assert ab.netmask == '255.255.255.0'
    assert ab.prefix == 24
    assert ab.size == 254
    assert ab.offsetBounds == (1, 254)
    assert ab.isIpV4 is True

    ab = AddressBlock.objects.get(site=s1, name='test8')
    assert ab.subnet == '2.0.0.0'
    assert ab._max_address == '2.255.255.255'
    assert ab.gateway == '2.0.0.1'
    assert ab.netmask == '255.0.0.0'
    assert ab.prefix == 8
    assert ab.size == 16777214
    assert ab.offsetBounds == (1, 16777214)
    assert ab.isIpV4 is True

    ab = AddressBlock(site=s1,
                      subnet=StrToIp('10.0.0.0'),
                      prefix=24,
                      name='test30')
    ab.full_clean()
    ab.save()

    ab = AddressBlock(site=s1, subnet='11.0.0.0', prefix=24, name='test31')
    ab.full_clean()
    ab.save()

    ab = AddressBlock.objects.get(name='test30')
    assert ab.subnet == '10.0.0.0'
    assert ab._max_address == '10.0.0.255'
    assert ab.gateway is None
    assert ab.netmask == '255.255.255.0'
    assert ab.prefix == 24
    assert ab.size == 254
    assert ab.offsetBounds == (1, 254)
    assert ab.isIpV4 is True

    ab = AddressBlock.objects.get(name='test31')
    assert ab.subnet == '11.0.0.0'
    assert ab._max_address == '11.0.0.255'
    assert ab.gateway is None
    assert ab.netmask == '255.255.255.0'
    assert ab.prefix == 24
    assert ab.size == 254
    assert ab.offsetBounds == (1, 254)
    assert ab.isIpV4 is True
Example #27
0
def test_network():
    s1 = Site(name='tsite1', description='test site1')
    s1.full_clean()
    s1.save()

    ab1 = AddressBlock(site=s1,
                       subnet=StrToIp('10.0.0.0'),
                       prefix=24,
                       name='test')
    ab1.full_clean()
    ab1.save()

    n1 = Network()
    with pytest.raises(ValidationError):
        n1.full_clean()

    n1 = Network(name='test', site=s1)
    n1.full_clean()
    n1.save()

    n2 = Network(name='test', site=s1)
    with pytest.raises(ValidationError):
        n2.full_clean()

    nab1 = NetworkAddressBlock(network=n1, address_block=ab1)
    nab1.full_clean()
    nab1.save()

    nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=0)
    nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=1)
    nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=-1)
    with pytest.raises(ValidationError):
        nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=4097)
    with pytest.raises(ValidationError):
        nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1,
                               address_block=ab1,
                               vlan=2,
                               vlan_tagged=False)
    nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1,
                               address_block=ab1,
                               vlan=2,
                               vlan_tagged=True)
    nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1,
                               address_block=ab1,
                               vlan=0,
                               vlan_tagged=False)
    nab2.full_clean()

    nab2 = NetworkAddressBlock(network=n1,
                               address_block=ab1,
                               vlan=0,
                               vlan_tagged=True)
    with pytest.raises(ValidationError):
        nab2.full_clean()
Example #28
0
def test_addressblock():
  s1 = Site( name='tsite1', description='test site1' )
  s1.full_clean()
  s1.save()

  ab = AddressBlock()
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ) )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  ab.full_clean()
  ab.save()

  ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=24 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=-1 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=33 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '255.0.0.0' ), prefix=1 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '2.0.0.0' ), prefix=8, gateway_offset=1 )
  ab.full_clean()
  ab.save()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=24, gateway_offset=1 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=0 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=1 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=2 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=0 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=1 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=2 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=0 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=1 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=2 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=3 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=0 )
  with pytest.raises( ValidationError ):
    ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=1 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=2 )
  ab.full_clean()

  ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=3 )
  ab.full_clean()

  ab = AddressBlock.objects.get( site=s1, subnet='0.0.0.0', prefix=24 )
  assert ab.gateway_ip is None
  assert ab.dns_servers == [ '192.168.200.1' ]
  assert ab.netmask == '255.255.255.0'
  assert ab.size == 254
  assert ab.offsetBounds == ( 1, 254 )
  assert ab.isIpV4 is True

  ab = AddressBlock.objects.get( site=s1, subnet='2.0.0.0', prefix=8, gateway_offset=1 )
  assert ab.gateway_ip == '2.0.0.1'
  assert ab.dns_servers == [ '192.168.200.1' ]
  assert ab.netmask == '255.0.0.0'
  assert ab.size == 16777214
  assert ab.offsetBounds == ( 1, 16777214 )
  assert ab.isIpV4 is True
Example #29
0
def test_cidrnetmask():
  assert CIDRNetmask( 24, False ) == StrToIp( '255.255.255.0' )
  assert CIDRNetmask( 25, False ) == StrToIp( '255.255.255.128' )
  assert CIDRNetmask( 26, False ) == StrToIp( '255.255.255.192' )
  assert CIDRNetmask( 27, False ) == StrToIp( '255.255.255.224' )
  assert CIDRNetmask( 23, False ) == StrToIp( '255.255.254.0' )
  assert CIDRNetmask( 32, False ) == StrToIp( '255.255.255.255' )
  assert CIDRNetmask( 31, False ) == StrToIp( '255.255.255.254' )
  assert CIDRNetmask( 8, False ) == StrToIp( '255.0.0.0' )
  assert CIDRNetmask( 8, True ) == StrToIp( 'ff00::' )
  assert CIDRNetmask( 16, True ) == StrToIp( 'ffff::' )
  assert CIDRNetmask( 128, True ) == StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' )
  assert CIDRNetmask( 127, True ) == StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe' )
  assert CIDRNetmask( 120, True ) == StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00' )
  assert CIDRNetmask( 32, True ) == StrToIp( 'ffff:ffff::' )
  assert CIDRNetmask( 64, True ) == StrToIp( 'ffff:ffff:ffff:ffff::' )
  assert CIDRNetmask( 96, True ) == StrToIp( 'ffff:ffff:ffff:ffff:ffff:ffff::' )
  assert CIDRNetmask( 80, True ) == StrToIp( 'ffff:ffff:ffff:ffff:ffff::' )

  with pytest.raises( ValueError ):
    CIDRNetmask( -1, True )
  with pytest.raises( ValueError ):
    CIDRNetmask( -1, False )
  with pytest.raises( ValueError ):
    CIDRNetmask( 33, False )
  with pytest.raises( ValueError ):
    CIDRNetmask( 129, True )
  with pytest.raises( ValueError ):
    CIDRNetmask( 'a', False )
  with pytest.raises( ValueError ):
    CIDRNetmask( 'a', True )
Example #30
0
def test_cidrnetworksizerange():  # NOTE: becarefull with the prefixes here, this can generate some pretty large networks even a /8 in ipv4 can make this test take a while
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 32 ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 32 ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 32, True ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 32, True ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 32, False ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 32, False ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 31 ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 31 ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 31, True ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 31, True ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 31, False ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 31, False ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 30 ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 30 ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 30, True ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 30, True ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 30, False ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 30, False ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 16 ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 16 ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 16, True ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 16, True ) ) )
  assert CIDRNetworkSize( StrToIp( '34.54.23.12' ), 16, False ) == len( list( CIDRNetworkRange( StrToIp( '34.54.23.12' ), 16, False ) ) )

  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 128 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 128 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 128, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 128, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 128, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 128, False ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 127 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 127 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 127, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 127, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 127, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 127, False ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 126 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 126 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 126, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 126, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 126, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 126, False ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 125 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 125 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 125, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 125, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 125, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 125, False ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 124 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 124 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 124, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 124, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 124, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 124, False ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 120 ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 120 ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 120, True ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 120, True ) ) )
  assert CIDRNetworkSize( StrToIp( '1:2:3::' ), 120, False ) == len( list( CIDRNetworkRange( StrToIp( '1:2:3::' ), 120, False ) ) )

  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32 ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32, True ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 32, False ) ) == [ StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31 ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31, True ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 31, False ) ) == [ StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30 ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30, True ) ) == [ StrToIp( '169.254.1.0' ), StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 30, False ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29 ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29, True ) ) == [ StrToIp( '169.254.1.0' ), StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ), StrToIp( '169.254.1.7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '169.254.1.3' ), 29, False ) ) == [ StrToIp( '169.254.1.1' ), StrToIp( '169.254.1.2' ), StrToIp( '169.254.1.3' ), StrToIp( '169.254.1.4' ), StrToIp( '169.254.1.5' ), StrToIp( '169.254.1.6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128 ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128, True ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 128, False ) ) == [ StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127 ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127, True ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 127, False ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126 ) ) == [ StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126, True ) ) == [ StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ), StrToIp( '2::7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 126, False ) ) == [ StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125 ) ) == [ StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125, True ) ) == [ StrToIp( '2::0' ), StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ), StrToIp( '2::7' ) ]
  assert list( CIDRNetworkRange( StrToIp( '2::5' ), 125, False ) ) == [ StrToIp( '2::1' ), StrToIp( '2::2' ), StrToIp( '2::3' ), StrToIp( '2::4' ), StrToIp( '2::5' ), StrToIp( '2::6' ) ]