A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
26 {
27 public:
29  virtual void DoRun (void);
30  virtual void DoTeardown (void);
31 };
32 
34  : TestCase ("Make sure the network number allocator is working on some of network prefixes.")
35 {
36 }
37 void
39 {
41 }
42 void
44 {
45  Ipv4Address network;
46  Ipv4AddressGenerator::Init (Ipv4Address ("1.0.0.0"), Ipv4Mask ("255.0.0.0"),
47  Ipv4Address ("0.0.0.0"));
48  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.0.0.0"));
49  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("1.0.0.0"), "XXX");
50  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
51  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("2.0.0.0"), "XXX");
52 
53  Ipv4AddressGenerator::Init (Ipv4Address ("0.1.0.0"),
54  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.0"));
55  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.255.0.0"));
56  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.1.0.0"), "XXX");
57  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
58  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.2.0.0"), "XXX");
59 
60  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.1.0"),
61  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.0"));
62  network = Ipv4AddressGenerator::GetNetwork (Ipv4Mask ("255.255.255.0"));
63  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.1.0"), "XXX");
64  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
65  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.2.0"), "XXX");
66 
67  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
68  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("3.0.0.0"), "XXX");
69  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
70  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.3.0.0"), "XXX");
71  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
72  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.3.0"), "XXX");
73 }
74 
76 {
77 public:
79 private:
80  virtual void DoRun (void);
81  virtual void DoTeardown (void);
82 };
83 
85  : TestCase ("Sanity check on allocation of addresses")
86 {
87 }
88 
89 void
91 {
92  Ipv4Address address;
93 
94  Ipv4AddressGenerator::Init (Ipv4Address ("1.0.0.0"), Ipv4Mask ("255.0.0.0"),
95  Ipv4Address ("0.0.0.3"));
96  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
97  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.3"), "XXX");
98  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
99  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.4"), "XXX");
100 
101  Ipv4AddressGenerator::Init (Ipv4Address ("0.1.0.0"),
102  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.3"));
103  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
104  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.3"), "XXX");
105  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
106  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.4"), "XXX");
107 
108  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.1.0"),
109  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
110  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
111  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.3"), "XXX");
112  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
113  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.4"), "XXX");
114 }
115 
116 void
118 {
120  Simulator::Destroy ();
121 }
122 
123 
125 {
126 public:
128  virtual void DoRun (void);
129  virtual void DoTeardown (void);
130 };
131 
133  : TestCase ("Make sure Network and address allocation play together.")
134 {
135 }
136 
137 void
139 {
141  Simulator::Destroy ();
142 }
143 
144 void
146 {
147  Ipv4Address address;
148  Ipv4Address network;
149 
150  Ipv4AddressGenerator::Init (Ipv4Address ("3.0.0.0"), Ipv4Mask ("255.0.0.0"),
151  Ipv4Address ("0.0.0.3"));
152  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
153  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("3.0.0.3"), "XXX");
154  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
155  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("3.0.0.4"), "XXX");
156 
157  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.0.0.0"));
158  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("4.0.0.0"), "XXX");
159  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.0.0.0"));
160  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("4.0.0.5"), "XXX");
161 
162  Ipv4AddressGenerator::Init (Ipv4Address ("0.3.0.0"),
163  Ipv4Mask ("255.255.0.0"), Ipv4Address ("0.0.0.3"));
164  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
165  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.3.0.3"), "XXX");
166  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
167  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.3.0.4"), "XXX");
168 
169  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.0.0"));
170  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.4.0.0"), "XXX");
171  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.0.0"));
172  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.4.0.5"), "XXX");
173 
174  Ipv4AddressGenerator::Init (Ipv4Address ("0.0.3.0"),
175  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
176  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
177  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.3.3"), "XXX");
178  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
179  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.3.4"), "XXX");
180 
181  network = Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
182  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.4.0"), "XXX");
183  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
184  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.4.5"), "XXX");
185 }
186 
188 {
189 public:
191 private:
192  virtual void DoRun (void);
193  virtual void DoTeardown (void);
194 };
195 
197  : TestCase ("A quick kindof-semi-almost-real example")
198 {
199 }
200 
201 void
203 {
205 }
206 
207 void
209 {
210  Ipv4Address address;
211  //
212  // First, initialize our /24 network to 192.168.0.0 and begin
213  // allocating with ip address 0.0.0.3 out of that prefix.
214  //
215  Ipv4AddressGenerator::Init (Ipv4Address ("192.168.0.0"),
216  Ipv4Mask ("255.255.255.0"), Ipv4Address ("0.0.0.3"));
217  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
218  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.3"), "XXX");
219  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
220  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.4"), "XXX");
221  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
222  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.0.5"), "XXX");
223  //
224  // Allocate the next network out of our /24 network (this should be
225  // 192.168.1.0) and begin allocating with IP address 0.0.0.3 out of that
226  // prefix.
227  //
228  Ipv4AddressGenerator::NextNetwork (Ipv4Mask ("255.255.255.0"));
229  Ipv4AddressGenerator::InitAddress (Ipv4Address ("0.0.0.3"),
230  Ipv4Mask ("255.255.255.0"));
231  //
232  // The first address we should get is the previous numbers ORed together, which
233  // is 192.168.1.3, of course.
234  //
235  address = Ipv4AddressGenerator::NextAddress (Ipv4Mask ("255.255.255.0"));
236  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("192.168.1.3"), "XXX");
237 }
238 
240 {
241 public:
243 private:
244  void DoRun (void);
245  void DoTeardown (void);
246 };
247 
249  : TestCase ("Make sure that the address collision logic works.")
250 {
251 }
252 
253 void
255 {
257  Simulator::Destroy ();
258 }
259 void
261 {
262  Ipv4AddressGenerator::AddAllocated ("0.0.0.5");
263  Ipv4AddressGenerator::AddAllocated ("0.0.0.10");
264  Ipv4AddressGenerator::AddAllocated ("0.0.0.15");
265  Ipv4AddressGenerator::AddAllocated ("0.0.0.20");
266 
267  Ipv4AddressGenerator::AddAllocated ("0.0.0.4");
268  Ipv4AddressGenerator::AddAllocated ("0.0.0.3");
269  Ipv4AddressGenerator::AddAllocated ("0.0.0.2");
270  Ipv4AddressGenerator::AddAllocated ("0.0.0.1");
271 
272  Ipv4AddressGenerator::AddAllocated ("0.0.0.6");
273  Ipv4AddressGenerator::AddAllocated ("0.0.0.7");
274  Ipv4AddressGenerator::AddAllocated ("0.0.0.8");
275  Ipv4AddressGenerator::AddAllocated ("0.0.0.9");
276 
277  Ipv4AddressGenerator::AddAllocated ("0.0.0.11");
278  Ipv4AddressGenerator::AddAllocated ("0.0.0.12");
279  Ipv4AddressGenerator::AddAllocated ("0.0.0.13");
280  Ipv4AddressGenerator::AddAllocated ("0.0.0.14");
281 
282  Ipv4AddressGenerator::AddAllocated ("0.0.0.19");
283  Ipv4AddressGenerator::AddAllocated ("0.0.0.18");
284  Ipv4AddressGenerator::AddAllocated ("0.0.0.17");
285  Ipv4AddressGenerator::AddAllocated ("0.0.0.16");
286 
287  Ipv4AddressGenerator::TestMode ();
288  bool added = Ipv4AddressGenerator::AddAllocated ("0.0.0.21");
289  NS_TEST_EXPECT_MSG_EQ (added, true, "XXX");
290 
291  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.4");
292  NS_TEST_EXPECT_MSG_EQ (added, false, "XXX");
293 
294  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.9");
295  NS_TEST_EXPECT_MSG_EQ (added, false, "XXX");
296 
297  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.16");
298  NS_TEST_EXPECT_MSG_EQ (added, false, "XXX");
299 
300  added = Ipv4AddressGenerator::AddAllocated ("0.0.0.21");
301  NS_TEST_EXPECT_MSG_EQ (added, false, "XXX");
302 }
303 
304 
306 {
307 public:
309  : TestSuite ("ipv4-address-generator")
310  {
311  AddTestCase (new NetworkNumberAllocatorTestCase (), TestCase::QUICK);
312  AddTestCase (new AddressAllocatorTestCase (), TestCase::QUICK);
313  AddTestCase (new NetworkAndAddressTestCase (), TestCase::QUICK);
314  AddTestCase (new ExampleAddressGeneratorTestCase (), TestCase::QUICK);
315  AddTestCase (new AddressCollisionTestCase (), TestCase::QUICK);
316  }