blob: 468d415e8ae0a2177fa532fcf9355de9adcb56c6 [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
18import os
19import time
20import unittest
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020021import socket
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020022
23import osmopy.obscvty as obscvty
24import osmopy.osmoutil as osmoutil
25
26confpath = '.'
27
28class TestVTYBase(unittest.TestCase):
29
30 def vty_command(self):
31 raise Exception("Needs to be implemented by a subclass")
32
33 def vty_app(self):
34 raise Exception("Needs to be implemented by a subclass")
35
36 def setUp(self):
37 osmo_vty_cmd = self.vty_command()[:]
38 config_index = osmo_vty_cmd.index('-c')
39 if config_index:
40 cfi = config_index + 1
41 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
42
43 try:
44 print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())
45 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
46 except OSError:
47 print >> sys.stderr, "Current directory: %s" % os.getcwd()
48 print >> sys.stderr, "Consider setting -b"
49 time.sleep(1)
50
51 appstring = self.vty_app()[2]
52 appport = self.vty_app()[0]
53 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
54
55 def tearDown(self):
56 self.vty = None
57 osmoutil.end_proc(self.proc)
58
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020059class TestVTYMGCP(TestVTYBase):
60 def vty_command(self):
61 return ["./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "-c",
62 "doc/examples/osmo-bsc_mgcp/mgcp.cfg"]
63
64 def vty_app(self):
65 return (4243, "./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "OpenBSC MGCP", "mgcp")
66
67 def testForcePtime(self):
68 self.vty.enable()
69 res = self.vty.command("show running-config")
70 self.assert_(res.find(' rtp force-ptime 20\r') > 0)
71 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
72
73 self.vty.command("configure terminal")
74 self.vty.command("mgcp")
75 self.vty.command("no rtp force-ptime")
76 res = self.vty.command("show running-config")
77 self.assertEquals(res.find(' rtp force-ptime 20\r'), -1)
78 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
79
Jacob Erlbeck96903c42013-09-02 13:17:14 +020080
81class TestVTYGenericBSC(TestVTYBase):
82
83 def checkForEndAndExit(self):
84 res = self.vty.command("list")
85 #print ('looking for "exit"\n')
86 self.assert_(res.find(' exit\r') > 0)
87 #print 'found "exit"\nlooking for "end"\n'
88 self.assert_(res.find(' end\r') > 0)
89 #print 'found "end"\n'
90
Jacob Erlbeck96903c42013-09-02 13:17:14 +020091 def _testConfigNetworkTree(self):
92 self.vty.enable()
93 self.assertTrue(self.vty.verify("configure terminal",['']))
94 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +010095 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +020096 self.assertTrue(self.vty.verify("network",['']))
97 self.assertEquals(self.vty.node(), 'config-net')
98 self.checkForEndAndExit()
99 self.assertTrue(self.vty.verify("bts 0",['']))
100 self.assertEquals(self.vty.node(), 'config-net-bts')
101 self.checkForEndAndExit()
102 self.assertTrue(self.vty.verify("trx 0",['']))
103 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
104 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +0200105 self.vty.command("write terminal")
106 self.assertTrue(self.vty.verify("exit",['']))
107 self.assertEquals(self.vty.node(), 'config-net-bts')
108 self.assertTrue(self.vty.verify("exit",['']))
109 self.assertTrue(self.vty.verify("bts 1",['']))
110 self.assertEquals(self.vty.node(), 'config-net-bts')
111 self.checkForEndAndExit()
112 self.assertTrue(self.vty.verify("trx 1",['']))
113 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
114 self.checkForEndAndExit()
115 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200116 self.assertTrue(self.vty.verify("exit",['']))
117 self.assertEquals(self.vty.node(), 'config-net-bts')
118 self.assertTrue(self.vty.verify("exit",['']))
119 self.assertEquals(self.vty.node(), 'config-net')
120 self.assertTrue(self.vty.verify("exit",['']))
121 self.assertEquals(self.vty.node(), 'config')
122 self.assertTrue(self.vty.verify("exit",['']))
123 self.assertTrue(self.vty.node() is None)
124
125class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200126
127 def vty_command(self):
128 return ["./src/osmo-nitb/osmo-nitb", "-c",
129 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
130
131 def vty_app(self):
132 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
133
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200134 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200135 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200136
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200137 def checkForSmpp(self):
138 """SMPP is not always enabled, check if it is"""
139 res = self.vty.command("list")
140 return "smpp" in res
141
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200142 def testVtyTree(self):
143 self.vty.enable()
144 self.assertTrue(self.vty.verify("configure terminal", ['']))
145 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100146 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200147 self.assertTrue(self.vty.verify('mncc-int', ['']))
148 self.assertEquals(self.vty.node(), 'config-mncc-int')
149 self.checkForEndAndExit()
150 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200151
152 if self.checkForSmpp():
153 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200154 self.assertTrue(self.vty.verify('smpp', ['']))
155 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100156 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200157 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200158
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200159 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200160 self.assertTrue(self.vty.verify("exit", ['']))
161 self.assertTrue(self.vty.node() is None)
162
163 # Check searching for outer node's commands
164 self.vty.command("configure terminal")
165 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200166
167 if self.checkForSmpp():
168 self.vty.command('smpp')
169 self.assertEquals(self.vty.node(), 'config-smpp')
170 self.vty.command('mncc-int')
171
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200172 self.assertEquals(self.vty.node(), 'config-mncc-int')
173
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200174 def testEnableDisablePeriodicLU(self):
175 self.vty.enable()
176 self.vty.command("configure terminal")
177 self.vty.command("network")
178 self.vty.command("bts 0")
179
180 # Test invalid input
181 self.vty.verify("periodic location update 0", ['% Unknown command.'])
182 self.vty.verify("periodic location update 5", ['% Unknown command.'])
183 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
184
185 # Enable periodic lu..
186 self.vty.verify("periodic location update 60", [''])
187 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200188 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200189 self.assertEquals(res.find('no periodic location update'), -1)
190
191 # Now disable it..
192 self.vty.verify("no periodic location update", [''])
193 res = self.vty.command("write terminal")
194 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200195 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200196
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100197 def testEnableDisableSiHacks(self):
198 self.vty.enable()
199 self.vty.command("configure terminal")
200 self.vty.command("network")
201 self.vty.command("bts 0")
202
203 # Enable periodic lu..
204 self.vty.verify("force-combined-si", [''])
205 res = self.vty.command("write terminal")
206 self.assert_(res.find(' force-combined-si') > 0)
207 self.assertEquals(res.find('no force-combined-si'), -1)
208
209 # Now disable it..
210 self.vty.verify("no force-combined-si", [''])
211 res = self.vty.command("write terminal")
212 self.assertEquals(res.find(' force-combined-si'), -1)
213 self.assert_(res.find('no force-combined-si') > 0)
214
Ivan Kluchnikov67920592013-09-16 13:13:04 +0400215 def testRachAccessControlClass(self):
216 self.vty.enable()
217 self.vty.command("configure terminal")
218 self.vty.command("network")
219 self.vty.command("bts 0")
220
221 # Test invalid input
222 self.vty.verify("rach access-control-class", ['% Command incomplete.'])
223 self.vty.verify("rach access-control-class 1", ['% Command incomplete.'])
224 self.vty.verify("rach access-control-class -1", ['% Unknown command.'])
225 self.vty.verify("rach access-control-class 10", ['% Unknown command.'])
226 self.vty.verify("rach access-control-class 16", ['% Unknown command.'])
227
228 # Barred rach access control classes
229 for classNum in range(16):
230 if classNum != 10:
231 self.vty.verify("rach access-control-class " + str(classNum) + " barred", [''])
232
233 # Verify settings
234 res = self.vty.command("write terminal")
235 for classNum in range(16):
236 if classNum != 10:
237 self.assert_(res.find("rach access-control-class " + str(classNum) + " barred") > 0)
238
239 # Allowed rach access control classes
240 for classNum in range(16):
241 if classNum != 10:
242 self.vty.verify("rach access-control-class " + str(classNum) + " allowed", [''])
243
244 # Verify settings
245 res = self.vty.command("write terminal")
246 for classNum in range(16):
247 if classNum != 10:
248 self.assertEquals(res.find("rach access-control-class " + str(classNum) + " barred"), -1)
249
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500250 def testSubscriberCreateDelete(self):
Alexander Chemerisbd6d40f2013-10-04 23:54:17 +0200251 self.vty.enable()
252
253 imsi = "204300854013739"
254
255 # Initially we don't have this subscriber
256 self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi])
257
258 # Lets create one
259 res = self.vty.command('subscriber create imsi '+imsi)
260 self.assert_(res.find(" IMSI: "+imsi) > 0)
261
262 # Now we have it
263 res = self.vty.command('show subscriber imsi '+imsi)
264 self.assert_(res.find(" IMSI: "+imsi) > 0)
265
Ruben Pollaned04a0d2014-09-24 20:50:13 -0500266 # Delete it
267 res = self.vty.command('subscriber delete imsi '+imsi)
268 self.assert_(res != "")
269
270 # Now it should not be there anymore
271 res = self.vty.command('show subscriber imsi '+imsi)
272 self.assert_(res != '% No subscriber found for imsi '+imsi)
273
Holger Hans Peter Freytherec37bb22013-02-05 09:39:09 +0100274 def testShowPagingGroup(self):
275 res = self.vty.command("show paging-group 255 1234567")
276 self.assertEqual(res, "% can't find BTS 255")
277 res = self.vty.command("show paging-group 0 1234567")
278 self.assertEquals(res, "%Paging group for IMSI 1234567 on BTS #0 is 7")
279
Ciabyec6e4f82014-03-06 17:20:55 +0100280 def testShowNetwork(self):
281 res = self.vty.command("show network")
282 self.assert_(res.startswith('BSC is on Country Code') >= 0)
283
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200284class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200285
286 def vty_command(self):
287 return ["./src/osmo-bsc/osmo-bsc", "-c",
288 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
289
290 def vty_app(self):
291 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
292
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200293 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200294 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200295
296 def testVtyTree(self):
297 self.vty.enable()
298 self.assertTrue(self.vty.verify("configure terminal", ['']))
299 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100300 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200301 self.assertTrue(self.vty.verify("msc 0", ['']))
302 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200303 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200304 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200305 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200306 self.assertTrue(self.vty.verify("bsc", ['']))
307 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200308 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200309 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200310 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200311 self.assertTrue(self.vty.verify("exit", ['']))
312 self.assertTrue(self.vty.node() is None)
313
314 # Check searching for outer node's commands
315 self.vty.command("configure terminal")
316 self.vty.command('msc 0')
317 self.vty.command("bsc")
318 self.assertEquals(self.vty.node(), 'config-bsc')
319 self.vty.command("msc 0")
320 self.assertEquals(self.vty.node(), 'config-msc')
321
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200322 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200323 self.vty.enable()
324 self.vty.command("configure terminal")
325 self.vty.command("msc")
326
327 # Test invalid input
328 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200329 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200330 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200331
332 # Enable USSD notifications
333 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200334 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200335 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200336
337 # Verify settings
338 res = self.vty.command("write terminal")
339 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
340 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200341 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
342 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200343 self.assert_(res.find('bsc-grace-text In grace period') > 0)
344 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200345
346 # Now disable it..
347 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200348 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200349 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200350
351 # Verify settings
352 res = self.vty.command("write terminal")
353 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
354 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200355 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200356 self.assert_(res.find('no bsc-welcome-text') > 0)
357 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
358 self.assert_(res.find('no bsc-grace-text') > 0)
359
360 def testUssdNotificationsBsc(self):
361 self.vty.enable()
362 self.vty.command("configure terminal")
363 self.vty.command("bsc")
364
365 # Test invalid input
366 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
367
368 # Enable USSD notifications
369 self.vty.verify("missing-msc-text No MSC found", [''])
370
371 # Verify settings
372 res = self.vty.command("write terminal")
373 self.assert_(res.find('missing-msc-text No MSC found') > 0)
374 self.assertEquals(res.find('no missing-msc-text'), -1)
375
376 # Now disable it..
377 self.vty.verify("no missing-msc-text", [''])
378
379 # Verify settings
380 res = self.vty.command("write terminal")
381 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
382 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200383
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200384 def testNetworkTimezone(self):
385 self.vty.enable()
386 self.vty.verify("configure terminal", [''])
387 self.vty.verify("network", [''])
388 self.vty.verify("bts 0", [''])
389
390 # Test invalid input
391 self.vty.verify("timezone", ['% Command incomplete.'])
392 self.vty.verify("timezone 20 0", ['% Unknown command.'])
393 self.vty.verify("timezone 0 11", ['% Unknown command.'])
394 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
395
396 # Set time zone without DST
397 self.vty.verify("timezone 2 30", [''])
398
399 # Verify settings
400 res = self.vty.command("write terminal")
401 self.assert_(res.find('timezone 2 30') > 0)
402 self.assertEquals(res.find('timezone 2 30 '), -1)
403
404 # Set time zone with DST
405 self.vty.verify("timezone 2 30 1", [''])
406
407 # Verify settings
408 res = self.vty.command("write terminal")
409 self.assert_(res.find('timezone 2 30 1') > 0)
410
411 # Now disable it..
412 self.vty.verify("no timezone", [''])
413
414 # Verify settings
415 res = self.vty.command("write terminal")
416 self.assertEquals(res.find(' timezone'), -1)
417
Ciabyec6e4f82014-03-06 17:20:55 +0100418 def testShowNetwork(self):
419 res = self.vty.command("show network")
420 self.assert_(res.startswith('BSC is on Country Code') >= 0)
421
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100422 def testPingPongConfiguration(self):
423 self.vty.enable()
424 self.vty.verify("configure terminal", [''])
425 self.vty.verify("network", [''])
426 self.vty.verify("msc 0", [''])
427
428 self.vty.verify("timeout-ping 12", [''])
429 self.vty.verify("timeout-pong 14", [''])
430 res = self.vty.command("show running-config")
431 self.assert_(res.find(" timeout-ping 12") > 0)
432 self.assert_(res.find(" timeout-pong 14") > 0)
433 self.assert_(res.find(" no timeout-ping advanced") > 0)
434
435 self.vty.verify("timeout-ping advanced", [''])
436 res = self.vty.command("show running-config")
437 self.assert_(res.find(" timeout-ping 12") > 0)
438 self.assert_(res.find(" timeout-pong 14") > 0)
439 self.assert_(res.find(" timeout-ping advanced") > 0)
440
441 self.vty.verify("no timeout-ping advanced", [''])
442 res = self.vty.command("show running-config")
443 self.assert_(res.find(" timeout-ping 12") > 0)
444 self.assert_(res.find(" timeout-pong 14") > 0)
445 self.assert_(res.find(" no timeout-ping advanced") > 0)
446
447 self.vty.verify("no timeout-ping", [''])
448 res = self.vty.command("show running-config")
449 self.assertEquals(res.find(" timeout-ping 12"), -1)
450 self.assertEquals(res.find(" timeout-pong 14"), -1)
451 self.assertEquals(res.find(" no timeout-ping advanced"), -1)
452 self.assert_(res.find(" no timeout-ping") > 0)
453
454 self.vty.verify("timeout-ping advanced", ['%ping handling is disabled. Enable it first.'])
455
456 # And back to enabling it
457 self.vty.verify("timeout-ping 12", [''])
458 self.vty.verify("timeout-pong 14", [''])
459 res = self.vty.command("show running-config")
460 self.assert_(res.find(" timeout-ping 12") > 0)
461 self.assert_(res.find(" timeout-pong 14") > 0)
462 self.assert_(res.find(" timeout-ping advanced") > 0)
463
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200464class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200465
466 def vty_command(self):
467 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
468 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
469
470 def vty_app(self):
471 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
472
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200473 def testVtyTree(self):
474 self.vty.enable()
475 self.assertTrue(self.vty.verify('configure terminal', ['']))
476 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100477 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200478 self.assertTrue(self.vty.verify('mgcp', ['']))
479 self.assertEquals(self.vty.node(), 'config-mgcp')
480 self.checkForEndAndExit()
481 self.assertTrue(self.vty.verify('exit', ['']))
482 self.assertEquals(self.vty.node(), 'config')
483 self.assertTrue(self.vty.verify('nat', ['']))
484 self.assertEquals(self.vty.node(), 'config-nat')
485 self.checkForEndAndExit()
486 self.assertTrue(self.vty.verify('bsc 0', ['']))
487 self.assertEquals(self.vty.node(), 'config-nat-bsc')
488 self.checkForEndAndExit()
489 self.assertTrue(self.vty.verify('exit', ['']))
490 self.assertEquals(self.vty.node(), 'config-nat')
491 self.assertTrue(self.vty.verify('exit', ['']))
492 self.assertEquals(self.vty.node(), 'config')
493 self.assertTrue(self.vty.verify('exit', ['']))
494 self.assertTrue(self.vty.node() is None)
495
496 # Check searching for outer node's commands
497 self.vty.command('configure terminal')
498 self.vty.command('mgcp')
499 self.vty.command('nat')
500 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200501 self.vty.command('mgcp')
502 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200503 self.vty.command('nat')
504 self.assertEquals(self.vty.node(), 'config-nat')
505 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200506 self.vty.command('mgcp')
507 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200508
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200509 def testRewriteNoRewrite(self):
510 self.vty.enable()
511 res = self.vty.command("configure terminal")
512 res = self.vty.command("nat")
513 res = self.vty.command("number-rewrite rewrite.cfg")
514 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200515
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200516 def testRewritePostNoRewrite(self):
517 self.vty.enable()
518 self.vty.command("configure terminal")
519 self.vty.command("nat")
520 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
521 self.vty.verify("no number-rewrite-post", [''])
522
523
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200524 def testPrefixTreeLoading(self):
525 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
526
527 self.vty.enable()
528 self.vty.command("configure terminal")
529 self.vty.command("nat")
530 res = self.vty.command("prefix-tree %s" % cfg)
531 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
532 self.vty.command("end")
533
534 res = self.vty.command("show prefix-tree")
535 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')
536
537 self.vty.command("configure terminal")
538 self.vty.command("nat")
539 self.vty.command("no prefix-tree")
540 self.vty.command("end")
541
542 res = self.vty.command("show prefix-tree")
543 self.assertEqual(res, "% there is now prefix tree loaded.")
544
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200545 def testUssdSideChannelProvider(self):
546 self.vty.command("end")
547 self.vty.enable()
548 self.vty.command("configure terminal")
549 self.vty.command("nat")
550 self.vty.command("ussd-token key")
551 self.vty.command("end")
552
553 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
554 self.assertTrue(res)
555
556 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
557 ussdSocket.connect(('127.0.0.1', 5001))
558 ussdSocket.settimeout(2.0)
559 print "Connected to %s:%d" % ussdSocket.getpeername()
560
561 print "Expecting ID_GET request"
562 data = ussdSocket.recv(4)
563 self.assertEqual(data, "\x00\x01\xfe\x04")
564
565 print "Going to send ID_RESP response"
566 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
567 self.assertEqual(res, 10)
568
569 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
570
571 print "Going to send PING request"
572 res = ussdSocket.send("\x00\x01\xfe\x00")
573 self.assertEqual(res, 4)
574
575 print "Expecting PONG response"
576 data = ussdSocket.recv(4)
577 self.assertEqual(data, "\x00\x01\xfe\x01")
578
579 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
580 self.assertTrue(res)
581
582 print "Going to shut down connection"
583 ussdSocket.shutdown(socket.SHUT_WR)
584
585 print "Expecting EOF"
586 data = ussdSocket.recv(4)
587 self.assertEqual(data, "")
588
589 ussdSocket.close()
590
591 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
592 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200593
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100594 def testAccessList(self):
595 """
596 Verify that the imsi-deny can have a reject cause or no reject cause
597 """
598 self.vty.enable()
599 self.vty.command("configure terminal")
600 self.vty.command("nat")
601
602 # Old default
603 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
604 res = self.vty.command("show running-config").split("\r\n")
605 asserted = False
606 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100607 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100608 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
609 asserted = True
610 self.assert_(asserted)
611
612 # Check the optional CM Service Reject Cause
613 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
614 res = self.vty.command("show running-config").split("\r\n")
615 asserted = False
616 for line in res:
617 if line.startswith(" access-list test-cm"):
618 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
619 asserted = True
620 self.assert_(asserted)
621
622 # Check the optional LU Reject Cause
623 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
624 res = self.vty.command("show running-config").split("\r\n")
625 asserted = False
626 for line in res:
627 if line.startswith(" access-list test-lu"):
628 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
629 asserted = True
630 self.assert_(asserted)
631
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200632class TestVTYGbproxy(TestVTYGenericBSC):
633
634 def vty_command(self):
635 return ["./src/gprs/osmo-gbproxy", "-c",
636 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
637
638 def vty_app(self):
639 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
640
641 def testVtyTree(self):
642 self.vty.enable()
643 self.assertTrue(self.vty.verify('configure terminal', ['']))
644 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100645 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200646 self.assertTrue(self.vty.verify('ns', ['']))
647 self.assertEquals(self.vty.node(), 'config-ns')
648 self.checkForEndAndExit()
649 self.assertTrue(self.vty.verify('exit', ['']))
650 self.assertEquals(self.vty.node(), 'config')
651 self.assertTrue(self.vty.verify('gbproxy', ['']))
652 self.assertEquals(self.vty.node(), 'config-gbproxy')
653 self.checkForEndAndExit()
654 self.assertTrue(self.vty.verify('exit', ['']))
655 self.assertEquals(self.vty.node(), 'config')
656
657 def testVtyShow(self):
658 res = self.vty.command("show ns")
659 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
660
661 res = self.vty.command("show gbproxy stats")
662 self.assert_(res.find('GBProxy Global Statistics') >= 0)
663
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200664 def testVtyDeletePeer(self):
665 self.vty.enable()
666 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
667 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
668 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
669 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
670 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
671 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
672 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
673 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
674 self.assert_(res.find('Not Deleted 0 BVC') < 0)
675 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
676 res = self.vty.command("delete-gbproxy-peer 9999 all")
677 self.assert_(res.find('Deleted 0 BVC') >= 0)
678 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
679
Jacob Erlbeck144b8b12014-11-04 11:15:01 +0100680class TestVTYSGSN(TestVTYGenericBSC):
681
682 def vty_command(self):
683 return ["./src/gprs/osmo-sgsn", "-c",
684 "doc/examples/osmo-sgsn/osmo-sgsn.cfg"]
685
686 def vty_app(self):
687 return (4245, "./src/gprs/osmo-sgsn", "OsmoSGSN", "sgsn")
688
689 def testVtyTree(self):
690 self.vty.enable()
691 self.assertTrue(self.vty.verify('configure terminal', ['']))
692 self.assertEquals(self.vty.node(), 'config')
693 self.checkForEndAndExit()
694 self.assertTrue(self.vty.verify('ns', ['']))
695 self.assertEquals(self.vty.node(), 'config-ns')
696 self.checkForEndAndExit()
697 self.assertTrue(self.vty.verify('exit', ['']))
698 self.assertEquals(self.vty.node(), 'config')
699 self.assertTrue(self.vty.verify('sgsn', ['']))
700 self.assertEquals(self.vty.node(), 'config-sgsn')
701 self.checkForEndAndExit()
702 self.assertTrue(self.vty.verify('exit', ['']))
703 self.assertEquals(self.vty.node(), 'config')
704
705 def testVtyShow(self):
706 res = self.vty.command("show ns")
707 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
708 self.assertTrue(self.vty.verify('show bssgp', ['']))
709 self.assertTrue(self.vty.verify('show bssgp stats', ['']))
710 # TODO: uncomment when the command does not segfault anymore
711 # self.assertTrue(self.vty.verify('show bssgp nsei 123', ['']))
712 # self.assertTrue(self.vty.verify('show bssgp nsei 123 stats', ['']))
713
714 self.assertTrue(self.vty.verify('show sgsn', ['']))
715 self.assertTrue(self.vty.verify('show mm-context all', ['']))
716 self.assertTrue(self.vty.verify('show mm-context imsi 000001234567', ['No MM context for IMSI 000001234567']))
717 self.assertTrue(self.vty.verify('show pdp-context all', ['']))
718
719 res = self.vty.command("show sndcp")
720 self.assert_(res.find('State of SNDCP Entities') >= 0)
721
722 res = self.vty.command("show llc")
723 self.assert_(res.find('State of LLC Entities') >= 0)
724
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100725 def testVtyAuth(self):
726 self.vty.enable()
727 self.assertTrue(self.vty.verify('configure terminal', ['']))
728 self.assertEquals(self.vty.node(), 'config')
729 self.assertTrue(self.vty.verify('sgsn', ['']))
730 self.assertEquals(self.vty.node(), 'config-sgsn')
731 self.assertTrue(self.vty.verify('auth-policy accept-all', ['']))
732 res = self.vty.command("show running-config")
733 self.assert_(res.find('auth-policy accept-all') > 0)
734 self.assertTrue(self.vty.verify('auth-policy acl-only', ['']))
735 res = self.vty.command("show running-config")
736 self.assert_(res.find('auth-policy acl-only') > 0)
737 self.assertTrue(self.vty.verify('auth-policy closed', ['']))
738 res = self.vty.command("show running-config")
739 self.assert_(res.find('auth-policy closed') > 0)
740
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200741def add_nat_test(suite, workdir):
742 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
743 print("Skipping the NAT test")
744 return
745 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
746 suite.addTest(test)
747
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200748def add_bsc_test(suite, workdir):
749 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
750 print("Skipping the BSC test")
751 return
752 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
753 suite.addTest(test)
754
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200755def add_gbproxy_test(suite, workdir):
756 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
757 print("Skipping the Gb-Proxy test")
758 return
759 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
760 suite.addTest(test)
761
Jacob Erlbeck144b8b12014-11-04 11:15:01 +0100762def add_sgsn_test(suite, workdir):
763 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-sgsn")):
764 print("Skipping the SGSN test")
765 return
766 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYSGSN)
767 suite.addTest(test)
768
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200769if __name__ == '__main__':
770 import argparse
771 import sys
772
773 workdir = '.'
774
775 parser = argparse.ArgumentParser()
776 parser.add_argument("-v", "--verbose", dest="verbose",
777 action="store_true", help="verbose mode")
778 parser.add_argument("-p", "--pythonconfpath", dest="p",
779 help="searchpath for config")
780 parser.add_argument("-w", "--workdir", dest="w",
781 help="Working directory")
782 args = parser.parse_args()
783
784 verbose_level = 1
785 if args.verbose:
786 verbose_level = 2
787
788 if args.w:
789 workdir = args.w
790
791 if args.p:
792 confpath = args.p
793
794 print "confpath %s, workdir %s" % (confpath, workdir)
795 os.chdir(workdir)
796 print "Running tests for specific VTY commands"
797 suite = unittest.TestSuite()
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +0200798 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200799 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200800 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200801 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200802 add_gbproxy_test(suite, workdir)
Jacob Erlbeck144b8b12014-11-04 11:15:01 +0100803 add_sgsn_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200804 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
805 sys.exit(len(res.errors) + len(res.failures))