1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

* Added all network recognition cases

* Added test to network recognition (should have started to do Unit tests a long ago, but... :-) )
This commit is contained in:
Adolfo Gómez 2013-04-16 10:05:42 +00:00
parent 3a175ff29c
commit 616300decb
7 changed files with 199 additions and 10 deletions

View File

@ -171,6 +171,9 @@ encoding//src/uds/services/Vmware_enterprise/client/Exceptions.py=utf-8
encoding//src/uds/services/Vmware_enterprise/client/Server.py=utf-8 encoding//src/uds/services/Vmware_enterprise/client/Server.py=utf-8
encoding//src/uds/services/Vmware_enterprise/client/Task.py=utf-8 encoding//src/uds/services/Vmware_enterprise/client/Task.py=utf-8
encoding//src/uds/services/__init__.py=utf-8 encoding//src/uds/services/__init__.py=utf-8
encoding//src/uds/tests/__init__.py=utf-8
encoding//src/uds/tests/core/__init__.py=utf-8
encoding//src/uds/tests/core/util/net.py=utf-8
encoding//src/uds/transports/HTML5RDP/HTML5RDP.py=utf-8 encoding//src/uds/transports/HTML5RDP/HTML5RDP.py=utf-8
encoding//src/uds/transports/HTML5RDP/__init__.py=utf-8 encoding//src/uds/transports/HTML5RDP/__init__.py=utf-8
encoding//src/uds/transports/NX/NXTransport.py=utf-8 encoding//src/uds/transports/NX/NXTransport.py=utf-8

View File

@ -189,7 +189,7 @@ def authInfoUrl(authenticator):
Helper method, so we can get the info url for an authenticator Helper method, so we can get the info url for an authenticator
''' '''
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
if type(authenticator) is str: if isinstance(authenticator,unicode) or isinstance(authenticator, str):
name = authenticator name = authenticator
else: else:
name = authenticator.name name = authenticator.name

View File

