A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
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
42void
44{
46}
47
48void
50{
51 Ipv4Address network;
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
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
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
104void
106{
107 Ipv4Address address;
108
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
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
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
134void
136{
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
159void
161{
164}
165
166void
168{
169 Ipv4Address address;
170 Ipv4Address network;
171
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
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
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
232void
234{
236}
237
238void
240{
241 Ipv4Address address;
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 //
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 //
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
290void
292{
295}
296
297void
299{
304
309
314
319
324
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", Type::UNIT)
367{
368 AddTestCase(new NetworkNumberAllocatorTestCase(), TestCase::Duration::QUICK);
369 AddTestCase(new AddressAllocatorTestCase(), TestCase::Duration::QUICK);
370 AddTestCase(new NetworkAndAddressTestCase(), TestCase::Duration::QUICK);
371 AddTestCase(new ExampleAddressGeneratorTestCase(), TestCase::Duration::QUICK);
372 AddTestCase(new AddressCollisionTestCase(), TestCase::Duration::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.
static Ipv4Address NextAddress(const Ipv4Mask mask)
Allocate the next Ipv4Address for the configured network and mask.
static void InitAddress(const Ipv4Address addr, const Ipv4Mask mask)
Set the address for the given mask.
static void TestMode()
Used to turn off fatal errors and assertions, for testing.
static Ipv4Address NextNetwork(const Ipv4Mask mask)
Get the next network according to the given Ipv4Mask.
static void Reset()
Reset the networks and Ipv4Address to zero.
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
static bool IsAddressAllocated(const Ipv4Address addr)
Check the Ipv4Address allocation in the list of IPv4 entries.
static Ipv4Address GetNetwork(const Ipv4Mask mask)
Get the current network of the given Ipv4Mask.
static void Init(const Ipv4Address net, const Ipv4Mask mask, const Ipv4Address addr="0.0.0.1")
Initialise the base network, mask and address for the generator.
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
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
#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:252
static Ipv4AddressGeneratorTestSuite g_ipv4AddressGeneratorTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.