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 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/ipv4-address-generator.h"
8#include "ns3/simulation-singleton.h"
9#include "ns3/test.h"
10
11using namespace ns3;
12
13/**
14 * @ingroup internet-test
15 *
16 * @brief IPv4 network number Test
17 */
19{
20 public:
22 void DoRun() override;
23 void DoTeardown() override;
24};
25
27 : TestCase("Make sure the network number allocator is working on some of network prefixes.")
28{
29}
30
31void
36
37void
39{
40 Ipv4Address network;
42 Ipv4Mask("255.0.0.0"),
43 Ipv4Address("0.0.0.0"));
44 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.0.0.0"));
45 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("1.0.0.0"), "001");
46 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
47 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "001");
48
50 Ipv4Mask("255.255.0.0"),
51 Ipv4Address("0.0.0.0"));
52 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.0.0"));
53 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.1.0.0"), "003");
54 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
55 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "004");
56
58 Ipv4Mask("255.255.255.0"),
59 Ipv4Address("0.0.0.0"));
60 network = Ipv4AddressGenerator::GetNetwork(Ipv4Mask("255.255.255.0"));
61 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.1.0"), "005");
62 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
63 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "006");
64
65 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
66 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("3.0.0.0"), "007");
67 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
68 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.3.0.0"), "008");
69 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
70 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.3.0"), "009");
71}
72
73/**
74 * @ingroup internet-test
75 *
76 * @brief IPv4 address allocator Test
77 */
79{
80 public:
82
83 private:
84 void DoRun() override;
85 void DoTeardown() override;
86};
87
89 : TestCase("Sanity check on allocation of addresses")
90{
91}
92
93void
95{
96 Ipv4Address address;
97
99 Ipv4Mask("255.0.0.0"),
100 Ipv4Address("0.0.0.3"));
101 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
102 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "100");
103 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
104 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "101");
105
107 Ipv4Mask("255.255.0.0"),
108 Ipv4Address("0.0.0.3"));
109 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
110 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "102");
111 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
112 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "103");
113
115 Ipv4Mask("255.255.255.0"),
116 Ipv4Address("0.0.0.3"));
117 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
118 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "104");
119 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
120 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "105");
121}
122
123void
129
130/**
131 * @ingroup internet-test
132 *
133 * @brief IPv4 network and address allocator Test
134 */
136{
137 public:
139 void DoRun() override;
140 void DoTeardown() override;
141};
142
144 : TestCase("Make sure Network and address allocation play together.")
145{
146}
147
148void
154
155void
157{
158 Ipv4Address address;
159 Ipv4Address network;
160
162 Ipv4Mask("255.0.0.0"),
163 Ipv4Address("0.0.0.3"));
164 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
165 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.3"), "200");
166 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
167 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("3.0.0.4"), "201");
168
169 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.0.0.0"));
170 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("4.0.0.0"), "202");
171 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.0.0.0"));
172 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("4.0.0.5"), "203");
173
175 Ipv4Mask("255.255.0.0"),
176 Ipv4Address("0.0.0.3"));
177 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
178 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.3"), "204");
179 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
180 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.3.0.4"), "205");
181
182 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.0.0"));
183 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.4.0.0"), "206");
184 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.0.0"));
185 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.4.0.5"), "207");
186
188 Ipv4Mask("255.255.255.0"),
189 Ipv4Address("0.0.0.3"));
190 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
191 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.3"), "208");
192 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
193 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.3.4"), "209");
194
195 network = Ipv4AddressGenerator::NextNetwork(Ipv4Mask("255.255.255.0"));
196 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.4.0"), "210");
197 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
198 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.4.5"), "211");
199}
200
201/**
202 * @ingroup internet-test
203 *
204 * @brief IPv4 AddressGenerator example (sort of) Test
205 */
207{
208 public:
210
211 private:
212 void DoRun() override;
213 void DoTeardown() override;
214};
215
217 : TestCase("A quick kindof-semi-almost-real example")
218{
219}
220
221void
226
227void
229{
230 Ipv4Address address;
231 //
232 // First, initialize our /24 network to 192.168.0.0 and begin
233 // allocating with ip address 0.0.0.3 out of that prefix.
234 //
236 Ipv4Mask("255.255.255.0"),
237 Ipv4Address("0.0.0.3"));
238 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
239 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.3"), "300");
240 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
241 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.4"), "301");
242 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
243 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.0.5"), "302");
244 //
245 // Allocate the next network out of our /24 network (this should be
246 // 192.168.1.0) and begin allocating with IP address 0.0.0.3 out of that
247 // prefix.
248 //
250 Ipv4AddressGenerator::InitAddress(Ipv4Address("0.0.0.3"), Ipv4Mask("255.255.255.0"));
251 //
252 // The first address we should get is the previous numbers ORed together, which
253 // is 192.168.1.3, of course.
254 //
255 address = Ipv4AddressGenerator::NextAddress(Ipv4Mask("255.255.255.0"));
256 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("192.168.1.3"), "304");
257}
258
259/**
260 * @ingroup internet-test
261 *
262 * @brief IPv4 address collision Test
263 */
265{
266 public:
268
269 private:
270 void DoRun() override;
271 void DoTeardown() override;
272};
273
275 : TestCase("Make sure that the address collision logic works.")
276{
277}
278
279void
285
286void
288{
293
298
303
308
313
315 bool allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.21");
316 NS_TEST_EXPECT_MSG_EQ(allocated, false, "0.0.0.21 should not be already allocated");
317 bool added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
318 NS_TEST_EXPECT_MSG_EQ(added, true, "400");
319
320 allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.4");
321 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.4 should be already allocated");
322 added = Ipv4AddressGenerator::AddAllocated("0.0.0.4");
323 NS_TEST_EXPECT_MSG_EQ(added, false, "401");
324
325 allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.9");
326 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.9 should be already allocated");
327 added = Ipv4AddressGenerator::AddAllocated("0.0.0.9");
328 NS_TEST_EXPECT_MSG_EQ(added, false, "402");
329
330 allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.16");
331 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.16 should be already allocated");
332 added = Ipv4AddressGenerator::AddAllocated("0.0.0.16");
333 NS_TEST_EXPECT_MSG_EQ(added, false, "403");
334
335 allocated = Ipv4AddressGenerator::IsAddressAllocated("0.0.0.21");
336 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0.0.0.21 should be already allocated");
337 added = Ipv4AddressGenerator::AddAllocated("0.0.0.21");
338 NS_TEST_EXPECT_MSG_EQ(added, false, "404");
339}
340
341/**
342 * @ingroup internet-test
343 *
344 * @brief IPv4 Address Generator TestSuite
345 */
347{
348 public:
350
351 private:
352};
353
355 : TestSuite("ipv4-address-generator", Type::UNIT)
356{
357 AddTestCase(new NetworkNumberAllocatorTestCase(), TestCase::Duration::QUICK);
358 AddTestCase(new AddressAllocatorTestCase(), TestCase::Duration::QUICK);
359 AddTestCase(new NetworkAndAddressTestCase(), TestCase::Duration::QUICK);
360 AddTestCase(new ExampleAddressGeneratorTestCase(), TestCase::Duration::QUICK);
361 AddTestCase(new AddressCollisionTestCase(), TestCase::Duration::QUICK);
362}
363
365 g_ipv4AddressGeneratorTestSuite; //!< Static variable for test initialization
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.
a class to represent an Ipv4 address mask
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#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:241
static Ipv4AddressGeneratorTestSuite g_ipv4AddressGeneratorTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.