A Discrete-Event Network Simulator
API
ipv4-address-helper-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/ipv4-address-helper.h"
20#include "ns3/simulator.h"
21#include "ns3/test.h"
22
23using namespace ns3;
24
32{
33 public:
35
36 private:
37 void DoRun() override;
38 void DoTeardown() override;
39};
40
42 : TestCase("Make sure the network allocator part is working on some common network prefixes.")
43{
44}
45
46void
48{
50 Simulator::Destroy();
51}
52
53void
55{
57 Ipv4Address network;
59
60 h.SetBase("1.0.0.0", "255.0.0.0");
61 network = h.NewNetwork();
62 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "100");
63 address = h.NewAddress();
64 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.1"), "101");
65
66 h.SetBase("0.1.0.0", "255.255.0.0");
67 network = h.NewNetwork();
68 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "102");
69 address = h.NewAddress();
70 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.1"), "103");
71
72 h.SetBase("0.0.1.0", "255.255.255.0");
73 network = h.NewNetwork();
74 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "104");
75 address = h.NewAddress();
76 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.1"), "105");
77}
78
86{
87 public:
89
90 private:
91 void DoRun() override;
92 void DoTeardown() override;
93};
94
96 : TestCase("Make sure the address allocator part is working")
97{
98}
99
100void
102{
104 Simulator::Destroy();
105}
106
107void
109{
110 Ipv4Address network;
113
114 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
115 address = h.NewAddress();
116 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "200");
117 address = h.NewAddress();
118 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "201");
119
120 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
121 address = h.NewAddress();
122 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "202");
123 address = h.NewAddress();
124 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "203");
125
126 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
127 address = h.NewAddress();
128 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "204");
129 address = h.NewAddress();
130 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "205");
131}
132
140{
141 public:
143 void DoRun() override;
144 void DoTeardown() override;
145};
146
148 : TestCase("Make sure the reset to base behavior is working")
149{
150}
151
152void
154{
155 Ipv4Address network;
158
159 //
160 // We're going to use some of the same addresses allocated above,
161 // so reset the Ipv4AddressGenerator to make it forget we did.
162 //
163
164 h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
165 address = h.NewAddress();
166 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "301");
167 address = h.NewAddress();
168 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "302");
169 network = h.NewNetwork();
170 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "303");
171 address = h.NewAddress();
172 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.3"), "304");
173
174 h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
175 address = h.NewAddress();
176 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "305");
177 address = h.NewAddress();
178 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "306");
179 network = h.NewNetwork();
180 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "307");
181 address = h.NewAddress();
182 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.3"), "308");
183
184 h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
185 address = h.NewAddress();
186 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "309");
187 address = h.NewAddress();
188 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "310");
189 network = h.NewNetwork();
190 NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "311");
191 address = h.NewAddress();
192 NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.3"), "312");
193}
194
195void
197{
199 Simulator::Destroy();
200}
201
209{
210 public:
213
214 private:
215 void DoRun() override;
216 void DoTeardown() override;
217};
218
220 : TestCase("IpAddressHelper Ipv4 test case (similar to IPv6)")
221{
222}
223
225{
226}
227
228void
230{
232 Ipv4Address ipAddr1;
233 ipAddr1 = ip1.NewAddress();
234 // Ipv4AddressHelper that is unconfigured
235 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("255.255.255.255"), "Ipv4AddressHelper failure");
236
237 ip1.SetBase("192.168.0.0", "255.255.255.0");
238 ipAddr1 = ip1.NewAddress();
239 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.1"), "Ipv4AddressHelper failure");
240 ipAddr1 = ip1.NewAddress();
241 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.2"), "Ipv4AddressHelper failure");
242 ip1.NewNetwork();
243 ipAddr1 = ip1.NewAddress();
244 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.1.1"), "Ipv4AddressHelper failure");
245 ip1.NewNetwork(); // 192.168.2
246 ip1.NewNetwork(); // 192.168.3
247 ip1.NewNetwork(); // 192.168.4
248 ipAddr1 = ip1.NewAddress(); // 4.1
249 ipAddr1 = ip1.NewAddress(); // 4.2
250 ipAddr1 = ip1.NewAddress(); // 4.3
251 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.4.3"), "Ipv4AddressHelper failure");
252
253 // reset base to start at 192.168.0.100
254 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.100");
255 ipAddr1 = ip1.NewAddress();
256 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.100"), "Ipv4AddressHelper failure");
257
258 // rollover
259 ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.254");
260 ipAddr1 = ip1.NewAddress(); // .254
261 NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.254"), "Ipv4AddressHelper failure");
262 // The below will overflow and assert, so it is commented out
263 // ipAddr1 = ip1.NewAddress (); // .255
264
265 // create with arguments
266 Ipv4AddressHelper ip2 = Ipv4AddressHelper("192.168.1.0", "255.255.255.0", "0.0.0.1");
267 // duplicate assignment
268 ip2.NewNetwork(); // 192.168.2
269 ip2.NewNetwork(); // 192.168.3
270 ip2.NewNetwork(); // 192.168.4
271 // Uncomment below, and 192.168.4.1 will crash since it was allocated above
272 // ipAddr1 = ip2.NewAddress (); // 4.1
273}
274
275void
277{
279 Simulator::Destroy();
280}
281
289{
290 public:
292
293 private:
294};
295
297 : TestSuite("ipv4-address-helper", UNIT)
298{
299 AddTestCase(new NetworkAllocatorHelperTestCase(), TestCase::QUICK);
300 AddTestCase(new AddressAllocatorHelperTestCase(), TestCase::QUICK);
301 AddTestCase(new ResetAllocatorHelperTestCase(), TestCase::QUICK);
302 AddTestCase(new IpAddressHelperTestCasev4(), TestCase::QUICK);
303}
304
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 Address Helper TestSuite.
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.
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.
Definition: ipv4-address.h:43
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
void Reset()
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition: config.cc:856
#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:144
#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:251
static Ipv4AddressHelperTestSuite g_ipv4AddressHelperTestSuite
Static variable for test initialization.
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.