A Discrete-Event Network Simulator
API
ipv4-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  *
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/test.h"
20 #include "ns3/ipv4-address-generator.h"
21 #include "ns3/simulation-singleton.h"
22 
23 using namespace ns3;
24 
32 {
33 public:
35  virtual void DoRun (void);
36  virtual void DoTeardown (void);
37 };
38 
40  : TestCase ("Make sure the network number allocator is working on some of network prefixes.")
41 {
42 }
43 void
45 {
47 }
48 void
50 {
51  Ipv4Address network;
52  Ipv4AddressGenerator::Init (Ipv4Address ("1.0.0.0"), Ipv4Mask ("255.0.0.0"),
53  Ipv4Address ("0.0.0.0"));
54  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.0.0.0"));
55  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("1.0.0.0"), "001");
56  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
57  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("2.0.0.0"), "001");
58 
59  Ipv4AddressGenerator::Init (Ipv4Address ("0.1.0.0"),
60  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.0"));
61  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.255.0.0"));
62  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.1.0.0"), "003");
63  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
64  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.2.0.0"), "004");
65 
66  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.1.0"),
67  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.0"));
68  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.255.255.0"));
69  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.1.0"), "005");
70  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
71  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.2.0"), "006");
72 
73  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
74  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("3.0.0.0"), "007");
75  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
76  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.3.0.0"), "008");
77  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
78  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.3.0"), "009");
79 }
80 
88 {
89 public:
91 private:
92  virtual void DoRun (void);
93  virtual void DoTeardown (void);
94 };
95 
97  : TestCase ("Sanity check on allocation of addresses")
98 {
99 }
100 
101 void
103 {
105 
106  Ipv4AddressGenerator::Init (Ipv4Address ("1.0.0.0"), Ipv4Mask ("255.0.0.0"),
107  Ipv4Address ("0.0.0.3"));
108  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
109  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.3"), "100");
110  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
111  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.4"), "101");
112 
113  Ipv4AddressGenerator::Init (Ipv4Address ("0.1.0.0"),
114  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.3"));
115  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
116  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.3"), "102");
117  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
118  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.4"), "103");
119 
120  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.1.0"),
121  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
122  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
123  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.3"), "104");
124  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
125  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.4"), "105");
126 }
127 
128 void
130 {
132  Simulator::Destroy ();
133 }
134 
135 
143 {
144 public:
146  virtual void DoRun (void);
147  virtual void DoTeardown (void);
148 };
149 
151  : TestCase ("Make sure Network and address allocation play together.")
152 {
153 }
154 
155 void
157 {
159  Simulator::Destroy ();
160 }
161 
162 void
164 {
166  Ipv4Address network;
167 
168  Ipv4AddressGenerator::Init (Ipv4Address ("3.0.0.0"), Ipv4Mask ("255.0.0.0"),
169  Ipv4Address ("0.0.0.3"));
170  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
171  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("3.0.0.3"), "200");
172  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
173  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("3.0.0.4"), "201");
174 
175  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
176  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("4.0.0.0"), "202");
177  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
178  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("4.0.0.5"), "203");
179 
180  Ipv4AddressGenerator::Init (Ipv4Address ("0.3.0.0"),
181  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.3"));
182  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
183  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.3.0.3"), "204");
184  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
185  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.3.0.4"), "205");
186 
187  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
188  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.4.0.0"), "206");
189  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
190  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.4.0.5"), "207");
191 
192  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.3.0"),
193  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
194  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
195  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.3.3"), "208");
196  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
197  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.3.4"), "209");
198 
199  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
200  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.4.0"), "210");
201  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
202  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.4.5"), "211");
203 }
204 
212 {
213 public:
215 private:
216  virtual void DoRun (void);
217  virtual void DoTeardown (void);
218 };
219 
221  : TestCase ("A quick kindof-semi-almost-real example")
222 {
223 }
224 
225 void
227 {
229 }
230 
231 void
233 {
235  //
236  // First, initialize our /24 network to 192.168.0.0 and begin
237  // allocating with ip address 0.0.0.3 out of that prefix.
238  //
239  Ipv4AddressGenerator::Init (Ipv4Address ("192.168.0.0"),
240  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
241  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
242  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.3"), "300");
243  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
244  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.4"), "301");
245  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
246  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.5"), "302");
247  //
248  // Allocate the next network out of our /24 network (this should be
249  // 192.168.1.0) and begin allocating with IP address 0.0.0.3 out of that
250  // prefix.
251  //
252  Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
253  Ipv4AddressGenerator::InitAddress (Ipv4Address ("0.0.0.3"),
254  Ipv4Mask ("255.255.255.0"));
255  //
256  // The first address we should get is the previous numbers ORed together, which
257  // is 192.168.1.3, of course.
258  //
259  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
260  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.1.3"), "304");
261 }
262 
270 {
271 public:
273 private:
274  void DoRun (void);
275  void DoTeardown (void);
276 };
277 
279  : TestCase ("Make sure that the address collision logic works.")
280 {
281 }
282 
283 void
285 {
287  Simulator::Destroy ();
288 }
289 void
291 {
292  Ipv4AddressGenerator::AddAllocated ("0.0.0.5");
293  Ipv4AddressGenerator::AddAllocated ("0.0.0.10");
294  Ipv4AddressGenerator::AddAllocated ("0.0.0.15");
295  Ipv4AddressGenerator::AddAllocated ("0.0.0.20");
296 
297  Ipv4AddressGenerator::AddAllocated ("0.0.0.4");
298  Ipv4AddressGenerator::AddAllocated ("0.0.0.3");
299  Ipv4AddressGenerator::AddAllocated ("0.0.0.2");
300  Ipv4AddressGenerator::AddAllocated ("0.0.0.1");
301 
302  Ipv4AddressGenerator::AddAllocated ("0.0.0.6");
303  Ipv4AddressGenerator::AddAllocated ("0.0.0.7");
304  Ipv4AddressGenerator::AddAllocated ("0.0.0.8");
305  Ipv4AddressGenerator::AddAllocated ("0.0.0.9");
306 
307  Ipv4AddressGenerator::AddAllocated ("0.0.0.11");
308  Ipv4AddressGenerator::AddAllocated ("0.0.0.12");
309  Ipv4AddressGenerator::AddAllocated ("0.0.0.13");
310  Ipv4AddressGenerator::AddAllocated ("0.0.0.14");
311 
312  Ipv4AddressGenerator::AddAllocated ("0.0.0.19");
313  Ipv4AddressGenerator::AddAllocated ("0.0.0.18");
314  Ipv4AddressGenerator::AddAllocated ("0.0.0.17");
315  Ipv4AddressGenerator::AddAllocated ("0.0.0.16");
316 
317  Ipv4AddressGenerator::TestMode ();
318  bool added = Ipv4AddressGenerator::AddAllocated ("0.0.0.21");
319  NS_TEST_EXPECT_MSG_EQ (added, true, "400");
320 
321  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.4");
322  NS_TEST_EXPECT_MSG_EQ (added, false, "401");
323 
324  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.9");
325  NS_TEST_EXPECT_MSG_EQ (added, false, "402");
326 
327  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.16");
328  NS_TEST_EXPECT_MSG_EQ (added, false, "403");
329 
330  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.21");
331  NS_TEST_EXPECT_MSG_EQ (added, false, "404");
332 }
333 
334 
342 {
343 public:
345 private:
346 };
347 
349  : TestSuite ("ipv4-address-generator", UNIT)
350 {
351  AddTestCase (new NetworkNumberAllocatorTestCase (), TestCase::QUICK);
352  AddTestCase (new AddressAllocatorTestCase (), TestCase::QUICK);
353  AddTestCase (new NetworkAndAddressTestCase (), TestCase::QUICK);
354  AddTestCase (new ExampleAddressGeneratorTestCase (), TestCase::QUICK);
355  AddTestCase (new AddressCollisionTestCase (), TestCase::QUICK);
356 }
357 
359 
IPv4 Address Generator TestSuite.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
A suite of tests to run.
Definition: test.h:1342
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
#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:285
encapsulates test code
Definition: test.h:1155
static Ipv4AddressGeneratorTestSuite g_ipv4AddressGeneratorTestSuite
Static variable for test initialization.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
IPv4 AddressGenerator example (sort of) Test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
IPv4 network and address allocator Test.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
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:757
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoRun(void)
Implementation to actually run this TestCase.