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

(-)a/src/internet/test/ipv4-forwarding-test.cc (-11 / +278 lines)
 Lines 21-26    Link Here 
21
#include "ns3/test.h"
21
#include "ns3/test.h"
22
#include "ns3/socket-factory.h"
22
#include "ns3/socket-factory.h"
23
#include "ns3/udp-socket-factory.h"
23
#include "ns3/udp-socket-factory.h"
24
#include "ns3/tcp-socket-factory.h"
24
#include "ns3/simulator.h"
25
#include "ns3/simulator.h"
25
#include "ns3/simple-channel.h"
26
#include "ns3/simple-channel.h"
26
#include "ns3/simple-net-device.h"
27
#include "ns3/simple-net-device.h"
 Lines 36-48    Link Here 
36
#include "ns3/ipv4-l3-protocol.h"
37
#include "ns3/ipv4-l3-protocol.h"
37
#include "ns3/icmpv4-l4-protocol.h"
38
#include "ns3/icmpv4-l4-protocol.h"
38
#include "ns3/udp-l4-protocol.h"
39
#include "ns3/udp-l4-protocol.h"
40
#include "ns3/tcp-l4-protocol.h"
39
#include "ns3/ipv4-static-routing.h"
41
#include "ns3/ipv4-static-routing.h"
42
#include "ns3/tcp-socket.h"
43
#include "ns3/ipv4-address.h"
40
44
41
#include <string>
45
#include <string>
42
#include <limits>
46
#include <limits>
43
47
44
using namespace ns3;
48
using namespace ns3;
45
49
50
NS_LOG_COMPONENT_DEFINE ("Ipv4ForwardingTest");
51
46
static void
52
static void
47
AddInternetStack (Ptr<Node> node)
53
AddInternetStack (Ptr<Node> node)
48
{
54
{
 Lines 62-67   AddInternetStack (Ptr<Node> node) Link Here 
62
  //UDP
68
  //UDP
63
  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
69
  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
64
  node->AggregateObject (udp);
70
  node->AggregateObject (udp);
71
  //UDP
72
  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
73
  node->AggregateObject (tcp);
65
}
74
}
66
75
67
76
 Lines 102-108   Ipv4ForwardingTest::DoSendData (Ptr<Socket> socket, std::string to) Link Here 
102
void
111
void
103
Ipv4ForwardingTest::SendData (Ptr<Socket> socket, std::string to)
112
Ipv4ForwardingTest::SendData (Ptr<Socket> socket, std::string to)
104
{
113
{
105
  m_receivedPacket = Create<Packet> ();
106
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
114
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
107
                                  &Ipv4ForwardingTest::DoSendData, this, socket, to);
115
                                  &Ipv4ForwardingTest::DoSendData, this, socket, to);
108
  Simulator::Run ();
116
  Simulator::Run ();
 Lines 181-199   Ipv4ForwardingTest::DoRun (void) Link Here 
181
  txDev->SetChannel (channel2);
189
  txDev->SetChannel (channel2);
182
190
183
  // Create the UDP sockets
191
  // Create the UDP sockets
184
  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
192
  Ptr<SocketFactory> udpRxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
185
  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
193
  Ptr<Socket> udpRxSocket = udpRxSocketFactory->CreateSocket ();
186
  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
194
  NS_TEST_EXPECT_MSG_EQ (udpRxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
187
  rxSocket->SetRecvCallback (MakeCallback (&Ipv4ForwardingTest::ReceivePkt, this));
195
  udpRxSocket->SetRecvCallback (MakeCallback (&Ipv4ForwardingTest::ReceivePkt, this));
188
196
189
  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
197
  Ptr<SocketFactory> udpTxSocketFactory = txNode->GetObject<UdpSocketFactory> ();
190
  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
198
  Ptr<Socket> udpTxSocket = udpTxSocketFactory->CreateSocket ();
191
  txSocket->SetAllowBroadcast (true);
199
  udpTxSocket->SetAllowBroadcast (true);
192
200
193
  // ------ Now the tests ------------
201
  // ------ Now the tests ------------
194
202
195
  // Unicast test
203
  // Unicast test
196
  SendData (txSocket, "10.0.0.2");
204
  SendData (udpTxSocket, "10.0.0.2");
197
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv4 Forwarding on");
205
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv4 Forwarding on");
198
206
199
  m_receivedPacket->RemoveAllByteTags ();
207
  m_receivedPacket->RemoveAllByteTags ();
 Lines 201-209   Ipv4ForwardingTest::DoRun (void) Link Here 
201
209
202
  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
210
  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
203
  ipv4->SetAttribute("IpForward", BooleanValue (false));
211
  ipv4->SetAttribute("IpForward", BooleanValue (false));
204
  SendData (txSocket, "10.0.0.2");
212
  SendData (udpTxSocket, "10.0.0.2");
205
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv4 Forwarding off");
213
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "IPv4 Forwarding off; no received packet");
214
215
  Simulator::Destroy ();
