Пример #1
0
    def test_add(self):
        r1 = IPv4Range('1.2.3.4', '1.2.3.5')
        r2 = IPv4Range('1.2.3.6', '1.2.3.9')

        for r in (r1 + r2, r2 + r1):
            self.assertEqual(str(r.fromIp), '1.2.3.4')
            self.assertEqual(str(r.toIp), '1.2.3.9')
Пример #2
0
    def test_len(self):
        '''Test IPv4Range length calculation'''
        r = IPv4Range('10.100.0.0', '10.100.0.100')
        self.assertEquals(len(r), 101)

        r = IPv4Range('10.100.1.100', '10.100.2.99')
        self.assertEquals(len(r), 256)

        r = IPv4Range('127.0.0.1', '127.0.0.1')
        self.assertEquals(len(r), 1)

        r = IPv4Range('1.2.3.4', '1.2.3.5')
        self.assertEqual(len(r), 2)
Пример #3
0
    def test_instanciation_fromto(self):
        '''Test IPv4Range instanciation using from and to addresses'''
        from_, to = '192.168.1.0', '192.168.1.10'

        r = IPv4Range(from_, to)
        self.assertEquals(str(r.fromIp), from_)
        self.assertEquals(str(r.toIp), to)

        r = IPv4Range(fromIp=from_, toIp=to)
        self.assertEquals(str(r.fromIp), from_)
        self.assertEquals(str(r.toIp), to)

        fromo = IPv4Address(from_)
        too = IPv4Address(to)

        r = IPv4Range(fromo, too)
        self.assertEquals(str(r.fromIp), from_)
        self.assertEquals(str(r.toIp), to)

        r = IPv4Range(fromIp=fromo, toIp=too)
        self.assertEquals(str(r.fromIp), from_)
        self.assertEquals(str(r.toIp), to)
Пример #4
0
    def test_add_exc(self):
        r1 = IPv4Range('1.2.3.4', '1.2.3.5')
        r2 = IPv4Range('1.2.3.7', '1.2.3.9')

        def addRanges():
            return r1 + "not a range"

        self.assertRaises(TypeError, addRanges)

        def addRanges():
            return r1 + r2

        self.assertRaises(ValueError, addRanges)

        def addRanges():
            return r2 + r1

        self.assertRaises(ValueError, addRanges)

        def addRanges():
            return r1 + r1

        self.assertRaises(ValueError, addRanges)
Пример #5
0
    def test_contains(self):
        '''Test whether the contains method works fine'''
        r = IPv4Range('10.100.0.10', '10.100.200.45')

        self.assert_('10.100.0.10' in r)
        self.assert_('10.100.200.45' in r)
        self.assert_('10.100.0.11' in r)
        self.assert_('10.100.200.44' in r)
        self.assert_('10.100.100.100' in r)

        self.assert_('10.100.0.9' not in r)
        self.assert_('10.100.200.46' not in r)
        self.assert_('192.168.10.100' not in r)

        self.assert_('0.0.0.0' not in r)
Пример #6
0
 def _askIpaddress(question,
                   startip=None,
                   endip=None,
                   network=None,
                   netmask=None):
     if startip and endip:
         return self.askString('%s(Range %s - %s)' %
                               (question, startip, endip),
                               regex=IPREGEX)
     if network and netmask:
         iprange = IPv4Range(netIp=network, netMask=netmask)
         return self.askString(
             '%s(Range %s - %s)' %
             (question, str(iprange.fromIp), str(iprange.toIp)),
             regex=IPREGEX)
Пример #7
0
 def test_netip_netmask_calculations(self):
     '''Test whether fromIp and toIp are calculated correctly when netIp and 
     netMask are provided'''
     ipr = IPv4Range(netIp='10.100.0.0', netMask='255.255.0.0')
     self.assertEqual(str(ipr.fromIp), '10.100.0.0')
     self.assertEqual(str(ipr.toIp), '10.100.255.255')
Пример #8
0
    def askIpaddressInRange(self,
                            question,
                            startip=None,
                            endip=None,
                            network=None,
                            netmask=None,
                            retry=-1):
        """
        Ask the user to enter a valid ipaddress
        
        Provide either startip and endip or network and netmask.
        
        @param question: The question that should be asked to the user
        @type question: string
        @param startip: Start ip of the available ip range to enter the ipaddress in
        @type startip: string
        @param endip: End ip of the available ip range to enter the ipaddress in
        @type endip: string
        @param network: Base IP address when using netmask-based range definition
        @type network: string
        @param netmask: Netmask to use in combination with C{network}
        @type netmask: string
        @return: Ip address
        @rtype: string

        """
        def _askIpaddress(question,
                          startip=None,
                          endip=None,
                          network=None,
                          netmask=None):
            if startip and endip:
                return self.askString('%s(Range %s - %s)' %
                                      (question, startip, endip),
                                      regex=IPREGEX)
            if network and netmask:
                iprange = IPv4Range(netIp=network, netMask=netmask)
                return self.askString(
                    '%s(Range %s - %s)' %
                    (question, str(iprange.fromIp), str(iprange.toIp)),
                    regex=IPREGEX)

        if (startip or endip) and (network or netmask):
            raise ValueError(
                "Provide either startip and endip or networkip and netmask")
        if (startip or endip) and not (startip and endip):
            raise ValueError(
                "Provide either startip and endip or networkip and netmask")
        if (network or netmask) and not (network and netmask):
            raise ValueError(
                "Provide either startip and endip or network and netmask")
        if (network and netmask) and (IPv4Address(
                int(IPv4Address(network)) & int(IPv4Address(netmask))) !=
                                      network):
            raise ValueError("Provided network and netmask don't match")

        retryCount = retry
        while retryCount != 0:
            ipaddress = _askIpaddress(question, startip, endip, network,
                                      netmask)
            iprange = IPv4Range(fromIp=startip,
                                toIp=endip,
                                netIp=network,
                                netMask=netmask)
            if ipaddress in IPv4Range(fromIp=startip,
                                      toIp=endip,
                                      netIp=network,
                                      netMask=netmask):
                return ipaddress
            self.echo("The provided ipaddress not in range, please try again")
            retryCount = retryCount - 1