View | Details | Raw Unified | Return to bug 1041
Collapse All | Expand All

(-)a/src/internet/test/ipv4-forwarding-test.cc (-2 / +166 lines)
 Lines 43-48    Link Here 
43
43
44
using namespace ns3;
44
using namespace ns3;
45
45
46
NS_LOG_COMPONENT_DEFINE ("Ipv4ForwardingTest");
47
46
static void
48
static void
47
AddInternetStack (Ptr<Node> node)
49
AddInternetStack (Ptr<Node> node)
48
{
50
{
 Lines 102-108    Link Here 
102
void
104
void
103
Ipv4ForwardingTest::SendData (Ptr<Socket> socket, std::string to)
105
Ipv4ForwardingTest::SendData (Ptr<Socket> socket, std::string to)
104
{
106
{
105
  m_receivedPacket = Create<Packet> ();
106
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
107
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
107
                                  &Ipv4ForwardingTest::DoSendData, this, socket, to);
108
                                  &Ipv4ForwardingTest::DoSendData, this, socket, to);
108
  Simulator::Run ();
109
  Simulator::Run ();
 Lines 202-208    Link Here 
202
  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
203
  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
203
  ipv4->SetAttribute("IpForward", BooleanValue (false));
204
  ipv4->SetAttribute("IpForward", BooleanValue (false));
204
  SendData (txSocket, "10.0.0.2");
205
  SendData (txSocket, "10.0.0.2");
205
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv4 Forwarding off");
206
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "IPv4 Forwarding off; no received packet");
207
208
  Simulator::Destroy ();
209
210
}
211
212
class Ipv4BindToNetDeviceTest : public TestCase
213
{
214
  Ptr<Packet> m_receivedPacket;
215
  void DoSendData (Ptr<Socket> socket, std::string to, int expectedReturnValue);
216
  void SendData (Ptr<Socket> socket, std::string to, int expectedReturnValue);
217
218
public:
219
  virtual void DoRun (void);
220
  Ipv4BindToNetDeviceTest ();
221
222
  void ReceivePkt (Ptr<Socket> socket);
223
};
224
225
Ipv4BindToNetDeviceTest::Ipv4BindToNetDeviceTest ()
226
  : TestCase ("Test that Socket::BindToNetDevice () works")
227
{
228
}
229
230
void Ipv4BindToNetDeviceTest::ReceivePkt (Ptr<Socket> socket)
231
{
232
  uint32_t availableData;
233
  availableData = socket->GetRxAvailable ();
234
  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
235
  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
236
}
237
238
void
239
Ipv4BindToNetDeviceTest::DoSendData (Ptr<Socket> socket, std::string to, int expectedReturnValue)
240
{
241
  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
242
  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
243
                         expectedReturnValue, "SendTo failure");
244
}
245
246
void
247
Ipv4BindToNetDeviceTest::SendData (Ptr<Socket> socket, std::string to, int expectedReturnValue)
248
{
249
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
250
                                  &Ipv4BindToNetDeviceTest::DoSendData, this, socket, to, expectedReturnValue);
251
}
252
253
void
254
Ipv4BindToNetDeviceTest::DoRun (void)
255
{
256
  // Create topology
257
258
  // Receiver Node
259
  Ptr<Node> rxNode = CreateObject<Node> ();
260
  AddInternetStack (rxNode);
261
  Ptr<SimpleNetDevice> rxDev1;
262
  Ptr<SimpleNetDevice> rxDev2;
263
  { // first interface
264
    rxDev1 = CreateObject<SimpleNetDevice> ();
265
    rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
266
    rxNode->AddDevice (rxDev1);
267
    Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
268
    uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
269
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
270
    ipv4->AddAddress (netdev_idx, ipv4Addr);
271
    ipv4->SetUp (netdev_idx);
272
  }
273
  { // second interface
274
    rxDev2 = CreateObject<SimpleNetDevice> ();
275
    rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
276
    rxNode->AddDevice (rxDev2);
277
    Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
278
    uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
279
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("11.0.0.2"), Ipv4Mask (0xffff0000U));
280
    ipv4->AddAddress (netdev_idx, ipv4Addr);
281
    ipv4->SetUp (netdev_idx);
282
  }
