A Discrete-Event Network Simulator
API
ipv6-address-generator-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 University of Washington
3  * Copyright (c) 2011 Atishay Jain
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 
19 #include "ns3/ipv6-address-generator.h"
20 #include "ns3/simulation-singleton.h"
21 #include "ns3/test.h"
22 
23 using namespace ns3;
24 
31 {
32  public:
34  void DoRun() override;
35  void DoTeardown() override;
36 };
37 
39  : TestCase("Make sure the network number allocator is working on some of network prefixes.")
40 {
41 }
42 
43 void
45 {
47 }
48 
49 void
51 {
52  Ipv6Address network;
53 
54  Ipv6AddressGenerator::Init(Ipv6Address("1::0:0:0"), Ipv6Prefix("FFFF::0"), Ipv6Address("::"));
55  network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF::0"));
56  NS_TEST_EXPECT_MSG_EQ(network,
57  Ipv6Address("1::0:0:0"),
58  "network should equal the initialized network for given prefix");
59  network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF::0"));
60  NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("2::0:0:0"), "network should equal next network");
61 
62  Ipv6AddressGenerator::Init(Ipv6Address("0:1::0:0"),
63  Ipv6Prefix("FFFF:FFFF::0"),
64  Ipv6Address("::"));
65  network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF:FFFF::0"));
66  NS_TEST_EXPECT_MSG_EQ(network,
67  Ipv6Address("0:1::0"),
68  "network should equal the initialized network for given prefix");
69  network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF::0"));
70  NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("0:2::0"), "network should equal next network");
71 
72  Ipv6AddressGenerator::Init(Ipv6Address("0:0:1::0"),
73  Ipv6Prefix("FFFF:FFFF:FFFF::0"),
74  Ipv6Address("::0"));
75  network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
76  NS_TEST_EXPECT_MSG_EQ(network,
77  Ipv6Address("0:0:1::0"),
78  "network should equal the initialized network for given prefix");
79  network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
80  NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("0:0:2::0"), "network should equal next network");
81 }
82 
89 {
90  public:
92 
93  private:
94  void DoRun() override;
95  void DoTeardown() override;
96 };
97 
99  : TestCase("Sanity check on allocation of addresses")
100 {
101 }
102 
103 void
105 {
107 
108  Ipv6AddressGenerator::Init(Ipv6Address("2001::0"), Ipv6Prefix(64));
109  address = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix(64));
111  Ipv6Address("2001::0"),
112  "address should equal the initialized address for given prefix");
113  Ipv6AddressGenerator::NextNetwork(Ipv6Prefix(64));
114  address = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix(64));
116  Ipv6Address("2001:0:0:1::0"),
117  "address should equal the initialized address for given prefix");
118  address = Ipv6AddressGenerator::GetAddress(Ipv6Prefix(64));
120  Ipv6Address("2001:0:0:1::1"),
121  "address should equal the initialized address for given prefix");
122  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(64));
124  Ipv6Address("2001:0:0:1::1"),
125  "address should equal the initialized address for given prefix");
126  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(64));
128  Ipv6Address("2001:0:0:1::2"),
129  "address should equal the initialized address for given prefix");
130 
131  Ipv6AddressGenerator::Init(Ipv6Address("1::"), Ipv6Prefix("FFFF::"), Ipv6Address("::3"));
132  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(16));
134  Ipv6Address("1::3"),
135  "address should equal initialized address for given prefix");
136  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(16));
137  NS_TEST_EXPECT_MSG_EQ(address, Ipv6Address("1::4"), "address should equal next address");
138 }
139 
140 void
142 {
144  Simulator::Destroy();
145 }
146 
153 {
154  public:
156  void DoRun() override;
157  void DoTeardown() override;
158 };
159 
161  : TestCase("Make sure Network and address allocation play together.")
162 {
163 }
164 
165 void
167 {
169  Simulator::Destroy();
170 }
171 
172 void
174 {
176  Ipv6Address network;
177 
178  Ipv6AddressGenerator::Init(Ipv6Address("3::"), Ipv6Prefix("FFFF::"), Ipv6Address("::3"));
179  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(16));
181  Ipv6Address("3::3"),
182  "address should equal initialized address for given prefix");
183  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(16));
185  Ipv6Address("3::4"),
186  "address should equal next address for given prefix");
187 
188  network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF::"));
189  NS_TEST_EXPECT_MSG_EQ(network,
190  Ipv6Address("4::0"),
191  "address should equal next address for given prefix");
192  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(16));
194  Ipv6Address("4::3"),
195  "address should equal next address for given prefix");
196 }
197 
204 {
205  public:
207 
208  private:
209  void DoRun() override;
210  void DoTeardown() override;
211 };
212 
214  : TestCase("A typical real-world example")
215 {
216 }
217 
218 void
220 {
222 }
223 
224 void
226 {
228 
229  Ipv6AddressGenerator::Init(Ipv6Address("2001:0AB8::"),
230  Ipv6Prefix("FFFF:FFFF:FFFF::0"),
231  Ipv6Address("::3"));
232  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(48));
234  Ipv6Address("2001:0AB8::0:3"),
235  "address should equal initialized address for given prefix");
236  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(48));
238  Ipv6Address("2001:0AB8::0:4"),
239  "address should equal next address for given prefix");
240  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(48));
242  Ipv6Address("2001:0AB8::0:5"),
243  "address should equal next address for given prefix");
244  //
245  // Allocate the next network based on the prefix passed in, which should
246  // be 2001:0AB0:0001
247  //
248  Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
249  //
250  // reset first address to be allocated back to ::0:3
251  //
252  Ipv6AddressGenerator::InitAddress(Ipv6Address("::3"), Ipv6Prefix(48));
253  //
254  // The first address we should get is the network and address ORed
255  //
256  address = Ipv6AddressGenerator::NextAddress(Ipv6Prefix(48));
258  Ipv6Address("2001:0AB8:1::3"),
259  "address should equal initialized address for given prefix");
260 }
261 
268 {
269  public:
271 
272  private:
273  void DoRun() override;
274  void DoTeardown() override;
275 };
276 
278  : TestCase("Make sure that the address collision logic works.")
279 {
280 }
281 
282 void
284 {
286  Simulator::Destroy();
287 }
288 
289 void
291 {
292  Ipv6AddressGenerator::AddAllocated("0::0:5");
293  Ipv6AddressGenerator::AddAllocated("0::0:10");
294  Ipv6AddressGenerator::AddAllocated("0::0:15");
295  Ipv6AddressGenerator::AddAllocated("0::0:20");
296 
297  Ipv6AddressGenerator::AddAllocated("0::0:4");
298  Ipv6AddressGenerator::AddAllocated("0::0:3");
299  Ipv6AddressGenerator::AddAllocated("0::0:2");
300  Ipv6AddressGenerator::AddAllocated("0::0:1");
301 
302  Ipv6AddressGenerator::AddAllocated("0::0:6");
303  Ipv6AddressGenerator::AddAllocated("0::0:7");
304  Ipv6AddressGenerator::AddAllocated("0::0:8");
305  Ipv6AddressGenerator::AddAllocated("0::0:9");
306 
307  Ipv6AddressGenerator::AddAllocated("0::0:11");
308  Ipv6AddressGenerator::AddAllocated("0::0:12");
309  Ipv6AddressGenerator::AddAllocated("0::0:13");
310  Ipv6AddressGenerator::AddAllocated("0::0:14");
311 
312  Ipv6AddressGenerator::AddAllocated("0::0:19");
313  Ipv6AddressGenerator::AddAllocated("0::0:18");
314  Ipv6AddressGenerator::AddAllocated("0::0:17");
315  Ipv6AddressGenerator::AddAllocated("0::0:16");
316 
317  Ipv6AddressGenerator::TestMode();
318  bool allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:21");
319  NS_TEST_EXPECT_MSG_EQ(allocated, false, "0::0:21 should not be already allocated");
320  bool added = Ipv6AddressGenerator::AddAllocated("0::0:21");
321  NS_TEST_EXPECT_MSG_EQ(added, true, "address should get allocated");
322 
323  allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:4");
324  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:4 should be already allocated");
325  added = Ipv6AddressGenerator::AddAllocated("0::0:4");
326  NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
327 
328  allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:9");
329  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:9 should be already allocated");
330  added = Ipv6AddressGenerator::AddAllocated("0::0:9");
331  NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
332 
333  allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:16");
334  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:16 should be already allocated");
335  added = Ipv6AddressGenerator::AddAllocated("0::0:16");
336  NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
337 
338  allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:21");
339  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:21 should be already allocated");
340  added = Ipv6AddressGenerator::AddAllocated("0::0:21");
341  NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
342 }
343 
350 {
351  public:
353  : TestSuite("ipv6-address-generator")
354  {
355  AddTestCase(new NetworkNumber6AllocatorTestCase(), TestCase::QUICK);
356  AddTestCase(new AddressAllocator6TestCase(), TestCase::QUICK);
357  AddTestCase(new NetworkAndAddress6TestCase(), TestCase::QUICK);
358  AddTestCase(new ExampleAddress6GeneratorTestCase(), TestCase::QUICK);
359  AddTestCase(new AddressCollision6TestCase(), TestCase::QUICK);
360  }
361 };
362 
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
IPv6 example of an address generator Test.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
IPv6 network number and address allocator Test.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
IPv6 network number allocator Test.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
void Reset()
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition: config.cc:855
#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:251
static Ipv6AddressGeneratorTestSuite g_ipv6AddressGeneratorTestSuite
Static variable for test initialization.
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.