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 address;
99
100 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
101 address = h.NewAddress();
102 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "200");
103 address = h.NewAddress();
104 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "201");
105
106 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
107 address = h.NewAddress();
108 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "202");
109 address = h.NewAddress();
110 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "203");
111
112 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
113 address = h.NewAddress();
114 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "204");
115 address = h.NewAddress();
116 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "205");
117}
118
119/**
120 * @ingroup internet-test
121 *
122 * @brief IPv4 reset allocator helper Test
123 */
125{
126 public:
128 void DoRun() override;
129 void DoTeardown() override;
130};
131
133 : TestCase("Make sure the reset to base behavior is working")
134{
135}
136
137void
139{
140 Ipv4Address network;
141 Ipv4Address address;
143
144 //
145 // We're going to use some of the same addresses allocated above,
146 // so reset the Ipv4AddressGenerator to make it forget we did.
147 //
148
149 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
150 address = h.NewAddress();
151 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "301");
152 address = h.NewAddress();
153 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "302");
154 network = h.NewNetwork();
155 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "303");
156 address = h.NewAddress();
157 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.3"), "304");
158
159 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
160 address = h.NewAddress();
161 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "305");
162 address = h.NewAddress();
163 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "306");
164 network = h.NewNetwork();
165 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "307");
166 address = h.NewAddress();
167 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.3"), "308");
168
169 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
170 address = h.NewAddress();
171 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "309");
172 address = h.NewAddress();
173 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "310");
174 network = h.NewNetwork();
175 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "311");
176 address = h.NewAddress();
177 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.3"), "312");
178}
179
180void
186
187/**
188 * @ingroup internet-test
189 *
190 * @brief IPv4 address helper Test
191 */
193{
194 public:
197
198 private:
199 void DoRun() override;
200 void DoTeardown() override;
201};
202
204 : TestCase("IpAddressHelper Ipv4 test case (similar to IPv6)")
205{
206}
207
211
212void
214{
216 Ipv4Address ipAddr1;
217 ipAddr1 = ip1.NewAddress();
218 // Ipv4AddressHelper that is unconfigured
219 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("255.255.255.255"), "Ipv4AddressHelper failure");
220
221 ip1.SetBase("192.168.0.0", "255.255.255.0");
222 ipAddr1 = ip1.NewAddress();
223 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.1"), "Ipv4AddressHelper failure");
224 ipAddr1 = ip1.NewAddress();
225 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.2"), "Ipv4AddressHelper failure");
226 ip1.NewNetwork();
227 ipAddr1 = ip1.NewAddress();
228 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.1.1"), "Ipv4AddressHelper failure");
229 ip1.NewNetwork(); // 192.168.2
230 ip1.NewNetwork(); // 192.168.3
231 ip1.NewNetwork(); // 192.168.4
232 ipAddr1 = ip1.NewAddress(); // 4.1
233 ipAddr1 = ip1.NewAddress(); // 4.2
234 ipAddr1 = ip1.NewAddress(); // 4.3
235 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.4.3"), "Ipv4AddressHelper failure");
236
237 // reset base to start at 192.168.0.100
238 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.100");
239 ipAddr1 = ip1.NewAddress();
240 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.100"), "Ipv4AddressHelper failure");
241
242 // rollover
243 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.254");
244 ipAddr1 = ip1.NewAddress(); // .254
245 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.254"), "Ipv4AddressHelper failure");
246 // The below will overflow and assert, so it is commented out
247 // ipAddr1 = ip1.NewAddress (); // .255
248
249 // create with arguments
250 Ipv4AddressHelper ip2 = Ipv4AddressHelper("192.168.1.0", "255.255.255.0", "0.0.0.1");
251 // duplicate assignment
252 ip2.NewNetwork(); // 192.168.2
253 ip2.NewNetwork(); // 192.168.3
254 ip2.NewNetwork(); // 192.168.4
255 // Uncomment below, and 192.168.4.1 will crash since it was allocated above
256 // ipAddr1 = ip2.NewAddress (); // 4.1
257}
258
259void
265
266/**
267 * @ingroup internet-test
268 *
269 * @brief IPv4 Address Helper TestSuite
270 */
272{
273 public:
275
276 private:
277};
278
287
289 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
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:293
@ QUICK
Fast test.
Definition test.h:1054
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1257
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:491
#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:133
#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:240
static Ipv4AddressHelperTestSuite g_ipv4AddressHelperTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.