A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-address-generator-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  * Copyright (c) 2011 Atishay Jain
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include "ns3/test.h"
21 #include "ns3/ipv6-address-generator.h"
22 #include "ns3/simulation-singleton.h"
23 
24 namespace ns3 {
25 
27 {
28 public:
30  virtual void DoRun (void);
31  virtual void DoTeardown (void);
32 };
33 
35  : TestCase ("Make sure the network number allocator is working on some of network prefixes.")
36 {
37 }
38 void
40 {
42 }
43 void
45 {
46  Ipv6Address network;
47 
48  Ipv6AddressGenerator::Init (Ipv6Address ("1::0:0:0"), Ipv6Prefix ("FFFF::0"),
49  Ipv6Address ("::"));
50  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF::0"));
51  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("1::0:0:0"), "network should equal the initialized network for given prefix");
52  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF::0"));
53  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("2::0:0:0"), "network should equal next network");
54 
56  Ipv6Prefix ("FFFF:FFFF::0"), Ipv6Address ("::"));
57  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF:FFFF::0"));
58  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:1::0"), "network should equal the initialized network for given prefix");
59  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF::0"));
60  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:2::0"), "network should equal next network");
61 
63  Ipv6Prefix ("FFFF:FFFF:FFFF::0"), Ipv6Address ("::0"));
64  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
65  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:0:1::0"), "network should equal the initialized network for given prefix");
66  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
67  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:0:2::0"), "network should equal next network");
68 
69 }
70 
72 {
73 public:
75 private:
76  virtual void DoRun (void);
77  virtual void DoTeardown (void);
78 };
79 
81  : TestCase ("Sanity check on allocation of addresses")
82 {
83 }
84 
85 void
87 {
88  Ipv6Address address;
89 
92  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001::0"), "address should equal the initialized address for given prefix");
95  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::0"), "address should equal the initialized address for given prefix");
97  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
99  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
101  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::2"), "address should equal the initialized address for given prefix");
102 
104  Ipv6Address ("::3"));
106  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::3"), "address should equal initialized address for given prefix");
108  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::4"), "address should equal next address");
109 
110 }
111 
112 void
114 {
117 }
118 
119 
121 {
122 public:
124  virtual void DoRun (void);
125  virtual void DoTeardown (void);
126 };
127 
129  : TestCase ("Make sure Network and address allocation play together.")
130 {
131 }
132 
133 void
135 {
138 }
139 
140 void
142 {
143  Ipv6Address address;
144  Ipv6Address network;
145 
147  Ipv6Address ("::3"));
149  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("3::3"), "address should equal initialized address for given prefix");
151  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("3::4"), "address should equal next address for given prefix");
152 
153  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF::"));
154  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("4::0"), "address should equal next address for given prefix");
156  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("4::3"), "address should equal next address for given prefix");
157 
158 }
159 
161 {
162 public:
164 private:
165  virtual void DoRun (void);
166  virtual void DoTeardown (void);
167 };
168 
170  : TestCase ("A typical real-world example")
171 {
172 }
173 
174 void
176 {
178 }
179 
180 void
182 {
183  Ipv6Address address;
184 
185  Ipv6AddressGenerator::Init (Ipv6Address ("2001:0AB8::"),
186  Ipv6Prefix ("FFFF:FFFF:FFFF::0"), Ipv6Address ("::3"));
188  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:3"), "address should equal initialized address for given prefix");
190  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:4"), "address should equal next address for given prefix");
192  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:5"), "address should equal next address for given prefix");
193  //
194  // Allocate the next network based on the prefix passed in, which should
195  // be 2001:0AB0:0001
196  //
197  Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
198  //
199  // reset first address to be allocated back to ::0:3
200  //
202  Ipv6Prefix (48));
203  //
204  // The first address we should get is the network and address ORed
205  //
207  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8:1::3"), "address should equal initialized address for given prefix");
208 }
209 
211 {
212 public:
214 private:
215  void DoRun (void);
216  void DoTeardown (void);
217 };
218 
220  : TestCase ("Make sure that the address collision logic works.")
221 {
222 }
223 
224 void
226 {
229 }
230 void
232 {
237 
242 
247 
252 
257 
259  bool added = Ipv6AddressGenerator::AddAllocated ("0::0:21");
260  NS_TEST_EXPECT_MSG_EQ (added, true, "address should get allocated");
261 
262  added = Ipv6AddressGenerator::AddAllocated ("0::0:4");
263  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
264 
265  added = Ipv6AddressGenerator::AddAllocated ("0::0:9");
266  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
267 
268  added = Ipv6AddressGenerator::AddAllocated ("0::0:16");
269  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
270 
271  added = Ipv6AddressGenerator::AddAllocated ("0::0:21");
272  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
273 }
274 
275 
277 {
278 public:
280  : TestSuite ("ipv6-address-generator")
281  {
287  }
289 
290 } // namespace ns3
291