def _assemble_device_list(args): if args.exclude is None: args.exclude = [] if args.intf is None: args.intf = [] devlist = make_device_list(includes=args.intf, excludes=args.exclude) return devlist
def setUp(self): self.scenario = TestScenario('test') self.scenario.add_interface('eth1', '11:11:11:11:11:11') self.scenario.add_interface('eth0', '00:00:00:00:00:00') self.scenario.add_interface('eth2', '22:22:22:22:22:22') self.scenario.add_interface('eth7', '77:77:77:77:77:77', ipaddr='192.168.1.1', netmask='255.255.255.0') self.scenario.done = Mock(return_value=False) self.ev = Mock() self.ev.match = Mock(return_value=None) self.scenario.next = Mock(return_value=self.ev) self.fake = LLNetTest(self.scenario) self.devs = make_device_list([], []) self.real = WrapLLNet(self.devs) self.real._fix_devinfo(self.devs) self.real._pcaps = Mock() self.real._pcaps.get = Mock(return_value=Mock())
def setUp(self): self.scenario = TestScenario('test') self.scenario.add_interface('eth1', '11:11:11:11:11:11') self.scenario.add_interface('eth0', '00:00:00:00:00:00') self.scenario.add_interface('eth2', '22:22:22:22:22:22') self.scenario.add_interface('eth7', '77:77:77:77:77:77', '192.168.1.1/24') self.scenario.done = Mock(return_value=False) self.ev = Mock() self.ev.match = Mock(return_value=None) self.scenario.next = Mock(return_value=self.ev) self.fake = LLNetTest(self.scenario) self.devs = make_device_list([], []) self.real = WrapLLNet(self.devs) self.real._fix_devinfo(self.devs) self.real._pcaps = Mock() self.real._pcaps.get = Mock(return_value=Mock())
def testDevListMaker(self): import switchyard.pcapffi as pf import socket as sock # name, intname, desc, loop, up, running dlist = [ pf.PcapInterface("a", "aint", "", False, True, True), ] devmock = Mock(return_value=dlist) ifnum = Mock(side_effect=range(0, 100)) setattr(intfmod, "pcap_devices", devmock) setattr(intfmod, "if_nametoindex", ifnum) # includes, excludes rv = intfmod.make_device_list(set({"a"}), set()) self.assertEqual(len(rv), 1) self.assertIn("a", rv) rv = intfmod.make_device_list(set(), set()) self.assertEqual(len(rv), 1) self.assertIn("a", rv) rv = intfmod.make_device_list(set(), set({"a"})) self.assertEqual(len(rv), 0) rv = intfmod.make_device_list(set({"xyz"}), set({"a"})) self.assertEqual(len(rv), 0) dlist = [ pf.PcapInterface("a", "aint", "", False, True, True), pf.PcapInterface("b", "bint", "", True, True, True), pf.PcapInterface("c", "cint", "", False, True, True), ] devmock = Mock(return_value=dlist) setattr(intfmod, "pcap_devices", devmock) rv = intfmod.make_device_list(includes=set({"xyz"}), excludes=set({"a"})) self.assertEqual(len(rv), 1) self.assertIn("c", rv) rv = intfmod.make_device_list(excludes=set({"a"})) self.assertEqual(len(rv), 1) self.assertIn("c", rv) ifnum = Mock(side_effect=Exception) setattr(intfmod, "if_nametoindex", ifnum) rv = intfmod.make_device_list(includes=set(), excludes=set()) self.assertEqual(len(rv), 0)
def testDevListMaker(self): import switchyard.pcapffi as pf import socket as sock # name, intname, desc, loop, up, running dlist = [ pf.PcapInterface("a", "aint", "", False, True, True), ] devmock = Mock(return_value=dlist) ifnum = Mock(side_effect=range(0,100)) setattr(intfmod, "pcap_devices", devmock) setattr(intfmod, "if_nametoindex", ifnum) # includes, excludes rv = intfmod.make_device_list(set({"a"}), set()) self.assertEqual(len(rv), 1) self.assertIn("a", rv) rv = intfmod.make_device_list(set(), set()) self.assertEqual(len(rv), 1) self.assertIn("a", rv) rv = intfmod.make_device_list(set(), set({"a"})) self.assertEqual(len(rv), 0) rv = intfmod.make_device_list(set({"xyz"}), set({"a"})) self.assertEqual(len(rv), 0) dlist = [ pf.PcapInterface("a", "aint", "", False, True, True), pf.PcapInterface("b", "bint", "", True, True, True), pf.PcapInterface("c", "cint", "", False, True, True), ] devmock = Mock(return_value=dlist) setattr(intfmod, "pcap_devices", devmock) rv = intfmod.make_device_list(includes=set({"xyz"}), excludes=set({"a"})) self.assertEqual(len(rv), 1) self.assertIn("c", rv) rv = intfmod.make_device_list(excludes=set({"a"})) self.assertEqual(len(rv), 1) self.assertIn("c", rv) ifnum = Mock(side_effect=Exception) setattr(intfmod, "if_nametoindex", ifnum) rv = intfmod.make_device_list(includes=set(), excludes=set()) self.assertEqual(len(rv), 0)
def start_framework(args): global _netobj, _setup_ok setup_logging(args.debug, args.logfile) # assume testmode if compile flag is set testmode = False if args.compile or args.tests: testmode = True args.codearg = _parse_codeargs(args.codearg) if args.verbose: VerboseOutput.enable() if args.cli: t = Topology() if args.topology: try: t = load_from_file(args.topology) except FileNotFoundError: print("No such file {} exists to load topology.".format( args.topology)) return run_simulation(t) return waiters = 1 if args.app: waiters += 1 barrier = Barrier(waiters) if args.app: ApplicationLayer._init() _appt = Thread(target=_start_app, args=(args.app, barrier)) _appt.start() if args.app: args.fwconfig = [] elif args.fwconfig is None or len(args.fwconfig) == 0: args.fwconfig = ('all', ) if testmode: if args.compile: if args.usercode: log_info("You specified user code to run with compile flag, " "but I'm just doing compile.") for scenario in args.compile: log_info("Compiling scenario {}".format(scenario)) compile_scenario(scenario) else: if not args.usercode: log_failure("In test mode, but not user code supplied.") return setattr(sys, "platform", "test") with Firewall([], args.fwconfig): _setup_ok = True barrier.wait() main_test(args) else: if sys.platform != 'win32' and os.geteuid() != 0: log_warn("You're running in real mode, but not as root. " "You should expect errors, but I'm going to " "continue anyway.") if args.exclude is None: args.exclude = [] if args.intf is None: args.intf = [] devlist = make_device_list(includes=args.intf, excludes=args.exclude) if not devlist: log_failure("There are no network interfaces I can use after " "processing include/exclude lists") alldevs = make_device_list([], []) log_failure("Here are all the interfaces I see on your system: " "{}".format(', '.join(list(alldevs)))) barrier.wait() return with Firewall(devlist, args.fwconfig): _setup_ok = True barrier.wait() _netobj = LLNetReal(devlist) main_real(args.usercode, _netobj, args)
def start_framework(args): global _netobj, _setup_ok setup_logging(args.debug, args.logfile) if args.listif: devlist = _assemble_device_list(args) dlist = [ str(d) for d in devlist ] log_info("Devices found: {}".format(','.join(dlist))) return # assume testmode if compile flag is set testmode = False if args.compile or args.tests: testmode = True args.codearg = _parse_codeargs(args.codearg) if args.verbose: VerboseOutput.enable() if args.cli: t = Topology() if args.topology: try: t = load_from_file(args.topology) except FileNotFoundError: print ("No such file {} exists to load topology.".format(args.topology)) return run_simulation(t) return waiters = 1 if args.app: waiters += 1 barrier = Barrier(waiters) if args.app: ApplicationLayer._init() _appt = Thread(target=_start_app, args=(args.app,barrier)) _appt.start() if args.app: args.fwconfig = [] elif args.fwconfig is None or len(args.fwconfig) == 0: args.fwconfig = ('all',) if testmode: if args.compile: if args.usercode: log_info("You specified user code to run with compile flag, " "but I'm just doing compile.") for scenario in args.compile: log_info("Compiling scenario {}".format(scenario)) compile_scenario(scenario) else: if not args.usercode: log_failure("In test mode, but not user code supplied.") return global platform platform = sys.platform setattr(sys, "origplatform", platform) setattr(sys, "platform", "test") with Firewall([], args.fwconfig): _setup_ok = True barrier.wait() main_test(args) else: if sys.platform != 'win32' and os.geteuid() != 0: log_warn("You're running in real mode, but not as root. " "You should expect errors, but I'm going to " "continue anyway.") devlist = _assemble_device_list(args) if not devlist: log_failure("There are no network interfaces I can use after " "processing include/exclude lists") alldevs = make_device_list([], []) log_failure("Here are all the interfaces I see on your system: " "{}".format(', '.join(list(alldevs)))) barrier.wait() return with Firewall(devlist, args.fwconfig): _setup_ok = True barrier.wait() _netobj = LLNetReal(devlist) main_real(args.usercode, _netobj, args)