Exemple #1
0
 def testCIDROutput(self):
     "Test IPRanges.tocidr for correct operation on basic input."
     for ival, res in self.knownCIDRValues:
         n = netblock.IPRanges(ival)
         self.assertEqual(n.tocidr(), res)
         # Do we get the same result if we feed the CIDRs
         # back in?
         n1 = netblock.IPRanges()
         for cidr in n.tocidr():
             n1.add(cidr)
         self.assertEqual(n1.tocidr(), res)
Exemple #2
0
 def testStrResults(self):
     "Test the result of str() of IPRanges on known values."
     for i, res in self.knownIPRStrs:
         if i == None:
             r = netblock.IPRanges()
         else:
             r = netblock.IPRanges(i)
         self.assertEqual(str(r), res)
         # Further test the .remove and .add code.
         if i != None:
             self.assertEqual(r.len() > 0, 1)
             r.remove(i)
             self.assertEqual(r.len(), 0)
             r.add(i)
             self.assertEqual(str(r), res)
Exemple #3
0
 def testKnownInitFailures(self):
     "Test that IPRanges fails to initialize in known situations."
     for a in self.knownBadInitArgs:
         self.assertRaises(netblock.NBError, netblock.IPRanges, a)
         # Test that the same failure is raised on add.
         r = netblock.IPRanges()
         self.assertRaises(netblock.NBError, r.add, a)
Exemple #4
0
 def testInOperator(self):
     """Test the 'in' operator for IPRanges."""
     r = netblock.IPRanges('127/8')
     self.assertEqual('127.0.0.1' in r, 1)
     self.assertEqual('126.255.255.255' in r, 0)
     # Check that everything actually, you know, works.
     r = netblock.IPRanges('127.0.0.0/24')
     r.add("128.0.0.0/23")
     r.add("255.255.255.0/24")
     r.add("0.0.0.0/24")
     # Three /24s and a /23 are 256*5.
     self.assertEqual(len(r), 1280)
     c = 0
     for e in r:
         self.assertEqual(e in r, 1)
         # Now test the number version.
         en = netblock.strtoip(e)
         self.assertEqual(en in r, 1)
         c += 1
     # Make sure we actually enumerated everything.
     self.assertEqual(c, len(r))
Exemple #5
0
def process(args):
    r = netblock.IPRanges()

    # people's republic of glorious special cases.
    if len(args) == 3 and args[1] == "-":
        process([
            "%s-%s" % (args[0], args[2]),
        ])
        return

    # We permit the rarely used calculator mode.
    opd = r.add
    for a in args:
        op = opd
        # Are we changing the operation?
        if a == "-":
            opd = r.remove
            continue
        elif a == "+":
            opd = r.add
            continue
        # We might be asking for a one-shot subtraction.
        if a[0] == '-':
            op = r.remove
            a = a[1:]
        # We notice bad input by the errors that netblock throws.
        try:
            op(a)
        except netblock.BadCIDRError as e:
            # Because this happens SO OFTEN, give a specific
            # message.
            c = netblock.convcidr(a, 0)
            die("bad CIDR %s. Should start at IP %s" % \
                (a, netblock.ipstr(c[0])))
        except netblock.NBError as e:
            die("bad argument %s: %s" % (a, str(e)))
    dumpout(r)
Exemple #6
0
 def __init__(self):
     self.ipranges = netblock.IPRanges()
     self.domains = []
     self.ip_domain = []
Exemple #7
0
 def testIterResult(self):
     "Test that iteration of a netblock results in IP addresses."
     r = netblock.IPRanges('127.0.0.0-127.0.0.2')
     self.assertEqual(tuple(r), ('127.0.0.0', '127.0.0.1', '127.0.0.2'))
Exemple #8
0
 def testRemoveoddcidr(self):
     """test that .removeoddcidr() at least passes a basic test."""
     n = netblock.IPRanges('127.0.0.0/23')
     n.removeoddcidr('127.0.0.1/24')
     self.assertEqual(str(n), '<IPRanges: 127.0.1.0-127.0.1.255>')
Exemple #9
0
 def testAddoddcidr(self):
     """test that .addoddcidr() at least passes a basic test."""
     n = netblock.IPRanges()
     n.addoddcidr('127.0.0.1/24')
     self.assertEqual(str(n), '<IPRanges: 127.0.0.0-127.0.0.255>')
Exemple #10
0
 def addIpRanges(self, key, val):
     if key not in self.kv:
         self.kv[key] = netblock.IPRanges()
     for ipr in val.split():
         self.kv[key].add(ipr)