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 
23 using namespace ns3;
24 
31 {
32  public:
34 
35  private:
36  void DoRun() override;
37  void DoTeardown() override;
38 };
39 
41  : TestCase("Make sure the network allocator part is working on some common network prefixes.")
42 {
43 }
44 
45 void
47 {
49  Simulator::Destroy();
50 }
51 
52 void
54 {
56  Ipv4Address network;
58 
59  h.SetBase("1.0.0.0", "255.0.0.0");
60  network = h.NewNetwork();
61  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "100");
62  address = h.NewAddress();
63  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.1"), "101");
64 
65  h.SetBase("0.1.0.0", "255.255.0.0");
66  network = h.NewNetwork();
67  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "102");
68  address = h.NewAddress();
69  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.1"), "103");
70 
71  h.SetBase("0.0.1.0", "255.255.255.0");
72  network = h.NewNetwork();
73  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "104");
74  address = h.NewAddress();
75  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.1"), "105");
76 }
77 
84 {
85  public:
87 
88  private:
89  void DoRun() override;
90  void DoTeardown() override;
91 };
92 
94  : TestCase("Make sure the address allocator part is working")
95 {
96 }
97 
98 void
100 {
102  Simulator::Destroy();
103 }
104 
105 void
107 {
108  Ipv4Address network;
111 
112  h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
113  address = h.NewAddress();
114  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "200");
115  address = h.NewAddress();
116  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "201");
117 
118  h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
119  address = h.NewAddress();
120  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "202");
121  address = h.NewAddress();
122  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "203");
123 
124  h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
125  address = h.NewAddress();
126  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "204");
127  address = h.NewAddress();
128  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "205");
129 }
130 
137 {
138  public:
140  void DoRun() override;
141  void DoTeardown() override;
142 };
143 
145  : TestCase("Make sure the reset to base behavior is working")
146 {
147 }
148 
149 void
151 {
152  Ipv4Address network;
155 
156  //
157  // We're going to use some of the same addresses allocated above,
158  // so reset the Ipv4AddressGenerator to make it forget we did.
159  //
160 
161  h.SetBase("1.0.0.0", "255.0.0.0", "0.0.0.3");
162  address = h.NewAddress();
163  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.3"), "301");
164  address = h.NewAddress();
165  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("1.0.0.4"), "302");
166  network = h.NewNetwork();
167  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("2.0.0.0"), "303");
168  address = h.NewAddress();
169  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("2.0.0.3"), "304");
170 
171  h.SetBase("0.1.0.0", "255.255.0.0", "0.0.0.3");
172  address = h.NewAddress();
173  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.3"), "305");
174  address = h.NewAddress();
175  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.1.0.4"), "306");
176  network = h.NewNetwork();
177  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.2.0.0"), "307");
178  address = h.NewAddress();
179  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.2.0.3"), "308");
180 
181  h.SetBase("0.0.1.0", "255.255.255.0", "0.0.0.3");
182  address = h.NewAddress();
183  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.3"), "309");
184  address = h.NewAddress();
185  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.1.4"), "310");
186  network = h.NewNetwork();
187  NS_TEST_EXPECT_MSG_EQ(network, Ipv4Address("0.0.2.0"), "311");
188  address = h.NewAddress();
189  NS_TEST_EXPECT_MSG_EQ(address, Ipv4Address("0.0.2.3"), "312");
190 }
191 
192 void
194 {
196  Simulator::Destroy();
197 }
198 
205 {
206  public:
208  ~IpAddressHelperTestCasev4() override;
209 
210  private:
211  void DoRun() override;
212  void DoTeardown() override;
213 };
214 
216  : TestCase("IpAddressHelper Ipv4 test case (similar to IPv6)")
217 {
218 }
219 
221 {
222 }
223 
224 void
226 {
227  Ipv4AddressHelper ip1;
228  Ipv4Address ipAddr1;
229  ipAddr1 = ip1.NewAddress();
230  // Ipv4AddressHelper that is unconfigured
231  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("255.255.255.255"), "Ipv4AddressHelper failure");
232 
233  ip1.SetBase("192.168.0.0", "255.255.255.0");
234  ipAddr1 = ip1.NewAddress();
235  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.1"), "Ipv4AddressHelper failure");
236  ipAddr1 = ip1.NewAddress();
237  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.2"), "Ipv4AddressHelper failure");
238  ip1.NewNetwork();
239  ipAddr1 = ip1.NewAddress();
240  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.1.1"), "Ipv4AddressHelper failure");
241  ip1.NewNetwork(); // 192.168.2
242  ip1.NewNetwork(); // 192.168.3
243  ip1.NewNetwork(); // 192.168.4
244  ipAddr1 = ip1.NewAddress(); // 4.1
245  ipAddr1 = ip1.NewAddress(); // 4.2
246  ipAddr1 = ip1.NewAddress(); // 4.3
247  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.4.3"), "Ipv4AddressHelper failure");
248 
249  // reset base to start at 192.168.0.100
250  ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.100");
251  ipAddr1 = ip1.NewAddress();
252  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.100"), "Ipv4AddressHelper failure");
253 
254  // rollover
255  ip1.SetBase("192.168.0.0", "255.255.255.0", "0.0.0.254");
256  ipAddr1 = ip1.NewAddress(); // .254
257  NS_TEST_ASSERT_MSG_EQ(ipAddr1, Ipv4Address("192.168.0.254"), "Ipv4AddressHelper failure");
258  // The below will overflow and assert, so it is commented out
259  // ipAddr1 = ip1.NewAddress (); // .255
260 
261  // create with arguments
262  Ipv4AddressHelper ip2 = Ipv4AddressHelper("192.168.1.0", "255.255.255.0", "0.0.0.1");
263  // duplicate assignment
264  ip2.NewNetwork(); // 192.168.2
265  ip2.NewNetwork(); // 192.168.3
266  ip2.NewNetwork(); // 192.168.4
267  // Uncomment below, and 192.168.4.1 will crash since it was allocated above
268  // ipAddr1 = ip2.NewAddress (); // 4.1
269 }
270 
271 void
273 {
275  Simulator::Destroy();
276 }
277 
284 {
285  public:
287 
288  private:
289 };
290 
292  : TestSuite("ipv4-address-helper", UNIT)
293 {
294  AddTestCase(new NetworkAllocatorHelperTestCase(), TestCase::QUICK);
295  AddTestCase(new AddressAllocatorHelperTestCase(), TestCase::QUICK);
296  AddTestCase(new ResetAllocatorHelperTestCase(), TestCase::QUICK);
297  AddTestCase(new IpAddressHelperTestCasev4(), TestCase::QUICK);
298 }
299 
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:42
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:301
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:855
#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:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.