A Discrete-Event Network Simulator
API
ipv4-address-helper-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/test.h"
20 #include "ns3/ipv4-address-generator.h"
21 #include "ns3/ipv4-address-helper.h"
22 #include "ns3/simulator.h"
23 
24 using namespace ns3;
25 
33 {
34 public:
36 private:
37  virtual void DoRun (void);
38  virtual void DoTeardown (void);
39 };
40 
42  : TestCase ("Make sure the network allocator part is working on some common network prefixes.")
43 {
44 }
45 
46 void
48 {
50  Simulator::Destroy ();
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 
85 {
86 public:
88 private:
89  virtual void DoRun (void);
90  virtual void DoTeardown (void);
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 
138 {
139 public:
141  virtual void DoRun (void);
142  virtual void DoTeardown (void);
143 };
144 
146  : TestCase ("Make sure the reset to base behavior is working")
147 {
148 }
149 
150 void
152 {
153  Ipv4Address network;
156 
157  //
158  // We're going to use some of the same addresses allocated above,
159  // so reset the Ipv4AddressGenerator to make it forget we did.
160  //
161 
162  h.SetBase ("1.0.0.0", "255.0.0.0", "0.0.0.3");
163  address = h.NewAddress ();
164  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.3"), "301");
165  address = h.NewAddress ();
166  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.4"), "302");
167  network = h.NewNetwork ();
168  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("2.0.0.0"), "303");
169  address = h.NewAddress ();
170  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("2.0.0.3"), "304");
171 
172  h.SetBase ("0.1.0.0", "255.255.0.0", "0.0.0.3");
173  address = h.NewAddress ();
174  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.3"), "305");
175  address = h.NewAddress ();
176  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.4"), "306");
177  network = h.NewNetwork ();
178  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.2.0.0"), "307");
179  address = h.NewAddress ();
180  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.2.0.3"), "308");
181 
182  h.SetBase ("0.0.1.0", "255.255.255.0", "0.0.0.3");
183  address = h.NewAddress ();
184  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.3"), "309");
185  address = h.NewAddress ();
186  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.4"), "310");
187  network = h.NewNetwork ();
188  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.2.0"), "311");
189  address = h.NewAddress ();
190  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.2.3"), "312");
191 }
192 
193 void
195 {
197  Simulator::Destroy ();
198 }
199 
207 {
208 public:
210  virtual ~IpAddressHelperTestCasev4 ();
211 
212 private:
213  virtual void DoRun (void);
214  virtual void DoTeardown (void);
215 };
216 
218  : TestCase ("IpAddressHelper Ipv4 test case (similar to IPv6)")
219 {
220 }
221 
223 {
224 }
225 
226 void
228 {
229  Ipv4AddressHelper ip1;
230  Ipv4Address ipAddr1;
231  ipAddr1 = ip1.NewAddress ();
232  // Ipv4AddressHelper that is unconfigured
233  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("255.255.255.255"), "Ipv4AddressHelper failure");
234 
235  ip1.SetBase ("192.168.0.0", "255.255.255.0");
236  ipAddr1 = ip1.NewAddress ();
237  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.1"), "Ipv4AddressHelper failure");
238  ipAddr1 = ip1.NewAddress ();
239  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.2"), "Ipv4AddressHelper failure");
240  ip1.NewNetwork ();
241  ipAddr1 = ip1.NewAddress ();
242  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.1.1"), "Ipv4AddressHelper failure");
243  ip1.NewNetwork (); // 192.168.2
244  ip1.NewNetwork (); // 192.168.3
245  ip1.NewNetwork (); // 192.168.4
246  ipAddr1 = ip1.NewAddress (); // 4.1
247  ipAddr1 = ip1.NewAddress (); // 4.2
248  ipAddr1 = ip1.NewAddress (); // 4.3
249  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.4.3"), "Ipv4AddressHelper failure");
250 
251  // reset base to start at 192.168.0.100
252  ip1.SetBase ("192.168.0.0", "255.255.255.0", "0.0.0.100");
253  ipAddr1 = ip1.NewAddress ();
254  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.100"), "Ipv4AddressHelper failure");
255 
256  // rollover
257  ip1.SetBase ("192.168.0.0", "255.255.255.0", "0.0.0.254");
258  ipAddr1 = ip1.NewAddress (); // .254
259  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.254"), "Ipv4AddressHelper failure");
260  // The below will overflow and assert, so it is commented out
261  // ipAddr1 = ip1.NewAddress (); // .255
262 
263  // create with arguments
264  Ipv4AddressHelper ip2 = Ipv4AddressHelper ("192.168.1.0", "255.255.255.0", "0.0.0.1");
265  // duplicate assignment
266  ip2.NewNetwork (); // 192.168.2
267  ip2.NewNetwork (); // 192.168.3
268  ip2.NewNetwork (); // 192.168.4
269  // Uncomment below, and 192.168.4.1 will crash since it was allocated above
270  // ipAddr1 = ip2.NewAddress (); // 4.1
271 
272 }
273 
274 void
276 {
278  Simulator::Destroy ();
279 }
280 
281 
289 {
290 public:
292 private:
293 };
294 
296  : TestSuite ("ipv4-address-helper", UNIT)
297 {
298  AddTestCase (new NetworkAllocatorHelperTestCase (), TestCase::QUICK);
299  AddTestCase (new AddressAllocatorHelperTestCase (), TestCase::QUICK);
300  AddTestCase (new ResetAllocatorHelperTestCase (), TestCase::QUICK);
301  AddTestCase (new IpAddressHelperTestCasev4 (), TestCase::QUICK);
302 }
303 
305 
306 
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
ns3::Ipv4AddressHelper::NewNetwork
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Definition: ipv4-address-helper.cc:126
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
AddressAllocatorHelperTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv4-address-helper-test-suite.cc:106
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::Config::Reset
void Reset(void)
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition: config.cc:820
AddressAllocatorHelperTestCase::AddressAllocatorHelperTestCase
AddressAllocatorHelperTestCase()
Definition: ipv4-address-helper-test-suite.cc:93
ResetAllocatorHelperTestCase
IPv4 reset allocator helper Test.
Definition: ipv4-address-helper-test-suite.cc:138
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ResetAllocatorHelperTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv4-address-helper-test-suite.cc:151
ResetAllocatorHelperTestCase::ResetAllocatorHelperTestCase
ResetAllocatorHelperTestCase()
Definition: ipv4-address-helper-test-suite.cc:145
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
AddressAllocatorHelperTestCase
IPv4 address allocator helper Test.
Definition: ipv4-address-helper-test-suite.cc:85
Ipv4AddressHelperTestSuite::Ipv4AddressHelperTestSuite
Ipv4AddressHelperTestSuite()
Definition: ipv4-address-helper-test-suite.cc:295
IpAddressHelperTestCasev4::IpAddressHelperTestCasev4
IpAddressHelperTestCasev4()
Definition: ipv4-address-helper-test-suite.cc:217
AddressAllocatorHelperTestCase::DoTeardown
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: ipv4-address-helper-test-suite.cc:99
first.address
address
Definition: first.py:44
NS_TEST_EXPECT_MSG_EQ
#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:283
g_ipv4AddressHelperTestSuite
static Ipv4AddressHelperTestSuite g_ipv4AddressHelperTestSuite
Static variable for test initialization.
Definition: ipv4-address-helper-test-suite.cc:304
NetworkAllocatorHelperTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv4-address-helper-test-suite.cc:53
ResetAllocatorHelperTestCase::DoTeardown
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: ipv4-address-helper-test-suite.cc:194
NetworkAllocatorHelperTestCase::DoTeardown
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: ipv4-address-helper-test-suite.cc:47
ns3::TestSuite
A suite of tests to run.
Definition: test.h:1344
NS_TEST_ASSERT_MSG_EQ
#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:166
IpAddressHelperTestCasev4::~IpAddressHelperTestCasev4
virtual ~IpAddressHelperTestCasev4()
Definition: ipv4-address-helper-test-suite.cc:222
NetworkAllocatorHelperTestCase
IPv4 network allocator helper Test.
Definition: ipv4-address-helper-test-suite.cc:33
IpAddressHelperTestCasev4
IPv4 address helper Test.
Definition: ipv4-address-helper-test-suite.cc:207
NetworkAllocatorHelperTestCase::NetworkAllocatorHelperTestCase
NetworkAllocatorHelperTestCase()
Definition: ipv4-address-helper-test-suite.cc:41
IpAddressHelperTestCasev4::DoTeardown
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: ipv4-address-helper-test-suite.cc:275
IpAddressHelperTestCasev4::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv4-address-helper-test-suite.cc:227
Ipv4AddressHelperTestSuite
IPv4 Address Helper TestSuite.
Definition: ipv4-address-helper-test-suite.cc:289
ns3::Ipv4AddressHelper::NewAddress
Ipv4Address NewAddress(void)
Increment the IP address counter used to allocate IP addresses.
Definition: ipv4-address-helper.cc:102