A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv6-address-generator-test-suite.cc
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 
27 {
28 public:
30  virtual void DoRun (void);
31  virtual void DoTeardown (void);
32 };
33 
34 NetworkNumber6AllocatorTestCase::NetworkNumber6AllocatorTestCase ()
35  : TestCase ("Make sure the network number allocator is working on some of network prefixes.")
36 {
37 }
38 void
40 {
41  Ipv6AddressGenerator::Reset ();
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 
55  Ipv6AddressGenerator::Init (Ipv6Address ("0:1::0:0"),
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 
62  Ipv6AddressGenerator::Init (Ipv6Address ("0:0:1::0"),
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 
80 AddressAllocator6TestCase::AddressAllocator6TestCase ()
81  : TestCase ("Sanity check on allocation of addresses")
82 {
83 }
84 
85 void
87 {
88  Ipv6Address address;
89 
90  Ipv6AddressGenerator::Init (Ipv6Address ("2001::0"), Ipv6Prefix (64));
91  address = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix (64));
92  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001::0"), "address should equal the initialized address for given prefix");
93  Ipv6AddressGenerator::NextNetwork (Ipv6Prefix (64));
94  address = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix (64));
95  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::0"), "address should equal the initialized address for given prefix");
96  address = Ipv6AddressGenerator::GetAddress (Ipv6Prefix (64));
97  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
98  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (64));
99  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::1"), "address should equal the initialized address for given prefix");
100  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (64));
101  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0:0:1::2"), "address should equal the initialized address for given prefix");
102 
103  Ipv6AddressGenerator::Init (Ipv6Address ("1::"), Ipv6Prefix ("FFFF::"),
104  Ipv6Address ("::3"));
105  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
106  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::3"), "address should equal initialized address for given prefix");
107  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
108  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("1::4"), "address should equal next address");
109 
110 }
111 
112 void
114 {
115  Ipv6AddressGenerator::Reset ();
116  Simulator::Destroy ();
117 }
118 
119 
121 {
122 public:
124  virtual void DoRun (void);
125  virtual void DoTeardown (void);
126 };
127 
128 NetworkAndAddress6TestCase::NetworkAndAddress6TestCase ()
129  : TestCase ("Make sure Network and address allocation play together.")
130 {
131 }
132 
133 void
135 {
136  Ipv6AddressGenerator::Reset ();
137  Simulator::Destroy ();
138 }
139 
140 void
142 {
143  Ipv6Address address;
144  Ipv6Address network;
145 
146  Ipv6AddressGenerator::Init (Ipv6Address ("3::"), Ipv6Prefix ("FFFF::"),
147  Ipv6Address ("::3"));
148  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
149  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("3::3"), "address should equal initialized address for given prefix");
150  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
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");
155  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (16));
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 
169 ExampleAddress6GeneratorTestCase::ExampleAddress6GeneratorTestCase ()
170  : TestCase ("A typical real-world example")
171 {
172 }
173 
174 void
176 {
177  Ipv6AddressGenerator::Reset ();
178 }
179 
180 void
182 {
183  Ipv6Address address;
184 
185  Ipv6AddressGenerator::Init (Ipv6Address ("2001:0AB8::"),
186  Ipv6Prefix ("FFFF:FFFF:FFFF::0"), Ipv6Address ("::3"));
187  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
188  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:3"), "address should equal initialized address for given prefix");
189  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
190  NS_TEST_EXPECT_MSG_EQ (address, Ipv6Address ("2001:0AB8::0:4"), "address should equal next address for given prefix");
191  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
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  //
201  Ipv6AddressGenerator::InitAddress (Ipv6Address ("::3"),
202  Ipv6Prefix (48));
203  //
204  // The first address we should get is the network and address ORed
205  //
206  address = Ipv6AddressGenerator::NextAddress (Ipv6Prefix (48));
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 
219 AddressCollision6TestCase::AddressCollision6TestCase ()
220  : TestCase ("Make sure that the address collision logic works.")
221 {
222 }
223 
224 void
226 {
227  Ipv6AddressGenerator::Reset ();
228  Simulator::Destroy ();
229 }
230 void
232 {
233  Ipv6AddressGenerator::AddAllocated ("0::0:5");
234  Ipv6AddressGenerator::AddAllocated ("0::0:10");
235  Ipv6AddressGenerator::AddAllocated ("0::0:15");
236  Ipv6AddressGenerator::AddAllocated ("0::0:20");
237 
238  Ipv6AddressGenerator::AddAllocated ("0::0:4");
239  Ipv6AddressGenerator::AddAllocated ("0::0:3");
240  Ipv6AddressGenerator::AddAllocated ("0::0:2");
241  Ipv6AddressGenerator::AddAllocated ("0::0:1");
242 
243  Ipv6AddressGenerator::AddAllocated ("0::0:6");
244  Ipv6AddressGenerator::AddAllocated ("0::0:7");
245  Ipv6AddressGenerator::AddAllocated ("0::0:8");
246  Ipv6AddressGenerator::AddAllocated ("0::0:9");
247 
248  Ipv6AddressGenerator::AddAllocated ("0::0:11");
249  Ipv6AddressGenerator::AddAllocated ("0::0:12");
250  Ipv6AddressGenerator::AddAllocated ("0::0:13");
251  Ipv6AddressGenerator::AddAllocated ("0::0:14");
252 
253  Ipv6AddressGenerator::AddAllocated ("0::0:19");
254  Ipv6AddressGenerator::AddAllocated ("0::0:18");
255  Ipv6AddressGenerator::AddAllocated ("0::0:17");
256  Ipv6AddressGenerator::AddAllocated ("0::0:16");
257 
258  Ipv6AddressGenerator::TestMode ();
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  {
282  AddTestCase (new NetworkNumber6AllocatorTestCase (), TestCase::QUICK);
283  AddTestCase (new AddressAllocator6TestCase (), TestCase::QUICK);
284  AddTestCase (new NetworkAndAddress6TestCase (), TestCase::QUICK);
285  AddTestCase (new ExampleAddress6GeneratorTestCase (), TestCase::QUICK);
286  AddTestCase (new AddressCollision6TestCase (), TestCase::QUICK);
287  }
288 } g_ipv6AddressGeneratorTestSuite;
virtual void DoRun(void)
Implementation to actually run this test case.
A suite of tests to run.
Definition: test.h:962
encapsulates test code
Definition: test.h:834
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
Describes an IPv6 address.
Definition: ipv6-address.h:44
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
void DoTeardown(void)
Implementation to do any local setup required for this test case.
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
Definition: ipv6-address.h:326
void DoRun(void)
Implementation to actually run this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.