def testConfigIncomplete(self): n = nat.NAT(dmroot=DeviceModelRoot()) self.loop.RunOnce(timeout=1) self.assertFalse(os.path.exists(self.outputfile4)) self.assertFalse(os.path.exists(self.outputfile6)) p = n.PortMapping() n.PortMappingList['1'] = p self.loop.RunOnce(timeout=1) self.assertTrue(os.path.exists(self.outputfile4)) self.assertTrue(os.path.exists(self.outputfile6)) p.AllInterfaces = True p.Description = 'Description' self.loop.RunOnce(timeout=1) self.assertFalse(os.stat(self.outputfile4).st_size) self.assertFalse(os.stat(self.outputfile6).st_size) p.InternalClient = '1.1.1.1' p.InternalPort = 80 p.Protocol = 'TCP' self.loop.RunOnce(timeout=1) self.assertFalse(os.stat(self.outputfile4).st_size) self.assertFalse(os.stat(self.outputfile6).st_size) p.Enable = True self.loop.RunOnce(timeout=1) self.assertTrue(os.stat(self.outputfile4).st_size) self.assertFalse(os.stat(self.outputfile6).st_size)
def testRestartFails(self): print 'The following tracebacks are normal, they are part of the test.' nat.RESTARTCMD = ['testdata/nat/restart_fails'] n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.InternalClient = '1.1.1.1' p.Enable = True n.PortMappingList['1'] = p self.loop.RunOnce(timeout=1) self.assertTrue(os.path.exists(self.outputfile4))
def testInterface(self): dmroot = DeviceModelRoot() n = nat.NAT(dmroot=dmroot) p = n.PortMapping() p.Interface = 'Device.IP.Interface.1' # should succeed self.assertEqual(p.Interface, 'Device.IP.Interface.1') del dmroot.Device.IP.InterfaceList['1'] self.assertEqual(p.Interface, '') # No such interface self.assertRaises(ValueError, setattr, p, 'Interface', 'Device.IP.Interface.2')
def testPortMappingPrecedence(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() self.assertEqual(p.Precedence(), 4) p.ExternalPort = 1 self.assertEqual(p.Precedence(), 3) p.RemoteHost = '1.2.3.4' p.ExternalPort = 0 self.assertEqual(p.Precedence(), 2) p.ExternalPort = 1 self.assertEqual(p.Precedence(), 1)
def testDmz4(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() n.PortMappingList['1'] = p p.InternalClient = '1.1.1.1' p.Enable = True self.assertFalse(os.path.exists(self.dmzfile4)) self.loop.RunOnce(timeout=1) self.assertTrue(os.path.exists(self.dmzfile4)) self.assertFalse(os.path.exists(self.dmzfile6)) self.assertTrue(os.path.exists(self.outputfile4)) config4 = open(self.outputfile4).read() self.assertFalse(config4) p.Enable = False self.loop.RunOnce(timeout=1) self.assertFalse(os.path.exists(self.dmzfile4)) # Add some static dmz mappings. for i in range(4): m = n.X_CATAWAMPUS_ORG_DmzMapping() m.LanAddress = '1.2.3.%d' % i m.WanAddress = '192.168.1.%d' % (i + 10) n.X_CATAWAMPUS_ORG_DmzMappingList['%s' % i] = m p.Enable = True self.loop.RunOnce(timeout=1) self.assertTrue(os.path.exists(self.dmzfile4)) config4 = open(self.dmzfile4).read().strip().split('\n') self.assertEqual(len(config4), 5) self.assertTrue('1.1.1.1' in config4) for i in range(4): self.assertTrue('192.168.1.%d 1.2.3.%d' % (i + 10, i)) # Disable one of the entries. n.X_CATAWAMPUS_ORG_DmzMappingList['2'].WanAddress = '' self.loop.RunOnce(timeout=1) config4 = open(self.dmzfile4).read().strip().split('\n') self.assertEqual(len(config4), 4) for i in range(4): if i != 2: self.assertTrue('192.168.1.%d 1.2.3.%d' % (i + 10, i)) # Make sure disable works with static entries. p.Enable = False self.loop.RunOnce(timeout=1) self.assertTrue(os.path.exists(self.dmzfile4)) config4 = open(self.dmzfile4).read().strip().split('\n') self.assertEqual(len(config4), 3) # Check that setting a bogus ip4 address does something sensible. got_exception = False try: n.X_CATAWAMPUS_ORG_DmzMappingList[ '1'].LanAddress = '512.513.514.515' except ValueError: got_exception = True self.assertTrue(got_exception)
def testStatus(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.Enable = False self.assertEqual(p.Status, 'Disabled') p.Enable = True self.assertEqual(p.Status, 'Error_Misconfigured') p.Protocol = 'TCP' self.assertEqual(p.Status, 'Error_Misconfigured') p.InternalPort = 80 self.assertEqual(p.Status, 'Error_Misconfigured') p.Interface = 'Device.IP.Interface.1' self.assertEqual(p.Status, 'Error_Misconfigured') p.InternalClient = '1.1.1.1' self.assertEqual(p.Status, 'Enabled')
def testDmz6(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() n.PortMappingList['1'] = p p.InternalClient = 'fe80::fa8f:caff:fe11:1111' p.Enable = True self.assertFalse(os.path.exists(self.dmzfile6)) self.loop.RunOnce(timeout=1) self.assertFalse(os.path.exists(self.dmzfile4)) self.assertTrue(os.path.exists(self.dmzfile6)) self.assertTrue(os.path.exists(self.outputfile6)) config4 = open(self.outputfile4).read() self.assertFalse(config4) p.Enable = False self.loop.RunOnce(timeout=1) self.assertFalse(os.path.exists(self.dmzfile6))
def testConfigDeleteObject(self): n = nat.NAT(dmroot=DeviceModelRoot()) h = tr.handle.Handle(n) p = n.PortMapping() p.AllInterfaces = True p.Enable = True p.InternalClient = '1.1.1.1' p.InternalPort = 80 p.Protocol = 'TCP' n.PortMappingList['1'] = p self.loop.RunOnce(timeout=1) h.DeleteExportObject('PortMapping', '1') self.loop.RunOnce(timeout=1) config = open(self.outputfile4).read() self.assertFalse(config)
def testConfigWritePortRangeSize(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.Enable = True p.ExternalPort = 1 p.X_CATAWAMPUS_ORG_PortRangeSize = 100 p.InternalClient = '4.4.4.4' p.InternalPort = 90 p.Interface = 'Device.IP.Interface.1' p.Protocol = 'UDP' p.RemoteHost = '5.5.5.5' n.PortMappingList['1'] = p p = n.PortMapping() p.Enable = True p.ExternalPort = 10 p.X_CATAWAMPUS_ORG_PortRangeSize = 51 p.InternalClient = 'fe80::fa8f:caff:fe11:1111' p.InternalPort = 91 p.Interface = 'Device.IP.Interface.1' p.Protocol = 'TCP' n.PortMappingList['2'] = p self.loop.RunOnce(timeout=1) config4 = open(self.outputfile4).read() expected = ['IDX_1:,UDP,4.4.4.4,1:100,90:189,1,5.5.5.5,9.9.9.9'] for line in config4.splitlines(): line = line.strip() if line and not line.startswith('#'): self.assertTrue(line in expected) expected.remove(line) self.assertEqual(len(expected), 0) config6 = open(self.outputfile6).read() expected = [ 'IDX_2:,TCP,fe80::fa8f:caff:fe11:1111,10:60,91:141,1,::/0,1000::0001' ] for line in config6.splitlines(): line = line.strip() if line and not line.startswith('#'): self.assertTrue(line in expected) expected.remove(line) self.assertEqual(len(expected), 0) self.assertTrue(os.path.exists(self.restartfile))
def testConfigWriteIP6(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.AllInterfaces = True p.Enable = True p.InternalClient = 'fe80::fa8f:caff:fe11:1111' p.InternalPort = 80 p.Protocol = 'TCP' n.PortMappingList['1'] = p self.loop.RunOnce(timeout=1) config4 = open(self.outputfile4).read() self.assertEqual(config4, '') config6 = open(self.outputfile6).read() expected = ['IDX_1:,TCP,fe80::fa8f:caff:fe11:1111,0,80,1,::/0,::/0'] for line in config6.splitlines(): line = line.strip() if line and not line.startswith('#'): self.assertTrue(line in expected) expected.remove(line) self.assertEqual(len(expected), 0) self.assertTrue(os.path.exists(self.restartfile))
def testConfigWrite(self): n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.AllInterfaces = True p.Description = 'Description' p.Enable = True p.InternalClient = '1.1.1.1' p.InternalPort = 80 p.Protocol = 'TCP' n.PortMappingList['1'] = p p = n.PortMapping() p.Enable = True p.ExternalPort = 1 p.ExternalPortEndRange = 9 p.InternalClient = '3.3.3.3' p.InternalPort = 90 p.Interface = 'Device.IP.Interface.1' p.Protocol = 'UDP' p.RemoteHost = '2.2.2.2' n.PortMappingList['2'] = p self.loop.RunOnce(timeout=1) config4 = open(self.outputfile4).read() expected = [ 'IDX_2:,UDP,3.3.3.3,1:9,90,1,2.2.2.2,9.9.9.9', 'IDX_1:4465736372697074696f6e,TCP,1.1.1.1,0,80,1,0/0,0/0' ] for line in config4.splitlines(): line = line.strip() if line and not line.startswith('#'): self.assertTrue(line in expected) expected.remove(line) self.assertEqual(len(expected), 0) config6 = open(self.outputfile6).read() self.assertEqual(config6, '') self.assertTrue(os.path.exists(self.restartfile))
def testLeaseDuration(self): """A non-zero LeaseDuration is not supported.""" n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.LeaseDuration = 0 # should succeed self.assertRaises(ValueError, setattr, p, 'LeaseDuration', 1)
def testValidateExports(self): n = nat.NAT(dmroot=DeviceModelRoot()) tr.handle.ValidateExports(n) p = n.PortMapping() tr.handle.ValidateExports(p)
def testDescriptionTooLong(self): """Description is stored in kernel ipt_comment. Must be short.""" n = nat.NAT(dmroot=DeviceModelRoot()) p = n.PortMapping() p.Description = 'A' * 256 # should succeed self.assertRaises(ValueError, setattr, p, 'Description', 'A' * 257)