A Discrete-Event Network Simulator
API
ipv4-address-generator-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "ns3/ipv4-address-generator.h"
19 #include "ns3/simulation-singleton.h"
20 #include "ns3/test.h"
21 
22 using namespace ns3;
23 
30 {
31  public:
33  void DoRun() override;
34  void DoTeardown() override;
35 };
36 
38  : TestCase("Make sure the network number allocator is working on some of network prefixes.")
39 {
40 }
41 
42 void
44 {
46 }
47 
48 void
50 {
51  Ipv4Address network;
52  Ipv4AddressGenerator::Init(Ipv4Address("1.0.0.0"),
53  Ipv4Mask("255.0.0.0"),
54  Ipv4Address("0.0.0.0"));
55  network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.0.0.0"));
56  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("1.0.0.0"), "001");
57  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
58  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "001");
59 
60  Ipv4AddressGenerator::Init(Ipv4Address("0.1.0.0"),
61  Ipv4Mask("255.255.0.0"),
62  Ipv4Address("0.0.0.0"));
63  network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.0.0"));
64  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.1.0.0"), "003");
65  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
66  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "004");
67 
68  Ipv4AddressGenerator::Init(Ipv4Address("0.0.1.0"),
69  Ipv4Mask("255.255.255.0"),
70  Ipv4Address("0.0.0.0"));
71  network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.255.0"));
72  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.1.0"), "005");
73  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
74  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "006");
75 
76  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
77  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("3.0.0.0"), "007");
78  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
79  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.3.0.0"), "008");
80  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
81  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.3.0"), "009");
82 }
83 
90 {
91  public:
93 
94  private:
95  void DoRun() override;
96  void DoTeardown() override;
97 };
98 
100  : TestCase("Sanity check on allocation of addresses")
101 {
102 }
103 
104 void
106 {
108 
109  Ipv4AddressGenerator::Init(Ipv4Address("1.0.0.0"),
110  Ipv4Mask("255.0.0.0"),
111  Ipv4Address("0.0.0.3"));
112  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
113  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "100");
114  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
115  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "101");
116 
117  Ipv4AddressGenerator::Init(Ipv4Address("0.1.0.0"),
118  Ipv4Mask("255.255.0.0"),
119  Ipv4Address("0.0.0.3"));
120  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
121  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "102");
122  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
123  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "103");
124 
125  Ipv4AddressGenerator::Init(Ipv4Address("0.0.1.0"),
126  Ipv4Mask("255.255.255.0"),
127  Ipv4Address("0.0.0.3"));
128  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
129  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "104");
130  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
131  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "105");
132 }
133 
134 void
136 {
138  Simulator::Destroy();
139 }
140 
147 {
148  public:
150  void DoRun() override;
151  void DoTeardown() override;
152 };
153 
155  : TestCase("Make sure Network and address allocation play together.")
156 {
157 }
158 
159 void
161 {
163  Simulator::Destroy();
164 }
165 
166 void
168 {
170  Ipv4Address network;
171 
172  Ipv4AddressGenerator::Init(Ipv4Address("3.0.0.0"),
173  Ipv4Mask("255.0.0.0"),
174  Ipv4Address("0.0.0.3"));
175  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
176  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.3"), "200");
177  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
178  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.4"), "201");
179 
180  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
181  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("4.0.0.0"), "202");
182  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
183  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("4.0.0.5"), "203");
184 
185  Ipv4AddressGenerator::Init(Ipv4Address("0.3.0.0"),
186  Ipv4Mask("255.255.0.0"),
187  Ipv4Address("0.0.0.3"));
188  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
189  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.3"), "204");
190  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
191  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.4"), "205");
192 
193  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
194  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.4.0.0"), "206");
195  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
196  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.4.0.5"), "207");
197 
198  Ipv4AddressGenerator::Init(Ipv4Address("0.0.3.0"),
199  Ipv4Mask("255.255.255.0"),
200  Ipv4Address("0.0.0.3"));
201  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
202  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.3"), "208");
203  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
204  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.4"), "209");
205 
206  network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
207  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.4.0"), "210");
208  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
209  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.4.5"), "211");
210 }
211 
218 {
219  public:
221 
222  private:
223  void DoRun() override;
224  void DoTeardown() override;
225 };
226 
228  : TestCase("A quick kindof-semi-almost-real example")
229 {
230 }
231 
232 void
234 {
236 }
237 
238 void
240 {
242  //
243  // First, initialize our /24 network to 192.168.0.0 and begin
244  // allocating with ip address 0.0.0.3 out of that prefix.
245  //
246  Ipv4AddressGenerator::Init(Ipv4Address("192.168.0.0"),
247  Ipv4Mask("255.255.255.0"),
248  Ipv4Address("0.0.0.3"));
249  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
250  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.3"), "300");
251  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
252  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.4"), "301");
253  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
254  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.5"), "302");
255  //
256  // Allocate the next network out of our /24 network (this should be
257  // 192.168.1.0) and begin allocating with IP address 0.0.0.3 out of that
258  // prefix.
259  //
260  Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
261  Ipv4AddressGenerator::InitAddress(Ipv4Address("0.0.0.3"), Ipv4Mask("255.255.255.0"));
262  //
263  // The first address we should get is the previous numbers ORed together, which
264  // is 192.168.1.3, of course.
265  //
266  address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
267  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.1.3"), "304");
268 }
269 
276 {
277  public:
279 
280  private:
281  void DoRun() override;
282  void DoTeardown() override;
283 };
284 
286  : TestCase("Make sure that the address collision logic works.")
287 {
288 }
289 
290 void
292 {
294  Simulator::Destroy();
295 }
296 
297 void
299 {
300  Ipv4AddressGenerator::AddAllocated("0.0.0.5");
301  Ipv4AddressGenerator::AddAllocated("0.0.0.10");
302  Ipv4AddressGenerator::AddAllocated("0.0.0.15");
303  Ipv4AddressGenerator::AddAllocated("0.0.0.20");
304 
305  Ipv4AddressGenerator::AddAllocated("0.0.0.4");
306  Ipv4AddressGenerator::AddAllocated("0.0.0.3");
307  Ipv4AddressGenerator::AddAllocated("0.0.0.2");
308  Ipv4AddressGenerator::AddAllocated("0.0.0.1");
309 
310  Ipv4AddressGenerator::AddAllocated("0.0.0.6");
311  Ipv4AddressGenerator::AddAllocated("0.0.0.7");
312  Ipv4AddressGenerator::AddAllocated("0.0.0.8");
313  Ipv4AddressGenerator::AddAllocated("0.0.0.9");
314 
315  Ipv4AddressGenerator::AddAllocated("0.0.0.11");
316  Ipv4AddressGenerator::AddAllocated("0.0.0.12");
317  Ipv4AddressGenerator::AddAllocated("0.0.0.13");
318  Ipv4AddressGenerator::AddAllocated("0.0.0.14");
319 
320  Ipv4AddressGenerator::AddAllocated("0.0.0.19");
321  Ipv4AddressGenerator::AddAllocated("0.0.0.18");
322  Ipv4AddressGenerator::AddAllocated("0.0.0.17");
323  Ipv4AddressGenerator::AddAllocated("0.0.0.16");
324 
325  Ipv4AddressGenerator::TestMode();
326  bool allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.21");
327  NS_TEST_EXPECT_MSG_EQ(allocated, false, "0.0.0.21 should not be already allocated");
328  bool added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
329  NS_TEST_EXPECT_MSG_EQ(added, true, "400");
330 
331  allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.4");
332  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.4 should be already allocated");
333  added = Ipv4AddressGenerator::AddAllocated("0.0.0.4");
334  NS_TEST_EXPECT_MSG_EQ(added, false, "401");
335 
336  allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.9");
337  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.9 should be already allocated");
338  added = Ipv4AddressGenerator::AddAllocated("0.0.0.9");
339  NS_TEST_EXPECT_MSG_EQ(added, false, "402");
340 
341  allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.16");
342  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.16 should be already allocated");
343  added = Ipv4AddressGenerator::AddAllocated("0.0.0.16");
344  NS_TEST_EXPECT_MSG_EQ(added, false, "403");
345 
346  allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.21");
347  NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.21 should be already allocated");
348  added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
349  NS_TEST_EXPECT_MSG_EQ(added, false, "404");
350 }
351 
358 {
359  public:
361 
362  private:
363 };
364 
366  : TestSuite("ipv4-address-generator", UNIT)
367 {
368  AddTestCase(new NetworkNumberAllocatorTestCase(), TestCase::QUICK);
369  AddTestCase(new AddressAllocatorTestCase(), TestCase::QUICK);
370  AddTestCase(new NetworkAndAddressTestCase(), TestCase::QUICK);
371  AddTestCase(new ExampleAddressGeneratorTestCase(), TestCase::QUICK);
372  AddTestCase(new AddressCollisionTestCase(), TestCase::QUICK);
373 }
374 
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
IPv4 AddressGenerator example (sort of) Test.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
IPv4 network 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.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
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 Ipv4AddressGeneratorTestSuite g_ipv4AddressGeneratorTestSuite
Static variable for test initialization.
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.