PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sixlowpan/test/sixlowpan-iphc-test.cc

https://gitlab.com/msepahkar/location_in_wireless_ndn
C++ | 214 lines | 147 code | 33 blank | 34 comment | 1 complexity | a72bcceb522cae6bc28ae0ac631cede9 MD5 | raw file
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2013 Universita' di Firenze, Italy
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation;
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
  19. */
  20. #include "ns3/test.h"
  21. #include "ns3/socket-factory.h"
  22. #include "ns3/udp-socket-factory.h"
  23. #include "ns3/simulator.h"
  24. #include "ns3/simple-channel.h"
  25. #include "ns3/simple-net-device.h"
  26. #include "ns3/drop-tail-queue.h"
  27. #include "ns3/socket.h"
  28. #include "ns3/boolean.h"
  29. #include "ns3/log.h"
  30. #include "ns3/node.h"
  31. #include "ns3/inet6-socket-address.h"
  32. #include "ns3/ipv6-l3-protocol.h"
  33. #include "ns3/icmpv6-l4-protocol.h"
  34. #include "ns3/udp-l4-protocol.h"
  35. #include "ns3/ipv6-list-routing.h"
  36. #include "ns3/ipv6-static-routing.h"
  37. #include "ns3/sixlowpan-net-device.h"
  38. #include <string>
  39. #include <limits>
  40. using namespace ns3;
  41. static void
  42. AddInternetStack6 (Ptr<Node> node)
  43. {
  44. //IPV6
  45. Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
  46. //Routing for Ipv6
  47. Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
  48. ipv6->SetRoutingProtocol (ipv6Routing);
  49. Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
  50. ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
  51. node->AggregateObject (ipv6);
  52. //ICMP
  53. Ptr<Icmpv6L4Protocol> icmp = CreateObject<Icmpv6L4Protocol> ();
  54. node->AggregateObject (icmp);
  55. //Ipv6 Extensions
  56. ipv6->RegisterExtensions ();
  57. ipv6->RegisterOptions ();
  58. //UDP
  59. Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
  60. node->AggregateObject (udp);
  61. }
  62. class SixlowpanIphcImplTest : public TestCase
  63. {
  64. Ptr<Packet> m_receivedPacket;
  65. void DoSendData (Ptr<Socket> socket, std::string to);
  66. void SendData (Ptr<Socket> socket, std::string to);
  67. public:
  68. virtual void DoRun (void);
  69. SixlowpanIphcImplTest ();
  70. void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
  71. void ReceivePkt (Ptr<Socket> socket);
  72. };
  73. SixlowpanIphcImplTest::SixlowpanIphcImplTest ()
  74. : TestCase ("Sixlowpan implementation")
  75. {
  76. }
  77. void SixlowpanIphcImplTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from)
  78. {
  79. m_receivedPacket = packet;
  80. }
  81. void SixlowpanIphcImplTest::ReceivePkt (Ptr<Socket> socket)
  82. {
  83. uint32_t availableData;
  84. availableData = socket->GetRxAvailable ();
  85. m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
  86. NS_ASSERT (availableData == m_receivedPacket->GetSize ());
  87. //cast availableData to void, to suppress 'availableData' set but not used
  88. //compiler warning
  89. (void) availableData;
  90. }
  91. void
  92. SixlowpanIphcImplTest::DoSendData (Ptr<Socket> socket, std::string to)
  93. {
  94. Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
  95. uint8_t buffer [] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her merchandise, he traded in his prize.";
  96. Ptr<Packet> packet = Create<Packet> (buffer, 180);
  97. NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
  98. 180, "200");
  99. }
  100. void
  101. SixlowpanIphcImplTest::SendData (Ptr<Socket> socket, std::string to)
  102. {
  103. m_receivedPacket = Create<Packet> ();
  104. Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
  105. &SixlowpanIphcImplTest::DoSendData, this, socket, to);
  106. Simulator::Run ();
  107. }
  108. void
  109. SixlowpanIphcImplTest::DoRun (void)
  110. {
  111. // Create topology
  112. // Receiver Node
  113. Ptr<Node> rxNode = CreateObject<Node> ();
  114. AddInternetStack6 (rxNode);
  115. Ptr<SimpleNetDevice> rxDev;
  116. { // first interface
  117. rxDev = CreateObject<SimpleNetDevice> ();
  118. rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  119. rxNode->AddDevice (rxDev);
  120. Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
  121. rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
  122. rxNode->AddDevice (rxSix);
  123. rxSix->SetNetDevice (rxDev);
  124. Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
  125. ipv6->AddInterface (rxDev);
  126. uint32_t netdev_idx = ipv6->AddInterface (rxSix);
  127. Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
  128. ipv6->AddAddress (netdev_idx, ipv6Addr);
  129. ipv6->SetUp (netdev_idx);
  130. }
  131. // Sender Node
  132. Ptr<Node> txNode = CreateObject<Node> ();
  133. AddInternetStack6 (txNode);
  134. Ptr<SimpleNetDevice> txDev;
  135. {
  136. txDev = CreateObject<SimpleNetDevice> ();
  137. txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
  138. txNode->AddDevice (txDev);
  139. Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
  140. txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
  141. txNode->AddDevice (txSix);
  142. txSix->SetNetDevice (txDev);
  143. Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
  144. ipv6->AddInterface (txDev);
  145. uint32_t netdev_idx = ipv6->AddInterface (txSix);
  146. Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
  147. ipv6->AddAddress (netdev_idx, ipv6Addr);
  148. ipv6->SetUp (netdev_idx);
  149. }
  150. // link the two nodes
  151. Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
  152. rxDev->SetChannel (channel1);
  153. txDev->SetChannel (channel1);
  154. // Create the UDP sockets
  155. Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
  156. Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
  157. NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
  158. rxSocket->SetRecvCallback (MakeCallback (&SixlowpanIphcImplTest::ReceivePkt, this));
  159. Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
  160. Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
  161. txSocket->SetAllowBroadcast (true);
  162. // ------ Now the tests ------------
  163. // Unicast test
  164. SendData (txSocket, "2001:0100::1");
  165. NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
  166. uint8_t rxBuffer [180];
  167. uint8_t txBuffer [180] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her merchandise, he traded in his prize.";
  168. m_receivedPacket->CopyData (rxBuffer, 180);
  169. NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
  170. m_receivedPacket->RemoveAllByteTags ();
  171. Simulator::Destroy ();
  172. }
  173. //-----------------------------------------------------------------------------
  174. class SixlowpanIphcTestSuite : public TestSuite
  175. {
  176. public:
  177. SixlowpanIphcTestSuite () : TestSuite ("sixlowpan-iphc", UNIT)
  178. {
  179. AddTestCase (new SixlowpanIphcImplTest, TestCase::QUICK);
  180. }
  181. } g_sixlowpanIphcTestSuite;