A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv4-address-helper-test-suite.cc
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 
27 {
28 public:
30 private:
31  virtual void DoRun (void);
32  virtual void DoTeardown (void);
33 };
34 
35 NetworkAllocatorHelperTestCase::NetworkAllocatorHelperTestCase ()
36  : TestCase ("Make sure the network allocator part is working on some common network prefixes.")
37 {
38 }
39 
40 void
42 {
43  Ipv4AddressGenerator::Reset ();
44  Simulator::Destroy ();
45 }
46 void
48 {
49  Ipv4Address address;
50  Ipv4Address network;
52 
53  h.SetBase ("1.0.0.0", "255.0.0.0");
54  network = h.NewNetwork ();
55  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("2.0.0.0"), "XXX");
56  address = h.NewAddress ();
57  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("2.0.0.1"), "XXX");
58 
59  h.SetBase ("0.1.0.0", "255.255.0.0");
60  network = h.NewNetwork ();
61  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.2.0.0"), "XXX");
62  address = h.NewAddress ();
63  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.2.0.1"), "XXX");
64 
65  h.SetBase ("0.0.1.0", "255.255.255.0");
66  network = h.NewNetwork ();
67  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.2.0"), "XXX");
68  address = h.NewAddress ();
69  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.2.1"), "XXX");
70 }
71 
73 {
74 public:
76 private:
77  virtual void DoRun (void);
78  virtual void DoTeardown (void);
79 };
80 
81 AddressAllocatorHelperTestCase::AddressAllocatorHelperTestCase ()
82  : TestCase ("Make sure the address allocator part is working")
83 {
84 }
85 
86 void
88 {
89  Ipv4AddressGenerator::Reset ();
90  Simulator::Destroy ();
91 }
92 
93 void
95 {
96  Ipv4Address network;
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"), "XXX");
103  address = h.NewAddress ();
104  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.4"), "XXX");
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"), "XXX");
109  address = h.NewAddress ();
110  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.4"), "XXX");
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"), "XXX");
115  address = h.NewAddress ();
116  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.4"), "XXX");
117 }
118 
120 {
121 public:
123  virtual void DoRun (void);
124  virtual void DoTeardown (void);
125 };
126 
127 ResetAllocatorHelperTestCase::ResetAllocatorHelperTestCase ()
128  : TestCase ("Make sure the reset to base behavior is working")
129 {
130 }
131 
132 void
134 {
135  Ipv4Address network;
136  Ipv4Address address;
138 
139  //
140  // We're going to use some of the same addresses allocated above,
141  // so reset the Ipv4AddressGenerator to make it forget we did.
142  //
143 
144  h.SetBase ("1.0.0.0", "255.0.0.0", "0.0.0.3");
145  address = h.NewAddress ();
146  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.3"), "XXX");
147  address = h.NewAddress ();
148  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("1.0.0.4"), "XXX");
149  network = h.NewNetwork ();
150  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("2.0.0.0"), "XXX");
151  address = h.NewAddress ();
152  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("2.0.0.3"), "XXX");
153 
154  h.SetBase ("0.1.0.0", "255.255.0.0", "0.0.0.3");
155  address = h.NewAddress ();
156  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.3"), "XXX");
157  address = h.NewAddress ();
158  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.1.0.4"), "XXX");
159  network = h.NewNetwork ();
160  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.2.0.0"), "XXX");
161  address = h.NewAddress ();
162  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.2.0.3"), "XXX");
163 
164  h.SetBase ("0.0.1.0", "255.255.255.0", "0.0.0.3");
165  address = h.NewAddress ();
166  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.3"), "XXX");
167  address = h.NewAddress ();
168  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.1.4"), "XXX");
169  network = h.NewNetwork ();
170  NS_TEST_EXPECT_MSG_EQ (network, Ipv4Address ("0.0.2.0"), "XXX");
171  address = h.NewAddress ();
172  NS_TEST_EXPECT_MSG_EQ (address, Ipv4Address ("0.0.2.3"), "XXX");
173 }
174 
175 void
177 {
178  Ipv4AddressGenerator::Reset ();
179  Simulator::Destroy ();
180 }
181 
183 {
184 public:
186  virtual ~IpAddressHelperTestCasev4 ();
187 
188 private:
189  virtual void DoRun (void);
190  virtual void DoTeardown (void);
191 };
192 
193 IpAddressHelperTestCasev4::IpAddressHelperTestCasev4 ()
194  : TestCase ("IpAddressHelper Ipv4 test case (similar to IPv6)")
195 {
196 }
197 
198 IpAddressHelperTestCasev4::~IpAddressHelperTestCasev4 ()
199 {
200 }
201 
202 void
204 {
205  Ipv4AddressHelper ip1;
206  Ipv4Address ipAddr1;
207  ipAddr1 = ip1.NewAddress ();
208  // Ipv4AddressHelper that is unconfigured
209  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("255.255.255.255"), "Ipv4AddressHelper failure");
210 
211  ip1.SetBase ("192.168.0.0", "255.255.255.0");
212  ipAddr1 = ip1.NewAddress ();
213  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.1"), "Ipv4AddressHelper failure");
214  ipAddr1 = ip1.NewAddress ();
215  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.2"), "Ipv4AddressHelper failure");
216  ip1.NewNetwork ();
217  ipAddr1 = ip1.NewAddress ();
218  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.1.1"), "Ipv4AddressHelper failure");
219  ip1.NewNetwork (); // 192.168.2
220  ip1.NewNetwork (); // 192.168.3
221  ip1.NewNetwork (); // 192.168.4
222  ipAddr1 = ip1.NewAddress (); // 4.1
223  ipAddr1 = ip1.NewAddress (); // 4.2
224  ipAddr1 = ip1.NewAddress (); // 4.3
225  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.4.3"), "Ipv4AddressHelper failure");
226 
227  // reset base to start at 192.168.0.100
228  ip1.SetBase ("192.168.0.0", "255.255.255.0", "0.0.0.100");
229  ipAddr1 = ip1.NewAddress ();
230  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.100"), "Ipv4AddressHelper failure");
231 
232  // rollover
233  ip1.SetBase ("192.168.0.0", "255.255.255.0", "0.0.0.254");
234  ipAddr1 = ip1.NewAddress (); // .254
235  NS_TEST_ASSERT_MSG_EQ (ipAddr1, Ipv4Address ("192.168.0.254"), "Ipv4AddressHelper failure");
236  // The below will overflow and assert, so it is commented out
237  // ipAddr1 = ip1.NewAddress (); // .255
238 
239  // create with arguments
240  Ipv4AddressHelper ip2 = Ipv4AddressHelper ("192.168.1.0", "255.255.255.0", "0.0.0.1");
241  // duplicate assignment
242  ip2.NewNetwork (); // 192.168.2
243  ip2.NewNetwork (); // 192.168.3
244  ip2.NewNetwork (); // 192.168.4
245  // Uncomment below, and 192.168.4.1 will crash since it was allocated above
246  // ipAddr1 = ip2.NewAddress (); // 4.1
247 
248 }
249 
250 void
252 {
253  Ipv4AddressGenerator::Reset ();
254  Simulator::Destroy ();
255 }
256 
257 
259 {
260 public:
262  : TestSuite ("ipv4-address-helper", UNIT)
263  {
264  AddTestCase (new NetworkAllocatorHelperTestCase (), TestCase::QUICK);
265  AddTestCase (new AddressAllocatorHelperTestCase (), TestCase::QUICK);
266  AddTestCase (new ResetAllocatorHelperTestCase (), TestCase::QUICK);
267  AddTestCase (new IpAddressHelperTestCasev4 (), TestCase::QUICK);
268  }
269 } g_ipv4AddressHelperTestSuite;
A suite of tests to run.
Definition: test.h:962
virtual void DoRun(void)
Implementation to actually run this test case.
encapsulates test code
Definition: test.h:834
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
Ipv4Address NewAddress(void)
Increment the IP address counter used to allocate IP addresses.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
virtual void DoRun(void)
Implementation to actually run this test case.