A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv6-test.cc
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 namespace ns3 {
34 
36 {
37 public:
42 
46  virtual
48 
53  virtual void
54  DoRun ();
55 };
57  TestCase ("Verify the IPv6 layer 3 protocol")
58 {
59 }
61 {
62 }
63 void
65 {
66  Ptr<Node> node = CreateObject<Node> ();
67  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
68  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
69  Ptr<Ipv6Interface> interface = CreateObject<Ipv6Interface> ();
70  Ptr<Ipv6Interface> interface2 = CreateObject<Ipv6Interface> ();
71  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
72  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
73  uint32_t index = 0;
74 
75  /* init */
76  icmpv6->SetAttribute ("DAD", BooleanValue (false));
77  node->AggregateObject (ipv6);
78  node->AggregateObject (icmpv6);
79  ipv6->Insert (icmpv6);
80 
81  /* first real interface (loopback is also installed) */
82  node->AddDevice (device);
83  interface->SetDevice (device);
84  interface->SetNode (node);
85  index = ipv6->AddIpv6Interface (interface);
86  NS_TEST_ASSERT_MSG_EQ (index, 1,"The index is not 1??");
87 
88  /* second interface */
89  node->AddDevice (device2);
90  interface2->SetDevice (device2);
91  interface2->SetNode (node);
92  index = ipv6->AddIpv6Interface (interface2);
93  NS_TEST_ASSERT_MSG_EQ (index, 2, "The index is not 2??");
94 
95  Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress ();
96  NS_TEST_ASSERT_MSG_EQ (ifaceAddr.GetAddress ().IsLinkLocal (), true,
97  "Should be link local??");
98 
99  interface->SetUp ();
100  NS_TEST_ASSERT_MSG_EQ (interface->GetNAddresses (), 1,
101  "interface has always a link-local address"); /* interface has always a link-local address */
102 
103  interface2->SetUp ();
104 
106  "2001:1234:5678:9000::1", Ipv6Prefix (64));
107  interface->AddAddress (ifaceAddr1);
109  "2001:ffff:5678:9000::1", Ipv6Prefix (64));
110  interface->AddAddress (ifaceAddr2);
111 
113  "2001:ffff:5678:9001::2", Ipv6Prefix (64));
114  interface2->AddAddress (ifaceAddr3);
115 
116  uint32_t num = interface->GetNAddresses ();
117  NS_TEST_ASSERT_MSG_EQ (num, 3, "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
118 
119  num = interface2->GetNAddresses ();
120  NS_TEST_ASSERT_MSG_EQ (num, 2, "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
121 
122  interface->RemoveAddress (2);
123  num = interface->GetNAddresses ();
124  NS_TEST_ASSERT_MSG_EQ (num, 2, "Number of addresses should be 2??");
125 
126  Ipv6InterfaceAddress output = interface->GetAddress (1);
127  NS_TEST_ASSERT_MSG_EQ (ifaceAddr1, output,
128  "Should be the interface address 1?");
129 
130  index = ipv6->GetInterfaceForPrefix ("2001:1234:5678:9000::0",
131  Ipv6Prefix (64));
132  NS_TEST_ASSERT_MSG_EQ (index, 1, "We should get one address??"); /* link-local address is always index 0 */
133 
134  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9001::2");
135  NS_TEST_ASSERT_MSG_EQ (index, 2, "Number of addresses should be 2??");
136 
137  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9000::1"); /* address we just remove */
138  NS_TEST_ASSERT_MSG_EQ (index, (uint32_t) -1, "Address should not be found??");
140 } //end DoRun
141 static class IPv6L3ProtocolTestSuite : public TestSuite
142 {
143 public:
145  TestSuite ("ipv6-protocol", UNIT)
146  {
147  AddTestCase (new Ipv6L3ProtocolTestCase (), TestCase::QUICK);
148  }
149 } g_ipv6protocolTestSuite;
150 
151 } // namespace ns3
Hold a bool native type.
Definition: boolean.h:38
A suite of tests to run.
Definition: test.h:962
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
IPv6 address associated with an interface.
encapsulates test code
Definition: test.h:834
virtual ~Ipv6L3ProtocolTestCase()
Destructor.
Definition: ipv6-test.cc:60
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
Ipv6Address GetAddress() const
Get the IPv6 address.
virtual void DoRun()
Run unit tests for this class.
Definition: ipv6-test.cc:64
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
static void Destroy(void)
Definition: simulator.cc:121
Ipv6L3ProtocolTestCase()
Constructor.
Definition: ipv6-test.cc:56
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
Definition: ipv6-address.h:326