blob: 387ea70c5a38627a685927c8f0feffebf443c436 [file] [log] [blame]
Pau Espin Pedrol7e786812017-11-24 12:00:35 +01001#!/usr/bin/env python2
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02002
3# (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
4# (C) 2013 by Holger Hans Peter Freyther
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
Max3e676892016-11-16 14:55:21 +010018import os, sys
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020019import time
20import unittest
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020021import socket
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +010022import subprocess
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020023
24import osmopy.obscvty as obscvty
25import osmopy.osmoutil as osmoutil
Maxf8dc5262017-12-19 18:04:18 +010026from osmopy.osmo_ipa import IPA
Max3e676892016-11-16 14:55:21 +010027
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010028# to be able to find $top_srcdir/doc/...
29confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020030
31class TestVTYBase(unittest.TestCase):
32
Neels Hofmeyrc29505e2016-05-20 21:59:55 +020033 def checkForEndAndExit(self):
34 res = self.vty.command("list")
35 #print ('looking for "exit"\n')
36 self.assert_(res.find(' exit\r') > 0)
37 #print 'found "exit"\nlooking for "end"\n'
38 self.assert_(res.find(' end\r') > 0)
39 #print 'found "end"\n'
40
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020041 def vty_command(self):
42 raise Exception("Needs to be implemented by a subclass")
43
44 def vty_app(self):
45 raise Exception("Needs to be implemented by a subclass")
46
47 def setUp(self):
48 osmo_vty_cmd = self.vty_command()[:]
49 config_index = osmo_vty_cmd.index('-c')
50 if config_index:
51 cfi = config_index + 1
52 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
53
54 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020055 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
56 except OSError:
57 print >> sys.stderr, "Current directory: %s" % os.getcwd()
58 print >> sys.stderr, "Consider setting -b"
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020059
60 appstring = self.vty_app()[2]
61 appport = self.vty_app()[0]
62 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
63
64 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010065 if self.vty:
66 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020067 self.vty = None
68 osmoutil.end_proc(self.proc)
69
Jacob Erlbeck96903c42013-09-02 13:17:14 +020070
71class TestVTYGenericBSC(TestVTYBase):
72
Neels Hofmeyrc29505e2016-05-20 21:59:55 +020073 def _testConfigNetworkTree(self, include_bsc_items=True):
Jacob Erlbeck96903c42013-09-02 13:17:14 +020074 self.vty.enable()
75 self.assertTrue(self.vty.verify("configure terminal",['']))
76 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +010077 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +020078 self.assertTrue(self.vty.verify("network",['']))
79 self.assertEquals(self.vty.node(), 'config-net')
80 self.checkForEndAndExit()
81 self.assertTrue(self.vty.verify("bts 0",['']))
82 self.assertEquals(self.vty.node(), 'config-net-bts')
83 self.checkForEndAndExit()
84 self.assertTrue(self.vty.verify("trx 0",['']))
85 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
86 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +020087 self.vty.command("write terminal")
88 self.assertTrue(self.vty.verify("exit",['']))
89 self.assertEquals(self.vty.node(), 'config-net-bts')
90 self.assertTrue(self.vty.verify("exit",['']))
91 self.assertTrue(self.vty.verify("bts 1",['']))
92 self.assertEquals(self.vty.node(), 'config-net-bts')
93 self.checkForEndAndExit()
94 self.assertTrue(self.vty.verify("trx 1",['']))
95 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
96 self.checkForEndAndExit()
97 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +020098 self.assertTrue(self.vty.verify("exit",['']))
99 self.assertEquals(self.vty.node(), 'config-net-bts')
100 self.assertTrue(self.vty.verify("exit",['']))
101 self.assertEquals(self.vty.node(), 'config-net')
102 self.assertTrue(self.vty.verify("exit",['']))
103 self.assertEquals(self.vty.node(), 'config')
104 self.assertTrue(self.vty.verify("exit",['']))
105 self.assertTrue(self.vty.node() is None)
106
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100107
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200108class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200109
110 def vty_command(self):
111 return ["./src/osmo-bsc/osmo-bsc", "-c",
112 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
113
114 def vty_app(self):
115 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
116
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200117 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200118 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200119
120 def testVtyTree(self):
121 self.vty.enable()
122 self.assertTrue(self.vty.verify("configure terminal", ['']))
123 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100124 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200125 self.assertTrue(self.vty.verify("msc 0", ['']))
126 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200127 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200128 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200129 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200130 self.assertTrue(self.vty.verify("bsc", ['']))
131 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200132 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200133 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200134 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200135 self.assertTrue(self.vty.verify("exit", ['']))
136 self.assertTrue(self.vty.node() is None)
137
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200138 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200139 self.vty.enable()
140 self.vty.command("configure terminal")
141 self.vty.command("msc")
142
143 # Test invalid input
144 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200145 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200146 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200147
148 # Enable USSD notifications
149 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200150 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200151 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200152
153 # Verify settings
154 res = self.vty.command("write terminal")
155 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
156 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200157 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
158 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200159 self.assert_(res.find('bsc-grace-text In grace period') > 0)
160 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200161
162 # Now disable it..
163 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200164 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200165 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200166
167 # Verify settings
168 res = self.vty.command("write terminal")
169 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
170 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200171 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200172 self.assert_(res.find('no bsc-welcome-text') > 0)
173 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
174 self.assert_(res.find('no bsc-grace-text') > 0)
175
176 def testUssdNotificationsBsc(self):
177 self.vty.enable()
178 self.vty.command("configure terminal")
179 self.vty.command("bsc")
180
181 # Test invalid input
182 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
183
184 # Enable USSD notifications
185 self.vty.verify("missing-msc-text No MSC found", [''])
186
187 # Verify settings
188 res = self.vty.command("write terminal")
189 self.assert_(res.find('missing-msc-text No MSC found') > 0)
190 self.assertEquals(res.find('no missing-msc-text'), -1)
191
192 # Now disable it..
193 self.vty.verify("no missing-msc-text", [''])
194
195 # Verify settings
196 res = self.vty.command("write terminal")
197 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
198 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200199
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200200 def testNetworkTimezone(self):
201 self.vty.enable()
202 self.vty.verify("configure terminal", [''])
203 self.vty.verify("network", [''])
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200204
205 # Test invalid input
206 self.vty.verify("timezone", ['% Command incomplete.'])
207 self.vty.verify("timezone 20 0", ['% Unknown command.'])
208 self.vty.verify("timezone 0 11", ['% Unknown command.'])
209 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
210
211 # Set time zone without DST
212 self.vty.verify("timezone 2 30", [''])
213
214 # Verify settings
215 res = self.vty.command("write terminal")
216 self.assert_(res.find('timezone 2 30') > 0)
217 self.assertEquals(res.find('timezone 2 30 '), -1)
218
219 # Set time zone with DST
220 self.vty.verify("timezone 2 30 1", [''])
221
222 # Verify settings
223 res = self.vty.command("write terminal")
224 self.assert_(res.find('timezone 2 30 1') > 0)
225
226 # Now disable it..
227 self.vty.verify("no timezone", [''])
228
229 # Verify settings
230 res = self.vty.command("write terminal")
231 self.assertEquals(res.find(' timezone'), -1)
232
Ciabyec6e4f82014-03-06 17:20:55 +0100233 def testShowNetwork(self):
234 res = self.vty.command("show network")
235 self.assert_(res.startswith('BSC is on Country Code') >= 0)
236
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100237 def testPingPongConfiguration(self):
238 self.vty.enable()
239 self.vty.verify("configure terminal", [''])
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100240 self.vty.verify("msc 0", [''])
241
242 self.vty.verify("timeout-ping 12", [''])
243 self.vty.verify("timeout-pong 14", [''])
244 res = self.vty.command("show running-config")
245 self.assert_(res.find(" timeout-ping 12") > 0)
246 self.assert_(res.find(" timeout-pong 14") > 0)
247 self.assert_(res.find(" no timeout-ping advanced") > 0)
248
249 self.vty.verify("timeout-ping advanced", [''])
250 res = self.vty.command("show running-config")
251 self.assert_(res.find(" timeout-ping 12") > 0)
252 self.assert_(res.find(" timeout-pong 14") > 0)
253 self.assert_(res.find(" timeout-ping advanced") > 0)
254
255 self.vty.verify("no timeout-ping advanced", [''])
256 res = self.vty.command("show running-config")
257 self.assert_(res.find(" timeout-ping 12") > 0)
258 self.assert_(res.find(" timeout-pong 14") > 0)
259 self.assert_(res.find(" no timeout-ping advanced") > 0)
260
261 self.vty.verify("no timeout-ping", [''])
262 res = self.vty.command("show running-config")
263 self.assertEquals(res.find(" timeout-ping 12"), -1)
264 self.assertEquals(res.find(" timeout-pong 14"), -1)
265 self.assertEquals(res.find(" no timeout-ping advanced"), -1)
266 self.assert_(res.find(" no timeout-ping") > 0)
267
268 self.vty.verify("timeout-ping advanced", ['%ping handling is disabled. Enable it first.'])
269
270 # And back to enabling it
271 self.vty.verify("timeout-ping 12", [''])
272 self.vty.verify("timeout-pong 14", [''])
273 res = self.vty.command("show running-config")
274 self.assert_(res.find(" timeout-ping 12") > 0)
275 self.assert_(res.find(" timeout-pong 14") > 0)
276 self.assert_(res.find(" timeout-ping advanced") > 0)
277
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200278 def testMscDataCoreLACCI(self):
279 self.vty.enable()
280 res = self.vty.command("show running-config")
281 self.assertEquals(res.find("core-location-area-code"), -1)
282 self.assertEquals(res.find("core-cell-identity"), -1)
283
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200284 self.vty.command("configure terminal")
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200285 self.vty.command("msc 0")
286 self.vty.command("core-location-area-code 666")
287 self.vty.command("core-cell-identity 333")
288
289 res = self.vty.command("show running-config")
290 self.assert_(res.find("core-location-area-code 666") > 0)
291 self.assert_(res.find("core-cell-identity 333") > 0)
292
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200293class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200294
295 def vty_command(self):
Max49364482016-04-13 11:36:39 +0200296 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-l", "127.0.0.1", "-c",
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200297 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
298
299 def vty_app(self):
300 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
301
Max49364482016-04-13 11:36:39 +0200302 def testBSCreload(self):
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400303 # Use different port for the mock msc to avoid clashing with
304 # the osmo-bsc_nat itself
Holger Hans Peter Freytherf1a61bb2016-04-14 08:50:25 -0400305 ip = "127.0.0.1"
Holger Hans Peter Freythere98c9c72016-04-14 10:58:58 -0400306 port = 5522
Max49364482016-04-13 11:36:39 +0200307 self.vty.enable()
308 bscs1 = self.vty.command("show bscs-config")
309 nat_bsc_reload(self)
310 bscs2 = self.vty.command("show bscs-config")
311 # check that multiple calls to bscs-config-file give the same result
312 self.assertEquals(bscs1, bscs2)
313
314 # add new bsc
315 self.vty.command("configure terminal")
316 self.vty.command("nat")
317 self.vty.command("bsc 5")
318 self.vty.command("token key")
319 self.vty.command("location_area_code 666")
320 self.vty.command("end")
321
322 # update bsc token
323 self.vty.command("configure terminal")
324 self.vty.command("nat")
325 self.vty.command("bsc 1")
326 self.vty.command("token xyu")
327 self.vty.command("end")
328
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400329 nat_msc_ip(self, ip, port)
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100330 msc_socket, msc = nat_msc_test(self, ip, port, verbose=True)
331 try:
332 b0 = nat_bsc_sock_test(0, "lol", verbose=True, proc=self.proc)
333 b1 = nat_bsc_sock_test(1, "xyu", verbose=True, proc=self.proc)
334 b2 = nat_bsc_sock_test(5, "key", verbose=True, proc=self.proc)
Max49364482016-04-13 11:36:39 +0200335
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100336 self.assertEquals("3 BSCs configured", self.vty.command("show nat num-bscs-configured"))
337 self.assertTrue(3 == nat_bsc_num_con(self))
338 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
Max49364482016-04-13 11:36:39 +0200339
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100340 nat_bsc_reload(self)
341 bscs2 = self.vty.command("show bscs-config")
342 # check that the reset to initial config succeeded
343 self.assertEquals(bscs1, bscs2)
Max49364482016-04-13 11:36:39 +0200344
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100345 self.assertEquals("2 BSCs configured", self.vty.command("show nat num-bscs-configured"))
346 self.assertTrue(1 == nat_bsc_num_con(self))
347 rem = self.vty.command("show bsc connections").split(' ')
348 # remaining connection is for BSC0
349 self.assertEquals('0', rem[2])
350 # remaining connection is authorized
351 self.assertEquals('1', rem[4])
352 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
353 finally:
354 msc.close()
355 msc_socket.close()
Max49364482016-04-13 11:36:39 +0200356
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200357 def testVtyTree(self):
358 self.vty.enable()
359 self.assertTrue(self.vty.verify('configure terminal', ['']))
360 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100361 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200362 self.assertTrue(self.vty.verify('mgcp', ['']))
363 self.assertEquals(self.vty.node(), 'config-mgcp')
364 self.checkForEndAndExit()
365 self.assertTrue(self.vty.verify('exit', ['']))
366 self.assertEquals(self.vty.node(), 'config')
367 self.assertTrue(self.vty.verify('nat', ['']))
368 self.assertEquals(self.vty.node(), 'config-nat')
369 self.checkForEndAndExit()
370 self.assertTrue(self.vty.verify('bsc 0', ['']))
371 self.assertEquals(self.vty.node(), 'config-nat-bsc')
372 self.checkForEndAndExit()
373 self.assertTrue(self.vty.verify('exit', ['']))
374 self.assertEquals(self.vty.node(), 'config-nat')
375 self.assertTrue(self.vty.verify('exit', ['']))
376 self.assertEquals(self.vty.node(), 'config')
377 self.assertTrue(self.vty.verify('exit', ['']))
378 self.assertTrue(self.vty.node() is None)
379
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200380 def testRewriteNoRewrite(self):
381 self.vty.enable()
382 res = self.vty.command("configure terminal")
383 res = self.vty.command("nat")
384 res = self.vty.command("number-rewrite rewrite.cfg")
385 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200386
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400387 def testEnsureNoEnsureModeSet(self):
388 self.vty.enable()
389 res = self.vty.command("configure terminal")
390 res = self.vty.command("nat")
391
392 # Ensure the default
393 res = self.vty.command("show running-config")
394 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
395
396 self.vty.command("sdp-ensure-amr-mode-set")
397 res = self.vty.command("show running-config")
398 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
399
400 self.vty.command("no sdp-ensure-amr-mode-set")
401 res = self.vty.command("show running-config")
402 self.assert_(res.find('\n no sdp-ensure-amr-mode-set') > 0)
403
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200404 def testRewritePostNoRewrite(self):
405 self.vty.enable()
406 self.vty.command("configure terminal")
407 self.vty.command("nat")
408 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
409 self.vty.verify("no number-rewrite-post", [''])
410
411
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200412 def testPrefixTreeLoading(self):
413 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
414
415 self.vty.enable()
416 self.vty.command("configure terminal")
417 self.vty.command("nat")
418 res = self.vty.command("prefix-tree %s" % cfg)
419 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
420 self.vty.command("end")
421
422 res = self.vty.command("show prefix-tree")
423 self.assertEqual(res, '1,1\r\n12,2\r\n123,3\r\n1234,4\r\n12345,5\r\n123456,6\r\n1234567,7\r\n12345678,8\r\n123456789,9\r\n1234567890,10\r\n13,11\r\n14,12\r\n15,13\r\n16,14\r\n82,16\r\n823455,15\r\n+49123,17')
424
425 self.vty.command("configure terminal")
426 self.vty.command("nat")
427 self.vty.command("no prefix-tree")
428 self.vty.command("end")
429
430 res = self.vty.command("show prefix-tree")
431 self.assertEqual(res, "% there is now prefix tree loaded.")
432
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200433 def testUssdSideChannelProvider(self):
434 self.vty.command("end")
435 self.vty.enable()
436 self.vty.command("configure terminal")
437 self.vty.command("nat")
438 self.vty.command("ussd-token key")
439 self.vty.command("end")
440
441 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
442 self.assertTrue(res)
443
444 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
445 ussdSocket.connect(('127.0.0.1', 5001))
446 ussdSocket.settimeout(2.0)
447 print "Connected to %s:%d" % ussdSocket.getpeername()
448
449 print "Expecting ID_GET request"
450 data = ussdSocket.recv(4)
451 self.assertEqual(data, "\x00\x01\xfe\x04")
452
453 print "Going to send ID_RESP response"
Max3e676892016-11-16 14:55:21 +0100454 res = ussdSocket.send(IPA().id_resp(IPA().tag_name('key')))
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200455 self.assertEqual(res, 10)
456
457 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
458
459 print "Going to send PING request"
Max3e676892016-11-16 14:55:21 +0100460 res = ussdSocket.send(IPA().ping())
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200461 self.assertEqual(res, 4)
462
463 print "Expecting PONG response"
464 data = ussdSocket.recv(4)
465 self.assertEqual(data, "\x00\x01\xfe\x01")
466
467 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
468 self.assertTrue(res)
469
470 print "Going to shut down connection"
471 ussdSocket.shutdown(socket.SHUT_WR)
472
473 print "Expecting EOF"
474 data = ussdSocket.recv(4)
475 self.assertEqual(data, "")
476
477 ussdSocket.close()
478
479 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
480 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200481
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100482 def testAccessList(self):
483 """
484 Verify that the imsi-deny can have a reject cause or no reject cause
485 """
486 self.vty.enable()
487 self.vty.command("configure terminal")
488 self.vty.command("nat")
489
490 # Old default
491 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
492 res = self.vty.command("show running-config").split("\r\n")
493 asserted = False
494 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100495 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100496 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
497 asserted = True
498 self.assert_(asserted)
499
500 # Check the optional CM Service Reject Cause
501 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
502 res = self.vty.command("show running-config").split("\r\n")
503 asserted = False
504 for line in res:
505 if line.startswith(" access-list test-cm"):
506 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
507 asserted = True
508 self.assert_(asserted)
509
510 # Check the optional LU Reject Cause
511 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
512 res = self.vty.command("show running-config").split("\r\n")
513 asserted = False
514 for line in res:
515 if line.startswith(" access-list test-lu"):
516 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
517 asserted = True
518 self.assert_(asserted)
519
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +0200520
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200521def add_nat_test(suite, workdir):
522 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
523 print("Skipping the NAT test")
524 return
525 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
526 suite.addTest(test)
527
Max49364482016-04-13 11:36:39 +0200528def nat_bsc_reload(x):
529 x.vty.command("configure terminal")
530 x.vty.command("nat")
Neels Hofmeyr5809e1e2017-07-20 17:57:37 +0200531 x.vty.command("bscs-config-file bscs.cfg")
Max49364482016-04-13 11:36:39 +0200532 x.vty.command("end")
533
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400534def nat_msc_ip(x, ip, port):
Max49364482016-04-13 11:36:39 +0200535 x.vty.command("configure terminal")
536 x.vty.command("nat")
537 x.vty.command("msc ip " + ip)
Holger Hans Peter Freyther84ae27e2016-04-14 10:40:06 -0400538 x.vty.command("msc port " + str(port))
Max49364482016-04-13 11:36:39 +0200539 x.vty.command("end")
540
541def data2str(d):
Holger Hans Peter Freyther8bb62042016-04-14 21:40:04 -0400542 return d.encode('hex').lower()
Max49364482016-04-13 11:36:39 +0200543
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400544def nat_msc_test(x, ip, port, verbose = False):
Max49364482016-04-13 11:36:39 +0200545 msc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Neels Hofmeyr53403452017-02-28 02:38:43 +0100546 msc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +0200547 msc.settimeout(5)
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400548 msc.bind((ip, port))
Max49364482016-04-13 11:36:39 +0200549 msc.listen(5)
550 if (verbose):
551 print "MSC is ready at " + ip
Neels Hofmeyr89d20b62016-09-26 12:59:36 +0200552 conn = None
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +0200553 while True:
554 vty_response = x.vty.command("show msc connection")
555 print "'show msc connection' says: %r" % vty_response
556 if vty_response == "MSC is connected: 1":
557 # success
558 break;
559 if vty_response != "MSC is connected: 0":
560 raise Exception("Unexpected response to 'show msc connection'"
561 " vty command: %r" % vty_response)
562
563 timeout_retries = 6
564 while timeout_retries > 0:
565 try:
566 conn, addr = msc.accept()
567 print "MSC got connection from ", addr
568 break
569 except socket.timeout:
570 print "socket timed out."
571 timeout_retries -= 1
572 continue
573
Neels Hofmeyr89d20b62016-09-26 12:59:36 +0200574 if not conn:
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200575 raise Exception("VTY reports MSC is connected, but I haven't"
576 " connected yet: %r %r" % (ip, port))
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100577 return msc, conn
Max49364482016-04-13 11:36:39 +0200578
579def ipa_handle_small(x, verbose = False):
580 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +0100581 if len(s) != 4*2:
582 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +0200583 if "0001fe00" == s:
584 if (verbose):
585 print "\tBSC <- NAT: PING?"
Max3e676892016-11-16 14:55:21 +0100586 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +0200587 elif "0001fe06" == s:
588 if (verbose):
589 print "\tBSC <- NAT: IPA ID ACK"
Max3e676892016-11-16 14:55:21 +0100590 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +0200591 elif "0001fe00" == s:
592 if (verbose):
593 print "\tBSC <- NAT: PONG!"
594 else:
595 if (verbose):
596 print "\tBSC <- NAT: ", s
597
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100598def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200599 s = data2str(x.recv(38))
600 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100601 retries = 3
602 while True:
603 print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
604 try:
605 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
606 print "\tdone sending IPA identity(%s) at %s" % (tk,
607 time.strftime("%T"))
608 break
609 except:
610 print "\tfailed sending IPA identity at", time.strftime("%T")
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100611 if proc:
612 print "\tproc.poll() = %r" % proc.poll()
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100613 if retries < 1:
614 print "\tgiving up"
615 raise
Neels Hofmeyre02e1e72017-02-03 05:55:26 +0100616 print "\tretrying (%d attempts left)" % retries
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100617 retries -= 1
Max49364482016-04-13 11:36:39 +0200618 else:
619 if (verbose):
620 print "\tBSC <- NAT: ", s
621
622def nat_bsc_num_con(x):
623 return len(x.vty.command("show bsc connections").split('\n'))
624
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100625def nat_bsc_sock_test(nr, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200626 bsc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Holger Hans Peter Freyther2abf2b02016-04-14 21:13:51 -0400627 bsc.bind(('127.0.0.1', 0))
Max49364482016-04-13 11:36:39 +0200628 bsc.connect(('127.0.0.1', 5000))
629 if (verbose):
630 print "BSC%d " %nr
631 print "\tconnected to %s:%d" % bsc.getpeername()
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100632 if proc:
633 print "\tproc.poll() = %r" % proc.poll()
634 print "\tproc.pid = %r" % proc.pid
Max49364482016-04-13 11:36:39 +0200635 ipa_handle_small(bsc, verbose)
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100636 ipa_handle_resp(bsc, tk, verbose, proc=proc)
637 if proc:
638 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +0200639 bsc.recv(27) # MGCP msg
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100640 if proc:
641 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +0200642 ipa_handle_small(bsc, verbose)
643 return bsc
644
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200645def add_bsc_test(suite, workdir):
646 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
647 print("Skipping the BSC test")
648 return
649 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
650 suite.addTest(test)
651
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200652if __name__ == '__main__':
653 import argparse
654 import sys
655
656 workdir = '.'
657
658 parser = argparse.ArgumentParser()
659 parser.add_argument("-v", "--verbose", dest="verbose",
660 action="store_true", help="verbose mode")
661 parser.add_argument("-p", "--pythonconfpath", dest="p",
662 help="searchpath for config")
663 parser.add_argument("-w", "--workdir", dest="w",
664 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100665 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200666 args = parser.parse_args()
667
668 verbose_level = 1
669 if args.verbose:
670 verbose_level = 2
671
672 if args.w:
673 workdir = args.w
674
675 if args.p:
676 confpath = args.p
677
678 print "confpath %s, workdir %s" % (confpath, workdir)
679 os.chdir(workdir)
680 print "Running tests for specific VTY commands"
681 suite = unittest.TestSuite()
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200682 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200683 add_nat_test(suite, workdir)
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100684
685 if args.test_name:
686 osmoutil.pick_tests(suite, *args.test_name)
687
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +0200688 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200689 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +0200690
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200691# vim: shiftwidth=4 expandtab nocin ai