@ -39,8 +39,8 @@ from exceptions import ValueError
reCIDR = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/([0-9]{1,2})$') reCIDR = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/([0-9]{1,2})$')
reMask = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})netmask([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$') reMask = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})netmask([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$')
re1Asterisk = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.\*$') re1Asterisk = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.\*$')
re2Asterisk = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.\*(\.\*)?$') re2Asterisk = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.\*\.?\*?$')
re3Asterisk = re.compile('^([0-9]{1,3})\.\*(\.\*)?(\.\*)?$') re3Asterisk = re.compile('^([0-9]{1,3})\.\*\.?\*?\.?\*?$')
reRange = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})-([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$') reRange = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})-([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$')
reHost = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$') reHost = re.compile('^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$')
@ -84,6 +84,9 @@ def networksFromString(strNets, allowMultipleNetworks = True):
If allowMultipleNetworks is True, it allows ',' and ';' separators (and, ofc, more than 1 network) If allowMultipleNetworks is True, it allows ',' and ';' separators (and, ofc, more than 1 network)
Returns a list of networks tuples in the form [(start1, end1), (start2, end2) ...] Returns a list of networks tuples in the form [(start1, end1), (start2, end2) ...]
''' '''
inputString = strNets
def check(*args): def check(*args):
for n in args: for n in args:
if int(n) < 0 or int(n) > 255: if int(n) < 0 or int(n) > 255:
@ -103,14 +106,18 @@ def networksFromString(strNets, allowMultipleNetworks = True):
v |= 1<<(31-n) v |= 1<<(31-n)
return v return v
nets = strNets.replace(' ', '')
if allowMultipleNetworks is True: if allowMultipleNetworks is True:
res = [] res = []
for strNet in re.split('[;,]',nets): for strNet in re.split('[;,]', strNets):
if strNet != '': if strNet != '':
res.append(networksFromString(strNet, False)) res.append(networksFromString(strNet, False))
return res return res
strNets = strNets.replace(' ', '')
if strNets == '*':
return (0, 4294967295)
try: try:
# Test patterns # Test patterns
m = reCIDR.match(strNets) m = reCIDR.match(strNets)
@ -127,16 +134,48 @@ def networksFromString(strNets, allowMultipleNetworks = True):
m = reMask.match(strNets) m = reMask.match(strNets)
if m is not None: if m is not None:
check(*m.groups()) check(*m.groups())
val = toNum(*(m.group(i+1) for i in xrange(4))) val = toNum(*(m.groups()[0:4]))
bits = toNum(*(m.group(i+5) for i in xrange(4))) bits = toNum(*(m.groups()[4:8]))
noBits = ~bits & 0xffffffff noBits = ~bits & 0xffffffff
return (val&bits, val|noBits) return (val&bits, val|noBits)
m = reRange.match(strNets)
if m is not None:
check(*m.groups())
val = toNum(*(m.groups()[0:4]))
val2 = toNum(*(m.groups()[4:8]))
if val2 < val:
raise Exception()
return (val, val2)
m = reHost.match(strNets)
if m is not None:
check(*m.groups())
val = toNum(*m.groups())
return (val, val)
for v in ((re1Asterisk, 3), (re2Asterisk, 2), (re3Asterisk, 1)):
m = v[0].match(strNets)
if m is not None:
check(*m.groups())
val = toNum(*(m.groups()[0:v[1]+1]))
bits = maskFromBits(v[1]*8)
noBits = ~bits & 0xffffffff
return (val&bits, val|noBits)
# No pattern recognized, invalid network # No pattern recognized, invalid network
raise Exception() raise Exception()
except: except:
raise ValueError(nets) raise ValueError(inputString)
def ipInNetwork(ip, network):
if isinstance(ip,unicode) or isinstance(ip,str):
ip = ipToLong(ip)
if isinstance(network,unicode) or isinstance(network,str):
network = networksFromString(network)
for net in network:
if ip >= net[0] and ip <= net[1]:
return True
return False

View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
'''
from core import *

View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
'''
from util import *

View File

@ -0,0 +1 @@
from net import *

View File

@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
@author: Adolfo Gómez, dkmaster at dkmon dot com
'''
from __future__ import unicode_literals
from django.utils import unittest
import uds.core.util.net
class NetTestCase(unittest.TestCase):
def testNetworkFromString(self):
for n in ( ('*', 0, 4294967295),
('192.168.0.1', 3232235521, 3232235521),
('192.168.0.*', 3232235520, 3232235775),
('192.168.*.*', 3232235520, 3232301055),
('192.168.*', 3232235520, 3232301055),
('192.*.*.*', 3221225472, 3238002687),
('192.*.*', 3221225472, 3238002687),
('192.*', 3221225472, 3238002687),
('192.168.0.1 netmask 255.255.255.0', 3232235520, 3232235775),
('192.168.0.1/8', 3221225472, 3238002687),
('192.168.0.1/28', 3232235520, 3232235535),
('192.168.0.1-192.168.0.87', 3232235521, 3232235607),
('192.168.0.1 netmask 255.255.255.0', 3232235520, 3232235775),
):
try:
nets = uds.core.util.net.networksFromString(n[0])
self.assertEqual(len(nets), 1, 'Incorrect number of network returned from {0}'.format(n[0]))
self.assertEquals(nets[0][0], n[1], 'Incorrect network start value for {0}'.format(n[0]))
self.assertEquals(nets[0][1], n[2], 'Incorrect network end value for {0}'.format(n[0]))
nets = uds.core.util.net.networksFromString(n[0],False)
self.assertEqual(len(nets), 2, 'Incorrect number of network returned from {0}'.format(n[0]))
self.assertEquals(nets[0], n[1], 'Incorrect network start value for {0}'.format(n[0]))
self.assertEquals(nets[1], n[2], 'Incorrect network end value for {0}'.format(n[0]))
except Exception as e:
raise Exception('Value Error: ' + str(e) + '. Input string: ' + n[0])
for n in ('192.168.0', '192.168.0.5-192.168.0.3', 'no net'):
with self.assertRaises(uds.core.util.net.ValueError):
uds.core.util.net.networksFromString(n)
self.assertEquals(True, uds.core.util.net.ipInNetwork('192.168.0.5', '192.168.0.0/24'))
self.assertEquals(True, uds.core.util.net.ipInNetwork(1000, [(0, 4294967295)]))