A Discrete-Event Network Simulator
API
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 using namespace ns3;
25 
33 {
34 public:
36  virtual void DoRun (void);
37  virtual void DoTeardown (void);
38 };
39 
41  : TestCase ("Make sure the network number allocator is working on some of network prefixes.")
42 {
43 }
44 void
46 {
48 }
49 void
51 {
52  Ipv6Address network;
53 
54  Ipv6AddressGenerator::Init (Ipv6Address ("1::0:0:0"), Ipv6Prefix ("FFFF::0"),
55  Ipv6Address ("::"));
56  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF::0"));
57  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("1::0:0:0"), "network should equal the initialized network for given prefix");
58  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF::0"));
59  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("2::0:0:0"), "network should equal next network");
60 
61  Ipv6AddressGenerator::Init (Ipv6Address ("0:1::0:0"),
62  Ipv6Prefix ("FFFF:FFFF::0"), Ipv6Address ("::"));
63  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF:FFFF::0"));
64  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:1::0"), "network should equal the initialized network for given prefix");
65  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF::0"));
66  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:2::0"), "network should equal next network");
67 
68  Ipv6AddressGenerator::Init (Ipv6Address ("0:0:1::0"),
69  Ipv6Prefix ("FFFF:FFFF:FFFF::0"), Ipv6Address ("::0"));
70  network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
71  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:0:1::0"), "network should equal the initialized network for given prefix");
72  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
73  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("0:0:2::0"), "network should equal next network");
74 
75 }
76 
84 {
85 public:
87 private:
88  virtual void DoRun (void);
89  virtual void DoTeardown (void);
90 };
91 
93  : TestCase ("Sanity check on allocation of addresses")
94 {
95 }
96 
97 void
99 {
101 
102  Ipv6AddressGenerator::Init (Ipv6Address ("2001::0"), Ipv6Prefix (64));
103  address = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix (64));
104  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001::0"), "address should equal the initialized address for given prefix");
105  Ipv6AddressGenerator::NextNetwork (Ipv6Prefix (64));
106  address = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix (64));
107  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::0"), "address should equal the initialized address for given prefix");
108  address = Ipv6AddressGenerator::GetAddress (Ipv6Prefix (64));
109  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
110  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (64));
111  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
112  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (64));
113  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::2"), "address should equal the initialized address for given prefix");
114 
115  Ipv6AddressGenerator::Init (Ipv6Address ("1::"), Ipv6Prefix ("FFFF::"),
116  Ipv6Address ("::3"));
117  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
118  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::3"), "address should equal initialized address for given prefix");
119  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
120  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::4"), "address should equal next address");
121 
122 }
123 
124 void
126 {
128  Simulator::Destroy ();
129 }
130 
131 
139 {
140 public:
142  virtual void DoRun (void);
143  virtual void DoTeardown (void);
144 };
145 
147  : TestCase ("Make sure Network and address allocation play together.")
148 {
149 }
150 
151 void
153 {
155  Simulator::Destroy ();
156 }
157 
158 void
160 {
162  Ipv6Address network;
163 
164  Ipv6AddressGenerator::Init (Ipv6Address ("3::"), Ipv6Prefix ("FFFF::"),
165  Ipv6Address ("::3"));
166  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
167  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("3::3"), "address should equal initialized address for given prefix");
168  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
169  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("3::4"), "address should equal next address for given prefix");
170 
171  network = Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF::"));
172  NS_TEST_EXPECT_MSG_EQ (network, Ipv6Address ("4::0"), "address should equal next address for given prefix");
173  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
174  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("4::3"), "address should equal next address for given prefix");
175 
176 }
177 
185 {
186 public:
188 private:
189  virtual void DoRun (void);
190  virtual void DoTeardown (void);
191 };
192 
194  : TestCase ("A typical real-world example")
195 {
196 }
197 
198 void
200 {
202 }
203 
204 void
206 {
208 
209  Ipv6AddressGenerator::Init (Ipv6Address ("2001:0AB8::"),
210  Ipv6Prefix ("FFFF:FFFF:FFFF::0"), Ipv6Address ("::3"));
211  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
212  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:3"), "address should equal initialized address for given prefix");
213  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
214  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:4"), "address should equal next address for given prefix");
215  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
216  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:5"), "address should equal next address for given prefix");
217  //
218  // Allocate the next network based on the prefix passed in, which should
219  // be 2001:0AB0:0001
220  //
221  Ipv6AddressGenerator::NextNetwork (Ipv6Prefix ("FFFF:FFFF:FFFF::0"));
222  //
223  // reset first address to be allocated back to ::0:3
224  //
225  Ipv6AddressGenerator::InitAddress (Ipv6Address ("::3"),
226  Ipv6Prefix (48));
227  //
228  // The first address we should get is the network and address ORed
229  //
230  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
231  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8:1::3"), "address should equal initialized address for given prefix");
232 }
233 
241 {
242 public:
244 private:
245  void DoRun (void);
246  void DoTeardown (void);
247 };
248 
250  : TestCase ("Make sure that the address collision logic works.")
251 {
252 }
253 
254 void
256 {
258  Simulator::Destroy ();
259 }
260 void
262 {
263  Ipv6AddressGenerator::AddAllocated ("0::0:5");
264  Ipv6AddressGenerator::AddAllocated ("0::0:10");
265  Ipv6AddressGenerator::AddAllocated ("0::0:15");
266  Ipv6AddressGenerator::AddAllocated ("0::0:20");
267 
268  Ipv6AddressGenerator::AddAllocated ("0::0:4");
269  Ipv6AddressGenerator::AddAllocated ("0::0:3");
270  Ipv6AddressGenerator::AddAllocated ("0::0:2");
271  Ipv6AddressGenerator::AddAllocated ("0::0:1");
272 
273  Ipv6AddressGenerator::AddAllocated ("0::0:6");
274  Ipv6AddressGenerator::AddAllocated ("0::0:7");
275  Ipv6AddressGenerator::AddAllocated ("0::0:8");
276  Ipv6AddressGenerator::AddAllocated ("0::0:9");
277 
278  Ipv6AddressGenerator::AddAllocated ("0::0:11");
279  Ipv6AddressGenerator::AddAllocated ("0::0:12");
280  Ipv6AddressGenerator::AddAllocated ("0::0:13");
281  Ipv6AddressGenerator::AddAllocated ("0::0:14");
282 
283  Ipv6AddressGenerator::AddAllocated ("0::0:19");
284  Ipv6AddressGenerator::AddAllocated ("0::0:18");
285  Ipv6AddressGenerator::AddAllocated ("0::0:17");
286  Ipv6AddressGenerator::AddAllocated ("0::0:16");
287 
288  Ipv6AddressGenerator::TestMode ();
289  bool added = Ipv6AddressGenerator::AddAllocated ("0::0:21");
290  NS_TEST_EXPECT_MSG_EQ (added, true, "address should get allocated");
291 
292  added = Ipv6AddressGenerator::AddAllocated ("0::0:4");
293  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
294 
295  added = Ipv6AddressGenerator::AddAllocated ("0::0:9");
296  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
297 
298  added = Ipv6AddressGenerator::AddAllocated ("0::0:16");
299  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
300 
301  added = Ipv6AddressGenerator::AddAllocated ("0::0:21");
302  NS_TEST_EXPECT_MSG_EQ (added, false, "address should not get allocated");
303 }
304 
305 
313 {
314 public:
316  : TestSuite ("ipv6-address-generator")
317  {
318  AddTestCase (new NetworkNumber6AllocatorTestCase (), TestCase::QUICK);
319  AddTestCase (new AddressAllocator6TestCase (), TestCase::QUICK);
320  AddTestCase (new NetworkAndAddress6TestCase (), TestCase::QUICK);
321  AddTestCase (new ExampleAddress6GeneratorTestCase (), TestCase::QUICK);
322  AddTestCase (new AddressCollision6TestCase (), TestCase::QUICK);
323  }
324 };
325 
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void DoRun(void)
Implementation to actually run this TestCase.
void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
IPv6 example of an address generator Test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
IPv6 network number and address allocator Test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
IPv6 network number allocator Test.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Describes an IPv6 address.
Definition: ipv6-address.h:50
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
void Reset(void)
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition: config.cc:820
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:240
static Ipv6AddressGeneratorTestSuite g_ipv6AddressGeneratorTestSuite
Static variable for test initialization.
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.