def test_arp_reply(self): """Receive a valid ARP packet and ensure the correct reply""" # Arrange iface1 = HostInterface(EthAddr( self.H2_I1_ETH), [IPAddr(self.H2_I1_IP1), IPAddr(self.H2_I1_IP2)]) iface2 = HostInterface(EthAddr(self.H2_I2_ETH), [IPAddr(self.H2_I2_IP)]) interfaces = [iface1, iface2] h = Host(interfaces) arp_req = arp() arp_req.hwsrc = EthAddr(self.H1_I1_ETH) arp_req.hwdst = EthAddr(b"\xff\xff\xff\xff\xff\xff") arp_req.opcode = arp.REQUEST arp_req.protosrc = IPAddr(self.H1_I1_IP) arp_req.protodst = IPAddr(self.H2_I1_IP1) ether = ethernet() ether.type = ethernet.ARP_TYPE ether.dst = EthAddr(b"\xff\xff\xff\xff\xff\xff") ether.src = EthAddr(self.H1_I1_ETH) ether.payload = arp_req # Act # Get the action and arp reply packet arp_reply = h.receive(interfaces[0], ether) # Assert self.assertIsNotNone(arp_reply) self.assertEquals(arp_reply.src, EthAddr(self.H2_I1_ETH)) self.assertEquals(arp_reply.dst, EthAddr(self.H1_I1_ETH)) self.assertEquals(arp_reply.type, ethernet.ARP_TYPE) reply_payload = arp_reply.payload self.assertEquals(reply_payload.opcode, arp.REPLY) self.assertEquals(reply_payload.hwsrc, EthAddr(self.H2_I1_ETH)) self.assertEquals(reply_payload.hwdst, EthAddr(self.H1_I1_ETH)) self.assertEquals(reply_payload.protosrc, self.H2_I1_IP1) self.assertEquals(reply_payload.protodst, self.H1_I1_IP)
def test_to_jsont(self, SwitchCls): # Arrange json_dict = { "node1": { "hid": 1, "interfaces": [{ "hw_addr": "11:22:33:44:55:66", "ips": ["127.0.0.1"], "__type__": "sts.entities.hosts.HostInterface", "name": "eth0" }], "__type__": "sts.entities.hosts.Host", "name": "h1" }, "port2": { "hw_addr": "00:00:00:00:00:00", "curr": 0, "name": "", "supported": 0, "__type__": "pox.openflow.libopenflow_01.ofp_phy_port", "state": 0, "advertised": 0, "peer": 0, "config": 0, "port_no": 1 }, "node2": 1, "__type__": "sts.entities.sts_entities.AccessLink", "port1": { "hw_addr": "11:22:33:44:55:66", "ips": ["127.0.0.1"], "__type__": "sts.entities.hosts.HostInterface", "name": "eth0" } } sw1 = SwitchCls() sw1.dpid = 1 sw1.to_json.return_value = 1 # It's really hard to mock this, because of using assert_type p1 = ofp_phy_port(port_no=1) hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 host = Host(interface, name=hname, hid=hid) # Act link = AccessLink.from_json(json_dict) # Assert self.assertEquals(link.host.to_json(), host.to_json()) self.assertEquals(link.interface.to_json(), interface.to_json()) self.assertEquals(link.switch, 1) self.assertEquals(link.switch_port.to_json(), p1.to_json())
def test_invalid_arp(self): """Receive a ARP packet that isn't desinated to it and ensure there is no reply""" # Arrange iface1 = HostInterface(EthAddr( self.H2_I1_ETH), [IPAddr(self.H2_I1_IP1), IPAddr(self.H2_I1_IP2)]) iface2 = HostInterface(EthAddr(self.H2_I2_ETH), [IPAddr(self.H2_I2_IP)]) interfaces = [iface1, iface2] h = Host(interfaces) arp_req = arp() arp_req.hwsrc = EthAddr(self.H1_I1_ETH) arp_req.hwdst = EthAddr(b"\xff\xff\xff\xff\xff\xff") arp_req.opcode = arp.REQUEST arp_req.protosrc = IPAddr(self.H1_I1_IP) arp_req.protodst = IPAddr(self.H3_I3_IP) ether = ethernet() ether.type = ethernet.ARP_TYPE ether.dst = EthAddr(b"\xff\xff\xff\xff\xff\xff") ether.src = EthAddr(self.H1_I1_ETH) ether.payload = arp_req # Act # Get the action and reply packet reply_packet = h.receive(interfaces[0], ether) # Assert self.assertIsNone(reply_packet)
def test_to_json(self): # Arrange io_master = mock.Mock() hw_addr_str = "0e:32:a4:91:e7:30" ip_str = "192.168.56.2" hw_addr = EthAddr(hw_addr_str) ip = IPAddr(ip_str) ifname = "test-host" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 cmd = '/bin/bash sleep' # Mocking external dependencies import sts.util.network_namespace as ns ns.launch_namespace = mock.Mock(return_value=(None, hw_addr_str, None)) ns.bind_raw_socket = mock.Mock(return_value=None) host = NamespaceHost(interface, io_master.create_worker_for_socket, name=hname, hid=hid, cmd=cmd) # Act json_dict = host.to_json() # Assert self.assertEquals(json_dict['name'], hname) self.assertEquals(json_dict['hid'], hid) self.assertEquals(json_dict['cmd'], cmd) self.assertEquals(len(json_dict['interfaces']), 1) self.assertEquals(json_dict['interfaces'][0], interface.to_json())
def test_eq(self): # Arrange hw_addr_str = "11:22:33:44:55:66" hw_addr_str2 = "66:55:44:33:22:11" hw_addr = EthAddr(hw_addr_str) hw_addr2 = EthAddr(hw_addr_str2) ip_str = "127.0.0.1" ip = IPAddr(ip_str) name = "eth0" # Act interface1 = HostInterface(hw_addr, ip, name=name) interface2 = HostInterface(hw_addr, ip, name=name) interface3 = HostInterface(hw_addr2, ip, name=name) # Assert self.assertEquals(interface1, interface2) self.assertNotEquals(interface1, interface3)
def test_from_json(self): # Arrange json_dict = { 'hid': 1, '__type__': 'sts.entities.hosts.Host', 'name': 'h1', 'interfaces': [{ '__type__': 'sts.entities.hosts.HostInterface', 'name': 'eth0', 'hw_addr': '11:22:33:44:55:66', 'ips': ['127.0.0.1'], }], } hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 # Act host = Host.from_json(json_dict) # Assert self.assertEquals(host.name, hname) self.assertEquals(host.hid, hid) self.assertEquals(len(host.interfaces), 1) self.assertEquals(host.interfaces[0], interface)
def test_to_json(self): # Arrange hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 host = Host(interface, name=hname, hid=hid) # Act json_dict = host.to_json() # Assert self.assertEquals(json_dict['name'], hname) self.assertEquals(json_dict['hid'], hid) self.assertEquals(len(json_dict['interfaces']), 1) self.assertEquals(json_dict['interfaces'][0], interface.to_json())
def test_to_json(self): # Arrange hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) name = "eth0" expected = { '__type__': 'sts.entities.hosts.HostInterface', 'name': name, 'ips': [ip], 'hw_addr': hw_addr_str } # Act interface = HostInterface(hw_addr, ip, name=name) json_str = interface.to_json() # Assert self.assertEquals(json_str, expected)
def test_none_arp(self): """Receive a non-ARP packet and ensure there is no reply""" # Arrange iface1 = HostInterface(EthAddr( self.H2_I1_ETH), [IPAddr(self.H2_I1_IP1), IPAddr(self.H2_I1_IP2)]) iface2 = HostInterface(EthAddr(self.H2_I2_ETH), [IPAddr(self.H2_I2_IP)]) interfaces = [iface1, iface2] h = Host(interfaces) ether = ethernet() ether.type = ethernet.IP_TYPE ether.dst = EthAddr(self.H2_I1_ETH) ether.src = EthAddr(self.H1_I1_ETH) # Act # Get the action and reply packet reply_packet = h.receive(interfaces[0], ether) # Assert self.assertIsNone(reply_packet)
def test_from_json(self): # Arrange json_dict = { '__type__': 'sts.entities.hosts.NamespaceHost', 'cmd': '/bin/bash sleep', 'name': 'h1', 'hid': 1, 'interfaces': [{ '__type__': 'sts.entities.hosts.HostInterface', 'hw_addr': '0e:32:a4:91:e7:30', 'ips': ['192.168.56.2'], 'name': 'test-host' }] } io_master = mock.Mock() hw_addr_str = "0e:32:a4:91:e7:30" ip_str = "192.168.56.2" hw_addr = EthAddr(hw_addr_str) ip = IPAddr(ip_str) ifname = "test-host" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 cmd = '/bin/bash sleep' # Mocking external dependencies import sts.util.network_namespace as ns ns.launch_namespace = mock.Mock(return_value=(None, hw_addr_str, None)) ns.bind_raw_socket = mock.Mock(return_value=None) # Act host = NamespaceHost.from_json(json_dict, io_master.create_worker_for_socket) # Assert self.assertEquals(host.name, hname) self.assertEquals(host.hid, hid) self.assertEquals(host.cmd, cmd) self.assertEquals(len(host.interfaces), 1) self.assertEquals(host.interfaces[0].to_json(), interface.to_json())
def test_init(self): # Arrange hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) name = "eth0" # Act interface = HostInterface(hw_addr, ip, name=name) # Assert self.assertEquals(interface.hw_addr, hw_addr) self.assertEquals(interface.ips, [ip]) self.assertEquals(interface.name, name)
def test_init(self): # Arrange name = "test-host" hid = 123 hw_addr_str = "0e:32:a4:91:e7:20" ip = "192.168.56.1" interfaces = [HostInterface(hw_addr_str, ip)] io_master = self.initialize_io_loop() # Act host = NamespaceHost(interfaces, io_master.create_worker_for_socket, name=name, hid=hid) # Assert self.assertEquals(host.interfaces, interfaces)
def test_to_jsont(self, SwitchCls): # Arrange sw1 = SwitchCls() sw1.dpid = 1 sw1.to_json.return_value = 1 # It's really hard to mock this, because of using assert_type p1 = ofp_phy_port(port_no=1) hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 host = Host(interface, name=hname, hid=hid) link = AccessLink(host, interface, sw1, p1) # Act json_dict = link.to_json() # Assert self.assertEquals(json_dict['node1'], host.to_json()) self.assertEquals(json_dict['port1'], interface.to_json()) self.assertEquals(json_dict['node2'], 1) self.assertEquals(json_dict['port2'], p1.to_json())
def test_from_json(self): # Arrange hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) name = "eth0" input_json = { '__type__': 'sts.entities.hosts.HostInterface', 'name': name, 'ips': [ip], 'hw_addr': hw_addr_str } # Act interface = HostInterface.from_json(input_json) # Assert self.assertEquals(interface.hw_addr, hw_addr) self.assertEquals(interface.ips, [ip]) self.assertEquals(interface.name, name)
def test_init(self, SwitchCls): # Arrange sw1 = SwitchCls() sw1.dpid = 1 # It's really hard to mock this, because of using assert_type p1 = ofp_phy_port(port_no=1) hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 host = Host(interface, name=hname, hid=hid) # Act link = AccessLink(host, interface, sw1, p1) # Assert self.assertEquals(link.host, host) self.assertEquals(link.interface, interface)
def test_init(self): # Arrange name = "test-host" hid = 123 hw_addr_str = "11:22:33:44:55:66" ip = "192.168.56.1" interfaces = [HostInterface(hw_addr_str, ip)] # Mocking external dependencies import sts.util.network_namespace as ns ns.launch_namespace = mock.Mock(return_value=(None, hw_addr_str, None)) ns.bind_raw_socket = mock.Mock(return_value=None) # Act host = NamespaceHost(interfaces, lambda x: mock.Mock(), name=name, hid=hid) # Assert self.assertEquals(host.interfaces, interfaces)
def test_to_jsont(self, SwitchCls): # Arrange json_dict = { "node1": { "hid": 1, "interfaces": [ { "hw_addr": "11:22:33:44:55:66", "ips": [ "127.0.0.1" ], "__type__": "sts.entities.hosts.HostInterface", "name": "eth0" } ], "__type__": "sts.entities.hosts.Host", "name": "h1" }, "port2": { "hw_addr": "00:00:00:00:00:00", "curr": 0, "name": "", "supported": 0, "__type__": "pox.openflow.libopenflow_01.ofp_phy_port", "state": 0, "advertised": 0, "peer": 0, "config": 0, "port_no": 1 }, "node2": 1, "__type__": "sts.entities.sts_entities.AccessLink", "port1": { "hw_addr": "11:22:33:44:55:66", "ips": [ "127.0.0.1" ], "__type__": "sts.entities.hosts.HostInterface", "name": "eth0" } } sw1 = SwitchCls() sw1.dpid = 1 sw1.to_json.return_value = 1 # It's really hard to mock this, because of using assert_type p1 = ofp_phy_port(port_no=1) hw_addr_str = "11:22:33:44:55:66" hw_addr = EthAddr(hw_addr_str) ip_str = "127.0.0.1" ip = IPAddr(ip_str) ifname = "eth0" interface = HostInterface(hw_addr, ip, name=ifname) hname = "h1" hid = 1 host = Host(interface, name=hname, hid=hid) # Act link = AccessLink.from_json(json_dict) # Assert self.assertEquals(link.host.to_json(), host.to_json()) self.assertEquals(link.interface.to_json(), interface.to_json()) self.assertEquals(link.switch, 1) self.assertEquals(link.switch_port.to_json(), p1.to_json())