A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-address-generator-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 * Copyright (c) 2011 Atishay Jain
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
8#include "ns3/ipv6-address-generator.h"
9#include "ns3/simulation-singleton.h"
10#include "ns3/test.h"
11
12using namespace ns3;
13
14/**
15 * \ingroup internet-test
16 *
17 * \brief IPv6 network number allocator Test
18 */
20{
21 public:
23 void DoRun() override;
24 void DoTeardown() override;
25};
26
28 : TestCase("Make sure the network number allocator is working on some of network prefixes.")
29{
30}
31
32void
37
38void
40{
41 Ipv6Address network;
42
43 Ipv6AddressGenerator::Init(Ipv6Address("1::0:0:0"), Ipv6Prefix("FFFF::0"), Ipv6Address("::"));
44 network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF::0"));
46 Ipv6Address("1::0:0:0"),
47 "network should equal the initialized network for given prefix");
49 NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("2::0:0:0"), "network should equal next network");
50
52 Ipv6Prefix("FFFF:FFFF::0"),
53 Ipv6Address("::"));
54 network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF:FFFF::0"));
56 Ipv6Address("0:1::0"),
57 "network should equal the initialized network for given prefix");
58 network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF::0"));
59 NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("0:2::0"), "network should equal next network");
60
62 Ipv6Prefix("FFFF:FFFF:FFFF::0"),
63 Ipv6Address("::0"));
64 network = Ipv6AddressGenerator::GetNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
66 Ipv6Address("0:0:1::0"),
67 "network should equal the initialized network for given prefix");
68 network = Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
69 NS_TEST_EXPECT_MSG_EQ(network, Ipv6Address("0:0:2::0"), "network should equal next network");
70}
71
72/**
73 * \ingroup internet-test
74 *
75 * \brief IPv6 address allocator Test
76 */
78{
79 public:
81
82 private:
83 void DoRun() override;
84 void DoTeardown() override;
85};
86
88 : TestCase("Sanity check on allocation of addresses")
89{
90}
91
92void
94{
95 Ipv6Address address;
96
100 Ipv6Address("2001::0"),
101 "address should equal the initialized address for given prefix");
104 NS_TEST_EXPECT_MSG_EQ(address,
105 Ipv6Address("2001:0:0:1::0"),
106 "address should equal the initialized address for given prefix");
108 NS_TEST_EXPECT_MSG_EQ(address,
109 Ipv6Address("2001:0:0:1::1"),
110 "address should equal the initialized address for given prefix");
112 NS_TEST_EXPECT_MSG_EQ(address,
113 Ipv6Address("2001:0:0:1::1"),
114 "address should equal the initialized address for given prefix");
116 NS_TEST_EXPECT_MSG_EQ(address,
117 Ipv6Address("2001:0:0:1::2"),
118 "address should equal the initialized address for given prefix");
119
122 NS_TEST_EXPECT_MSG_EQ(address,
123 Ipv6Address("1::3"),
124 "address should equal initialized address for given prefix");
126 NS_TEST_EXPECT_MSG_EQ(address, Ipv6Address("1::4"), "address should equal next address");
127}
128
129void
135
136/**
137 * \ingroup internet-test
138 *
139 * \brief IPv6 network number and address allocator Test
140 */
142{
143 public:
145 void DoRun() override;
146 void DoTeardown() override;
147};
148
150 : TestCase("Make sure Network and address allocation play together.")
151{
152}
153
154void
160
161void
163{
164 Ipv6Address address;
165 Ipv6Address network;
166
169 NS_TEST_EXPECT_MSG_EQ(address,
170 Ipv6Address("3::3"),
171 "address should equal initialized address for given prefix");
173 NS_TEST_EXPECT_MSG_EQ(address,
174 Ipv6Address("3::4"),
175 "address should equal next address for given prefix");
176
178 NS_TEST_EXPECT_MSG_EQ(network,
179 Ipv6Address("4::0"),
180 "address should equal next address for given prefix");
182 NS_TEST_EXPECT_MSG_EQ(address,
183 Ipv6Address("4::3"),
184 "address should equal next address for given prefix");
185}
186
187/**
188 * \ingroup internet-test
189 *
190 * \brief IPv6 example of an address generator Test
191 */
193{
194 public:
196
197 private:
198 void DoRun() override;
199 void DoTeardown() override;
200};
201
203 : TestCase("A typical real-world example")
204{
205}
206
207void
212
213void
215{
216 Ipv6Address address;
217
219 Ipv6Prefix("FFFF:FFFF:FFFF::0"),
220 Ipv6Address("::3"));
222 NS_TEST_EXPECT_MSG_EQ(address,
223 Ipv6Address("2001:0AB8::0:3"),
224 "address should equal initialized address for given prefix");
226 NS_TEST_EXPECT_MSG_EQ(address,
227 Ipv6Address("2001:0AB8::0:4"),
228 "address should equal next address for given prefix");
230 NS_TEST_EXPECT_MSG_EQ(address,
231 Ipv6Address("2001:0AB8::0:5"),
232 "address should equal next address for given prefix");
233 //
234 // Allocate the next network based on the prefix passed in, which should
235 // be 2001:0AB0:0001
236 //
237 Ipv6AddressGenerator::NextNetwork(Ipv6Prefix("FFFF:FFFF:FFFF::0"));
238 //
239 // reset first address to be allocated back to ::0:3
240 //
242 //
243 // The first address we should get is the network and address ORed
244 //
246 NS_TEST_EXPECT_MSG_EQ(address,
247 Ipv6Address("2001:0AB8:1::3"),
248 "address should equal initialized address for given prefix");
249}
250
251/**
252 * \ingroup internet-test
253 *
254 * \brief IPv6 address collision Test
255 */
257{
258 public:
260
261 private:
262 void DoRun() override;
263 void DoTeardown() override;
264};
265
267 : TestCase("Make sure that the address collision logic works.")
268{
269}
270
271void
277
278void
280{
285
290
295
300
305
307 bool allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:21");
308 NS_TEST_EXPECT_MSG_EQ(allocated, false, "0::0:21 should not be already allocated");
309 bool added = Ipv6AddressGenerator::AddAllocated("0::0:21");
310 NS_TEST_EXPECT_MSG_EQ(added, true, "address should get allocated");
311
312 allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:4");
313 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:4 should be already allocated");
314 added = Ipv6AddressGenerator::AddAllocated("0::0:4");
315 NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
316
317 allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:9");
318 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:9 should be already allocated");
319 added = Ipv6AddressGenerator::AddAllocated("0::0:9");
320 NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
321
322 allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:16");
323 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:16 should be already allocated");
324 added = Ipv6AddressGenerator::AddAllocated("0::0:16");
325 NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
326
327 allocated = Ipv6AddressGenerator::IsAddressAllocated("0::0:21");
328 NS_TEST_EXPECT_MSG_EQ(allocated, true, "0::0:21 should be already allocated");
329 added = Ipv6AddressGenerator::AddAllocated("0::0:21");
330 NS_TEST_EXPECT_MSG_EQ(added, false, "address should not get allocated");
331}
332
333/**
334 * \ingroup internet-test
335 *
336 * \brief IPv6 address generator TestSuite
337 */
339{
340 public:
342 : TestSuite("ipv6-address-generator")
343 {
344 AddTestCase(new NetworkNumber6AllocatorTestCase(), TestCase::Duration::QUICK);
345 AddTestCase(new AddressAllocator6TestCase(), TestCase::Duration::QUICK);
346 AddTestCase(new NetworkAndAddress6TestCase(), TestCase::Duration::QUICK);
347 AddTestCase(new ExampleAddress6GeneratorTestCase(), TestCase::Duration::QUICK);
348 AddTestCase(new AddressCollision6TestCase(), TestCase::Duration::QUICK);
349 }
350};
351
353 g_ipv6AddressGeneratorTestSuite; //!< 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 DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
IPv6 example of an address generator Test.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
IPv6 network number 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 void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
static void TestMode()
Used to turn off fatal errors and assertions, for testing.
static Ipv6Address NextAddress(const Ipv6Prefix prefix)
Allocate the next Ipv6Address for the configured network and prefix.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
static void InitAddress(const Ipv6Address interfaceId, const Ipv6Prefix prefix)
Set the interfaceId for the given Ipv6Prefix.
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
static Ipv6Address GetAddress(const Ipv6Prefix prefix)
Get the Ipv6Address that will be allocated upon NextAddress ()
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
static bool IsAddressAllocated(const Ipv6Address addr)
Check the Ipv6Address allocation in the list of IPv6 entries.
static void Reset()
Reset the networks and Ipv6Address to zero.
Describes an IPv6 address.
Describes an IPv6 prefix.
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
#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 Ipv6AddressGeneratorTestSuite g_ipv6AddressGeneratorTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.