Add a couple of Udp socket unit tests: unicast (passing) and broadcast (failing, bug #51).
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Fri, 21 Sep 2007 15:54:00 +0100
changeset 1519 9b1cbc4c63f8
parent 1518 b6983560649f
child 1520 47d4ee8f48b8
Add a couple of Udp socket unit tests: unicast (passing) and broadcast (failing, bug #51).
src/internet-node/udp-socket.cc
--- a/src/internet-node/udp-socket.cc	Sat Sep 15 14:07:35 2007 -0700
+++ b/src/internet-node/udp-socket.cc	Fri Sep 21 15:54:00 2007 +0100
@@ -332,3 +332,132 @@
 }
 
 } //namespace ns3
+
+
+#ifdef RUN_SELF_TESTS
+
+#include "ns3/test.h"
+#include "ns3/internet-node.h"
+#include "ns3/socket-factory.h"
+#include "ns3/udp.h"
+#include "ns3/simulator.h"
+#include "ns3/point-to-point-channel.h"
+#include "ns3/point-to-point-net-device.h"
+#include "ns3/drop-tail-queue.h"
+#include <string>
+
+namespace ns3 {
+
+class UdpSocketTest: public Test
+{
+  Packet m_receivedPacket;
+  Packet m_receivedPacket2;
+
+public:
+  virtual bool RunTests (void);
+  UdpSocketTest ();
+
+  void ReceivePacket (Ptr<Socket> socket, const Packet &packet, const Address &from);
+  void ReceivePacket2 (Ptr<Socket> socket, const Packet &packet, const Address &from);
+};
+
+
+UdpSocketTest::UdpSocketTest ()
+  : Test ("UdpSocket") {}
+
+
+void UdpSocketTest::ReceivePacket (Ptr<Socket> socket, const Packet &packet, const Address &from)
+{
+  m_receivedPacket = packet;
+}
+
+void UdpSocketTest::ReceivePacket2 (Ptr<Socket> socket, const Packet &packet, const Address &from)
+{
+  m_receivedPacket2 = packet;
+}
+
+bool
+UdpSocketTest::RunTests (void)
+{
+  bool result = true;
+
+  // Create topology
+  
+  // Receiver Node
+  Ptr<Node> rxNode = Create<InternetNode> ();
+  Ptr<PointToPointNetDevice> rxDev = Create<PointToPointNetDevice> (rxNode);
+  rxDev->AddQueue(Create<DropTailQueue> ());
+  Ptr<Ipv4> ipv4 = rxNode->QueryInterface<Ipv4> (Ipv4::iid);
+  uint32_t netdev_idx = ipv4->AddInterface (rxDev);
+  ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.1"));
+  ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
+  ipv4->SetUp (netdev_idx);
+
+  // Sender Node
+  Ptr<Node> txNode = Create<InternetNode> ();
+  Ptr<PointToPointNetDevice> txDev = Create<PointToPointNetDevice> (txNode);
+  txDev->AddQueue(Create<DropTailQueue> ());
+  ipv4 = txNode->QueryInterface<Ipv4> (Ipv4::iid);
+  netdev_idx = ipv4->AddInterface (txDev);
+  ipv4->SetAddress (netdev_idx, Ipv4Address ("10.0.0.2"));
+  ipv4->SetNetworkMask (netdev_idx, Ipv4Mask (0xffff0000U));
+  ipv4->SetUp (netdev_idx);
+
+  // link the two nodes
+  Ptr<PointToPointChannel> channel = Create<PointToPointChannel> ();
+  rxDev->Attach (channel);
+  txDev->Attach (channel);
+
+
+  // Create the UDP sockets
+  Ptr<SocketFactory> socketFactory = rxNode->QueryInterface<SocketFactory> (Udp::iid);
+  Ptr<Socket> rxSocket = socketFactory->CreateSocket ();
+  NS_TEST_ASSERT_EQUAL (rxSocket->Bind (InetSocketAddress (1234)), 0);
+  rxSocket->SetRecvCallback (MakeCallback (&UdpSocketTest::ReceivePacket, this));
+
+  socketFactory = txNode->QueryInterface<SocketFactory> (Udp::iid);
+  Ptr<Socket> txSocket = socketFactory->CreateSocket ();
+
+  // ------ Now the tests ------------
+
+  // Unicast test
+  m_receivedPacket = Packet ();
+  NS_TEST_ASSERT_EQUAL (txSocket->SendTo (InetSocketAddress (Ipv4Address("10.0.0.1"), 1234),
+                                          Packet (123)), 0);
+  Simulator::Run ();
+  NS_TEST_ASSERT_EQUAL (m_receivedPacket.GetSize (), 123);
+
+
+  // Simple broadcast test
+
+  m_receivedPacket = Packet ();
+  NS_TEST_ASSERT_EQUAL (txSocket->SendTo (InetSocketAddress (Ipv4Address("255.255.255.255"), 1234),
+                                          Packet (123)), 0);
+  Simulator::Run ();
+  NS_TEST_ASSERT_EQUAL (m_receivedPacket.GetSize (), 123);
+
+  // Broadcast test with multiple receiving sockets
+
+  // When receiving broadcast packets, all sockets sockets bound to
+  // the address/port should receive a copy of the same packet.
+  Ptr<Socket> rxSocket2 = socketFactory->CreateSocket ();
+  rxSocket2->SetRecvCallback (MakeCallback (&UdpSocketTest::ReceivePacket, this));
+  NS_TEST_ASSERT_EQUAL (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.0.1"), 1234)), 0);
+
+  m_receivedPacket = Packet ();
+  m_receivedPacket2 = Packet ();
+  NS_TEST_ASSERT_EQUAL (txSocket->SendTo (InetSocketAddress (Ipv4Address("255.255.255.255"), 1234),
+                                          Packet (123)), 0);
+  Simulator::Run ();
+  NS_TEST_ASSERT_EQUAL (m_receivedPacket.GetSize (), 123);
+  NS_TEST_ASSERT_EQUAL (m_receivedPacket2.GetSize (), 123);
+
+  return result;
+}
+
+
+static UdpSocketTest gUdpSocketTest;
+
+}; // namespace ns3
+
+#endif /* RUN_SELF_TESTS */