216
217
}
218
219
class Ipv4BindToNetDeviceTest : public TestCase
220
{
221
  Ptr<Packet> m_receivedPacket;
222
  void DoSendData (Ptr<Socket> socket, std::string to, int expectedReturnValue);
223
  void SendData (Ptr<Socket> socket, std::string to, int expectedReturnValue);
224
  //, std::string to, int expectedReturnValue
225
  void OnConnection (Ptr<Socket> socket
226
//                     , const ns3::Address&
227
                     );
228
229
public:
230
  virtual void DoRun (void);
231
  Ipv4BindToNetDeviceTest ();
232
233
  void ReceivePkt (Ptr<Socket> socket);
234
};
235
236
Ipv4BindToNetDeviceTest::Ipv4BindToNetDeviceTest ()
237
  : TestCase ("Test that Socket::BindToNetDevice () works")
238
{
239
}
240
241
void Ipv4BindToNetDeviceTest::ReceivePkt (Ptr<Socket> socket)
242
{
243
  NS_LOG_DEBUG ("Recv cb");
244
  uint32_t availableData;
245
  availableData = socket->GetRxAvailable ();
246
  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
247
  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
248
}
249
250
void
251
Ipv4BindToNetDeviceTest::DoSendData (Ptr<Socket> socket, std::string to, int expectedReturnValue)
252
{
253
  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
254
  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
255
                         expectedReturnValue, "SendTo failure");
256
}
257
258
void
259
Ipv4BindToNetDeviceTest::SendData (Ptr<Socket> socket, std::string to, int expectedReturnValue)
260
{
261
  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
262
                                  &Ipv4BindToNetDeviceTest::DoSendData, this, socket, to, expectedReturnValue);
263
}
264
265
266
void
267
Ipv4BindToNetDeviceTest::OnConnection (Ptr<Socket> socket
268
//                                       , const ns3::Address&
269
                                       )
270
{
271
    NS_LOG_DEBUG ("Send connection");
272
    int ret = socket->Send(Create<Packet> (123));
273
    NS_TEST_EXPECT_MSG_EQ ( ret, 0, "Could not send packet");
274
}
275
276
void
277
Ipv4BindToNetDeviceTest::DoRun (void)
278
{
279
  /** Create topology
280
  ________________________________________
281
  |      txNode                           |
282
  | txDev1 (10.0.0.1) | txDev2 (11.0.0.1) |
283
  |___________________|___________________|
284
          |                    |
285
          |                    |
286
          |                    |
287
          |                    |
288
  ________________________________________
289
  |      rxNode                           |
290
  | rxDev1 (10.0.0.2) | rxDev2 (11.0.0.2) |
291
  |___________________|___________________|
292
  **/
293
294
  // Receiver Node
295
  Ptr<Node> rxNode = CreateObject<Node> ();
296
  AddInternetStack (rxNode);
297
  Ptr<SimpleNetDevice> rxDev1;
298
  Ptr<SimpleNetDevice> rxDev2;
299
  { // first interface
300
    rxDev1 = CreateObject<SimpleNetDevice> ();
301
    rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
302
    rxNode->AddDevice (rxDev1);
303
    Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
304
    uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
305
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
306
    ipv4->AddAddress (netdev_idx, ipv4Addr);
307
    ipv4->SetUp (netdev_idx);
308
  }
309
  { // second interface
310
    rxDev2 = CreateObject<SimpleNetDevice> ();
311
    rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
312
    rxNode->AddDevice (rxDev2);
313
    Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
314
    uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
315
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("11.0.0.2"), Ipv4Mask (0xffff0000U));
316
    ipv4->AddAddress (netdev_idx, ipv4Addr);
317
    ipv4->SetUp (netdev_idx);
318
  }
