A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-address-helper-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/ipv4-address-helper.h"
9#include "ns3/simulator.h"
10#include "ns3/test.h"
11
12using namespace ns3;
13
14/**
15 * \ingroup internet-test
16 *
17 * \brief IPv4 network allocator helper Test
18 */
20{
21 public:
23
24 private:
25 void DoRun() override;
26 void DoTeardown() override;
27};
28
30 : TestCase("Make sure the network allocator part is working on some common network prefixes.")
31{
32}
33
34void
40
41void
43{
44 Ipv4Address address;
45 Ipv4Address network;
47
48 h.SetBase("1.0.0.0", "255.0.0.0");
49 network = h.NewNetwork();
50 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "100");
51 address = h.NewAddress();
52 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.1"), "101");
53
54 h.SetBase("0.1.0.0", "255.255.0.0");
55 network = h.NewNetwork();
56 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "102");
57 address = h.NewAddress();
58 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.1"), "103");
59
60 h.SetBase("0.0.1.0", "255.255.255.0");
61 network = h.NewNetwork();
62 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "104");
63 address = h.NewAddress();
64 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.1"), "105");
65}
66
67/**
68 * \ingroup internet-test
69 *
70 * \brief IPv4 address allocator helper Test
71 */
73{
74 public:
76
77 private:
78 void DoRun() override;
79 void DoTeardown() override;
80};
81
83 : TestCase("Make sure the address allocator part is working")
84{
85}
86
87void
93
94void
96{
97 Ipv4Address network;
98 Ipv4Address address;
100
101 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
102 address = h.NewAddress();
103 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "200");
104 address = h.NewAddress();
105 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "201");
106
107 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
108 address = h.NewAddress();
109 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "202");
110 address = h.NewAddress();
111 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "203");
112
113 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
114 address = h.NewAddress();
115 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "204");
116 address = h.NewAddress();
117 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "205");
118}
119
120/**
121 * \ingroup internet-test
122 *
123 * \brief IPv4 reset allocator helper Test
124 */
126{
127 public:
129 void DoRun() override;
130 void DoTeardown() override;
131};
132
134 : TestCase("Make sure the reset to base behavior is working")
135{
136}
137
138void
140{
141 Ipv4Address network;
142 Ipv4Address address;
144
145 //
146 // We're going to use some of the same addresses allocated above,
147 // so reset the Ipv4AddressGenerator to make it forget we did.
148 //
149
150 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
151 address = h.NewAddress();
152 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "301");
153 address = h.NewAddress();
154 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "302");
155 network = h.NewNetwork();
156 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "303");
157 address = h.NewAddress();
158 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.3"), "304");
159
160 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
161 address = h.NewAddress();
162 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "305");
163 address = h.NewAddress();
164 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "306");
165 network = h.NewNetwork();
166 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "307");
167 address = h.NewAddress();
168 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.3"), "308");
169
170 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
171 address = h.NewAddress();
172 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "309");
173 address = h.NewAddress();
174 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "310");
175 network = h.NewNetwork();
176 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "311");
177 address = h.NewAddress();
178 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.3"), "312");
179}
180
181void
187
188/**
189 * \ingroup internet-test
190 *
191 * \brief IPv4 address helper Test
192 */
194{
195 public:
198
199 private:
200 void DoRun() override;
201 void DoTeardown() override;
202};
203
205 : TestCase("IpAddressHelper Ipv4 test case (similar to IPv6)")
206{
207}
208
212
213void
215{
217 Ipv4Address ipAddr1;
218 ipAddr1 = ip1.NewAddress();
219 // Ipv4AddressHelper that is unconfigured
220 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("255.255.255.255"), "Ipv4AddressHelper failure");
221
222 ip1.SetBase("192.168.0.0", "255.255.255.0");
223 ipAddr1 = ip1.NewAddress();
224 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.1"), "Ipv4AddressHelper failure");
225 ipAddr1 = ip1.NewAddress();
226 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.2"), "Ipv4AddressHelper failure");
227 ip1.NewNetwork();
228 ipAddr1 = ip1.NewAddress();
229 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.1.1"), "Ipv4AddressHelper failure");
230 ip1.NewNetwork(); // 192.168.2
231 ip1.NewNetwork(); // 192.168.3
232 ip1.NewNetwork(); // 192.168.4
233 ipAddr1 = ip1.NewAddress(); // 4.1
234 ipAddr1 = ip1.NewAddress(); // 4.2
235 ipAddr1 = ip1.NewAddress(); // 4.3
236 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.4.3"), "Ipv4AddressHelper failure");
237
238 // reset base to start at 192.168.0.100
239 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.100");
240 ipAddr1 = ip1.NewAddress();
241 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.100"), "Ipv4AddressHelper failure");
242
243 // rollover
244 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.254");
245 ipAddr1 = ip1.NewAddress(); // .254
246 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.254"), "Ipv4AddressHelper failure");
247 // The below will overflow and assert, so it is commented out
248 // ipAddr1 = ip1.NewAddress (); // .255
249
250 // create with arguments
251 Ipv4AddressHelper ip2 = Ipv4AddressHelper("192.168.1.0", "255.255.255.0", "0.0.0.1");
252 // duplicate assignment
253 ip2.NewNetwork(); // 192.168.2
254 ip2.NewNetwork(); // 192.168.3
255 ip2.NewNetwork(); // 192.168.4
256 // Uncomment below, and 192.168.4.1 will crash since it was allocated above
257 // ipAddr1 = ip2.NewAddress (); // 4.1
258}
259
260void
266
267/**
268 * \ingroup internet-test
269 *
270 * \brief IPv4 Address Helper TestSuite
271 */
273{
274 public:
276
277 private:
278};
279
281 : TestSuite("ipv4-address-helper", Type::UNIT)
282{
283 AddTestCase(new NetworkAllocatorHelperTestCase(), TestCase::Duration::QUICK);
284 AddTestCase(new AddressAllocatorHelperTestCase(), TestCase::Duration::QUICK);
285 AddTestCase(new ResetAllocatorHelperTestCase(), TestCase::Duration::QUICK);
286 AddTestCase(new IpAddressHelperTestCasev4(), TestCase::Duration::QUICK);
287}
288
290 g_ipv4AddressHelperTestSuite; //!< Static variable for test initialization
IPv4 address allocator helper Test.
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.
IPv4 network allocator helper Test.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
IPv4 reset allocator helper Test.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
static void Reset()
Reset the networks and Ipv4Address to zero.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4Address NewAddress()
Increment the IP address counter used to allocate IP addresses.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4 addresses are stored in host order in this class.
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_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#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 Ipv4AddressHelperTestSuite g_ipv4AddressHelperTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.