blob: fe4d3f038ed1ab4b6cbf711c9c040c34ed449c4c [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
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020037 def checkForEndAndExit(self):
38 res = self.vty.command("list")
39 #print ('looking for "exit"\n')
40 self.assert_(res.find(' exit\r') > 0)
41 #print 'found "exit"\nlooking for "end"\n'
42 self.assert_(res.find(' end\r') > 0)
43 #print 'found "end"\n'
44
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020045 def vty_command(self):
46 raise Exception("Needs to be implemented by a subclass")
47
48 def vty_app(self):
49 raise Exception("Needs to be implemented by a subclass")
50
51 def setUp(self):
52 osmo_vty_cmd = self.vty_command()[:]
53 config_index = osmo_vty_cmd.index('-c')
54 if config_index:
55 cfi = config_index + 1
56 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
57
58 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020059 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
60 except OSError:
61 print >> sys.stderr, "Current directory: %s" % os.getcwd()
62 print >> sys.stderr, "Consider setting -b"
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020063
64 appstring = self.vty_app()[2]
65 appport = self.vty_app()[0]
66 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
67
68 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010069 if self.vty:
70 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020071 self.vty = None
72 osmoutil.end_proc(self.proc)
73
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020074class TestVTYMGCP(TestVTYBase):
75 def vty_command(self):
76 return ["./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "-c",
77 "doc/examples/osmo-bsc_mgcp/mgcp.cfg"]
78
79 def vty_app(self):
80 return (4243, "./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "OpenBSC MGCP", "mgcp")
81
82 def testForcePtime(self):
Neels Hofmeyr0867b722016-09-28 23:28:06 +020083 self.vty.enable()
84 res = self.vty.command("show running-config")
85 self.assert_(res.find(' rtp force-ptime 20\r') > 0)
86 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020087
Neels Hofmeyr0867b722016-09-28 23:28:06 +020088 self.vty.command("configure terminal")
89 self.vty.command("mgcp")
90 self.vty.command("no rtp force-ptime")
91 res = self.vty.command("show running-config")
92 self.assertEquals(res.find(' rtp force-ptime 20\r'), -1)
93 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020094
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +010095 def testOmitAudio(self):
96 self.vty.enable()
Neels Hofmeyr0867b722016-09-28 23:28:06 +020097 res = self.vty.command("show running-config")
98 self.assert_(res.find(' sdp audio-payload send-name\r') > 0)
99 self.assertEquals(res.find(' no sdp audio-payload send-name\r'), -1)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +0100100
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200101 self.vty.command("configure terminal")
102 self.vty.command("mgcp")
103 self.vty.command("no sdp audio-payload send-name")
104 res = self.vty.command("show running-config")
105 self.assertEquals(res.find(' rtp sdp audio-payload send-name\r'), -1)
106 self.assert_(res.find(' no sdp audio-payload send-name\r') > 0)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +0100107
108 # TODO: test it for the trunk!
109
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200110 def testBindAddr(self):
111 self.vty.enable()
112
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200113 self.vty.command("configure terminal")
114 self.vty.command("mgcp")
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200115
116 # enable.. disable bts-bind-ip
117 self.vty.command("rtp bts-bind-ip 254.253.252.250")
118 res = self.vty.command("show running-config")
119 self.assert_(res.find('rtp bts-bind-ip 254.253.252.250') > 0)
120 self.vty.command("no rtp bts-bind-ip")
121 res = self.vty.command("show running-config")
122 self.assertEquals(res.find(' rtp bts-bind-ip'), -1)
123
124 # enable.. disable net-bind-ip
125 self.vty.command("rtp net-bind-ip 254.253.252.250")
126 res = self.vty.command("show running-config")
127 self.assert_(res.find('rtp net-bind-ip 254.253.252.250') > 0)
128 self.vty.command("no rtp net-bind-ip")
129 res = self.vty.command("show running-config")
130 self.assertEquals(res.find(' rtp net-bind-ip'), -1)
131
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200132
133class TestVTYGenericBSC(TestVTYBase):
134
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200135 def _testConfigNetworkTree(self, include_bsc_items=True):
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200136 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
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200169class TestVTYMSC(TestVTYBase):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200170
171 def vty_command(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200172 return ["./src/osmo-msc/osmo-msc", "-c",
173 "doc/examples/osmo-msc/osmo-msc.cfg"]
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200174
175 def vty_app(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200176 return (4254, "./src/osmo-msc/osmo-msc", "OsmoMSC", "msc")
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200177
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200178 def testConfigNetworkTree(self, include_bsc_items=True):
179 self.vty.enable()
180 self.assertTrue(self.vty.verify("configure terminal",['']))
181 self.assertEquals(self.vty.node(), 'config')
182 self.checkForEndAndExit()
183 self.assertTrue(self.vty.verify("network",['']))
184 self.assertEquals(self.vty.node(), 'config-net')
185 self.checkForEndAndExit()
186 self.vty.command("write terminal")
187 self.assertTrue(self.vty.verify("exit",['']))
188 self.assertEquals(self.vty.node(), 'config')
189 self.assertTrue(self.vty.verify("exit",['']))
190 self.assertTrue(self.vty.node() is None)
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200191
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200192 def checkForSmpp(self):
193 """SMPP is not always enabled, check if it is"""
194 res = self.vty.command("list")
195 return "smpp" in res
196
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200197 def testSmppFirst(self):
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200198 # enable the configuration
199 self.vty.enable()
200 self.vty.command("configure terminal")
Holger Hans Peter Freythera2c41c42015-07-13 11:23:53 +0200201
202 if not self.checkForSmpp():
203 return
204
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200205 self.vty.command("smpp")
206
207 # check the default
208 res = self.vty.command("write terminal")
209 self.assert_(res.find(' no smpp-first') > 0)
210
211 self.vty.verify("smpp-first", [''])
212 res = self.vty.command("write terminal")
213 self.assert_(res.find(' smpp-first') > 0)
214 self.assertEquals(res.find('no smpp-first'), -1)
215
216 self.vty.verify("no smpp-first", [''])
217 res = self.vty.command("write terminal")
218 self.assert_(res.find('no smpp-first') > 0)
219
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200220 def testVtyTree(self):
221 self.vty.enable()
222 self.assertTrue(self.vty.verify("configure terminal", ['']))
223 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100224 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200225 self.assertTrue(self.vty.verify('mncc-int', ['']))
226 self.assertEquals(self.vty.node(), 'config-mncc-int')
227 self.checkForEndAndExit()
228 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200229
230 if self.checkForSmpp():
231 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200232 self.assertTrue(self.vty.verify('smpp', ['']))
233 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100234 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200235 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200236
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200237 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200238 self.assertTrue(self.vty.verify("exit", ['']))
239 self.assertTrue(self.vty.node() is None)
240
241 # Check searching for outer node's commands
242 self.vty.command("configure terminal")
243 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200244
245 if self.checkForSmpp():
246 self.vty.command('smpp')
247 self.assertEquals(self.vty.node(), 'config-smpp')
248 self.vty.command('mncc-int')
249
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200250 self.assertEquals(self.vty.node(), 'config-mncc-int')
251
Maxddee01f2016-05-24 14:23:27 +0200252 def testVtyAuthorization(self):
253 self.vty.enable()
254 self.vty.command("configure terminal")
255 self.vty.command("network")
256 self.assertTrue(self.vty.verify("auth policy closed", ['']))
257 self.assertTrue(self.vty.verify("auth policy regexp", ['']))
258 self.assertTrue(self.vty.verify("authorized-regexp ^001", ['']))
259 self.assertTrue(self.vty.verify("authorized-regexp 02$", ['']))
260 self.assertTrue(self.vty.verify("authorized-regexp *123.*", ['']))
261 self.vty.command("end")
Maxddee01f2016-05-24 14:23:27 +0200262
Max0c1bc262016-04-20 12:06:06 +0200263 def testSi2Q(self):
264 self.vty.enable()
265 self.vty.command("configure terminal")
266 self.vty.command("network")
267 self.vty.command("bts 0")
268 before = self.vty.command("show running-config")
269 self.vty.command("si2quater neighbor-list add earfcn 1911 threshold 11 2")
270 self.vty.command("si2quater neighbor-list add earfcn 1924 threshold 11 3")
271 self.vty.command("si2quater neighbor-list add earfcn 2111 threshold 11")
272 self.vty.command("si2quater neighbor-list del earfcn 1911")
273 self.vty.command("si2quater neighbor-list del earfcn 1924")
274 self.vty.command("si2quater neighbor-list del earfcn 2111")
275 self.assertEquals(before, self.vty.command("show running-config"))
Max26679e02016-04-20 15:57:13 +0200276 self.vty.command("si2quater neighbor-list add uarfcn 1976 13 1")
277 self.vty.command("si2quater neighbor-list add uarfcn 1976 38 1")
278 self.vty.command("si2quater neighbor-list add uarfcn 1976 44 1")
279 self.vty.command("si2quater neighbor-list add uarfcn 1976 120 1")
280 self.vty.command("si2quater neighbor-list add uarfcn 1976 140 1")
281 self.vty.command("si2quater neighbor-list add uarfcn 1976 163 1")
282 self.vty.command("si2quater neighbor-list add uarfcn 1976 166 1")
283 self.vty.command("si2quater neighbor-list add uarfcn 1976 217 1")
284 self.vty.command("si2quater neighbor-list add uarfcn 1976 224 1")
285 self.vty.command("si2quater neighbor-list add uarfcn 1976 225 1")
286 self.vty.command("si2quater neighbor-list add uarfcn 1976 226 1")
287 self.vty.command("si2quater neighbor-list del uarfcn 1976 13")
288 self.vty.command("si2quater neighbor-list del uarfcn 1976 38")
289 self.vty.command("si2quater neighbor-list del uarfcn 1976 44")
290 self.vty.command("si2quater neighbor-list del uarfcn 1976 120")
291 self.vty.command("si2quater neighbor-list del uarfcn 1976 140")
292 self.vty.command("si2quater neighbor-list del uarfcn 1976 163")
293 self.vty.command("si2quater neighbor-list del uarfcn 1976 166")
294 self.vty.command("si2quater neighbor-list del uarfcn 1976 217")
295 self.vty.command("si2quater neighbor-list del uarfcn 1976 224")
296 self.vty.command("si2quater neighbor-list del uarfcn 1976 225")
297 self.vty.command("si2quater neighbor-list del uarfcn 1976 226")
298 self.assertEquals(before, self.vty.command("show running-config"))
Max0c1bc262016-04-20 12:06:06 +0200299
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200300 def testEnableDisablePeriodicLU(self):
301 self.vty.enable()
302 self.vty.command("configure terminal")
303 self.vty.command("network")
304 self.vty.command("bts 0")
305
306 # Test invalid input
307 self.vty.verify("periodic location update 0", ['% Unknown command.'])
308 self.vty.verify("periodic location update 5", ['% Unknown command.'])
309 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
310
311 # Enable periodic lu..
312 self.vty.verify("periodic location update 60", [''])
313 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200314 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200315 self.assertEquals(res.find('no periodic location update'), -1)
316
317 # Now disable it..
318 self.vty.verify("no periodic location update", [''])
319 res = self.vty.command("write terminal")
320 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200321 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200322
Ciabyec6e4f82014-03-06 17:20:55 +0100323 def testShowNetwork(self):
324 res = self.vty.command("show network")
325 self.assert_(res.startswith('BSC is on Country Code') >= 0)
326
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100327 def testMeasurementFeed(self):
328 self.vty.enable()
329 self.vty.command("configure terminal")
330 self.vty.command("mncc-int")
331
332 res = self.vty.command("write terminal")
333 self.assertEquals(res.find('meas-feed scenario'), -1)
334
335 self.vty.command("meas-feed scenario bla")
336 res = self.vty.command("write terminal")
337 self.assert_(res.find('meas-feed scenario bla') > 0)
338
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200339 self.vty.command("meas-feed scenario abcdefghijklmnopqrstuvwxyz01234567890")
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100340 res = self.vty.command("write terminal")
341 self.assertEquals(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz01234567890'), -1)
342 self.assertEquals(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz012345'), -1)
343 self.assert_(res.find('meas-feed scenario abcdefghijklmnopqrstuvwxyz01234') > 0)
344
345
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200346class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200347
348 def vty_command(self):
349 return ["./src/osmo-bsc/osmo-bsc", "-c",
350 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
351
352 def vty_app(self):
353 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
354
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200355 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200356 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200357
358 def testVtyTree(self):
359 self.vty.enable()
360 self.assertTrue(self.vty.verify("configure terminal", ['']))
361 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100362 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200363 self.assertTrue(self.vty.verify("msc 0", ['']))
364 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200365 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200366 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200367 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200368 self.assertTrue(self.vty.verify("bsc", ['']))
369 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200370 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200371 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200372 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200373 self.assertTrue(self.vty.verify("exit", ['']))
374 self.assertTrue(self.vty.node() is None)
375
376 # Check searching for outer node's commands
377 self.vty.command("configure terminal")
378 self.vty.command('msc 0')
379 self.vty.command("bsc")
380 self.assertEquals(self.vty.node(), 'config-bsc')
381 self.vty.command("msc 0")
382 self.assertEquals(self.vty.node(), 'config-msc')
383
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200384 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200385 self.vty.enable()
386 self.vty.command("configure terminal")
387 self.vty.command("msc")
388
389 # Test invalid input
390 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200391 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200392 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200393
394 # Enable USSD notifications
395 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200396 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200397 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200398
399 # Verify settings
400 res = self.vty.command("write terminal")
401 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
402 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200403 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
404 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200405 self.assert_(res.find('bsc-grace-text In grace period') > 0)
406 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200407
408 # Now disable it..
409 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200410 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200411 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200412
413 # Verify settings
414 res = self.vty.command("write terminal")
415 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
416 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200417 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200418 self.assert_(res.find('no bsc-welcome-text') > 0)
419 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
420 self.assert_(res.find('no bsc-grace-text') > 0)
421
422 def testUssdNotificationsBsc(self):
423 self.vty.enable()
424 self.vty.command("configure terminal")
425 self.vty.command("bsc")
426
427 # Test invalid input
428 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
429
430 # Enable USSD notifications
431 self.vty.verify("missing-msc-text No MSC found", [''])
432
433 # Verify settings
434 res = self.vty.command("write terminal")
435 self.assert_(res.find('missing-msc-text No MSC found') > 0)
436 self.assertEquals(res.find('no missing-msc-text'), -1)
437
438 # Now disable it..
439 self.vty.verify("no missing-msc-text", [''])
440
441 # Verify settings
442 res = self.vty.command("write terminal")
443 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
444 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200445
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200446 def testNetworkTimezone(self):
447 self.vty.enable()
448 self.vty.verify("configure terminal", [''])
449 self.vty.verify("network", [''])
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200450
451 # Test invalid input
452 self.vty.verify("timezone", ['% Command incomplete.'])
453 self.vty.verify("timezone 20 0", ['% Unknown command.'])
454 self.vty.verify("timezone 0 11", ['% Unknown command.'])
455 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
456
457 # Set time zone without DST
458 self.vty.verify("timezone 2 30", [''])
459
460 # Verify settings
461 res = self.vty.command("write terminal")
462 self.assert_(res.find('timezone 2 30') > 0)
463 self.assertEquals(res.find('timezone 2 30 '), -1)
464
465 # Set time zone with DST
466 self.vty.verify("timezone 2 30 1", [''])
467
468 # Verify settings
469 res = self.vty.command("write terminal")
470 self.assert_(res.find('timezone 2 30 1') > 0)
471
472 # Now disable it..
473 self.vty.verify("no timezone", [''])
474
475 # Verify settings
476 res = self.vty.command("write terminal")
477 self.assertEquals(res.find(' timezone'), -1)
478
Ciabyec6e4f82014-03-06 17:20:55 +0100479 def testShowNetwork(self):
480 res = self.vty.command("show network")
481 self.assert_(res.startswith('BSC is on Country Code') >= 0)
482
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100483 def testPingPongConfiguration(self):
484 self.vty.enable()
485 self.vty.verify("configure terminal", [''])
486 self.vty.verify("network", [''])
487 self.vty.verify("msc 0", [''])
488
489 self.vty.verify("timeout-ping 12", [''])
490 self.vty.verify("timeout-pong 14", [''])
491 res = self.vty.command("show running-config")
492 self.assert_(res.find(" timeout-ping 12") > 0)
493 self.assert_(res.find(" timeout-pong 14") > 0)
494 self.assert_(res.find(" no timeout-ping advanced") > 0)
495
496 self.vty.verify("timeout-ping advanced", [''])
497 res = self.vty.command("show running-config")
498 self.assert_(res.find(" timeout-ping 12") > 0)
499 self.assert_(res.find(" timeout-pong 14") > 0)
500 self.assert_(res.find(" timeout-ping advanced") > 0)
501
502 self.vty.verify("no timeout-ping advanced", [''])
503 res = self.vty.command("show running-config")
504 self.assert_(res.find(" timeout-ping 12") > 0)
505 self.assert_(res.find(" timeout-pong 14") > 0)
506 self.assert_(res.find(" no timeout-ping advanced") > 0)
507
508 self.vty.verify("no timeout-ping", [''])
509 res = self.vty.command("show running-config")
510 self.assertEquals(res.find(" timeout-ping 12"), -1)
511 self.assertEquals(res.find(" timeout-pong 14"), -1)
512 self.assertEquals(res.find(" no timeout-ping advanced"), -1)
513 self.assert_(res.find(" no timeout-ping") > 0)
514
515 self.vty.verify("timeout-ping advanced", ['%ping handling is disabled. Enable it first.'])
516
517 # And back to enabling it
518 self.vty.verify("timeout-ping 12", [''])
519 self.vty.verify("timeout-pong 14", [''])
520 res = self.vty.command("show running-config")
521 self.assert_(res.find(" timeout-ping 12") > 0)
522 self.assert_(res.find(" timeout-pong 14") > 0)
523 self.assert_(res.find(" timeout-ping advanced") > 0)
524
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200525 def testMscDataCoreLACCI(self):
526 self.vty.enable()
527 res = self.vty.command("show running-config")
528 self.assertEquals(res.find("core-location-area-code"), -1)
529 self.assertEquals(res.find("core-cell-identity"), -1)
530
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200531 self.vty.command("configure terminal")
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200532 self.vty.command("msc 0")
533 self.vty.command("core-location-area-code 666")
534 self.vty.command("core-cell-identity 333")
535
536 res = self.vty.command("show running-config")
537 self.assert_(res.find("core-location-area-code 666") > 0)
538 self.assert_(res.find("core-cell-identity 333") > 0)
539
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200540class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200541
542 def vty_command(self):
Max49364482016-04-13 11:36:39 +0200543 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-l", "127.0.0.1", "-c",
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200544 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
545
546 def vty_app(self):
547 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
548
Max49364482016-04-13 11:36:39 +0200549 def testBSCreload(self):
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400550 # Use different port for the mock msc to avoid clashing with
551 # the osmo-bsc_nat itself
Holger Hans Peter Freytherf1a61bb2016-04-14 08:50:25 -0400552 ip = "127.0.0.1"
Holger Hans Peter Freythere98c9c72016-04-14 10:58:58 -0400553 port = 5522
Max49364482016-04-13 11:36:39 +0200554 self.vty.enable()
555 bscs1 = self.vty.command("show bscs-config")
556 nat_bsc_reload(self)
557 bscs2 = self.vty.command("show bscs-config")
558 # check that multiple calls to bscs-config-file give the same result
559 self.assertEquals(bscs1, bscs2)
560
561 # add new bsc
562 self.vty.command("configure terminal")
563 self.vty.command("nat")
564 self.vty.command("bsc 5")
565 self.vty.command("token key")
566 self.vty.command("location_area_code 666")
567 self.vty.command("end")
568
569 # update bsc token
570 self.vty.command("configure terminal")
571 self.vty.command("nat")
572 self.vty.command("bsc 1")
573 self.vty.command("token xyu")
574 self.vty.command("end")
575
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400576 nat_msc_ip(self, ip, port)
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100577 msc_socket, msc = nat_msc_test(self, ip, port, verbose=True)
578 try:
579 b0 = nat_bsc_sock_test(0, "lol", verbose=True, proc=self.proc)
580 b1 = nat_bsc_sock_test(1, "xyu", verbose=True, proc=self.proc)
581 b2 = nat_bsc_sock_test(5, "key", verbose=True, proc=self.proc)
Max49364482016-04-13 11:36:39 +0200582
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100583 self.assertEquals("3 BSCs configured", self.vty.command("show nat num-bscs-configured"))
584 self.assertTrue(3 == nat_bsc_num_con(self))
585 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
Max49364482016-04-13 11:36:39 +0200586
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100587 nat_bsc_reload(self)
588 bscs2 = self.vty.command("show bscs-config")
589 # check that the reset to initial config succeeded
590 self.assertEquals(bscs1, bscs2)
Max49364482016-04-13 11:36:39 +0200591
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100592 self.assertEquals("2 BSCs configured", self.vty.command("show nat num-bscs-configured"))
593 self.assertTrue(1 == nat_bsc_num_con(self))
594 rem = self.vty.command("show bsc connections").split(' ')
595 # remaining connection is for BSC0
596 self.assertEquals('0', rem[2])
597 # remaining connection is authorized
598 self.assertEquals('1', rem[4])
599 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
600 finally:
601 msc.close()
602 msc_socket.close()
Max49364482016-04-13 11:36:39 +0200603
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200604 def testVtyTree(self):
605 self.vty.enable()
606 self.assertTrue(self.vty.verify('configure terminal', ['']))
607 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100608 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200609 self.assertTrue(self.vty.verify('mgcp', ['']))
610 self.assertEquals(self.vty.node(), 'config-mgcp')
611 self.checkForEndAndExit()
612 self.assertTrue(self.vty.verify('exit', ['']))
613 self.assertEquals(self.vty.node(), 'config')
614 self.assertTrue(self.vty.verify('nat', ['']))
615 self.assertEquals(self.vty.node(), 'config-nat')
616 self.checkForEndAndExit()
617 self.assertTrue(self.vty.verify('bsc 0', ['']))
618 self.assertEquals(self.vty.node(), 'config-nat-bsc')
619 self.checkForEndAndExit()
620 self.assertTrue(self.vty.verify('exit', ['']))
621 self.assertEquals(self.vty.node(), 'config-nat')
622 self.assertTrue(self.vty.verify('exit', ['']))
623 self.assertEquals(self.vty.node(), 'config')
624 self.assertTrue(self.vty.verify('exit', ['']))
625 self.assertTrue(self.vty.node() is None)
626
627 # Check searching for outer node's commands
628 self.vty.command('configure terminal')
629 self.vty.command('mgcp')
630 self.vty.command('nat')
631 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200632 self.vty.command('mgcp')
633 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200634 self.vty.command('nat')
635 self.assertEquals(self.vty.node(), 'config-nat')
636 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200637 self.vty.command('mgcp')
638 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200639
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200640 def testRewriteNoRewrite(self):
641 self.vty.enable()
642 res = self.vty.command("configure terminal")
643 res = self.vty.command("nat")
644 res = self.vty.command("number-rewrite rewrite.cfg")
645 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200646
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400647 def testEnsureNoEnsureModeSet(self):
648 self.vty.enable()
649 res = self.vty.command("configure terminal")
650 res = self.vty.command("nat")
651
652 # Ensure the default
653 res = self.vty.command("show running-config")
654 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
655
656 self.vty.command("sdp-ensure-amr-mode-set")
657 res = self.vty.command("show running-config")
658 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
659
660 self.vty.command("no sdp-ensure-amr-mode-set")
661 res = self.vty.command("show running-config")
662 self.assert_(res.find('\n no sdp-ensure-amr-mode-set') > 0)
663
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200664 def testRewritePostNoRewrite(self):
665 self.vty.enable()
666 self.vty.command("configure terminal")
667 self.vty.command("nat")
668 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
669 self.vty.verify("no number-rewrite-post", [''])
670
671
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200672 def testPrefixTreeLoading(self):
673 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
674
675 self.vty.enable()
676 self.vty.command("configure terminal")
677 self.vty.command("nat")
678 res = self.vty.command("prefix-tree %s" % cfg)
679 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
680 self.vty.command("end")
681
682 res = self.vty.command("show prefix-tree")
683 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')
684
685 self.vty.command("configure terminal")
686 self.vty.command("nat")
687 self.vty.command("no prefix-tree")
688 self.vty.command("end")
689
690 res = self.vty.command("show prefix-tree")
691 self.assertEqual(res, "% there is now prefix tree loaded.")
692
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200693 def testUssdSideChannelProvider(self):
694 self.vty.command("end")
695 self.vty.enable()
696 self.vty.command("configure terminal")
697 self.vty.command("nat")
698 self.vty.command("ussd-token key")
699 self.vty.command("end")
700
701 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
702 self.assertTrue(res)
703
704 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
705 ussdSocket.connect(('127.0.0.1', 5001))
706 ussdSocket.settimeout(2.0)
707 print "Connected to %s:%d" % ussdSocket.getpeername()
708
709 print "Expecting ID_GET request"
710 data = ussdSocket.recv(4)
711 self.assertEqual(data, "\x00\x01\xfe\x04")
712
713 print "Going to send ID_RESP response"
Max3e676892016-11-16 14:55:21 +0100714 res = ussdSocket.send(IPA().id_resp(IPA().tag_name('key')))
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200715 self.assertEqual(res, 10)
716
717 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
718
719 print "Going to send PING request"
Max3e676892016-11-16 14:55:21 +0100720 res = ussdSocket.send(IPA().ping())
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200721 self.assertEqual(res, 4)
722
723 print "Expecting PONG response"
724 data = ussdSocket.recv(4)
725 self.assertEqual(data, "\x00\x01\xfe\x01")
726
727 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
728 self.assertTrue(res)
729
730 print "Going to shut down connection"
731 ussdSocket.shutdown(socket.SHUT_WR)
732
733 print "Expecting EOF"
734 data = ussdSocket.recv(4)
735 self.assertEqual(data, "")
736
737 ussdSocket.close()
738
739 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
740 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200741
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100742 def testAccessList(self):
743 """
744 Verify that the imsi-deny can have a reject cause or no reject cause
745 """
746 self.vty.enable()
747 self.vty.command("configure terminal")
748 self.vty.command("nat")
749
750 # Old default
751 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
752 res = self.vty.command("show running-config").split("\r\n")
753 asserted = False
754 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100755 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100756 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
757 asserted = True
758 self.assert_(asserted)
759
760 # Check the optional CM Service Reject Cause
761 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
762 res = self.vty.command("show running-config").split("\r\n")
763 asserted = False
764 for line in res:
765 if line.startswith(" access-list test-cm"):
766 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
767 asserted = True
768 self.assert_(asserted)
769
770 # Check the optional LU Reject Cause
771 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
772 res = self.vty.command("show running-config").split("\r\n")
773 asserted = False
774 for line in res:
775 if line.startswith(" access-list test-lu"):
776 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
777 asserted = True
778 self.assert_(asserted)
779
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200780class TestVTYGbproxy(TestVTYGenericBSC):
781
782 def vty_command(self):
783 return ["./src/gprs/osmo-gbproxy", "-c",
784 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
785
786 def vty_app(self):
787 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
788
789 def testVtyTree(self):
790 self.vty.enable()
791 self.assertTrue(self.vty.verify('configure terminal', ['']))
792 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100793 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200794 self.assertTrue(self.vty.verify('ns', ['']))
795 self.assertEquals(self.vty.node(), 'config-ns')
796 self.checkForEndAndExit()
797 self.assertTrue(self.vty.verify('exit', ['']))
798 self.assertEquals(self.vty.node(), 'config')
799 self.assertTrue(self.vty.verify('gbproxy', ['']))
800 self.assertEquals(self.vty.node(), 'config-gbproxy')
801 self.checkForEndAndExit()
802 self.assertTrue(self.vty.verify('exit', ['']))
803 self.assertEquals(self.vty.node(), 'config')
804
805 def testVtyShow(self):
806 res = self.vty.command("show ns")
807 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
808
809 res = self.vty.command("show gbproxy stats")
810 self.assert_(res.find('GBProxy Global Statistics') >= 0)
811
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200812 def testVtyDeletePeer(self):
813 self.vty.enable()
814 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
815 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
816 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
817 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
818 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
819 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
820 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
821 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
822 self.assert_(res.find('Not Deleted 0 BVC') < 0)
823 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
824 res = self.vty.command("delete-gbproxy-peer 9999 all")
825 self.assert_(res.find('Deleted 0 BVC') >= 0)
826 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
827
Jacob Erlbeck144b8b12014-11-04 11:15:01 +0100828class TestVTYSGSN(TestVTYGenericBSC):
829
830 def vty_command(self):
831 return ["./src/gprs/osmo-sgsn", "-c",
832 "doc/examples/osmo-sgsn/osmo-sgsn.cfg"]
833
834 def vty_app(self):
835 return (4245, "./src/gprs/osmo-sgsn", "OsmoSGSN", "sgsn")
836
837 def testVtyTree(self):
838 self.vty.enable()
839 self.assertTrue(self.vty.verify('configure terminal', ['']))
840 self.assertEquals(self.vty.node(), 'config')
841 self.checkForEndAndExit()
842 self.assertTrue(self.vty.verify('ns', ['']))
843 self.assertEquals(self.vty.node(), 'config-ns')
844 self.checkForEndAndExit()
845 self.assertTrue(self.vty.verify('exit', ['']))
846 self.assertEquals(self.vty.node(), 'config')
847 self.assertTrue(self.vty.verify('sgsn', ['']))
848 self.assertEquals(self.vty.node(), 'config-sgsn')
849 self.checkForEndAndExit()
850 self.assertTrue(self.vty.verify('exit', ['']))
851 self.assertEquals(self.vty.node(), 'config')
852
853 def testVtyShow(self):
854 res = self.vty.command("show ns")
855 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
856 self.assertTrue(self.vty.verify('show bssgp', ['']))
857 self.assertTrue(self.vty.verify('show bssgp stats', ['']))
858 # TODO: uncomment when the command does not segfault anymore
859 # self.assertTrue(self.vty.verify('show bssgp nsei 123', ['']))
860 # self.assertTrue(self.vty.verify('show bssgp nsei 123 stats', ['']))
861
862 self.assertTrue(self.vty.verify('show sgsn', ['']))
863 self.assertTrue(self.vty.verify('show mm-context all', ['']))
864 self.assertTrue(self.vty.verify('show mm-context imsi 000001234567', ['No MM context for IMSI 000001234567']))
865 self.assertTrue(self.vty.verify('show pdp-context all', ['']))
866
867 res = self.vty.command("show sndcp")
868 self.assert_(res.find('State of SNDCP Entities') >= 0)
869
870 res = self.vty.command("show llc")
871 self.assert_(res.find('State of LLC Entities') >= 0)
872
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100873 def testVtyAuth(self):
874 self.vty.enable()
875 self.assertTrue(self.vty.verify('configure terminal', ['']))
876 self.assertEquals(self.vty.node(), 'config')
877 self.assertTrue(self.vty.verify('sgsn', ['']))
878 self.assertEquals(self.vty.node(), 'config-sgsn')
879 self.assertTrue(self.vty.verify('auth-policy accept-all', ['']))
880 res = self.vty.command("show running-config")
881 self.assert_(res.find('auth-policy accept-all') > 0)
882 self.assertTrue(self.vty.verify('auth-policy acl-only', ['']))
883 res = self.vty.command("show running-config")
884 self.assert_(res.find('auth-policy acl-only') > 0)
885 self.assertTrue(self.vty.verify('auth-policy closed', ['']))
886 res = self.vty.command("show running-config")
887 self.assert_(res.find('auth-policy closed') > 0)
Max176b62a2016-07-04 11:09:07 +0200888 self.assertTrue(self.vty.verify('gsup remote-ip 127.0.0.4', ['']))
889 self.assertTrue(self.vty.verify('gsup remote-port 2222', ['']))
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100890 self.assertTrue(self.vty.verify('auth-policy remote', ['']))
891 res = self.vty.command("show running-config")
892 self.assert_(res.find('auth-policy remote') > 0)
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100893
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100894 def testVtySubscriber(self):
895 self.vty.enable()
896 res = self.vty.command('show subscriber cache')
897 self.assert_(res.find('1234567890') < 0)
Jacob Erlbeckd9193432015-01-19 14:11:46 +0100898 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 create', ['']))
899 res = self.vty.command('show subscriber cache')
900 self.assert_(res.find('1234567890') >= 0)
901 self.assert_(res.find('Authorized: 0') >= 0)
902 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 update-location-result ok', ['']))
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100903 res = self.vty.command('show subscriber cache')
904 self.assert_(res.find('1234567890') >= 0)
905 self.assert_(res.find('Authorized: 1') >= 0)
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100906 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 cancel update-procedure', ['']))
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100907 res = self.vty.command('show subscriber cache')
Jacob Erlbecke988ae42015-01-27 12:41:19 +0100908 self.assert_(res.find('1234567890') >= 0)
909 self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 destroy', ['']))
910 res = self.vty.command('show subscriber cache')
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100911 self.assert_(res.find('1234567890') < 0)
912
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100913 def testVtyGgsn(self):
914 self.vty.enable()
915 self.assertTrue(self.vty.verify('configure terminal', ['']))
916 self.assertEquals(self.vty.node(), 'config')
917 self.assertTrue(self.vty.verify('sgsn', ['']))
918 self.assertEquals(self.vty.node(), 'config-sgsn')
919 self.assertTrue(self.vty.verify('ggsn 0 remote-ip 127.99.99.99', ['']))
920 self.assertTrue(self.vty.verify('ggsn 0 gtp-version 1', ['']))
921 self.assertTrue(self.vty.verify('apn * ggsn 0', ['']))
922 self.assertTrue(self.vty.verify('apn apn1.test ggsn 0', ['']))
923 self.assertTrue(self.vty.verify('apn apn1.test ggsn 1', ['% a GGSN with id 1 has not been defined']))
924 self.assertTrue(self.vty.verify('apn apn1.test imsi-prefix 123456 ggsn 0', ['']))
925 self.assertTrue(self.vty.verify('apn apn2.test imsi-prefix 123456 ggsn 0', ['']))
926 res = self.vty.command("show running-config")
927 self.assert_(res.find('ggsn 0 remote-ip 127.99.99.99') >= 0)
928 self.assert_(res.find('ggsn 0 gtp-version 1') >= 0)
929 self.assert_(res.find('apn * ggsn 0') >= 0)
930 self.assert_(res.find('apn apn1.test ggsn 0') >= 0)
931 self.assert_(res.find('apn apn1.test imsi-prefix 123456 ggsn 0') >= 0)
932 self.assert_(res.find('apn apn2.test imsi-prefix 123456 ggsn 0') >= 0)
933
Holger Hans Peter Freyther9c20a5f2015-02-06 16:23:29 +0100934 def testVtyEasyAPN(self):
935 self.vty.enable()
936 self.assertTrue(self.vty.verify('configure terminal', ['']))
937 self.assertEquals(self.vty.node(), 'config')
938 self.assertTrue(self.vty.verify('sgsn', ['']))
939 self.assertEquals(self.vty.node(), 'config-sgsn')
940
941 res = self.vty.command("show running-config")
942 self.assertEquals(res.find("apn internet"), -1)
943
944 self.assertTrue(self.vty.verify("access-point-name internet.apn", ['']))
945 res = self.vty.command("show running-config")
946 self.assert_(res.find("apn internet.apn ggsn 0") >= 0)
947
948 self.assertTrue(self.vty.verify("no access-point-name internet.apn", ['']))
949 res = self.vty.command("show running-config")
950 self.assertEquals(res.find("apn internet"), -1)
951
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +0200952 def testVtyCDR(self):
953 self.vty.enable()
954 self.assertTrue(self.vty.verify('configure terminal', ['']))
955 self.assertEquals(self.vty.node(), 'config')
956 self.assertTrue(self.vty.verify('sgsn', ['']))
957 self.assertEquals(self.vty.node(), 'config-sgsn')
958
959 res = self.vty.command("show running-config")
960 self.assert_(res.find("no cdr filename") > 0)
961
962 self.vty.command("cdr filename bla.cdr")
963 res = self.vty.command("show running-config")
964 self.assertEquals(res.find("no cdr filename"), -1)
965 self.assert_(res.find(" cdr filename bla.cdr") > 0)
966
967 self.vty.command("no cdr filename")
968 res = self.vty.command("show running-config")
969 self.assert_(res.find("no cdr filename") > 0)
970 self.assertEquals(res.find(" cdr filename bla.cdr"), -1)
971
972 res = self.vty.command("show running-config")
973 self.assert_(res.find(" cdr interval 600") > 0)
974
975 self.vty.command("cdr interval 900")
976 res = self.vty.command("show running-config")
977 self.assert_(res.find(" cdr interval 900") > 0)
978 self.assertEquals(res.find(" cdr interval 600"), -1)
979
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200980def add_nat_test(suite, workdir):
981 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
982 print("Skipping the NAT test")
983 return
984 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
985 suite.addTest(test)
986
Max49364482016-04-13 11:36:39 +0200987def nat_bsc_reload(x):
988 x.vty.command("configure terminal")
989 x.vty.command("nat")
Neels Hofmeyr625e05a2017-07-20 17:57:37 +0200990 x.vty.command("bscs-config-file bscs.cfg")
Max49364482016-04-13 11:36:39 +0200991 x.vty.command("end")
992
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400993def nat_msc_ip(x, ip, port):
Max49364482016-04-13 11:36:39 +0200994 x.vty.command("configure terminal")
995 x.vty.command("nat")
996 x.vty.command("msc ip " + ip)
Holger Hans Peter Freyther84ae27e2016-04-14 10:40:06 -0400997 x.vty.command("msc port " + str(port))
Max49364482016-04-13 11:36:39 +0200998 x.vty.command("end")
999
1000def data2str(d):
Holger Hans Peter Freyther8bb62042016-04-14 21:40:04 -04001001 return d.encode('hex').lower()
Max49364482016-04-13 11:36:39 +02001002
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -04001003def nat_msc_test(x, ip, port, verbose = False):
Max49364482016-04-13 11:36:39 +02001004 msc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Neels Hofmeyr53403452017-02-28 02:38:43 +01001005 msc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +02001006 msc.settimeout(5)
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -04001007 msc.bind((ip, port))
Max49364482016-04-13 11:36:39 +02001008 msc.listen(5)
1009 if (verbose):
1010 print "MSC is ready at " + ip
Neels Hofmeyr89d20b62016-09-26 12:59:36 +02001011 conn = None
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +02001012 while True:
1013 vty_response = x.vty.command("show msc connection")
1014 print "'show msc connection' says: %r" % vty_response
1015 if vty_response == "MSC is connected: 1":
1016 # success
1017 break;
1018 if vty_response != "MSC is connected: 0":
1019 raise Exception("Unexpected response to 'show msc connection'"
1020 " vty command: %r" % vty_response)
1021
1022 timeout_retries = 6
1023 while timeout_retries > 0:
1024 try:
1025 conn, addr = msc.accept()
1026 print "MSC got connection from ", addr
1027 break
1028 except socket.timeout:
1029 print "socket timed out."
1030 timeout_retries -= 1
1031 continue
1032
Neels Hofmeyr89d20b62016-09-26 12:59:36 +02001033 if not conn:
Neels Hofmeyr0867b722016-09-28 23:28:06 +02001034 raise Exception("VTY reports MSC is connected, but I haven't"
1035 " connected yet: %r %r" % (ip, port))
Neels Hofmeyr0b619322017-02-28 02:37:39 +01001036 return msc, conn
Max49364482016-04-13 11:36:39 +02001037
1038def ipa_handle_small(x, verbose = False):
1039 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +01001040 if len(s) != 4*2:
1041 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +02001042 if "0001fe00" == s:
1043 if (verbose):
1044 print "\tBSC <- NAT: PING?"
Max3e676892016-11-16 14:55:21 +01001045 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +02001046 elif "0001fe06" == s:
1047 if (verbose):
1048 print "\tBSC <- NAT: IPA ID ACK"
Max3e676892016-11-16 14:55:21 +01001049 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +02001050 elif "0001fe00" == s:
1051 if (verbose):
1052 print "\tBSC <- NAT: PONG!"
1053 else:
1054 if (verbose):
1055 print "\tBSC <- NAT: ", s
1056
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001057def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +02001058 s = data2str(x.recv(38))
1059 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001060 retries = 3
1061 while True:
1062 print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
1063 try:
1064 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
1065 print "\tdone sending IPA identity(%s) at %s" % (tk,
1066 time.strftime("%T"))
1067 break
1068 except:
1069 print "\tfailed sending IPA identity at", time.strftime("%T")
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001070 if proc:
1071 print "\tproc.poll() = %r" % proc.poll()
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001072 if retries < 1:
1073 print "\tgiving up"
1074 raise
Neels Hofmeyre02e1e72017-02-03 05:55:26 +01001075 print "\tretrying (%d attempts left)" % retries
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +01001076 retries -= 1
Max49364482016-04-13 11:36:39 +02001077 else:
1078 if (verbose):
1079 print "\tBSC <- NAT: ", s
1080
1081def nat_bsc_num_con(x):
1082 return len(x.vty.command("show bsc connections").split('\n'))
1083
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001084def nat_bsc_sock_test(nr, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +02001085 bsc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Holger Hans Peter Freyther2abf2b02016-04-14 21:13:51 -04001086 bsc.bind(('127.0.0.1', 0))
Max49364482016-04-13 11:36:39 +02001087 bsc.connect(('127.0.0.1', 5000))
1088 if (verbose):
1089 print "BSC%d " %nr
1090 print "\tconnected to %s:%d" % bsc.getpeername()
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001091 if proc:
1092 print "\tproc.poll() = %r" % proc.poll()
1093 print "\tproc.pid = %r" % proc.pid
Max49364482016-04-13 11:36:39 +02001094 ipa_handle_small(bsc, verbose)
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001095 ipa_handle_resp(bsc, tk, verbose, proc=proc)
1096 if proc:
1097 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +02001098 bsc.recv(27) # MGCP msg
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +01001099 if proc:
1100 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +02001101 ipa_handle_small(bsc, verbose)
1102 return bsc
1103
Jacob Erlbeck1b894022013-08-28 10:16:54 +02001104def add_bsc_test(suite, workdir):
1105 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
1106 print("Skipping the BSC test")
1107 return
1108 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
1109 suite.addTest(test)
1110
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001111def add_gbproxy_test(suite, workdir):
1112 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
1113 print("Skipping the Gb-Proxy test")
1114 return
1115 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
1116 suite.addTest(test)
1117
Jacob Erlbeck144b8b12014-11-04 11:15:01 +01001118def add_sgsn_test(suite, workdir):
1119 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-sgsn")):
1120 print("Skipping the SGSN test")
1121 return
1122 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYSGSN)
1123 suite.addTest(test)
1124
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001125if __name__ == '__main__':
1126 import argparse
1127 import sys
1128
1129 workdir = '.'
1130
1131 parser = argparse.ArgumentParser()
1132 parser.add_argument("-v", "--verbose", dest="verbose",
1133 action="store_true", help="verbose mode")
1134 parser.add_argument("-p", "--pythonconfpath", dest="p",
1135 help="searchpath for config")
1136 parser.add_argument("-w", "--workdir", dest="w",
1137 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +01001138 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001139 args = parser.parse_args()
1140
1141 verbose_level = 1
1142 if args.verbose:
1143 verbose_level = 2
1144
1145 if args.w:
1146 workdir = args.w
1147
1148 if args.p:
1149 confpath = args.p
1150
1151 print "confpath %s, workdir %s" % (confpath, workdir)
1152 os.chdir(workdir)
1153 print "Running tests for specific VTY commands"
1154 suite = unittest.TestSuite()
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +02001155 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001156 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMSC))
Jacob Erlbeck1b894022013-08-28 10:16:54 +02001157 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001158 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +02001159 add_gbproxy_test(suite, workdir)
Jacob Erlbeck144b8b12014-11-04 11:15:01 +01001160 add_sgsn_test(suite, workdir)
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +01001161
1162 if args.test_name:
1163 osmoutil.pick_tests(suite, *args.test_name)
1164
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +02001165 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001166 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +02001167
Neels Hofmeyr0867b722016-09-28 23:28:06 +02001168# vim: shiftwidth=4 expandtab nocin ai