blob: ece9ac554116caa0dc8e8e0f27aa68f9194abcd5 [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
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200422class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200423
424 def vty_command(self):
425 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
426 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
427
428 def vty_app(self):
429 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
430
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200431 def testVtyTree(self):
432 self.vty.enable()
433 self.assertTrue(self.vty.verify('configure terminal', ['']))
434 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100435 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200436 self.assertTrue(self.vty.verify('mgcp', ['']))
437 self.assertEquals(self.vty.node(), 'config-mgcp')
438 self.checkForEndAndExit()
439 self.assertTrue(self.vty.verify('exit', ['']))
440 self.assertEquals(self.vty.node(), 'config')
441 self.assertTrue(self.vty.verify('nat', ['']))
442 self.assertEquals(self.vty.node(), 'config-nat')
443 self.checkForEndAndExit()
444 self.assertTrue(self.vty.verify('bsc 0', ['']))
445 self.assertEquals(self.vty.node(), 'config-nat-bsc')
446 self.checkForEndAndExit()
447 self.assertTrue(self.vty.verify('exit', ['']))
448 self.assertEquals(self.vty.node(), 'config-nat')
449 self.assertTrue(self.vty.verify('exit', ['']))
450 self.assertEquals(self.vty.node(), 'config')
451 self.assertTrue(self.vty.verify('exit', ['']))
452 self.assertTrue(self.vty.node() is None)
453
454 # Check searching for outer node's commands
455 self.vty.command('configure terminal')
456 self.vty.command('mgcp')
457 self.vty.command('nat')
458 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200459 self.vty.command('mgcp')
460 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200461 self.vty.command('nat')
462 self.assertEquals(self.vty.node(), 'config-nat')
463 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200464 self.vty.command('mgcp')
465 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200466
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200467 def testRewriteNoRewrite(self):
468 self.vty.enable()
469 res = self.vty.command("configure terminal")
470 res = self.vty.command("nat")
471 res = self.vty.command("number-rewrite rewrite.cfg")
472 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200473
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200474 def testRewritePostNoRewrite(self):
475 self.vty.enable()
476 self.vty.command("configure terminal")
477 self.vty.command("nat")
478 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
479 self.vty.verify("no number-rewrite-post", [''])
480
481
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200482 def testPrefixTreeLoading(self):
483 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
484
485 self.vty.enable()
486 self.vty.command("configure terminal")
487 self.vty.command("nat")
488 res = self.vty.command("prefix-tree %s" % cfg)
489 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
490 self.vty.command("end")
491
492 res = self.vty.command("show prefix-tree")
493 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')
494
495 self.vty.command("configure terminal")
496 self.vty.command("nat")
497 self.vty.command("no prefix-tree")
498 self.vty.command("end")
499
500 res = self.vty.command("show prefix-tree")
501 self.assertEqual(res, "% there is now prefix tree loaded.")
502
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200503 def testUssdSideChannelProvider(self):
504 self.vty.command("end")
505 self.vty.enable()
506 self.vty.command("configure terminal")
507 self.vty.command("nat")
508 self.vty.command("ussd-token key")
509 self.vty.command("end")
510
511 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
512 self.assertTrue(res)
513
514 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
515 ussdSocket.connect(('127.0.0.1', 5001))
516 ussdSocket.settimeout(2.0)
517 print "Connected to %s:%d" % ussdSocket.getpeername()
518
519 print "Expecting ID_GET request"
520 data = ussdSocket.recv(4)
521 self.assertEqual(data, "\x00\x01\xfe\x04")
522
523 print "Going to send ID_RESP response"
524 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
525 self.assertEqual(res, 10)
526
527 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
528
529 print "Going to send PING request"
530 res = ussdSocket.send("\x00\x01\xfe\x00")
531 self.assertEqual(res, 4)
532
533 print "Expecting PONG response"
534 data = ussdSocket.recv(4)
535 self.assertEqual(data, "\x00\x01\xfe\x01")
536
537 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
538 self.assertTrue(res)
539
540 print "Going to shut down connection"
541 ussdSocket.shutdown(socket.SHUT_WR)
542
543 print "Expecting EOF"
544 data = ussdSocket.recv(4)
545 self.assertEqual(data, "")
546
547 ussdSocket.close()
548
549 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
550 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200551
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100552 def testAccessList(self):
553 """
554 Verify that the imsi-deny can have a reject cause or no reject cause
555 """
556 self.vty.enable()
557 self.vty.command("configure terminal")
558 self.vty.command("nat")
559
560 # Old default
561 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
562 res = self.vty.command("show running-config").split("\r\n")
563 asserted = False
564 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100565 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100566 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
567 asserted = True
568 self.assert_(asserted)
569
570 # Check the optional CM Service Reject Cause
571 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
572 res = self.vty.command("show running-config").split("\r\n")
573 asserted = False
574 for line in res:
575 if line.startswith(" access-list test-cm"):
576 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
577 asserted = True
578 self.assert_(asserted)
579
580 # Check the optional LU Reject Cause
581 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
582 res = self.vty.command("show running-config").split("\r\n")
583 asserted = False
584 for line in res:
585 if line.startswith(" access-list test-lu"):
586 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
587 asserted = True
588 self.assert_(asserted)
589
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200590class TestVTYGbproxy(TestVTYGenericBSC):
591
592 def vty_command(self):
593 return ["./src/gprs/osmo-gbproxy", "-c",
594 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
595
596 def vty_app(self):
597 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
598
599 def testVtyTree(self):
600 self.vty.enable()
601 self.assertTrue(self.vty.verify('configure terminal', ['']))
602 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100603 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200604 self.assertTrue(self.vty.verify('ns', ['']))
605 self.assertEquals(self.vty.node(), 'config-ns')
606 self.checkForEndAndExit()
607 self.assertTrue(self.vty.verify('exit', ['']))
608 self.assertEquals(self.vty.node(), 'config')
609 self.assertTrue(self.vty.verify('gbproxy', ['']))
610 self.assertEquals(self.vty.node(), 'config-gbproxy')
611 self.checkForEndAndExit()
612 self.assertTrue(self.vty.verify('exit', ['']))
613 self.assertEquals(self.vty.node(), 'config')
614
615 def testVtyShow(self):
616 res = self.vty.command("show ns")
617 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
618
619 res = self.vty.command("show gbproxy stats")
620 self.assert_(res.find('GBProxy Global Statistics') >= 0)
621
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200622 def testVtyDeletePeer(self):
623 self.vty.enable()
624 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
625 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
626 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
627 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
628 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
629 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
630 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
631 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
632 self.assert_(res.find('Not Deleted 0 BVC') < 0)
633 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
634 res = self.vty.command("delete-gbproxy-peer 9999 all")
635 self.assert_(res.find('Deleted 0 BVC') >= 0)
636 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
637
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200638def add_nat_test(suite, workdir):
639 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
640 print("Skipping the NAT test")
641 return
642 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
643 suite.addTest(test)
644
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200645def add_bsc_test(suite, workdir):
646 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
647 print("Skipping the BSC test")
648 return
649 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
650 suite.addTest(test)
651
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200652def add_gbproxy_test(suite, workdir):
653 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
654 print("Skipping the Gb-Proxy test")
655 return
656 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
657 suite.addTest(test)
658
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200659if __name__ == '__main__':
660 import argparse
661 import sys
662
663 workdir = '.'
664
665 parser = argparse.ArgumentParser()
666 parser.add_argument("-v", "--verbose", dest="verbose",
667 action="store_true", help="verbose mode")
668 parser.add_argument("-p", "--pythonconfpath", dest="p",
669 help="searchpath for config")
670 parser.add_argument("-w", "--workdir", dest="w",
671 help="Working directory")
672 args = parser.parse_args()
673
674 verbose_level = 1
675 if args.verbose:
676 verbose_level = 2
677
678 if args.w:
679 workdir = args.w
680
681 if args.p:
682 confpath = args.p
683
684 print "confpath %s, workdir %s" % (confpath, workdir)
685 os.chdir(workdir)
686 print "Running tests for specific VTY commands"
687 suite = unittest.TestSuite()
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +0200688 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200689 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200690 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200691 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200692 add_gbproxy_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200693 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
694 sys.exit(len(res.errors) + len(res.failures))