A Discrete-Event Network Simulator
API
ipv6-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Strasbourg University
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  * Authors: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  * Faker Moatamri <faker.moatamri@sophia.inria.fr>
20  */
21 #include "ns3/simulator.h"
22 #include "ns3/test.h"
23 #include "ns3/log.h"
24 #include "ns3/boolean.h"
25 #include "ns3/inet6-socket-address.h"
26 #include "ns3/node.h"
27 #include "ns3/simple-net-device.h"
28 
29 #include "ns3/ipv6-interface.h"
30 #include "ns3/ipv6-l3-protocol.h"
31 #include "ns3/icmpv6-l4-protocol.h"
32 
33 using namespace ns3;
34 
42 {
43 public:
45 
46  virtual ~Ipv6L3ProtocolTestCase ();
47  virtual void DoRun ();
48 };
49 
51  TestCase ("Verify the IPv6 layer 3 protocol")
52 {
53 }
54 
56 {
57 }
58 
59 void
61 {
62  Ptr<Node> node = CreateObject<Node> ();
63  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
64  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
66  Ptr<Ipv6Interface> interface2 = CreateObject<Ipv6Interface> ();
67  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
68  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
69  uint32_t index = 0;
70 
71  /* init */
72  icmpv6->SetAttribute ("DAD", BooleanValue (false));
73  node->AggregateObject (ipv6);
74  node->AggregateObject (icmpv6);
75  ipv6->Insert (icmpv6);
76 
77  /* first real interface (loopback is also installed) */
78  node->AddDevice (device);
79  interface->SetDevice (device);
80  interface->SetNode (node);
81  index = ipv6->AddIpv6Interface (interface);
82  NS_TEST_ASSERT_MSG_EQ (index, 1,"The index is not 1??");
83 
84  /* second interface */
85  node->AddDevice (device2);
86  interface2->SetDevice (device2);
87  interface2->SetNode (node);
88  index = ipv6->AddIpv6Interface (interface2);
89  NS_TEST_ASSERT_MSG_EQ (index, 2, "The index is not 2??");
90 
91  Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress ();
92  NS_TEST_ASSERT_MSG_EQ (ifaceAddr.GetAddress ().IsLinkLocal (), true,
93  "Should be link local??");
94 
95  interface->SetUp ();
96  NS_TEST_ASSERT_MSG_EQ (interface->GetNAddresses (), 1,
97  "interface has always a link-local address"); /* interface has always a link-local address */
98 
99  interface2->SetUp ();
100 
102  "2001:1234:5678:9000::1", Ipv6Prefix (64));
103  interface->AddAddress (ifaceAddr1);
105  "2001:ffff:5678:9000::1", Ipv6Prefix (64));
106  interface->AddAddress (ifaceAddr2);
107 
109  "2001:ffff:5678:9001::2", Ipv6Prefix (64));
110  interface2->AddAddress (ifaceAddr3);
111 
112  uint32_t num = interface->GetNAddresses ();
113  NS_TEST_ASSERT_MSG_EQ (num, 3, "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
114 
115  num = interface2->GetNAddresses ();
116  NS_TEST_ASSERT_MSG_EQ (num, 2, "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
117 
118  interface->RemoveAddress (2);
119  num = interface->GetNAddresses ();
120  NS_TEST_ASSERT_MSG_EQ (num, 2, "Number of addresses should be 2??");
121 
122  Ipv6InterfaceAddress output = interface->GetAddress (1);
123  NS_TEST_ASSERT_MSG_EQ (ifaceAddr1, output,
124  "Should be the interface address 1?");
125 
126  index = ipv6->GetInterfaceForPrefix ("2001:1234:5678:9000::0",
127  Ipv6Prefix (64));
128  NS_TEST_ASSERT_MSG_EQ (index, 1, "We should get one address??"); /* link-local address is always index 0 */
129 
130  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9001::2");
131  NS_TEST_ASSERT_MSG_EQ (index, 2, "Number of addresses should be 2??");
132 
133  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9000::1"); /* address we just remove */
134  NS_TEST_ASSERT_MSG_EQ (index, (uint32_t) -1, "Address should not be found??");
135 
136  /* Test Ipv6Interface()::RemoveAddress(address) */
137  output = interface->RemoveAddress (Ipv6Address ("2001:1234:5678:9000::1"));
138  NS_TEST_ASSERT_MSG_EQ (ifaceAddr1, output, "Wrong Interface Address Removed??");
139  num = interface->GetNAddresses ();
140  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
141 
142  /* Remove a non-existent Address */
143  output = interface->RemoveAddress (Ipv6Address ("2001:1234:5678:9000::1"));
145  "Removed non-existent address??");
146  num = interface->GetNAddresses ();
147  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
148 
149  /* Remove a loopback Address */
150  output = interface->RemoveAddress (Ipv6Address::GetLoopback ());
152  "Able to remove loopback address??");
153  num = interface->GetNAddresses ();
154  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
155 
156  /* Test Ipv6Address::RemoveAddress(index, address) */
157  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9001::2");
158  bool result = ipv6->RemoveAddress (index, Ipv6Address
159  ("2001:ffff:5678:9001::2"));
160  NS_TEST_ASSERT_MSG_EQ (result, true, "Unable to remove Address??");
161  num = interface2->GetNAddresses ();
162  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
163 
164  /* Remove a non-existent Address */
165  result = ipv6->RemoveAddress (index, Ipv6Address
166  ("2001:ffff:5678:9001::2"));
167  NS_TEST_ASSERT_MSG_EQ (result, false, "Removed Non-existent address??");
168  num = interface2->GetNAddresses ();
169  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
170 
171  /* Remove a loopback Address */
172  result = ipv6->RemoveAddress (index, Ipv6Address::GetLoopback ());
173  NS_TEST_ASSERT_MSG_EQ (result, false, "Able to remove loopback address??");
174  num = interface2->GetNAddresses ();
175  NS_TEST_ASSERT_MSG_EQ (num, 1, "Number of addresses should be 1??");
176 
177  Simulator::Destroy ();
178 } //end DoRun
179 
180 
188 {
189 public:
191  TestSuite ("ipv6-protocol", UNIT)
192  {
193  AddTestCase (new Ipv6L3ProtocolTestCase (), TestCase::QUICK);
194  }
195 };
196 
Ipv6Address GetAddress() const
Get the IPv6 address.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
A suite of tests to run.
Definition: test.h:1342
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
virtual ~Ipv6L3ProtocolTestCase()
Definition: ipv6-test.cc:55
IPv6 address associated with an interface.
encapsulates test code
Definition: test.h:1155
static IPv6L3ProtocolTestSuite g_ipv6protocolTestSuite
Static variable for test initialization.
Definition: ipv6-test.cc:197
virtual void DoRun()
Implementation to actually run this TestCase.
Definition: ipv6-test.cc:60
The IPv6 representation of a network interface.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Describes an IPv6 address.
Definition: ipv6-address.h:49
IPv6 TestSuite.
Definition: ipv6-test.cc:187
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
Ptr< T > CreateObject(void)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:528
This test suite implements a Unit Test.
Definition: test.h:1352