blob: 68a697d7856eeb064685d003b4caf083b9485f92 [file] [log] [blame]
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001#!/usr/bin/env python
2
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
26
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010027# add $top_srcdir/contrib to find ipa.py
28sys.path.append(os.path.join(sys.path[0], '..', 'contrib'))
29
Max3e676892016-11-16 14:55:21 +010030from ipa import IPA
31
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010032# to be able to find $top_srcdir/doc/...
33confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020034
35class TestVTYBase(unittest.TestCase):
36
37 def vty_command(self):
38 raise Exception("Needs to be implemented by a subclass")
39
40 def vty_app(self):
41 raise Exception("Needs to be implemented by a subclass")
42
43 def setUp(self):
44 osmo_vty_cmd = self.vty_command()[:]
45 config_index = osmo_vty_cmd.index('-c')
46 if config_index:
47 cfi = config_index + 1
48 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
49
50 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020051 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
52 except OSError:
53 print >> sys.stderr, "Current directory: %s" % os.getcwd()
54 print >> sys.stderr, "Consider setting -b"
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020055
56 appstring = self.vty_app()[2]
57 appport = self.vty_app()[0]
58 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
59
60 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010061 if self.vty:
62 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020063 self.vty = None
64 osmoutil.end_proc(self.proc)
65
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020066class TestVTYMGCP(TestVTYBase):
67 def vty_command(self):
68 return ["./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "-c",
69 "doc/examples/osmo-bsc_mgcp/mgcp.cfg"]
70
71 def vty_app(self):
72 return (4243, "./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "OpenBSC MGCP", "mgcp")
73
74 def testForcePtime(self):
Neels Hofmeyr0867b722016-09-28 23:28:06 +020075 self.vty.enable()
76 res = self.vty.command("show running-config")
77 self.assert_(res.find(' rtp force-ptime 20\r') > 0)
78 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020079
Neels Hofmeyr0867b722016-09-28 23:28:06 +020080 self.vty.command("configure terminal")
81 self.vty.command("mgcp")
82 self.vty.command("no rtp force-ptime")
83 res = self.vty.command("show running-config")
84 self.assertEquals(res.find(' rtp force-ptime 20\r'), -1)
85 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020086
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +010087 def testOmitAudio(self):
88 self.vty.enable()
Neels Hofmeyr0867b722016-09-28 23:28:06 +020089 res = self.vty.command("show running-config")
90 self.assert_(res.find(' sdp audio-payload send-name\r') > 0)
91 self.assertEquals(res.find(' no sdp audio-payload send-name\r'), -1)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +010092
Neels Hofmeyr0867b722016-09-28 23:28:06 +020093 self.vty.command("configure terminal")
94 self.vty.command("mgcp")
95 self.vty.command("no sdp audio-payload send-name")
96 res = self.vty.command("show running-config")
97 self.assertEquals(res.find(' rtp sdp audio-payload send-name\r'), -1)
98 self.assert_(res.find(' no sdp audio-payload send-name\r') > 0)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +010099
100 # TODO: test it for the trunk!
101
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200102 def testBindAddr(self):
103 self.vty.enable()
104
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200105 self.vty.command("configure terminal")
106 self.vty.command("mgcp")
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200107
108 # enable.. disable bts-bind-ip
109 self.vty.command("rtp bts-bind-ip 254.253.252.250")
110 res = self.vty.command("show running-config")
111 self.assert_(res.find('rtp bts-bind-ip 254.253.252.250') > 0)
112 self.vty.command("no rtp bts-bind-ip")
113 res = self.vty.command("show running-config")
114 self.assertEquals(res.find(' rtp bts-bind-ip'), -1)
115
116 # enable.. disable net-bind-ip
117 self.vty.command("rtp net-bind-ip 254.253.252.250")
118 res = self.vty.command("show running-config")
119 self.assert_(res.find('rtp net-bind-ip 254.253.252.250') > 0)
120 self.vty.command("no rtp net-bind-ip")
121 res = self.vty.command("show running-config")
122 self.assertEquals(res.find(' rtp net-bind-ip'), -1)
123
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200124
125class TestVTYGenericBSC(TestVTYBase):
126
127 def checkForEndAndExit(self):
128 res = self.vty.command("list")
129 #print ('looking for "exit"\n')
130 self.assert_(res.find(' exit\r') > 0)
131 #print 'found "exit"\nlooking for "end"\n'
132 self.assert_(res.find(' end\r') > 0)
133 #print 'found "end"\n'
134
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200135 def _testConfigNetworkTree(self):
136 self.vty.enable()
137 self.assertTrue(self.vty.verify("configure terminal",['']))
138 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100139 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200140 self.assertTrue(self.vty.verify("network",['']))
141 self.assertEquals(self.vty.node(), 'config-net')
142 self.checkForEndAndExit()
143 self.assertTrue(self.vty.verify("bts 0",['']))
144 self.assertEquals(self.vty.node(), 'config-net-bts')
145 self.checkForEndAndExit()
146 self.assertTrue(self.vty.verify("trx 0",['']))
147 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
148 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +0200149 self.vty.command("write terminal")
150 self.assertTrue(self.vty.verify("exit",['']))
151 self.assertEquals(self.vty.node(), 'config-net-bts')
152 self.assertTrue(self.vty.verify("exit",['']))
153 self.assertTrue(self.vty.verify("bts 1",['']))
154 self.assertEquals(self.vty.node(), 'config-net-bts')
155 self.checkForEndAndExit()
156 self.assertTrue(self.vty.verify("trx 1",['']))
157 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
158 self.checkForEndAndExit()
159 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200160 self.assertTrue(self.vty.verify("exit",['']))
161 self.assertEquals(self.vty.node(), 'config-net-bts')
162 self.assertTrue(self.vty.verify("exit",['']))
163 self.assertEquals(self.vty.node(), 'config-net')
164 self.assertTrue(self.vty.verify("exit",['']))
165 self.assertEquals(self.vty.node(), 'config')
166 self.assertTrue(self.vty.verify("exit",['']))
167 self.assertTrue(self.vty.node() is None)
168
169class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200170
171 def vty_command(self):
172 return ["./src/osmo-nitb/osmo-nitb", "-c",
173 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
174
175 def vty_app(self):
176 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
177
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200178 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200179 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200180
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200181 def checkForSmpp(self):
182 """SMPP is not always enabled, check if it is"""
183 res = self.vty.command("list")
184 return "smpp" in res
185
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200186 def testSmppFirst(self):
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200187 # enable the configuration
188 self.vty.enable()
189 self.vty.command("configure terminal")
Holger Hans Peter Freythera2c41c42015-07-13 11:23:53 +0200190
191 if not self.checkForSmpp():
192 return
193
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200194 self.vty.command("smpp")
195
196 # check the default
197 res = self.vty.command("write terminal")
198 self.assert_(res.find(' no smpp-first') > 0)
199
200 self.vty.verify("smpp-first", [''])
201 res = self.vty.command("write terminal")
202 self.assert_(res.find(' smpp-first') > 0)
203 self.assertEquals(res.find('no smpp-first'), -1)
204
205 self.vty.verify("no smpp-first", [''])
206 res = self.vty.command("write terminal")
207 self.assert_(res.find('no smpp-first') > 0)
208
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200209 def testVtyTree(self):
210 self.vty.enable()
211 self.assertTrue(self.vty.verify("configure terminal", ['']))
212 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100213 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200214 self.assertTrue(self.vty.verify('mncc-int', ['']))
215 self.assertEquals(self.vty.node(), 'config-mncc-int')
216 self.checkForEndAndExit()
217 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200218
219 if self.checkForSmpp():
220 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200221 self.assertTrue(self.vty.verify('smpp', ['']))
222 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100223 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200224 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200225
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200226 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200227 self.assertTrue(self.vty.verify("exit", ['']))
228 self.assertTrue(self.vty.node() is None)
229
230 # Check searching for outer node's commands
231 self.vty.command("configure terminal")
232 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200233
234 if self.checkForSmpp():
235 self.vty.command('smpp')
236 self.assertEquals(self.vty.node(), 'config-smpp')
237 self.vty.command('mncc-int')
238
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200239 self.assertEquals(self.vty.node(), 'config-mncc-int')
240
Maxddee01f2016-05-24 14:23:27 +0200241 def testVtyAuthorization(self):
242 self.vty.enable()
243 self.vty.command("configure terminal")
244 self.vty.command("network")
245 self.assertTrue(self.vty.verify("auth policy closed", ['']))
246 self.assertTrue(self.vty.verify("auth policy regexp", ['']))
247 self.assertTrue(self.vty.verify("authorized-regexp ^001", ['']))
248 self.assertTrue(self.vty.verify("authorized-regexp 02$", ['']))
249 self.assertTrue(self.vty.verify("authorized-regexp *123.*", ['']))
250 self.vty.command("end")
251 self.vty.command("configure terminal")
252 self.vty.command("nitb")
253 self.assertTrue(self.vty.verify("subscriber-create-on-demand", ['']))
Maxe6052c42016-06-30 10:25:49 +0200254 self.assertTrue(self.vty.verify("subscriber-create-on-demand no-extension", ['']))
Maxddee01f2016-05-24 14:23:27 +0200255 self.vty.command("end")
256
Max0c1bc262016-04-20 12:06:06 +0200257 def testSi2Q(self):
258 self.vty.enable()
259 self.vty.command("configure terminal")
260 self.vty.command("network")
261 self.vty.command("bts 0")
262 before = self.vty.command("show running-config")
263 self.vty.command("si2quater neighbor-list add earfcn 1911 threshold 11 2")
264 self.vty.command("si2quater neighbor-list add earfcn 1924 threshold 11 3")
265 self.vty.command("si2quater neighbor-list add earfcn 2111 threshold 11")
266 self.vty.command("si2quater neighbor-list del earfcn 1911")
267 self.vty.command("si2quater neighbor-list del earfcn 1924")
268 self.vty.command("si2quater neighbor-list del earfcn 2111")
269 self.assertEquals(before, self.vty.command("show running-config"))
Max26679e02016-04-20 15:57:13 +0200270 self.vty.command("si2quater neighbor-list add uarfcn 1976 13 1")
271 self.vty.command("si2quater neighbor-list add uarfcn 1976 38 1")
272 self.vty.command("si2quater neighbor-list add uarfcn 1976 44 1")
273 self.vty.command("si2quater neighbor-list add uarfcn 1976 120 1")
274 self.vty.command("si2quater neighbor-list add uarfcn 1976 140 1")
275 self.vty.command("si2quater neighbor-list add uarfcn 1976 163 1")
276 self.vty.command("si2quater neighbor-list add uarfcn 1976 166 1")
277 self.vty.command("si2quater neighbor-list add uarfcn 1976 217 1")
278 self.vty.command("si2quater neighbor-list add uarfcn 1976 224 1")
279 self.vty.command("si2quater neighbor-list add uarfcn 1976 225 1")
280 self.vty.command("si2quater neighbor-list add uarfcn 1976 226 1")
281 self.vty.command("si2quater neighbor-list del uarfcn 1976 13")
282 self.vty.command("si2quater neighbor-list del uarfcn 1976 38")
283 self.vty.command("si2quater neighbor-list del uarfcn 1976 44")
284 self.vty.command("si2quater neighbor-list del uarfcn 1976 120")
285 self.vty.command("si2quater neighbor-list del uarfcn 1976 140")
286 self.vty.command("si2quater neighbor-list del uarfcn 1976 163")
287 self.vty.command("si2quater neighbor-list del uarfcn 1976 166")
288 self.vty.command("si2quater neighbor-list del uarfcn 1976 217")
289 self.vty.command("si2quater neighbor-list del uarfcn 1976 224")
290 self.vty.command("si2quater neighbor-list del uarfcn 1976 225")
291 self.vty.command("si2quater neighbor-list del uarfcn 1976 226")
292 self.assertEquals(before, self.vty.command("show running-config"))
Max0c1bc262016-04-20 12:06:06 +0200293
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200294 def testEnableDisablePeriodicLU(self):
295 self.vty.enable()
296 self.vty.command("configure terminal")
297 self.vty.command("network")
298 self.vty.command("bts 0")
299
300 # Test invalid input
301 self.vty.verify("periodic location update 0", ['% Unknown command.'])
302 self.vty.verify("periodic location update 5", ['% Unknown command.'])
303 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
304
305 # Enable periodic lu..
306 self.vty.verify("periodic location update 60", [''])
307 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200308 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200309 self.assertEquals(res.find('no periodic location update'), -1)
310
311 # Now disable it..
312 self.vty.verify("no periodic location update", [''])
313 res = self.vty.command("write terminal")
314 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200315 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200316
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100317 def testEnableDisableSiHacks(self):
318 self.vty.enable()
319 self.vty.command("configure terminal")
320 self.vty.command("network")
321 self.vty.command("bts 0")
322
323 # Enable periodic lu..
324 self.vty.verify("force-combined-si", [''])
325 res = self.vty.command("write terminal")
326 self.assert_(res.find(' force-combined-si') > 0)
327 self.assertEquals(res.find('no force-combined-si'), -1)
328
329 # Now disable it..
330 self.vty.verify("no force-combined-si", [''])
331 res = self.vty.command("write terminal")
332 self.assertEquals(res.find(' force-combined-si'), -1)
333 self.assert_(res.find('no force-combined-si') > 0)
334
Ivan Kluchnikov67920592013-09-16 13:13:04 +0400335 def testRachAccessControlClass(self):
336 self.vty.enable()
337 self.vty.command("configure terminal")
338 self.vty.command("network")
339 self.vty.command("bts 0")
340
341 # Test invalid input
342 self.vty.verify("rach access-control-class", ['% Command incomplete.'])
343 self.vty.verify("rach access-control-class 1", ['% Command incomplete.'])
344 self.vty.verify("rach access-control-class -1", ['% Unknown command.'])
345 self.vty.verify("rach access-control-class 10", ['% Unknown command.'])
346 self.vty.verify("rach access-control-class 16", ['% Unknown command.'])
347
348 # Barred rach access control classes
349 for classNum in range(16):
350 if classNum != 10:
351 self.vty.verify("rach access-control-class " + str(classNum) + " barred", [''])
352
353 # Verify settings
354 res = self.vty.command("write terminal")
355 for classNum in range(16):
356 if classNum != 10:
357 self.assert_(res.find("rach access-control-class " + str(classNum) + " barred") > 0)
358
359 # Allowed rach access control classes
360 for classNum in range(16):
361 if classNum != 10:
362 self.vty.verify("rach access-control-class " + str(classNum) + " allowed", [''])
363
364 # Verify settings
365 res = self.vty.command("write terminal")
366 for classNum in range(16):
367 if classNum != 10:
368 self.assertEquals(res.find("rach access-control-class " + str(classNum) + " barred"), -1)
369
Holger Hans Peter Freytherde392252016-04-01 19:44:00 +0200370 def testSubscriberCreateDeleteTwice(self):
371 """
372 OS#1657 indicates that there might be an issue creating the
373 same subscriber twice. This test will use the VTY command to
374 create a subscriber and then issue a second create command
375 with the same IMSI. The test passes if the VTY continues to
376 respond to VTY commands.
377 """
378 self.vty.enable()
379
380 imsi = "204300854013739"
381
382 # Initially we don't have this subscriber
383 self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi])
384
385 # Lets create one
386 res = self.vty.command('subscriber create imsi '+imsi)
387 self.assert_(res.find(" IMSI: "+imsi) > 0)
388 # And now create one again.
389 res2 = self.vty.command('subscriber create imsi '+imsi)
390 self.assert_(res2.find(" IMSI: "+imsi) > 0)
391 self.assertEqual(res, res2)
392
393 # Verify it has been created
394 res = self.vty.command('show subscriber imsi '+imsi)
395 self.assert_(res.find(" IMSI: "+imsi) > 0)
396
397 # Delete it
Max488902d2016-06-22 14:02:39 +0200398 res = self.vty.command('subscriber imsi ' + imsi + ' delete')
399 self.assert_("" == res)
Holger Hans Peter Freytherde392252016-04-01 19:44:00 +0200400
401 # Now it should not be there anymore
402 res = self.vty.command('show subscriber imsi '+imsi)
Max488902d2016-06-22 14:02:39 +0200403 self.assert_(('% No subscriber found for imsi ' + imsi) == res)
Holger Hans Peter Freytherde392252016-04-01 19:44:00 +0200404
405
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500406 def testSubscriberCreateDelete(self):
Alexander Chemerisbd6d40f2013-10-04 23:54:17 +0200407 self.vty.enable()
408
409 imsi = "204300854013739"
Maxe6052c42016-06-30 10:25:49 +0200410 imsi2 = "222301824913762"
411 imsi3 = "333500854113763"
412 imsi4 = "444583744053764"
Alexander Chemerisbd6d40f2013-10-04 23:54:17 +0200413
414 # Initially we don't have this subscriber
415 self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi])
416
417 # Lets create one
418 res = self.vty.command('subscriber create imsi '+imsi)
419 self.assert_(res.find(" IMSI: "+imsi) > 0)
Maxe6052c42016-06-30 10:25:49 +0200420 self.assert_(res.find("Extension") > 0)
Alexander Chemerisbd6d40f2013-10-04 23:54:17 +0200421
422 # Now we have it
423 res = self.vty.command('show subscriber imsi '+imsi)
424 self.assert_(res.find(" IMSI: "+imsi) > 0)
425
Maxe6052c42016-06-30 10:25:49 +0200426 # With narrow random interval
427 self.vty.command("configure terminal")
428 self.vty.command("nitb")
429 self.assertTrue(self.vty.verify("subscriber-create-on-demand", ['']))
430 # wrong interval
431 res = self.vty.command("subscriber-create-on-demand random 221 122")
432 # error string will contain arguments
433 self.assert_(res.find("122") > 0)
434 self.assert_(res.find("221") > 0)
435 # correct interval - silent ok
436 self.assertTrue(self.vty.verify("subscriber-create-on-demand random 221 222", ['']))
437 self.vty.command("end")
438
439 res = self.vty.command('subscriber create imsi ' + imsi2)
440 self.assert_(res.find(" IMSI: " + imsi2) > 0)
441 self.assert_(res.find("221") > 0 or res.find("222") > 0)
442 self.assert_(res.find(" Extension: ") > 0)
443
444 # Without extension
445 self.vty.command("configure terminal")
446 self.vty.command("nitb")
447 self.assertTrue(self.vty.verify("subscriber-create-on-demand no-extension", ['']))
448 self.vty.command("end")
449 res = self.vty.command('subscriber create imsi ' + imsi3)
450 self.assert_(res.find(" IMSI: " + imsi3) > 0)
451 self.assertEquals(res.find("Extension"), -1)
452
453 # With extension again
454 self.vty.command("configure terminal")
455 self.vty.command("nitb")
456 self.assertTrue(self.vty.verify("no subscriber-create-on-demand", ['']))
457 self.assertTrue(self.vty.verify("subscriber-create-on-demand", ['']))
458 self.assertTrue(self.vty.verify("subscriber-create-on-demand random 221 666", ['']))
459 self.vty.command("end")
460
461 res = self.vty.command('subscriber create imsi ' + imsi4)
462 self.assert_(res.find(" IMSI: " + imsi4) > 0)
463 self.assert_(res.find(" Extension: ") > 0)
464
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500465 # Delete it
Max488902d2016-06-22 14:02:39 +0200466 res = self.vty.command('subscriber imsi ' + imsi + ' delete')
467 self.assert_("" == res)
Maxe6052c42016-06-30 10:25:49 +0200468 res = self.vty.command('subscriber imsi ' + imsi2 + ' delete')
469 self.assert_("" == res)
470 res = self.vty.command('subscriber imsi ' + imsi3 + ' delete')
471 self.assert_("" == res)
472 res = self.vty.command('subscriber imsi ' + imsi4 + ' delete')
473 self.assert_("" == res)
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500474
475 # Now it should not be there anymore
476 res = self.vty.command('show subscriber imsi '+imsi)
Max488902d2016-06-22 14:02:39 +0200477 self.assert_(('% No subscriber found for imsi ' + imsi) == res)
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500478
Neels Hofmeyr34ce3d92017-05-12 14:56:25 +0200479 # range
480 self.vty.command("end")
481 self.vty.command("configure terminal")
482 self.vty.command("nitb")
483 self.assertTrue(self.vty.verify("subscriber-create-on-demand random 9999999998 9999999999", ['']))
484 res = self.vty.command("show running-config")
485 self.assert_(res.find("subscriber-create-on-demand random 9999999998 9999999999"))
486 self.vty.command("end")
487
Neels Hofmeyr7590ff32017-05-12 15:37:27 +0200488 res = self.vty.command('subscriber create imsi ' + imsi)
489 print(res)
490 self.assert_(res.find(" IMSI: " + imsi) > 0)
491 self.assert_(res.find("9999999998") > 0 or res.find("9999999999") > 0)
492 self.assert_(res.find(" Extension: ") > 0)
493
494 res = self.vty.command('subscriber imsi ' + imsi + ' delete')
495 self.assert_("" == res)
496
497 res = self.vty.command('show subscriber imsi '+imsi)
498 self.assert_(('% No subscriber found for imsi ' + imsi) == res)
Neels Hofmeyr34ce3d92017-05-12 14:56:25 +0200499
500
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200501 def testSubscriberSettings(self):
502 self.vty.enable()
503
504 imsi = "204300854013739"
Max0fcd2e22016-06-07 15:32:16 +0200505 imsi2 = "204301824913769"
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200506 wrong_imsi = "204300999999999"
507
508 # Lets create one
509 res = self.vty.command('subscriber create imsi '+imsi)
510 self.assert_(res.find(" IMSI: "+imsi) > 0)
Max0fcd2e22016-06-07 15:32:16 +0200511 self.assert_(res.find("Extension") > 0)
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200512
513 self.vty.verify('subscriber imsi '+wrong_imsi+' name wrong', ['% No subscriber found for imsi '+wrong_imsi])
514 res = self.vty.command('subscriber imsi '+imsi+' name '+('X' * 160))
515 self.assert_(res.find("NAME is too long") > 0)
516
517 self.vty.verify('subscriber imsi '+imsi+' name '+('G' * 159), [''])
518
519 self.vty.verify('subscriber imsi '+wrong_imsi+' extension 840', ['% No subscriber found for imsi '+wrong_imsi])
520 res = self.vty.command('subscriber imsi '+imsi+' extension '+('9' * 15))
521 self.assert_(res.find("EXTENSION is too long") > 0)
522
523 self.vty.verify('subscriber imsi '+imsi+' extension '+('1' * 14), [''])
524
Max0fcd2e22016-06-07 15:32:16 +0200525 # With narrow random interval
526 self.vty.command("configure terminal")
527 self.vty.command("nitb")
528 self.assertTrue(self.vty.verify("subscriber-create-on-demand", ['']))
529 # wrong interval
530 res = self.vty.command("subscriber-create-on-demand random 221 122")
531 self.assert_(res.find("122") > 0)
532 self.assert_(res.find("221") > 0)
533 # correct interval
534 self.assertTrue(self.vty.verify("subscriber-create-on-demand random 221 222", ['']))
535 self.vty.command("end")
536
Max488902d2016-06-22 14:02:39 +0200537 # create subscriber with extension in a configured interval
Max0fcd2e22016-06-07 15:32:16 +0200538 res = self.vty.command('subscriber create imsi ' + imsi2)
539 self.assert_(res.find(" IMSI: " + imsi2) > 0)
540 self.assert_(res.find("221") > 0 or res.find("222") > 0)
541 self.assert_(res.find(" Extension: ") > 0)
542
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200543 # Delete it
Max488902d2016-06-22 14:02:39 +0200544 res = self.vty.command('subscriber imsi ' + imsi + ' delete')
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200545 self.assert_(res != "")
Max488902d2016-06-22 14:02:39 +0200546 # imsi2 is inactive so deletion should succeed
547 res = self.vty.command('subscriber imsi ' + imsi2 + ' delete')
548 self.assert_("" == res)
Jacob Erlbeck322b1492015-04-07 17:49:49 +0200549
Holger Hans Peter Freytherec37bb22013-02-05 09:39:09 +0100550 def testShowPagingGroup(self):
551 res = self.vty.command("show paging-group 255 1234567")
552 self.assertEqual(res, "% can't find BTS 255")
553 res = self.vty.command("show paging-group 0 1234567")
554 self.assertEquals(res, "%Paging group for IMSI 1234567 on BTS #0 is 7")
555
Ciabyec6e4f82014-03-06 17:20:55 +0100556 def testShowNetwork(self):
557 res = self.vty.command("show network")
558 self.assert_(res.startswith('BSC is on Country Code') >= 0)
559
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100560 def testMeasurementFeed(self):
561 self.vty.enable()
562 self.vty.command("configure terminal")
563 self.vty.command("mncc-int")
564
565 res = self.vty.command("write terminal")
566 self.assertEquals(res.find('meas-feed scenario'), -1)
567
568 self.vty.command("meas-feed scenario bla")
569 res = self.vty.command("write terminal")
570 self.assert_(res.find('meas-feed scenario bla') > 0)
571
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200572 self.vty.command("meas-feed scenario abcdefghijklmnopqrstuvwxyz01234567890")
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100573 res = self.vty.command("write terminal")
574 self.assertEquals(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz01234567890'), -1)
575 self.assertEquals(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz012345'), -1)
576 self.assert_(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz01234') > 0)
577
578
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200579class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200580
581 def vty_command(self):
582 return ["./src/osmo-bsc/osmo-bsc", "-c",
583 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
584
585 def vty_app(self):
586 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
587
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200588 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200589 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200590
591 def testVtyTree(self):
592 self.vty.enable()
593 self.assertTrue(self.vty.verify("configure terminal", ['']))
594 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100595 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200596 self.assertTrue(self.vty.verify("msc 0", ['']))
597 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200598 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200599 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200600 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200601 self.assertTrue(self.vty.verify("bsc", ['']))
602 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200603 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200604 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200605 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200606 self.assertTrue(self.vty.verify("exit", ['']))
607 self.assertTrue(self.vty.node() is None)
608
609 # Check searching for outer node's commands
610 self.vty.command("configure terminal")
611 self.vty.command('msc 0')
612 self.vty.command("bsc")
613 self.assertEquals(self.vty.node(), 'config-bsc')
614 self.vty.command("msc 0")
615 self.assertEquals(self.vty.node(), 'config-msc')
616
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200617 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200618 self.vty.enable()
619 self.vty.command("configure terminal")
620 self.vty.command("msc")
621
622 # Test invalid input
623 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200624 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200625 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200626
627 # Enable USSD notifications
628 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200629 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200630 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200631
632 # Verify settings
633 res = self.vty.command("write terminal")
634 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
635 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200636 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
637 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200638 self.assert_(res.find('bsc-grace-text In grace period') > 0)
639 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200640
641 # Now disable it..
642 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200643 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200644 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200645
646 # Verify settings
647 res = self.vty.command("write terminal")
648 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
649 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200650 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200651 self.assert_(res.find('no bsc-welcome-text') > 0)
652 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
653 self.assert_(res.find('no bsc-grace-text') > 0)
654
655 def testUssdNotificationsBsc(self):
656 self.vty.enable()
657 self.vty.command("configure terminal")
658 self.vty.command("bsc")
659
660 # Test invalid input
661 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
662
663 # Enable USSD notifications
664 self.vty.verify("missing-msc-text No MSC found", [''])
665
666 # Verify settings
667 res = self.vty.command("write terminal")
668 self.assert_(res.find('missing-msc-text No MSC found') > 0)
669 self.assertEquals(res.find('no missing-msc-text'), -1)
670
671 # Now disable it..
672 self.vty.verify("no missing-msc-text", [''])
673
674 # Verify settings
675 res = self.vty.command("write terminal")
676 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
677 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200678
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200679 def testNetworkTimezone(self):
680 self.vty.enable()
681 self.vty.verify("configure terminal", [''])
682 self.vty.verify("network", [''])
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200683
684 # Test invalid input
685 self.vty.verify("timezone", ['% Command incomplete.'])
686 self.vty.verify("timezone 20 0", ['% Unknown command.'])
687 self.vty.verify("timezone 0 11", ['% Unknown command.'])
688 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
689
690 # Set time zone without DST
691 self.vty.verify("timezone 2 30", [''])
692
693 # Verify settings
694 res = self.vty.command("write terminal")
695 self.assert_(res.find('timezone 2 30') > 0)
696 self.assertEquals(res.find('timezone 2 30 '), -1)
697
698 # Set time zone with DST
699 self.vty.verify("timezone 2 30 1", [''])
700
701 # Verify settings
702 res = self.vty.command("write terminal")
703 self.assert_(res.find('timezone 2 30 1') > 0)
704
705 # Now disable it..
706 self.vty.verify("no timezone", [''])
707
708 # Verify settings
709 res = self.vty.command("write terminal")
710 self.assertEquals(res.find(' timezone'), -1)
711
Ciabyec6e4f82014-03-06 17:20:55 +0100712 def testShowNetwork(self):
713 res = self.vty.command("show network")
714 self.assert_(res.startswith('BSC is on Country Code') >= 0)
715
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100716 def testPingPongConfiguration(self):
717 self.vty.enable()
718 self.vty.verify("configure terminal", [''])
719 self.vty.verify("network", [''])
720 self.vty.verify("msc 0", [''])
721
722 self.vty.verify("timeout-ping 12", [''])
723 self.vty.verify("timeout-pong 14", [''])
724 res = self.vty.command("show running-config")
725 self.assert_(res.find(" timeout-ping 12") > 0)
726 self.assert_(res.find(" timeout-pong 14") > 0)
727 self.assert_(res.find(" no timeout-ping advanced") > 0)
728
729 self.vty.verify("timeout-ping advanced", [''])
730 res = self.vty.command("show running-config")
731 self.assert_(res.find(" timeout-ping 12") > 0)
732 self.assert_(res.find(" timeout-pong 14") > 0)
733 self.assert_(res.find(" timeout-ping advanced") > 0)
734
735 self.vty.verify("no timeout-ping advanced", [''])
736 res = self.vty.command("show running-config")
737 self.assert_(res.find(" timeout-ping 12") > 0)
738 self.assert_(res.find(" timeout-pong 14") > 0)
739 self.assert_(res.find(" no timeout-ping advanced") > 0)
740
741 self.vty.verify("no timeout-ping", [''])
742 res = self.vty.command("show running-config")
743 self.assertEquals(res.find(" timeout-ping 12"), -1)
744 self.assertEquals(res.find(" timeout-pong 14"), -1)
745 self.assertEquals(res.find(" no timeout-ping advanced"), -1)
746 self.assert_(res.find(" no timeout-ping") > 0)
747
748 self.vty.verify("timeout-ping advanced", ['%ping handling is disabled. Enable it first.'])
749
750 # And back to enabling it
751 self.vty.verify("timeout-ping 12", [''])
752 self.vty.verify("timeout-pong 14", [''])
753 res = self.vty.command("show running-config")
754 self.assert_(res.find(" timeout-ping 12") > 0)
755 self.assert_(res.find(" timeout-pong 14") > 0)
756 self.assert_(res.find(" timeout-ping advanced") > 0)
757
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200758 def testMscDataCoreLACCI(self):
759 self.vty.enable()
760 res = self.vty.command("show running-config")
761 self.assertEquals(res.find("core-location-area-code"), -1)
762 self.assertEquals(res.find("core-cell-identity"), -1)
763
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200764 self.vty.command("configure terminal")
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200765 self.vty.command("msc 0")
766 self.vty.command("core-location-area-code 666")
767 self.vty.command("core-cell-identity 333")
768
769 res = self.vty.command("show running-config")
770 self.assert_(res.find("core-location-area-code 666") > 0)
771 self.assert_(res.find("core-cell-identity 333") > 0)
772
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200773class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200774
775 def vty_command(self):
Max49364482016-04-13 11:36:39 +0200776 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-l", "127.0.0.1", "-c",
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200777 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
778
779 def vty_app(self):
780 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
781
Max49364482016-04-13 11:36:39 +0200782 def testBSCreload(self):
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400783 # Use different port for the mock msc to avoid clashing with
784 # the osmo-bsc_nat itself
Holger Hans Peter Freytherf1a61bb2016-04-14 08:50:25 -0400785 ip = "127.0.0.1"
Holger Hans Peter Freythere98c9c72016-04-14 10:58:58 -0400786 port = 5522
Max49364482016-04-13 11:36:39 +0200787 self.vty.enable()
788 bscs1 = self.vty.command("show bscs-config")
789 nat_bsc_reload(self)
790 bscs2 = self.vty.command("show bscs-config")
791 # check that multiple calls to bscs-config-file give the same result
792 self.assertEquals(bscs1, bscs2)
793
794 # add new bsc
795 self.vty.command("configure terminal")
796 self.vty.command("nat")
797 self.vty.command("bsc 5")
798 self.vty.command("token key")
799 self.vty.command("location_area_code 666")
800 self.vty.command("end")
801
802 # update bsc token
803 self.vty.command("configure terminal")
804 self.vty.command("nat")
805 self.vty.command("bsc 1")
806 self.vty.command("token xyu")
807 self.vty.command("end")
808
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400809 nat_msc_ip(self, ip, port)
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100810 msc_socket, msc = nat_msc_test(self, ip, port, verbose=True)
811 try:
812 b0 = nat_bsc_sock_test(0, "lol", verbose=True, proc=self.proc)
813 b1 = nat_bsc_sock_test(1, "xyu", verbose=True, proc=self.proc)
814 b2 = nat_bsc_sock_test(5, "key", verbose=True, proc=self.proc)
Max49364482016-04-13 11:36:39 +0200815
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100816 self.assertEquals("3 BSCs configured", self.vty.command("show nat num-bscs-configured"))
817 self.assertTrue(3 == nat_bsc_num_con(self))
818 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
Max49364482016-04-13 11:36:39 +0200819
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100820 nat_bsc_reload(self)
821 bscs2 = self.vty.command("show bscs-config")
822 # check that the reset to initial config succeeded
823 self.assertEquals(bscs1, bscs2)
Max49364482016-04-13 11:36:39 +0200824
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100825 self.assertEquals("2 BSCs configured", self.vty.command("show nat num-bscs-configured"))
826 self.assertTrue(1 == nat_bsc_num_con(self))
827 rem = self.vty.command("show bsc connections").split(' ')
828 # remaining connection is for BSC0
829 self.assertEquals('0', rem[2])
830 # remaining connection is authorized
831 self.assertEquals('1', rem[4])
832 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
833 finally:
834 msc.close()
835 msc_socket.close()
Max49364482016-04-13 11:36:39 +0200836
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200837 def testVtyTree(self):
838 self.vty.enable()
839 self.assertTrue(self.vty.verify('configure terminal', ['']))
840 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100841 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200842 self.assertTrue(self.vty.verify('mgcp', ['']))
843 self.assertEquals(self.vty.node(), 'config-mgcp')
844 self.checkForEndAndExit()
845 self.assertTrue(self.vty.verify('exit', ['']))
846 self.assertEquals(self.vty.node(), 'config')
847 self.assertTrue(self.vty.verify('nat', ['']))
848 self.assertEquals(self.vty.node(), 'config-nat')
849 self.checkForEndAndExit()
850 self.assertTrue(self.vty.verify('bsc 0', ['']))
851 self.assertEquals(self.vty.node(), 'config-nat-bsc')
852 self.checkForEndAndExit()
853 self.assertTrue(self.vty.verify('exit', ['']))
854 self.assertEquals(self.vty.node(), 'config-nat')
855 self.assertTrue(self.vty.verify('exit', ['']))
856 self.assertEquals(self.vty.node(), 'config')
857 self.assertTrue(self.vty.verify('exit', ['']))
858 self.assertTrue(self.vty.node() is None)
859
860 # Check searching for outer node's commands
861 self.vty.command('configure terminal')
862 self.vty.command('mgcp')
863 self.vty.command('nat')
864 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200865 self.vty.command('mgcp')
866 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200867 self.vty.command('nat')
868 self.assertEquals(self.vty.node(), 'config-nat')
869 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200870 self.vty.command('mgcp')
871 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200872
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200873 def testRewriteNoRewrite(self):
874 self.vty.enable()
875 res = self.vty.command("configure terminal")
876 res = self.vty.command("nat")
877 res = self.vty.command("number-rewrite rewrite.cfg")
878 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200879
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400880 def testEnsureNoEnsureModeSet(self):
881 self.vty.enable()
882 res = self.vty.command("configure terminal")
883 res = self.vty.command("nat")
884
885 # Ensure the default
886 res = self.vty.command("show running-config")
887 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
888
889 self.vty.command("sdp-ensure-amr-mode-set")
890 res = self.vty.command("show running-config")
891 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
892
893 self.vty.command("no sdp-ensure-amr-mode-set")
894 res = self.vty.command("show running-config")
895 self.assert_(res.find('\n no sdp-ensure-amr-mode-set') > 0)
896
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200897 def testRewritePostNoRewrite(self):
898 self.vty.enable()
899 self.vty.command("configure terminal")
900 self.vty.command("nat")
901 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
902 self.vty.verify("no number-rewrite-post", [''])
903
904
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200905 def testPrefixTreeLoading(self):
906 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
907
908 self.vty.enable()
909 self.vty.command("configure terminal")
910 self.vty.command("nat")
911 res = self.vty.command("prefix-tree %s" % cfg)
912 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
913 self.vty.command("end")
914
915 res = self.vty.command("show prefix-tree")
916 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')
917
918 self.vty.command("configure terminal")
919 self.vty.command("nat")
920 self.vty.command("no prefix-tree")
921 self.vty.command("end")
922
923 res = self.vty.command("show prefix-tree")
924 self.assertEqual(res, "% there is now prefix tree loaded.")
925
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200926 def testUssdSideChannelProvider(self):
927 self.vty.command("end")
928 self.vty.enable()
929 self.vty.command("configure terminal")
930 self.vty.command("nat")
931 self.vty.command("ussd-token key")
932 self.vty.command("end")
933
934 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
935 self.assertTrue(res)
936
937 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
938 ussdSocket.connect(('127.0.0.1', 5001))
939 ussdSocket.settimeout(2.0)
940 print "Connected to %s:%d" % ussdSocket.getpeername()
941
942 print "Expecting ID_GET request"
943 data = ussdSocket.recv(4)
944 self.assertEqual(data, "\x00\x01\xfe\x04")
945
946 print "Going to send ID_RESP response"
Max3e676892016-11-16 14:55:21 +0100947 res = ussdSocket.send(IPA().id_resp(IPA().tag_name('key')))
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200948 self.assertEqual(res, 10)
949
950 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
951
952 print "Going to send PING request"
Max3e676892016-11-16 14:55:21 +0100953 res = ussdSocket.send(IPA().ping())
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200954 self.assertEqual(res, 4)
955
956 print "Expecting PONG response"
957 data = ussdSocket.recv(4)
958 self.assertEqual(data, "\x00\x01\xfe\x01")
959
960 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
961 self.assertTrue(res)
962
963 print "Going to shut down connection"
964 ussdSocket.shutdown(socket.SHUT_WR)
965
966 print "Expecting EOF"
967 data = ussdSocket.recv(4)
968 self.assertEqual(data, "")
969
970 ussdSocket.close()
971
972 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
973 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200974
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100975 def testAccessList(self):
976 """
977 Verify that the imsi-deny can have a reject cause or no reject cause
978 """
979 self.vty.enable()
980 self.vty.command("configure terminal")
981 self.vty.command("nat")
982
983 # Old default
984 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
985 res = self.vty.command("show running-config").split("\r\n")
986 asserted = False
987 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100988 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100989 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
990 asserted = True
991 self.assert_(asserted)
992
993 # Check the optional CM Service Reject Cause
994 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
995 res = self.vty.command("show running-config").split("\r\n")
996 asserted = False
997 for line in res:
998 if line.startswith(" access-list test-cm"):
999 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
1000 asserted = True
1001 self.assert_(asserted)
1002
1003 # Check the optional LU Reject Cause
1004 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
1005 res = self.vty.command("show running-config").split("\r\n")
1006 asserted = False
1007 for line in res:
1008 if line.startswith(" access-list test-lu"):
1009 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
1010 asserted = True
1011 self.assert_(asserted)
1012
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001013class TestVTYGbproxy(TestVTYGenericBSC):
1014
1015 def vty_command(self):
1016 return ["./src/gprs/osmo-gbproxy", "-c",
1017 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
1018
1019 def vty_app(self):
1020 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
1021
1022 def testVtyTree(self):
1023 self.vty.enable()
1024 self.assertTrue(self.vty.verify('configure terminal', ['']))
1025 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +01001026 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001027 self.assertTrue(self.vty.verify('ns', ['']))
1028 self.assertEquals(self.vty.node(), 'config-ns')
1029 self.checkForEndAndExit()
1030 self.assertTrue(self.vty.verify('exit', ['']))
1031 self.assertEquals(self.vty.node(), 'config')
1032 self.assertTrue(self.vty.verify('gbproxy', ['']))
1033 self.assertEquals(self.vty.node(), 'config-gbproxy')
1034 self.checkForEndAndExit()
1035 self.assertTrue(self.vty.verify('exit', ['']))
1036 self.assertEquals(self.vty.node(), 'config')
1037
1038 def testVtyShow(self):
1039 res = self.vty.command("show ns")
1040 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
1041
1042 res = self.vty.command("show gbproxy stats")
1043 self.assert_(res.find('GBProxy Global Statistics') >= 0)
1044
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001045 def testVtyDeletePeer(self):
1046 self.vty.enable()
1047 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
1048 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
1049 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
1050 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
1051 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
1052 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
1053 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
1054 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
1055 self.assert_(res.find('Not Deleted 0 BVC') < 0)
1056 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
1057 res = self.vty.command("delete-gbproxy-peer 9999 all")
1058 self.assert_(res.find('Deleted 0 BVC') >= 0)
1059 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
1060
Jacob Erlbeck144b8b12014-11-04 11:15:01 +01001061class TestVTYSGSN(TestVTYGenericBSC):
1062
1063 def vty_command(self):
1064 return ["./src/gprs/osmo-sgsn", "-c",
1065 "doc/examples/osmo-sgsn/osmo-sgsn.cfg"]
1066
1067 def vty_app(self):
1068 return (4245, "./src/gprs/osmo-sgsn", "OsmoSGSN", "sgsn")
1069
1070 def testVtyTree(self):
1071 self.vty.enable()
1072 self.assertTrue(self.vty.verify('configure terminal', ['']))
1073 self.assertEquals(self.vty.node(), 'config')
1074 self.checkForEndAndExit()
1075 self.assertTrue(self.vty.verify('ns', ['']))
1076 self.assertEquals(self.vty.node(), 'config-ns')
1077 self.checkForEndAndExit()
1078 self.assertTrue(self.vty.verify('exit', ['']))
1079 self.assertEquals(self.vty.node(), 'config')
1080 self.assertTrue(self.vty.verify('sgsn', ['']))
1081 self.assertEquals(self.vty.node(), 'config-sgsn')
1082 self.checkForEndAndExit()
1083 self.assertTrue(self.vty.verify('exit', ['']))
1084 self.assertEquals(self.vty.node(), 'config')
1085
1086 def testVtyShow(self):
1087 res = self.vty.command("show ns")
1088 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
1089 self.assertTrue(self.vty.verify('show bssgp', ['']))
1090 self.assertTrue(self.vty.verify('show bssgp stats', ['']))
1091 # TODO: uncomment when the command does not segfault anymore
1092 # self.assertTrue(self.vty.verify('show bssgp nsei 123', ['']))
1093 # self.assertTrue(self.vty.verify('show bssgp nsei 123 stats', ['']))
1094
1095 self.assertTrue(self.vty.verify('show sgsn', ['']))
1096 self.assertTrue(self.vty.verify('show mm-context all', ['']))
1097 self.assertTrue(self.vty.verify('show mm-context imsi 000001234567', ['No MM context for IMSI 000001234567']))
1098 self.assertTrue(self.vty.verify('show pdp-context all', ['']))
1099
1100 res = self.vty.command("show sndcp")
1101 self.assert_(res.find('State of SNDCP Entities') >= 0)
1102
1103 res = self.vty.command("show llc")
1104 self.assert_(res.find('State of LLC Entities') >= 0)
1105
Jacob Erlbeck106f5472014-11-04 10:08:37 +01001106 def testVtyAuth(self):
1107 self.vty.enable()
1108 self.assertTrue(self.vty.verify('configure terminal', ['']))
1109 self.assertEquals(self.vty.node(), 'config')
1110 self.assertTrue(self.vty.verify('sgsn', ['']))
1111 self.assertEquals(self.vty.node(), 'config-sgsn')
1112 self.assertTrue(self.vty.verify('auth-policy accept-all', ['']))
1113 res = self.vty.command("show running-config")
1114 self.assert_(res.find('auth-policy accept-all') > 0)
1115 self.assertTrue(self.vty.verify('auth-policy acl-only', ['']))
1116 res = self.vty.command("show running-config")
1117 self.assert_(res.find('auth-policy acl-only') > 0)
1118 self.assertTrue(self.vty.verify('auth-policy closed', ['']))
1119 res = self.vty.command("show running-config")
1120 self.assert_(res.find('auth-policy closed') > 0)
Max176b62a2016-07-04 11:09:07 +02001121 self.assertTrue(self.vty.verify('gsup remote-ip 127.0.0.4', ['']))
1122 self.assertTrue(self.vty.verify('gsup remote-port 2222', ['']))
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +01001123 self.assertTrue(self.vty.verify('auth-policy remote', ['']))
1124 res = self.vty.command("show running-config")
1125 self.assert_(res.find('auth-policy remote') > 0)
Jacob Erlbeck106f5472014-11-04 10:08:37 +01001126
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001127 def testVtySubscriber(self):
1128 self.vty.enable()
1129 res = self.vty.command('show subscriber cache')
1130 self.assert_(res.find('1234567890') < 0)
Jacob Erlbeckd9193432015-01-19 14:11:46 +01001131 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 create', ['']))
1132 res = self.vty.command('show subscriber cache')
1133 self.assert_(res.find('1234567890') >= 0)
1134 self.assert_(res.find('Authorized: 0') >= 0)
1135 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 update-location-result ok', ['']))
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001136 res = self.vty.command('show subscriber cache')
1137 self.assert_(res.find('1234567890') >= 0)
1138 self.assert_(res.find('Authorized: 1') >= 0)
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +01001139 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 cancel update-procedure', ['']))
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001140 res = self.vty.command('show subscriber cache')
Jacob Erlbecke988ae42015-01-27 12:41:19 +01001141 self.assert_(res.find('1234567890') >= 0)
1142 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 destroy', ['']))
1143 res = self.vty.command('show subscriber cache')
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001144 self.assert_(res.find('1234567890') < 0)
1145
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +01001146 def testVtyGgsn(self):
1147 self.vty.enable()
1148 self.assertTrue(self.vty.verify('configure terminal', ['']))
1149 self.assertEquals(self.vty.node(), 'config')
1150 self.assertTrue(self.vty.verify('sgsn', ['']))
1151 self.assertEquals(self.vty.node(), 'config-sgsn')
1152 self.assertTrue(self.vty.verify('ggsn 0 remote-ip 127.99.99.99', ['']))
1153 self.assertTrue(self.vty.verify('ggsn 0 gtp-version 1', ['']))
1154 self.assertTrue(self.vty.verify('apn * ggsn 0', ['']))
1155 self.assertTrue(self.vty.verify('apn apn1.test ggsn 0', ['']))
1156 self.assertTrue(self.vty.verify('apn apn1.test ggsn 1', ['% a GGSN with id 1 has not been defined']))
1157 self.assertTrue(self.vty.verify('apn apn1.test imsi-prefix 123456 ggsn 0', ['']))
1158 self.assertTrue(self.vty.verify('apn apn2.test imsi-prefix 123456 ggsn 0', ['']))
1159 res = self.vty.command("show running-config")
1160 self.assert_(res.find('ggsn 0 remote-ip 127.99.99.99') >= 0)
1161 self.assert_(res.find('ggsn 0 gtp-version 1') >= 0)
1162 self.assert_(res.find('apn * ggsn 0') >= 0)
1163 self.assert_(res.find('apn apn1.test ggsn 0') >= 0)
1164 self.assert_(res.find('apn apn1.test imsi-prefix 123456 ggsn 0') >= 0)
1165 self.assert_(res.find('apn apn2.test imsi-prefix 123456 ggsn 0') >= 0)
1166
Holger Hans Peter Freyther9c20a5f2015-02-06 16:23:29 +01001167 def testVtyEasyAPN(self):
1168 self.vty.enable()
1169 self.assertTrue(self.vty.verify('configure terminal', ['']))
1170 self.assertEquals(self.vty.node(), 'config')
1171 self.assertTrue(self.vty.verify('sgsn', ['']))
1172 self.assertEquals(self.vty.node(), 'config-sgsn')
1173
1174 res = self.vty.command("show running-config")
1175 self.assertEquals(res.find("apn internet"), -1)
1176
1177 self.assertTrue(self.vty.verify("access-point-name internet.apn", ['']))
1178 res = self.vty.command("show running-config")
1179 self.assert_(res.find("apn internet.apn ggsn 0") >= 0)
1180
1181 self.assertTrue(self.vty.verify("no access-point-name internet.apn", ['']))
1182 res = self.vty.command("show running-config")
1183 self.assertEquals(res.find("apn internet"), -1)
1184
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +02001185 def testVtyCDR(self):
1186 self.vty.enable()
1187 self.assertTrue(self.vty.verify('configure terminal', ['']))
1188 self.assertEquals(self.vty.node(), 'config')
1189 self.assertTrue(self.vty.verify('sgsn', ['']))
1190 self.assertEquals(self.vty.node(), 'config-sgsn')
1191
1192 res = self.vty.command("show running-config")
1193 self.assert_(res.find("no cdr filename") > 0)
1194
1195 self.vty.command("cdr filename bla.cdr")
1196 res = self.vty.command("show running-config")
1197 self.assertEquals(res.find("no cdr filename"), -1)
1198 self.assert_(res.find(" cdr filename bla.cdr") > 0)
1199
1200 self.vty.command("no cdr filename")
1201 res = self.vty.command("show running-config")
1202 self.assert_(res.find("no cdr filename") > 0)
1203 self.assertEquals(res.find(" cdr filename bla.cdr"), -1)
1204
1205 res = self.vty.command("show running-config")
1206 self.assert_(res.find(" cdr interval 600") > 0)
1207
1208 self.vty.command("cdr interval 900")
1209 res = self.vty.command("show running-config")
1210 self.assert_(res.find(" cdr interval 900") > 0)
1211 self.assertEquals(res.find(" cdr interval 600"), -1)
1212
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001213def add_nat_test(suite, workdir):
1214 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
1215 print("Skipping the NAT test")
1216 return
1217 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
1218 suite.addTest(test)
1219
Max49364482016-04-13 11:36:39 +02001220def nat_bsc_reload(x):
1221 x.vty.command("configure terminal")
1222 x.vty.command("nat")
Neels Hofmeyra4cd7f82017-07-20 17:57:37 +02001223 x.vty.command("bscs-config-file bscs.cfg")
Max49364482016-04-13 11:36:39 +02001224 x.vty.command("end")
1225
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -04001226def nat_msc_ip(x, ip, port):
Max49364482016-04-13 11:36:39 +02001227 x.vty.command("configure terminal")
1228 x.vty.command("nat")
1229 x.vty.command("msc ip " + ip)
Holger Hans Peter Freyther84ae27e2016-04-14 10:40:06 -04001230 x.vty.command("msc port " + str(port))
Max49364482016-04-13 11:36:39 +02001231 x.vty.command("end")
1232
1233def data2str(d):
Holger Hans Peter Freyther8bb62042016-04-14 21:40:04 -04001234 return d.encode('hex').lower()
Max49364482016-04-13 11:36:39 +02001235
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -04001236def nat_msc_test(x, ip, port, verbose = False):
Max49364482016-04-13 11:36:39 +02001237 msc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Neels Hofmeyr53403452017-02-28 02:38:43 +01001238 msc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +02001239 msc.settimeout(5)
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -04001240 msc.bind((ip, port))
Max49364482016-04-13 11:36:39 +02001241 msc.listen(5)
1242 if (verbose):
1243 print "MSC is ready at " + ip
Neels Hofmeyr89d20b62016-09-26 12:59:36 +02001244 conn = None
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +02001245 while True:
1246 vty_response = x.vty.command("show msc connection")
1247 print "'show msc connection' says: %r" % vty_response
1248 if vty_response == "MSC is connected: 1":
1249 # success
1250 break;
1251 if vty_response != "MSC is connected: 0":
1252 raise Exception("Unexpected response to 'show msc connection'"
1253 " vty command: %r" % vty_response)
1254
1255 timeout_retries = 6
1256 while timeout_retries > 0:
1257 try:
1258 conn, addr = msc.accept()
1259 print "MSC got connection from ", addr
1260 break
1261 except socket.timeout:
1262 print "socket timed out."
1263 timeout_retries -= 1
1264 continue
1265
Neels Hofmeyr89d20b62016-09-26 12:59:36 +02001266 if not conn:
Neels Hofmeyr0867b722016-09-28 23:28:06 +02001267 raise Exception("VTY reports MSC is connected, but I haven't"
1268 " connected yet: %r %r" % (ip, port))
Neels Hofmeyr0b619322017-02-28 02:37:39 +01001269 return msc, conn
Max49364482016-04-13 11:36:39 +02001270
1271def ipa_handle_small(x, verbose = False):
1272 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +01001273 if len(s) != 4*2:
1274 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +02001275 if "0001fe00" == s:
1276 if (verbose):
1277 print "\tBSC <- NAT: PING?"
Max3e676892016-11-16 14:55:21 +01001278 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +02001279 elif "0001fe06" == s:
1280 if (verbose):
1281 print "\tBSC <- NAT: IPA ID ACK"
Max3e676892016-11-16 14:55:21 +01001282 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +02001283 elif "0001fe00" == s:
1284 if (verbose):
1285 print "\tBSC <- NAT: PONG!"
1286 else:
1287 if (verbose):
1288 print "\tBSC <- NAT: ", s
1289
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001290def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +02001291 s = data2str(x.recv(38))
1292 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001293 retries = 3
1294 while True:
1295 print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
1296 try:
1297 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
1298 print "\tdone sending IPA identity(%s) at %s" % (tk,
1299 time.strftime("%T"))
1300 break
1301 except:
1302 print "\tfailed sending IPA identity at", time.strftime("%T")
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001303 if proc:
1304 print "\tproc.poll() = %r" % proc.poll()
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001305 if retries < 1:
1306 print "\tgiving up"
1307 raise
Neels Hofmeyre02e1e72017-02-03 05:55:26 +01001308 print "\tretrying (%d attempts left)" % retries
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001309 retries -= 1
Max49364482016-04-13 11:36:39 +02001310 else:
1311 if (verbose):
1312 print "\tBSC <- NAT: ", s
1313
1314def nat_bsc_num_con(x):
1315 return len(x.vty.command("show bsc connections").split('\n'))
1316
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001317def nat_bsc_sock_test(nr, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +02001318 bsc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Holger Hans Peter Freyther2abf2b02016-04-14 21:13:51 -04001319 bsc.bind(('127.0.0.1', 0))
Max49364482016-04-13 11:36:39 +02001320 bsc.connect(('127.0.0.1', 5000))
1321 if (verbose):
1322 print "BSC%d " %nr
1323 print "\tconnected to %s:%d" % bsc.getpeername()
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001324 if proc:
1325 print "\tproc.poll() = %r" % proc.poll()
1326 print "\tproc.pid = %r" % proc.pid
Max49364482016-04-13 11:36:39 +02001327 ipa_handle_small(bsc, verbose)
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001328 ipa_handle_resp(bsc, tk, verbose, proc=proc)
1329 if proc:
1330 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +02001331 bsc.recv(27) # MGCP msg
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001332 if proc:
1333 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +02001334 ipa_handle_small(bsc, verbose)
1335 return bsc
1336
Jacob Erlbeck1b894022013-08-28 10:16:54 +02001337def add_bsc_test(suite, workdir):
1338 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
1339 print("Skipping the BSC test")
1340 return
1341 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
1342 suite.addTest(test)
1343
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001344def add_gbproxy_test(suite, workdir):
1345 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
1346 print("Skipping the Gb-Proxy test")
1347 return
1348 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
1349 suite.addTest(test)
1350
Jacob Erlbeck144b8b12014-11-04 11:15:01 +01001351def add_sgsn_test(suite, workdir):
1352 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-sgsn")):
1353 print("Skipping the SGSN test")
1354 return
1355 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYSGSN)
1356 suite.addTest(test)
1357
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001358if __name__ == '__main__':
1359 import argparse
1360 import sys
1361
1362 workdir = '.'
1363
1364 parser = argparse.ArgumentParser()
1365 parser.add_argument("-v", "--verbose", dest="verbose",
1366 action="store_true", help="verbose mode")
1367 parser.add_argument("-p", "--pythonconfpath", dest="p",
1368 help="searchpath for config")
1369 parser.add_argument("-w", "--workdir", dest="w",
1370 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +01001371 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001372 args = parser.parse_args()
1373
1374 verbose_level = 1
1375 if args.verbose:
1376 verbose_level = 2
1377
1378 if args.w:
1379 workdir = args.w
1380
1381 if args.p:
1382 confpath = args.p
1383
1384 print "confpath %s, workdir %s" % (confpath, workdir)
1385 os.chdir(workdir)
1386 print "Running tests for specific VTY commands"
1387 suite = unittest.TestSuite()
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +02001388 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001389 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +02001390 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001391 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001392 add_gbproxy_test(suite, workdir)
Jacob Erlbeck144b8b12014-11-04 11:15:01 +01001393 add_sgsn_test(suite, workdir)
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +01001394
1395 if args.test_name:
1396 osmoutil.pick_tests(suite, *args.test_name)
1397
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +02001398 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001399 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +02001400
Neels Hofmeyr0867b722016-09-28 23:28:06 +02001401# vim: shiftwidth=4 expandtab nocin ai