2022-01-26 01:53:28 -03:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
2021-01-19 18:15:44 -08:00
|
|
|
# Copyright (C) 2008-2011 INESC Porto
|
|
|
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
|
# Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
|
|
|
|
|
|
2008-07-08 10:43:58 -07:00
|
|
|
import unittest
|
2011-07-20 16:02:19 +01:00
|
|
|
from ns.core import Simulator, Seconds, Config, int64x64_t
|
|
|
|
|
import ns.core
|
|
|
|
|
import ns.network
|
|
|
|
|
import ns.internet
|
|
|
|
|
import ns.mobility
|
|
|
|
|
import ns.csma
|
2012-02-14 16:03:50 +00:00
|
|
|
import ns.applications
|
2011-07-20 16:02:19 +01:00
|
|
|
|
2022-01-26 01:53:28 -03:00
|
|
|
UINT32_MAX = 0xFFFFFFFF
|
|
|
|
|
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2017-02-24 09:23:49 -08:00
|
|
|
## TestSimulator class
|
2008-07-08 10:43:58 -07:00
|
|
|
class TestSimulator(unittest.TestCase):
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var _received_packet
|
|
|
|
|
# received packet
|
|
|
|
|
## @var _args_received
|
|
|
|
|
# args
|
|
|
|
|
## @var _cb_time
|
|
|
|
|
# current time
|
|
|
|
|
## @var _context_received
|
|
|
|
|
# context
|
2008-07-08 10:43:58 -07:00
|
|
|
|
|
|
|
|
def testScheduleNow(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test schedule now
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
def callback(args):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Callback function
|
|
|
|
|
@param args arguments
|
|
|
|
|
return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.ScheduleNow(callback, "args")
|
|
|
|
|
Simulator.Run()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 0.0)
|
|
|
|
|
|
|
|
|
|
def testSchedule(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test schedule
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
def callback(args):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Callback function
|
|
|
|
|
@param args arguments
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Schedule(Seconds(123), callback, "args")
|
|
|
|
|
Simulator.Run()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
|
|
|
|
|
|
|
|
|
|
def testScheduleDestroy(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test schedule destroy
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
def callback(args):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Callback function
|
|
|
|
|
@param args
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = args
|
2011-07-20 16:02:19 +01:00
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
|
|
|
|
def null(): pass
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Schedule(Seconds(123), null)
|
|
|
|
|
Simulator.ScheduleDestroy(callback, "args")
|
|
|
|
|
Simulator.Run()
|
|
|
|
|
Simulator.Destroy()
|
2008-07-08 10:43:58 -07:00
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
|
|
|
|
|
|
2012-02-14 17:45:50 +00:00
|
|
|
def testScheduleWithContext(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test schedule with context
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2012-02-14 17:45:50 +00:00
|
|
|
def callback(context, args):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Callback
|
|
|
|
|
@param context the cntet
|
|
|
|
|
@param args the arguments
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2012-02-14 17:45:50 +00:00
|
|
|
self._context_received = context
|
|
|
|
|
self._args_received = args
|
|
|
|
|
self._cb_time = Simulator.Now()
|
|
|
|
|
Simulator.Destroy()
|
|
|
|
|
self._args_received = None
|
|
|
|
|
self._cb_time = None
|
|
|
|
|
self._context_received = None
|
|
|
|
|
Simulator.ScheduleWithContext(54321, Seconds(123), callback, "args")
|
|
|
|
|
Simulator.Run()
|
|
|
|
|
self.assertEqual(self._context_received, 54321)
|
|
|
|
|
self.assertEqual(self._args_received, "args")
|
|
|
|
|
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
|
|
|
|
|
|
2008-10-06 17:39:35 +01:00
|
|
|
def testTimeComparison(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test time comparison
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2021-01-19 18:15:44 -08:00
|
|
|
self.assertTrue(Seconds(123) == Seconds(123))
|
|
|
|
|
self.assertTrue(Seconds(123) >= Seconds(123))
|
|
|
|
|
self.assertTrue(Seconds(123) <= Seconds(123))
|
|
|
|
|
self.assertTrue(Seconds(124) > Seconds(123))
|
|
|
|
|
self.assertTrue(Seconds(123) < Seconds(124))
|
2008-10-06 17:39:35 +01:00
|
|
|
|
|
|
|
|
def testTimeNumericOperations(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test numeric operations
|
2018-06-27 10:50:27 +03:00
|
|
|
@param self this object
|
2017-02-24 09:23:49 -08:00
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
|
|
|
|
|
self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
|
2022-06-05 21:01:11 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
v1 = int64x64_t(5.0)*int64x64_t(10)
|
|
|
|
|
self.assertEqual(v1, int64x64_t(50))
|
2008-07-08 10:43:58 -07:00
|
|
|
|
|
|
|
|
def testConfig(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test configuration
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
|
2008-07-08 10:43:58 -07:00
|
|
|
# hm.. no Config.Get?
|
|
|
|
|
|
2008-07-09 18:50:28 +01:00
|
|
|
def testSocket(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test socket
|
|
|
|
|
@param self
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
node = ns.network.Node()
|
|
|
|
|
internet = ns.internet.InternetStackHelper()
|
2009-05-28 21:37:25 -07:00
|
|
|
internet.Install(node)
|
2008-07-09 18:50:28 +01:00
|
|
|
self._received_packet = None
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2008-07-09 18:50:28 +01:00
|
|
|
def rx_callback(socket):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Receive Callback
|
|
|
|
|
@param socket the socket to receive
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2008-07-09 18:50:28 +01:00
|
|
|
assert self._received_packet is None
|
2022-01-26 01:53:28 -03:00
|
|
|
self._received_packet = socket.Recv(maxSize=UINT32_MAX, flags=0)
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
|
|
|
|
|
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
|
2008-07-09 18:50:28 +01:00
|
|
|
sink.SetRecvCallback(rx_callback)
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
|
|
|
|
|
source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
Simulator.Run()
|
2021-01-19 18:15:44 -08:00
|
|
|
self.assertTrue(self._received_packet is not None)
|
2008-07-09 18:50:28 +01:00
|
|
|
self.assertEqual(self._received_packet.GetSize(), 19)
|
2008-07-17 17:57:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def testAttributes(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test attributes function
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2021-01-19 18:15:44 -08:00
|
|
|
# Templated class DropTailQueue<Packet> in C++
|
|
|
|
|
queue = ns.network.DropTailQueue__Ns3Packet()
|
|
|
|
|
queueSizeValue = ns.network.QueueSizeValue (ns.network.QueueSize ("500p"))
|
|
|
|
|
queue.SetAttribute("MaxSize", queueSizeValue)
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2021-01-19 18:15:44 -08:00
|
|
|
limit = ns.network.QueueSizeValue()
|
|
|
|
|
queue.GetAttribute("MaxSize", limit)
|
|
|
|
|
self.assertEqual(limit.Get(), ns.network.QueueSize ("500p"))
|
2008-07-17 17:57:18 +01:00
|
|
|
|
|
|
|
|
## -- object pointer values
|
2011-07-20 16:02:19 +01:00
|
|
|
mobility = ns.mobility.RandomWaypointMobilityModel()
|
|
|
|
|
ptr = ns.core.PointerValue()
|
2009-04-16 10:55:42 +02:00
|
|
|
mobility.GetAttribute("PositionAllocator", ptr)
|
2008-07-17 17:57:18 +01:00
|
|
|
self.assertEqual(ptr.GetObject(), None)
|
2022-06-05 21:01:11 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
pos = ns.mobility.ListPositionAllocator()
|
|
|
|
|
mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
ptr = ns.core.PointerValue()
|
2009-04-16 10:55:42 +02:00
|
|
|
mobility.GetAttribute("PositionAllocator", ptr)
|
2021-01-19 18:15:44 -08:00
|
|
|
self.assertTrue(ptr.GetObject() is not None)
|
2008-07-08 10:43:58 -07:00
|
|
|
|
2008-08-01 23:11:42 +01:00
|
|
|
def testIdentity(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test identify
|
2018-06-27 10:50:27 +03:00
|
|
|
@param self this object
|
2017-02-24 09:23:49 -08:00
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
csma = ns.csma.CsmaNetDevice()
|
|
|
|
|
channel = ns.csma.CsmaChannel()
|
2008-08-01 23:11:42 +01:00
|
|
|
csma.Attach(channel)
|
2022-06-05 21:01:11 -07:00
|
|
|
|
2008-08-01 23:11:42 +01:00
|
|
|
c1 = csma.GetChannel()
|
|
|
|
|
c2 = csma.GetChannel()
|
|
|
|
|
|
2021-01-19 18:15:44 -08:00
|
|
|
self.assertTrue(c1 is c2)
|
2008-07-17 17:57:18 +01:00
|
|
|
|
2008-10-15 15:55:09 +01:00
|
|
|
def testTypeId(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test type ID
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
|
2008-10-15 15:55:09 +01:00
|
|
|
self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
|
2022-06-05 21:01:11 -07:00
|
|
|
|
2011-07-20 16:02:19 +01:00
|
|
|
self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
|
2008-10-15 15:55:09 +01:00
|
|
|
|
2008-11-26 12:11:11 +00:00
|
|
|
def testCommandLine(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test command line
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2011-07-20 16:02:19 +01:00
|
|
|
cmd = ns.core.CommandLine()
|
2008-11-26 12:11:11 +00:00
|
|
|
cmd.AddValue("Test1", "this is a test option")
|
|
|
|
|
cmd.AddValue("Test2", "this is a test option")
|
|
|
|
|
cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
|
|
|
|
|
cmd.Test1 = None
|
|
|
|
|
cmd.Test2 = None
|
|
|
|
|
cmd.test_xxx = None
|
|
|
|
|
class Foo:
|
|
|
|
|
pass
|
|
|
|
|
foo = Foo()
|
|
|
|
|
foo.test_foo = None
|
|
|
|
|
cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
|
2022-06-05 21:01:11 -07:00
|
|
|
|
2008-11-26 12:11:11 +00:00
|
|
|
cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
|
|
|
|
|
|
|
|
|
|
self.assertEqual(cmd.Test1, "value1")
|
|
|
|
|
self.assertEqual(cmd.Test2, "value2")
|
|
|
|
|
self.assertEqual(cmd.test_xxx, "123")
|
|
|
|
|
self.assertEqual(foo.test_foo, "xpto")
|
|
|
|
|
|
2010-05-06 14:21:20 +01:00
|
|
|
def testSubclass(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Test subclass
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
|
|
|
|
## MyNode class
|
2011-07-20 16:02:19 +01:00
|
|
|
class MyNode(ns.network.Node):
|
2010-05-06 14:21:20 +01:00
|
|
|
def __init__(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Initializer
|
|
|
|
|
@param self this object
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-05-06 14:21:20 +01:00
|
|
|
super(MyNode, self).__init__()
|
|
|
|
|
|
|
|
|
|
node = MyNode()
|
|
|
|
|
|
|
|
|
|
|
2008-07-08 10:43:58 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|