2017-04-27 21:34:23 +01:00
|
|
|
"""
|
2017-08-01 17:28:11 +01:00
|
|
|
Unit tests for testing basic CORE networks.
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
2017-04-27 22:49:51 +01:00
|
|
|
|
2017-08-03 00:39:38 +01:00
|
|
|
import os
|
2018-02-27 18:48:01 +00:00
|
|
|
import stat
|
2017-08-03 00:39:38 +01:00
|
|
|
import threading
|
2017-07-27 22:17:33 +01:00
|
|
|
|
2018-02-27 18:48:01 +00:00
|
|
|
import pytest
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
from core.emulator.emudata import NodeOptions
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.emulator.enumerations import MessageFlags, NodeTypes
|
2019-10-11 07:01:16 +01:00
|
|
|
from core.errors import CoreCommandError
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
|
2019-04-30 07:31:47 +01:00
|
|
|
from core.nodes.client import VnodeClient
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2017-08-03 00:39:38 +01:00
|
|
|
_PATH = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
_MOBILITY_FILE = os.path.join(_PATH, "mobility.scen")
|
2019-09-10 23:10:24 +01:00
|
|
|
_WIRED = [NodeTypes.PEER_TO_PEER, NodeTypes.HUB, NodeTypes.SWITCH]
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2018-02-27 18:48:01 +00:00
|
|
|
def createclients(sessiondir, clientcls=VnodeClient, cmdchnlfilterfunc=None):
|
|
|
|
"""
|
|
|
|
Create clients
|
|
|
|
|
|
|
|
:param str sessiondir: session directory to create clients
|
|
|
|
:param class clientcls: class to create clients from
|
|
|
|
:param func cmdchnlfilterfunc: command channel filter function
|
|
|
|
:return: list of created clients
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
direntries = map(lambda x: os.path.join(sessiondir, x), os.listdir(sessiondir))
|
2019-06-03 22:36:21 +01:00
|
|
|
cmdchnls = list(filter(lambda x: stat.S_ISSOCK(os.stat(x).st_mode), direntries))
|
2018-02-27 18:48:01 +00:00
|
|
|
if cmdchnlfilterfunc:
|
2019-06-03 22:36:21 +01:00
|
|
|
cmdchnls = list(filter(cmdchnlfilterfunc, cmdchnls))
|
2018-02-27 18:48:01 +00:00
|
|
|
cmdchnls.sort()
|
|
|
|
return map(lambda x: clientcls(os.path.basename(x), x), cmdchnls)
|
|
|
|
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
def ping(from_node, to_node, ip_prefixes):
|
|
|
|
address = ip_prefixes.ip4_address(to_node)
|
2019-10-11 07:01:16 +01:00
|
|
|
try:
|
|
|
|
from_node.node_net_cmd(["ping", "-c", "3", address])
|
|
|
|
status = 0
|
|
|
|
except CoreCommandError as e:
|
|
|
|
status = e.returncode
|
|
|
|
return status
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
|
2017-07-27 22:59:40 +01:00
|
|
|
class TestCore:
|
2018-04-26 00:33:58 +01:00
|
|
|
@pytest.mark.parametrize("net_type", _WIRED)
|
|
|
|
def test_wired_ping(self, session, net_type, ip_prefixes):
|
2017-07-27 22:17:33 +01:00
|
|
|
"""
|
|
|
|
Test ptp node network.
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param core.enumerations.NodeTypes net_type: type of net node to create
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 22:17:33 +01:00
|
|
|
"""
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# create net node
|
|
|
|
net_node = session.add_node(_type=net_type)
|
2017-07-27 22:17:33 +01:00
|
|
|
|
|
|
|
# create nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
node_one = session.add_node()
|
|
|
|
node_two = session.add_node()
|
2017-07-27 22:17:33 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to net node
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.add_link(node.id, net_node.id, interface_one=interface)
|
2017-07-27 22:17:33 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 22:17:33 +01:00
|
|
|
|
|
|
|
# ping n2 from n1 and assert success
|
2018-04-26 00:33:58 +01:00
|
|
|
status = ping(node_one, node_two, ip_prefixes)
|
2017-07-27 22:17:33 +01:00
|
|
|
assert not status
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
def test_vnode_client(self, session, ip_prefixes):
|
2017-07-27 19:57:01 +01:00
|
|
|
"""
|
|
|
|
Test vnode client methods.
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 19:57:01 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create ptp
|
2018-04-26 00:33:58 +01:00
|
|
|
ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# create nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
node_one = session.add_node()
|
|
|
|
node_two = session.add_node()
|
2017-07-27 19:57:01 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to ptp net
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.add_link(node.id, ptp_node.id, interface_one=interface)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# get node client for testing
|
2018-04-26 00:33:58 +01:00
|
|
|
client = node_one.client
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# check we are connected
|
|
|
|
assert client.connected()
|
|
|
|
|
|
|
|
# check various command using vcmd module
|
|
|
|
command = ["ls"]
|
|
|
|
assert not client.icmd(command)
|
|
|
|
|
|
|
|
# check various command using command line
|
|
|
|
assert not client.icmd(command)
|
|
|
|
|
|
|
|
# check module methods
|
2018-04-26 00:33:58 +01:00
|
|
|
assert createclients(session.session_dir)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
def test_netif(self, session, ip_prefixes):
|
2017-07-27 00:52:17 +01:00
|
|
|
"""
|
|
|
|
Test netif methods.
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 00:52:17 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create ptp
|
2018-04-26 00:33:58 +01:00
|
|
|
ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# create nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
node_one = session.add_node()
|
|
|
|
node_two = session.add_node()
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to ptp net
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.add_link(node.id, ptp_node.id, interface_one=interface)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check link data gets generated
|
|
|
|
assert ptp_node.all_link_data(MessageFlags.ADD.value)
|
|
|
|
|
|
|
|
# check common nets exist between linked nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
assert node_one.commonnets(node_two)
|
|
|
|
assert node_two.commonnets(node_one)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check we can retrieve netif index
|
2018-04-26 00:33:58 +01:00
|
|
|
assert node_one.getifindex(0)
|
|
|
|
assert node_two.getifindex(0)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check interface parameters
|
2018-04-26 00:33:58 +01:00
|
|
|
interface = node_one.netif(0)
|
|
|
|
interface.setparam("test", 1)
|
|
|
|
assert interface.getparam("test") == 1
|
|
|
|
assert interface.getparams()
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# delete netif and test that if no longer exists
|
2018-04-26 00:33:58 +01:00
|
|
|
node_one.delnetif(0)
|
|
|
|
assert not node_one.netif(0)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2018-04-27 18:12:01 +01:00
|
|
|
def test_wlan_ping(self, session, ip_prefixes):
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
|
|
|
Test basic wlan network.
|
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
2018-04-26 00:33:58 +01:00
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create wlan
|
2018-04-26 00:33:58 +01:00
|
|
|
wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN)
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, BasicRangeModel)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
|
|
|
# create nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
node_options = NodeOptions()
|
|
|
|
node_options.set_position(0, 0)
|
|
|
|
node_one = session.create_wireless_node(node_options=node_options)
|
|
|
|
node_two = session.create_wireless_node(node_options=node_options)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.add_link(node.id, wlan_node.id, interface_one=interface)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-04-27 21:34:23 +01:00
|
|
|
|
|
|
|
# ping n2 from n1 and assert success
|
2018-04-26 00:33:58 +01:00
|
|
|
status = ping(node_one, node_two, ip_prefixes)
|
2017-04-27 21:34:23 +01:00
|
|
|
assert not status
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
def test_mobility(self, session, ip_prefixes):
|
2017-08-03 00:39:38 +01:00
|
|
|
"""
|
|
|
|
Test basic wlan network.
|
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
2018-04-26 00:33:58 +01:00
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-08-03 00:39:38 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create wlan
|
2018-04-26 00:33:58 +01:00
|
|
|
wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN)
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, BasicRangeModel)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# create nodes
|
2018-04-26 00:33:58 +01:00
|
|
|
node_options = NodeOptions()
|
|
|
|
node_options.set_position(0, 0)
|
|
|
|
node_one = session.create_wireless_node(node_options=node_options)
|
|
|
|
node_two = session.create_wireless_node(node_options=node_options)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.add_link(node.id, wlan_node.id, interface_one=interface)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# configure mobility script for session
|
2018-06-07 20:57:32 +01:00
|
|
|
config = {
|
|
|
|
"file": _MOBILITY_FILE,
|
|
|
|
"refresh_ms": "50",
|
|
|
|
"loop": "1",
|
|
|
|
"autostart": "0.0",
|
|
|
|
"map": "",
|
|
|
|
"script_start": "",
|
|
|
|
"script_pause": "",
|
|
|
|
"script_stop": "",
|
|
|
|
}
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, Ns2ScriptedMobility, config)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# add handler for receiving node updates
|
|
|
|
event = threading.Event()
|
|
|
|
|
|
|
|
def node_update(_):
|
|
|
|
event.set()
|
2017-08-03 17:48:26 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
session.node_handlers.append(node_update)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# validate we receive a node message for updating its location
|
|
|
|
assert event.wait(5)
|