319
320
  // Sender Node
321
  Ptr<Node> txNode = CreateObject<Node> ();
322
  AddInternetStack (txNode);
323
  Ptr<SimpleNetDevice> txDev1;
324
  Ptr<SimpleNetDevice> txDev2;
325
  {
326
    // first interface
327
    txDev1 = CreateObject<SimpleNetDevice> ();
328
    txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
329
    txNode->AddDevice (txDev1);
330
    Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
331
    uint32_t netdev_idx = ipv4->AddInterface (txDev1);
332
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
333
    ipv4->AddAddress (netdev_idx, ipv4Addr);
334
    ipv4->SetUp (netdev_idx);
335
  }
336
  {
337
    // second interface
338
    txDev2 = CreateObject<SimpleNetDevice> ();
339
    txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
340
    txNode->AddDevice (txDev2);
341
    Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
342
    uint32_t netdev_idx = ipv4->AddInterface (txDev2);
343
    Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask (0xffff0000U));
344
    ipv4->AddAddress (netdev_idx, ipv4Addr);
345
    ipv4->SetUp (netdev_idx);
346
  }
347
348
  // link the two nodes
349
  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
350
  rxDev1->SetChannel (channel1);
351
  txDev1->SetChannel (channel1);
352
353
  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
354
  rxDev2->SetChannel (channel2);
355
  txDev2->SetChannel (channel2);
356
357
  // Create the UDP sockets
358
  Ptr<SocketFactory> udpRxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
359
  Ptr<Socket> udpRxSocket = udpRxSocketFactory->CreateSocket ();
