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
22using namespace ns3;
23
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
43void
45{
47}
48
49void
51{
52 Ipv4Address network;
53 Ipv4AddressGenerator::Init(Ipv4Address("1.0.0.0"),
54 Ipv4Mask("255.0.0.0"),
55 Ipv4Address("0.0.0.0"));
56 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.0.0.0"));
57 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("1.0.0.0"), "001");
58 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
59 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "001");
60
61 Ipv4AddressGenerator::Init(Ipv4Address("0.1.0.0"),
62 Ipv4Mask("255.255.0.0"),
63 Ipv4Address("0.0.0.0"));
64 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.0.0"));
65 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.1.0.0"), "003");
66 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
67 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "004");
68
69 Ipv4AddressGenerator::Init(Ipv4Address("0.0.1.0"),
70 Ipv4Mask("255.255.255.0"),
71 Ipv4Address("0.0.0.0"));
72 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.255.0"));
73 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.1.0"), "005");
74 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
75 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "006");
76
77 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
78 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("3.0.0.0"), "007");
79 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
80 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.3.0.0"), "008");
81 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
82 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.3.0"), "009");
83}
84
92{
93 public:
95
96 private:
97 void DoRun() override;
98 void DoTeardown() override;
99};
100
102 : TestCase("Sanity check on allocation of addresses")
103{
104}
105
106void
108{
110
111 Ipv4AddressGenerator::Init(Ipv4Address("1.0.0.0"),
112 Ipv4Mask("255.0.0.0"),
113 Ipv4Address("0.0.0.3"));
114 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
115 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "100");
116 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
117 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "101");
118
119 Ipv4AddressGenerator::Init(Ipv4Address("0.1.0.0"),
120 Ipv4Mask("255.255.0.0"),
121 Ipv4Address("0.0.0.3"));
122 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
123 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "102");
124 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
125 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "103");
126
127 Ipv4AddressGenerator::Init(Ipv4Address("0.0.1.0"),
128 Ipv4Mask("255.255.255.0"),
129 Ipv4Address("0.0.0.3"));
130 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
131 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "104");
132 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
133 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "105");
134}
135
136void
138{
140 Simulator::Destroy();
141}
142
150{
151 public:
153 void DoRun() override;
154 void DoTeardown() override;
155};
156
158 : TestCase("Make sure Network and address allocation play together.")
159{
160}
161
162void
164{
166 Simulator::Destroy();
167}
168
169void
171{
173 Ipv4Address network;
174
175 Ipv4AddressGenerator::Init(Ipv4Address("3.0.0.0"),
176 Ipv4Mask("255.0.0.0"),
177 Ipv4Address("0.0.0.3"));
178 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
179 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.3"), "200");
180 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
181 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.4"), "201");
182
183 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
184 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("4.0.0.0"), "202");
185 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
186 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("4.0.0.5"), "203");
187
188 Ipv4AddressGenerator::Init(Ipv4Address("0.3.0.0"),
189 Ipv4Mask("255.255.0.0"),
190 Ipv4Address("0.0.0.3"));
191 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
192 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.3"), "204");
193 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
194 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.4"), "205");
195
196 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
197 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.4.0.0"), "206");
198 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
199 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.4.0.5"), "207");
200
201 Ipv4AddressGenerator::Init(Ipv4Address("0.0.3.0"),
202 Ipv4Mask("255.255.255.0"),
203 Ipv4Address("0.0.0.3"));
204 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
205 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.3"), "208");
206 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
207 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.4"), "209");
208
209 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
210 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.4.0"), "210");
211 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
212 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.4.5"), "211");
213}
214
222{
223 public:
225
226 private:
227 void DoRun() override;
228 void DoTeardown() override;
229};
230
232 : TestCase("A quick kindof-semi-almost-real example")
233{
234}
235
236void
238{
240}
241
242void
244{
246 //
247 // First, initialize our /24 network to 192.168.0.0 and begin
248 // allocating with ip address 0.0.0.3 out of that prefix.
249 //
250 Ipv4AddressGenerator::Init(Ipv4Address("192.168.0.0"),
251 Ipv4Mask("255.255.255.0"),
252 Ipv4Address("0.0.0.3"));
253 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
254 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.3"), "300");
255 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
256 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.4"), "301");
257 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
258 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.5"), "302");
259 //
260 // Allocate the next network out of our /24 network (this should be
261 // 192.168.1.0) and begin allocating with IP address 0.0.0.3 out of that
262 // prefix.
263 //
264 Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
265 Ipv4AddressGenerator::InitAddress(Ipv4Address("0.0.0.3"), Ipv4Mask("255.255.255.0"));
266 //
267 // The first address we should get is the previous numbers ORed together, which
268 // is 192.168.1.3, of course.
269 //
270 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
271 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.1.3"), "304");
272}
273
281{
282 public:
284
285 private:
286 void DoRun() override;
287 void DoTeardown() override;
288};
289
291 : TestCase("Make sure that the address collision logic works.")
292{
293}
294
295void
297{
299 Simulator::Destroy();
300}
301
302void
304{
305 Ipv4AddressGenerator::AddAllocated("0.0.0.5");
306 Ipv4AddressGenerator::AddAllocated("0.0.0.10");
307 Ipv4AddressGenerator::AddAllocated("0.0.0.15");
308 Ipv4AddressGenerator::AddAllocated("0.0.0.20");
309
310 Ipv4AddressGenerator::AddAllocated("0.0.0.4");
311 Ipv4AddressGenerator::AddAllocated("0.0.0.3");
312 Ipv4AddressGenerator::AddAllocated("0.0.0.2");
313 Ipv4AddressGenerator::AddAllocated("0.0.0.1");
314
315 Ipv4AddressGenerator::AddAllocated("0.0.0.6");
316 Ipv4AddressGenerator::AddAllocated("0.0.0.7");
317 Ipv4AddressGenerator::AddAllocated("0.0.0.8");
318 Ipv4AddressGenerator::AddAllocated("0.0.0.9");
319
320 Ipv4AddressGenerator::AddAllocated("0.0.0.11");
321 Ipv4AddressGenerator::AddAllocated("0.0.0.12");
322 Ipv4AddressGenerator::AddAllocated("0.0.0.13");
323 Ipv4AddressGenerator::AddAllocated("0.0.0.14");
324
325 Ipv4AddressGenerator::AddAllocated("0.0.0.19");
326 Ipv4AddressGenerator::AddAllocated("0.0.0.18");
327 Ipv4AddressGenerator::AddAllocated("0.0.0.17");
328 Ipv4AddressGenerator::AddAllocated("0.0.0.16");
329
330 Ipv4AddressGenerator::TestMode();
331 bool added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
332 NS_TEST_EXPECT_MSG_EQ(added, true, "400");
333
334 added = Ipv4AddressGenerator::AddAllocated("0.0.0.4");
335 NS_TEST_EXPECT_MSG_EQ(added, false, "401");
336
337 added = Ipv4AddressGenerator::AddAllocated("0.0.0.9");
338 NS_TEST_EXPECT_MSG_EQ(added, false, "402");
339
340 added = Ipv4AddressGenerator::AddAllocated("0.0.0.16");
341 NS_TEST_EXPECT_MSG_EQ(added, false, "403");
342
343 added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
344 NS_TEST_EXPECT_MSG_EQ(added, false, "404");
345}
346
354{
355 public:
357
358 private:
359};
360
362 : TestSuite("ipv4-address-generator", UNIT)
363{
364 AddTestCase(new NetworkNumberAllocatorTestCase(), TestCase::QUICK);
365 AddTestCase(new AddressAllocatorTestCase(), TestCase::QUICK);
366 AddTestCase(new NetworkAndAddressTestCase(), TestCase::QUICK);
367 AddTestCase(new ExampleAddressGeneratorTestCase(), TestCase::QUICK);
368 AddTestCase(new AddressCollisionTestCase(), TestCase::QUICK);
369}
370
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:43
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
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:305
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:856
#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:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.