A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 namespace ns3 {
35 {
36 public:
41 
45  virtual
47 
52  virtual void
53  DoRun ();
54 };
56  TestCase ("Verify the IPv6 layer 3 protocol")
57 {
58 }
60 {
61 }
62 void
64 {
65  Ptr<Node> node = CreateObject<Node> ();
66  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
67  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
68  Ptr<Ipv6Interface> interface = CreateObject<Ipv6Interface> ();
69  Ptr<Ipv6Interface> interface2 = CreateObject<Ipv6Interface> ();
70  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
71  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
72  uint32_t index = 0;
73 
74  /* init */
75  icmpv6->SetAttribute ("DAD", BooleanValue (false));
76  node->AggregateObject (ipv6);
77  node->AggregateObject (icmpv6);
78  ipv6->Insert (icmpv6);
79 
80  /* first real interface (loopback is also installed) */
81  node->AddDevice (device);
82  interface->SetDevice (device);
83  interface->SetNode (node);
84  index = ipv6->AddIpv6Interface (interface);
85  NS_TEST_ASSERT_MSG_EQ (index, 1,"The index is not 1??");
86 
87  /* second interface */
88  node->AddDevice (device2);
89  interface2->SetDevice (device2);
90  interface2->SetNode (node);
91  index = ipv6->AddIpv6Interface (interface2);
92  NS_TEST_ASSERT_MSG_EQ (index, 2, "The index is not 2??");
93 
94  Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress ();
95  NS_TEST_ASSERT_MSG_EQ (ifaceAddr.GetAddress ().IsLinkLocal (), true,
96  "Should be link local??");
97 
98  interface->SetUp ();
99  NS_TEST_ASSERT_MSG_EQ (interface->GetNAddresses (), 1,
100  "interface has always a link-local address"); /* interface has always a link-local address */
101 
102  interface2->SetUp ();
103 
105  "2001:1234:5678:9000::1", Ipv6Prefix (64));
106  interface->AddAddress (ifaceAddr1);
108  "2001:ffff:5678:9000::1", Ipv6Prefix (64));
109  interface->AddAddress (ifaceAddr2);
110 
112  "2001:ffff:5678:9001::2", Ipv6Prefix (64));
113  interface2->AddAddress (ifaceAddr3);
114 
115  uint32_t num = interface->GetNAddresses ();
116  NS_TEST_ASSERT_MSG_EQ (num, 3, "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
117 
118  num = interface2->GetNAddresses ();
119  NS_TEST_ASSERT_MSG_EQ (num, 2, "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
120 
121  interface->RemoveAddress (2);
122  num = interface->GetNAddresses ();
123  NS_TEST_ASSERT_MSG_EQ (num, 2, "Number of addresses should be 2??");
124 
125  Ipv6InterfaceAddress output = interface->GetAddress (1);
126  NS_TEST_ASSERT_MSG_EQ (ifaceAddr1, output,
127  "Should be the interface address 1?");
128 
129  index = ipv6->GetInterfaceForPrefix ("2001:1234:5678:9000::0",
130  Ipv6Prefix (64));
131  NS_TEST_ASSERT_MSG_EQ (index, 1, "We should get one address??"); /* link-local address is always index 0 */
132 
133  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9001::2");
134  NS_TEST_ASSERT_MSG_EQ (index, 2, "Number of addresses should be 2??");
135 
136  index = ipv6->GetInterfaceForAddress ("2001:ffff:5678:9000::1"); /* address we just remove */
137  NS_TEST_ASSERT_MSG_EQ (index, (uint32_t) -1, "Address should not be found??");
139 } //end DoRun
140 static class IPv6L3ProtocolTestSuite : public TestSuite
141 {
142 public:
144  TestSuite ("ipv6-protocol", UNIT)
145  {
147  }
149 } // namespace ns3