LEFT | RIGHT |
(no file at all) | |
| 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 |
| 21 #include "ns3/test.h" |
| 22 #include "ns3/socket-factory.h" |
| 23 #include "ns3/udp-socket-factory.h" |
| 24 #include "ns3/simulator.h" |
| 25 #include "ns3/simple-channel.h" |
| 26 #include "ns3/simple-net-device.h" |
| 27 #include "ns3/drop-tail-queue.h" |
| 28 #include "ns3/socket.h" |
| 29 #include "ns3/boolean.h" |
| 30 |
| 31 #include "ns3/log.h" |
| 32 #include "ns3/node.h" |
| 33 #include "ns3/inet6-socket-address.h" |
| 34 |
| 35 #include "ns3/ipv6-l3-protocol.h" |
| 36 #include "ns3/icmpv6-l4-protocol.h" |
| 37 #include "ns3/udp-l4-protocol.h" |
| 38 #include "ns3/ipv6-list-routing.h" |
| 39 #include "ns3/ipv6-static-routing.h" |
| 40 |
| 41 #include "ns3/sixlowpan-net-device.h" |
| 42 |
| 43 #include <string> |
| 44 #include <limits> |
| 45 |
| 46 using namespace ns3; |
| 47 |
| 48 static void |
| 49 AddInternetStack6 (Ptr<Node> node) |
| 50 { |
| 51 //IPV6 |
| 52 Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> (); |
| 53 //Routing for Ipv6 |
| 54 Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> (); |
| 55 ipv6->SetRoutingProtocol (ipv6Routing); |
| 56 Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> (); |
| 57 ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0); |
| 58 node->AggregateObject (ipv6); |
| 59 //ICMP |
| 60 Ptr<Icmpv6L4Protocol> icmp = CreateObject<Icmpv6L4Protocol> (); |
| 61 node->AggregateObject (icmp); |
| 62 //Ipv6 Extensions |
| 63 ipv6->RegisterExtensions (); |
| 64 ipv6->RegisterOptions (); |
| 65 //UDP |
| 66 Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> (); |
| 67 node->AggregateObject (udp); |
| 68 } |
| 69 |
| 70 |
| 71 class SixlowpanHc1ImplTest : public TestCase |
| 72 { |
| 73 Ptr<Packet> m_receivedPacket; |
| 74 void DoSendData (Ptr<Socket> socket, std::string to); |
| 75 void SendData (Ptr<Socket> socket, std::string to); |
| 76 |
| 77 public: |
| 78 virtual void DoRun (void); |
| 79 SixlowpanHc1ImplTest (); |
| 80 |
| 81 void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &fro
m); |
| 82 void ReceivePkt (Ptr<Socket> socket); |
| 83 }; |
| 84 |
| 85 SixlowpanHc1ImplTest::SixlowpanHc1ImplTest () |
| 86 : TestCase ("Sixlowpan implementation") |
| 87 { |
| 88 } |
| 89 |
| 90 void SixlowpanHc1ImplTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet
, const Address &from) |
| 91 { |
| 92 m_receivedPacket = packet; |
| 93 } |
| 94 |
| 95 void SixlowpanHc1ImplTest::ReceivePkt (Ptr<Socket> socket) |
| 96 { |
| 97 uint32_t availableData; |
| 98 availableData = socket->GetRxAvailable (); |
| 99 m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0); |
| 100 NS_ASSERT (availableData == m_receivedPacket->GetSize ()); |
| 101 //cast availableData to void, to suppress 'availableData' set but not used |
| 102 //compiler warning |
| 103 (void) availableData; |
| 104 } |
| 105 |
| 106 void |
| 107 SixlowpanHc1ImplTest::DoSendData (Ptr<Socket> socket, std::string to) |
| 108 { |
| 109 Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234); |
| 110 uint8_t buffer [] = "\"Can you tell me where my country lies?\" \\ said the un
ifaun to his true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe
\\ - for her merchandise, he traded in his prize."; |
| 111 |
| 112 Ptr<Packet> packet = Create<Packet> (buffer, 180); |
| 113 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo), |
| 114 180, "200"); |
| 115 } |
| 116 |
| 117 void |
| 118 SixlowpanHc1ImplTest::SendData (Ptr<Socket> socket, std::string to) |
| 119 { |
| 120 m_receivedPacket = Create<Packet> (); |
| 121 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), |
| 122 &SixlowpanHc1ImplTest::DoSendData, this, socke
t, to); |
| 123 Simulator::Run (); |
| 124 } |
| 125 |
| 126 void |
| 127 SixlowpanHc1ImplTest::DoRun (void) |
| 128 { |
| 129 // Create topology |
| 130 |
| 131 // Receiver Node |
| 132 Ptr<Node> rxNode = CreateObject<Node> (); |
| 133 AddInternetStack6 (rxNode); |
| 134 Ptr<SimpleNetDevice> rxDev; |
| 135 { // first interface |
| 136 rxDev = CreateObject<SimpleNetDevice> (); |
| 137 rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
| 138 rxNode->AddDevice (rxDev); |
| 139 |
| 140 Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> (); |
| 141 rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) ); |
| 142 rxSix->SetAttribute ("Rfc6282", BooleanValue (false) ); |
| 143 rxNode->AddDevice (rxSix); |
| 144 rxSix->SetNetDevice (rxDev); |
| 145 |
| 146 Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> (); |
| 147 ipv6->AddInterface (rxDev); |
| 148 uint32_t netdev_idx = ipv6->AddInterface (rxSix); |
| 149 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:010
0::1"), Ipv6Prefix (64)); |
| 150 ipv6->AddAddress (netdev_idx, ipv6Addr); |
| 151 ipv6->SetUp (netdev_idx); |
| 152 } |
| 153 |
| 154 // Sender Node |
| 155 Ptr<Node> txNode = CreateObject<Node> (); |
| 156 AddInternetStack6 (txNode); |
| 157 Ptr<SimpleNetDevice> txDev; |
| 158 { |
| 159 txDev = CreateObject<SimpleNetDevice> (); |
| 160 txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
| 161 txNode->AddDevice (txDev); |
| 162 |
| 163 Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> (); |
| 164 txSix->SetAttribute ("ForceEtherType", BooleanValue (true) ); |
| 165 txSix->SetAttribute ("Rfc6282", BooleanValue (false) ); |
| 166 txNode->AddDevice (txSix); |
| 167 txSix->SetNetDevice (txDev); |
| 168 |
| 169 Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> (); |
| 170 ipv6->AddInterface (txDev); |
| 171 uint32_t netdev_idx = ipv6->AddInterface (txSix); |
| 172 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:010
0::2"), Ipv6Prefix (64)); |
| 173 ipv6->AddAddress (netdev_idx, ipv6Addr); |
| 174 ipv6->SetUp (netdev_idx); |
| 175 } |
| 176 |
| 177 // link the two nodes |
| 178 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> (); |
| 179 rxDev->SetChannel (channel1); |
| 180 txDev->SetChannel (channel1); |
| 181 |
| 182 // Create the UDP sockets |
| 183 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> (); |
| 184 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket (); |
| 185 NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:
0100::1"), 1234)), 0, "trivial"); |
| 186 rxSocket->SetRecvCallback (MakeCallback (&SixlowpanHc1ImplTest::ReceivePkt, th
is)); |
| 187 |
| 188 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> (); |
| 189 Ptr<Socket> txSocket = txSocketFactory->CreateSocket (); |
| 190 txSocket->SetAllowBroadcast (true); |
| 191 // ------ Now the tests ------------ |
| 192 |
| 193 // Unicast test |
| 194 SendData (txSocket, "2001:0100::1"); |
| 195 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial"); |
| 196 uint8_t rxBuffer [180]; |
| 197 uint8_t txBuffer [180] = "\"Can you tell me where my country lies?\" \\ said t
he unifaun to his true love's eyes. \\ \"It lies with me!\" cried the Queen of M
aybe \\ - for her merchandise, he traded in his prize."; |
| 198 m_receivedPacket->CopyData (rxBuffer, 180); |
| 199 NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial"); |
| 200 |
| 201 m_receivedPacket->RemoveAllByteTags (); |
| 202 |
| 203 Simulator::Destroy (); |
| 204 |
| 205 } |
| 206 |
| 207 |
| 208 //----------------------------------------------------------------------------- |
| 209 class SixlowpanHc1TestSuite : public TestSuite |
| 210 { |
| 211 public: |
| 212 SixlowpanHc1TestSuite () : TestSuite ("sixlowpan-hc1", UNIT) |
| 213 { |
| 214 AddTestCase (new SixlowpanHc1ImplTest, TestCase::QUICK); |
| 215 } |
| 216 } g_sixlowpanHc1TestSuite; |
LEFT | RIGHT |