360
  NS_TEST_EXPECT_MSG_EQ (udpRxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
361
  // Must call BindToNetDevice() after Bind()
362
  udpRxSocket->SetRecvCallback (MakeCallback (&Ipv4BindToNetDeviceTest::ReceivePkt, this));
363
364
  Ptr<SocketFactory> udpTxSocketFactory = txNode->GetObject<UdpSocketFactory> ();
365
  Ptr<Socket> udpTxSocket = udpTxSocketFactory->CreateSocket ();
366
  udpTxSocket->SetAllowBroadcast (true);
367
368
  // Create the TCP sockets
369
  Ptr<SocketFactory> tcpRxSocketFactory = rxNode->GetObject<TcpSocketFactory> ();
370
  Ptr<Socket> tcpRxSocket = tcpRxSocketFactory->CreateSocket ();
371
  NS_TEST_EXPECT_MSG_EQ (tcpRxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
372
  // Must call BindToNetDevice() after Bind()
373
  tcpRxSocket->SetRecvCallback (MakeCallback (&Ipv4BindToNetDeviceTest::ReceivePkt, this));
374
375
  Ptr<SocketFactory> tcpTxSocketFactory = txNode->GetObject<UdpSocketFactory> ();
376
//  Ptr<TcpSocket> tcpTxSocket = DynamicCast<TcpSocket>(tcpTxSocketFactory->CreateSocket () );
377
  Ptr<Socket> tcpTxSocket = tcpTxSocketFactory->CreateSocket ();
378
//  tcpTxSocket->SetAllowBroadcast (true);
379
380
  // ------ Now the UDP tests ------------
381
382
  // Test that data is successful when RxNode binds to rxDev1 and TxNode binds
383
  // to txDev1
384
  NS_LOG_DEBUG ("Bind test case 1");
385
  udpRxSocket->BindToNetDevice (rxDev1);
386
  udpTxSocket->BindToNetDevice (txDev1);
387
  SendData (udpTxSocket, "10.0.0.2", 123);  // 123 is the expected Send() return value
388
  Simulator::Run ();
389
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Correctly bound NetDevices");
390
  m_receivedPacket = 0;
391
392
  // All three other bind combinations should fail 
393
  
394
  NS_LOG_DEBUG ("Bind test case 2");
395
  udpTxSocket = udpTxSocketFactory->CreateSocket ();
396
  udpRxSocket->BindToNetDevice (rxDev1);
397
  udpTxSocket->BindToNetDevice (txDev2);
398
  SendData (udpTxSocket, "10.0.0.2", -1);
399
  Simulator::Run ();
400
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
401
402
  NS_LOG_DEBUG ("Bind test case 3");
403
  udpTxSocket = udpTxSocketFactory->CreateSocket ();
404
  udpRxSocket->BindToNetDevice (rxDev2);
405
  udpTxSocket->BindToNetDevice (txDev2);
406
  SendData (udpTxSocket, "10.0.0.2", -1);
407
  Simulator::Run ();
408
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
409
410
  NS_LOG_DEBUG ("Bind test case 4");
411
  udpTxSocket = udpTxSocketFactory->CreateSocket ();
412
  udpRxSocket->BindToNetDevice (rxDev2);
413
  udpTxSocket->BindToNetDevice (txDev1);
414
  SendData (udpTxSocket, "10.0.0.2", 123);
415
  Simulator::Run ();
416
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
417
418
  // ------ Now the TCP tests ------------
419
  
420
  // Test that data is successful when RxNode binds to rxDev1 and TxNode binds
421
  // to txDev1
422
  NS_LOG_DEBUG ("Bind test case 1");
423
  InetSocketAddress serverAddr ("11.0.0.2", 1234 );
424
  InetSocketAddress clientAddr ("11.0.0.1", 4321 );
425
  
426
//  tcpRxSocket->BindToNetDevice (rxDev1);
427
//  tcpTxSocket->BindToNetDevice (txDev1);
428
  tcpTxSocket->Bind( clientAddr );
429
//  tcpRxSocket->SetAcceptCallback( MakeNullCallback<bool, Ptr<Socket>, const ns3::Address& > (),
430
//                                 MakeCallback(&Ipv4BindToNetDeviceTest::OnConnection, this) );
431
432
  tcpTxSocket->SetConnectCallback ( MakeCallback(&Ipv4BindToNetDeviceTest::OnConnection, this),
433
                                   MakeNullCallback<void, Ptr<Socket> > ()
434
                                  );
435
436
  tcpTxSocket->Connect( serverAddr );
437
  //! Make sure packet can only travel through channel2
438
  channel1->BlackList(txDev1, rxDev1);
439
  
440
//  SendData (tcpTxSocket, "10.0.0.2", 123);  // 123 is the expected Send() return value
441
  
442
  Simulator::Run ();
443
  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Correctly bound NetDevices");
444
  m_receivedPacket = 0;
445
446
  // All three other bind combinations should fail 
447
  
448
//  NS_LOG_DEBUG ("Bind test case 2");
449
//  tcpTxSocket = tcpTxSocketFactory->CreateSocket ();
450
//  tcpRxSocket->BindToNetDevice (rxDev1);
451
//  tcpTxSocket->BindToNetDevice (txDev2);
452
//  SendData (tcpTxSocket, "10.0.0.2", -1);
453
//  Simulator::Run ();
454
//  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
455
//
456
//  NS_LOG_DEBUG ("Bind test case 3");
457
//  tcpTxSocket = tcpTxSocketFactory->CreateSocket ();
458
//  tcpRxSocket->BindToNetDevice (rxDev2);
459
//  tcpTxSocket->BindToNetDevice (txDev2);
460
//  SendData (tcpTxSocket, "10.0.0.2", -1);
461
//  Simulator::Run ();
462
//  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
463
//
464
//  NS_LOG_DEBUG ("Bind test case 4");
465
//  tcpTxSocket = tcpTxSocketFactory->CreateSocket ();
466
//  tcpRxSocket->BindToNetDevice (rxDev2);
467
//  tcpTxSocket->BindToNetDevice (txDev1);
468
//  SendData (tcpTxSocket, "10.0.0.2", 123);
469
//  Simulator::Run ();
470
//  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket, 0, "No received packet");
206
471
472
  
207
  Simulator::Destroy ();
473
  Simulator::Destroy ();
208
474
209
}
475
}
 Lines 217-221   public: Link Here 
217
  Ipv4ForwardingTestSuite () : TestSuite ("ipv4-forwarding", UNIT)
483
  Ipv4ForwardingTestSuite () : TestSuite ("ipv4-forwarding", UNIT)
218
  {
484
  {
219
    AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
485
    AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
486
    AddTestCase (new Ipv4BindToNetDeviceTest, TestCase::QUICK);
220
  }
487
  }
221
} g_ipv4forwardingTestSuite;
488
} g_ipv4forwardingTestSuite;

Return to bug 1041