283
284
  // Sender Node
285
  Ptr<Node> txNode = CreateObject<Node> ();
286
  AddInternetStack (txNode);
287
  Ptr<SimpleNetDevice> txDev1;
288
  Ptr<SimpleNetDevice> txDev2;
289
  {
290
    // first interface
291
    txDev1 = CreateObject<SimpleNetDevice> ();
292
    txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
293
    txNode->AddDevice (txDev1);
294
    Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
295
    uint32_t netdev_idx = ipv4->AddInterface (txDev1);
296
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
297
    ipv4->AddAddress (netdev_idx, ipv4Addr);
298
    ipv4->SetUp (netdev_idx);
299
  }
300
  {
301
    // second interface
302
    txDev2 = CreateObject<SimpleNetDevice> ();
303
    txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
304
    txNode->AddDevice (txDev2);
305
    Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
306
    uint32_t netdev_idx = ipv4->AddInterface (txDev2);
307
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask (0xffff0000U));
308
    ipv4->AddAddress (netdev_idx, ipv4Addr);
309
    ipv4->SetUp (netdev_idx);
310
  }
311
312
  // link the two nodes
313
  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
314
  rxDev1->SetChannel (channel1);
315
  txDev1->SetChannel (channel1);
316
317
  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
318
  rxDev2->SetChannel (channel2);
319
  txDev2->SetChannel (channel2);
320
321
  // Create the UDP sockets
322
  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
323
  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
324
  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
325
  // Must call BindToNetDevice() after Bind()
326
  rxSocket->SetRecvCallback (MakeCallback (&Ipv4BindToNetDeviceTest::ReceivePkt, this));
327
328
  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
329
  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
330
  txSocket->SetAllowBroadcast (true);
331
332
  // ------ Now the tests ------------
333
334
  // Test that data is successful when RxNode binds to rxDev1 and TxNode binds
335
  // to txDev1
336
  NS_LOG_DEBUG ("Bind test case 1");
337
  rxSocket->BindToNetDevice (rxDev1);
338
  txSocket->BindToNetDevice (txDev1);
339
  SendData (txSocket, "10.0.0.2", 123);  // 123 is the expected Send() return value
340
  Simulator::Run ();
341
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Correctly bound NetDevices");
342
  m_receivedPacket = 0;
343
344
  // All three other bind combinations should fail 
345
  
346
  NS_LOG_DEBUG ("Bind test case 2");
347
  txSocket = txSocketFactory->CreateSocket ();
348
  rxSocket->BindToNetDevice (rxDev1);
349
  txSocket->BindToNetDevice (txDev2);
350
  SendData (txSocket, "10.0.0.2", -1);
351
  Simulator::Run ();
352
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
353
354
  NS_LOG_DEBUG ("Bind test case 3");
355
  txSocket = txSocketFactory->CreateSocket ();
356
  rxSocket->BindToNetDevice (rxDev2);
357
  txSocket->BindToNetDevice (txDev2);
358
  SendData (txSocket, "10.0.0.2", -1);
359
  Simulator::Run ();
360
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
361
362
  NS_LOG_DEBUG ("Bind test case 4");
363
  txSocket = txSocketFactory->CreateSocket ();
364
  rxSocket->BindToNetDevice (rxDev2);
365
  txSocket->BindToNetDevice (txDev1);
366
  SendData (txSocket, "10.0.0.2", 123);
367
  Simulator::Run ();
368
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
206
369
207
  Simulator::Destroy ();
370
  Simulator::Destroy ();
208
371
 Lines 217-221    Link Here 
217
  Ipv4ForwardingTestSuite () : TestSuite ("ipv4-forwarding", UNIT)
380
  Ipv4ForwardingTestSuite () : TestSuite ("ipv4-forwarding", UNIT)
218
  {
381
  {
219
    AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
382
    AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
383
    AddTestCase (new Ipv4BindToNetDeviceTest, TestCase::QUICK);
220
  }
384
  }
221
} g_ipv4forwardingTestSuite;
385
} g_ipv4forwardingTestSuite;

Return to bug 1041