blob: 092f5ae6f0072e686b9fb8c66dc3abf3b4b0523d [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
Jacob Erlbeck96903c42013-09-02 13:17:14 +020059
60class TestVTYGenericBSC(TestVTYBase):
61
62 def checkForEndAndExit(self):
63 res = self.vty.command("list")
64 #print ('looking for "exit"\n')
65 self.assert_(res.find(' exit\r') > 0)
66 #print 'found "exit"\nlooking for "end"\n'
67 self.assert_(res.find(' end\r') > 0)
68 #print 'found "end"\n'
69
Jacob Erlbeck96903c42013-09-02 13:17:14 +020070 def _testConfigNetworkTree(self):
71 self.vty.enable()
72 self.assertTrue(self.vty.verify("configure terminal",['']))
73 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +010074 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +020075 self.assertTrue(self.vty.verify("network",['']))
76 self.assertEquals(self.vty.node(), 'config-net')
77 self.checkForEndAndExit()
78 self.assertTrue(self.vty.verify("bts 0",['']))
79 self.assertEquals(self.vty.node(), 'config-net-bts')
80 self.checkForEndAndExit()
81 self.assertTrue(self.vty.verify("trx 0",['']))
82 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
83 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +020084 self.vty.command("write terminal")
85 self.assertTrue(self.vty.verify("exit",['']))
86 self.assertEquals(self.vty.node(), 'config-net-bts')
87 self.assertTrue(self.vty.verify("exit",['']))
88 self.assertTrue(self.vty.verify("bts 1",['']))
89 self.assertEquals(self.vty.node(), 'config-net-bts')
90 self.checkForEndAndExit()
91 self.assertTrue(self.vty.verify("trx 1",['']))
92 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
93 self.checkForEndAndExit()
94 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +020095 self.assertTrue(self.vty.verify("exit",['']))
96 self.assertEquals(self.vty.node(), 'config-net-bts')
97 self.assertTrue(self.vty.verify("exit",['']))
98 self.assertEquals(self.vty.node(), 'config-net')
99 self.assertTrue(self.vty.verify("exit",['']))
100 self.assertEquals(self.vty.node(), 'config')
101 self.assertTrue(self.vty.verify("exit",['']))
102 self.assertTrue(self.vty.node() is None)
103
104class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200105
106 def vty_command(self):
107 return ["./src/osmo-nitb/osmo-nitb", "-c",
108 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
109
110 def vty_app(self):
111 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
112
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200113 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200114 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200115
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200116 def checkForSmpp(self):
117 """SMPP is not always enabled, check if it is"""
118 res = self.vty.command("list")
119 return "smpp" in res
120
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200121 def testVtyTree(self):
122 self.vty.enable()
123 self.assertTrue(self.vty.verify("configure terminal", ['']))
124 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100125 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200126 self.assertTrue(self.vty.verify('mncc-int', ['']))
127 self.assertEquals(self.vty.node(), 'config-mncc-int')
128 self.checkForEndAndExit()
129 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200130
131 if self.checkForSmpp():
132 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200133 self.assertTrue(self.vty.verify('smpp', ['']))
134 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100135 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200136 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200137
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200138 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200139 self.assertTrue(self.vty.verify("exit", ['']))
140 self.assertTrue(self.vty.node() is None)
141
142 # Check searching for outer node's commands
143 self.vty.command("configure terminal")
144 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200145
146 if self.checkForSmpp():
147 self.vty.command('smpp')
148 self.assertEquals(self.vty.node(), 'config-smpp')
149 self.vty.command('mncc-int')
150
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200151 self.assertEquals(self.vty.node(), 'config-mncc-int')
152
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200153 def testEnableDisablePeriodicLU(self):
154 self.vty.enable()
155 self.vty.command("configure terminal")
156 self.vty.command("network")
157 self.vty.command("bts 0")
158
159 # Test invalid input
160 self.vty.verify("periodic location update 0", ['% Unknown command.'])
161 self.vty.verify("periodic location update 5", ['% Unknown command.'])
162 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
163
164 # Enable periodic lu..
165 self.vty.verify("periodic location update 60", [''])
166 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200167 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200168 self.assertEquals(res.find('no periodic location update'), -1)
169
170 # Now disable it..
171 self.vty.verify("no periodic location update", [''])
172 res = self.vty.command("write terminal")
173 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200174 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200175
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100176 def testEnableDisableSiHacks(self):
177 self.vty.enable()
178 self.vty.command("configure terminal")
179 self.vty.command("network")
180 self.vty.command("bts 0")
181
182 # Enable periodic lu..
183 self.vty.verify("force-combined-si", [''])
184 res = self.vty.command("write terminal")
185 self.assert_(res.find(' force-combined-si') > 0)
186 self.assertEquals(res.find('no force-combined-si'), -1)
187
188 # Now disable it..
189 self.vty.verify("no force-combined-si", [''])
190 res = self.vty.command("write terminal")
191 self.assertEquals(res.find(' force-combined-si'), -1)
192 self.assert_(res.find('no force-combined-si') > 0)
193
Ivan Kluchnikov67920592013-09-16 13:13:04 +0400194 def testRachAccessControlClass(self):
195 self.vty.enable()
196 self.vty.command("configure terminal")
197 self.vty.command("network")
198 self.vty.command("bts 0")
199
200 # Test invalid input
201 self.vty.verify("rach access-control-class", ['% Command incomplete.'])
202 self.vty.verify("rach access-control-class 1", ['% Command incomplete.'])
203 self.vty.verify("rach access-control-class -1", ['% Unknown command.'])
204 self.vty.verify("rach access-control-class 10", ['% Unknown command.'])
205 self.vty.verify("rach access-control-class 16", ['% Unknown command.'])
206
207 # Barred rach access control classes
208 for classNum in range(16):
209 if classNum != 10:
210 self.vty.verify("rach access-control-class " + str(classNum) + " barred", [''])
211
212 # Verify settings
213 res = self.vty.command("write terminal")
214 for classNum in range(16):
215 if classNum != 10:
216 self.assert_(res.find("rach access-control-class " + str(classNum) + " barred") > 0)
217
218 # Allowed rach access control classes
219 for classNum in range(16):
220 if classNum != 10:
221 self.vty.verify("rach access-control-class " + str(classNum) + " allowed", [''])
222
223 # Verify settings
224 res = self.vty.command("write terminal")
225 for classNum in range(16):
226 if classNum != 10:
227 self.assertEquals(res.find("rach access-control-class " + str(classNum) + " barred"), -1)
228
Alexander Chemerisbd6d40f2013-10-04 23:54:17 +0200229 def testSubscriberCreate(self):
230 self.vty.enable()
231
232 imsi = "204300854013739"
233
234 # Initially we don't have this subscriber
235 self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi])
236
237 # Lets create one
238 res = self.vty.command('subscriber create imsi '+imsi)
239 self.assert_(res.find(" IMSI: "+imsi) > 0)
240
241 # Now we have it
242 res = self.vty.command('show subscriber imsi '+imsi)
243 self.assert_(res.find(" IMSI: "+imsi) > 0)
244
Holger Hans Peter Freytherec37bb22013-02-05 09:39:09 +0100245 def testShowPagingGroup(self):
246 res = self.vty.command("show paging-group 255 1234567")
247 self.assertEqual(res, "% can't find BTS 255")
248 res = self.vty.command("show paging-group 0 1234567")
249 self.assertEquals(res, "%Paging group for IMSI 1234567 on BTS #0 is 7")
250
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200251class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200252
253 def vty_command(self):
254 return ["./src/osmo-bsc/osmo-bsc", "-c",
255 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
256
257 def vty_app(self):
258 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
259
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200260 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200261 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200262
263 def testVtyTree(self):
264 self.vty.enable()
265 self.assertTrue(self.vty.verify("configure terminal", ['']))
266 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100267 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200268 self.assertTrue(self.vty.verify("msc 0", ['']))
269 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200270 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200271 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200272 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200273 self.assertTrue(self.vty.verify("bsc", ['']))
274 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200275 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200276 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200277 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200278 self.assertTrue(self.vty.verify("exit", ['']))
279 self.assertTrue(self.vty.node() is None)
280
281 # Check searching for outer node's commands
282 self.vty.command("configure terminal")
283 self.vty.command('msc 0')
284 self.vty.command("bsc")
285 self.assertEquals(self.vty.node(), 'config-bsc')
286 self.vty.command("msc 0")
287 self.assertEquals(self.vty.node(), 'config-msc')
288
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200289 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200290 self.vty.enable()
291 self.vty.command("configure terminal")
292 self.vty.command("msc")
293
294 # Test invalid input
295 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200296 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200297 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200298
299 # Enable USSD notifications
300 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200301 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200302 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200303
304 # Verify settings
305 res = self.vty.command("write terminal")
306 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
307 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200308 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
309 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200310 self.assert_(res.find('bsc-grace-text In grace period') > 0)
311 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200312
313 # Now disable it..
314 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200315 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200316 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200317
318 # Verify settings
319 res = self.vty.command("write terminal")
320 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
321 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200322 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200323 self.assert_(res.find('no bsc-welcome-text') > 0)
324 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
325 self.assert_(res.find('no bsc-grace-text') > 0)
326
327 def testUssdNotificationsBsc(self):
328 self.vty.enable()
329 self.vty.command("configure terminal")
330 self.vty.command("bsc")
331
332 # Test invalid input
333 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
334
335 # Enable USSD notifications
336 self.vty.verify("missing-msc-text No MSC found", [''])
337
338 # Verify settings
339 res = self.vty.command("write terminal")
340 self.assert_(res.find('missing-msc-text No MSC found') > 0)
341 self.assertEquals(res.find('no missing-msc-text'), -1)
342
343 # Now disable it..
344 self.vty.verify("no missing-msc-text", [''])
345
346 # Verify settings
347 res = self.vty.command("write terminal")
348 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
349 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200350
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200351 def testNetworkTimezone(self):
352 self.vty.enable()
353 self.vty.verify("configure terminal", [''])
354 self.vty.verify("network", [''])
355 self.vty.verify("bts 0", [''])
356
357 # Test invalid input
358 self.vty.verify("timezone", ['% Command incomplete.'])
359 self.vty.verify("timezone 20 0", ['% Unknown command.'])
360 self.vty.verify("timezone 0 11", ['% Unknown command.'])
361 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
362
363 # Set time zone without DST
364 self.vty.verify("timezone 2 30", [''])
365
366 # Verify settings
367 res = self.vty.command("write terminal")
368 self.assert_(res.find('timezone 2 30') > 0)
369 self.assertEquals(res.find('timezone 2 30 '), -1)
370
371 # Set time zone with DST
372 self.vty.verify("timezone 2 30 1", [''])
373
374 # Verify settings
375 res = self.vty.command("write terminal")
376 self.assert_(res.find('timezone 2 30 1') > 0)
377
378 # Now disable it..
379 self.vty.verify("no timezone", [''])
380
381 # Verify settings
382 res = self.vty.command("write terminal")
383 self.assertEquals(res.find(' timezone'), -1)
384
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200385class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200386
387 def vty_command(self):
388 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
389 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
390
391 def vty_app(self):
392 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
393
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200394 def testVtyTree(self):
395 self.vty.enable()
396 self.assertTrue(self.vty.verify('configure terminal', ['']))
397 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100398 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200399 self.assertTrue(self.vty.verify('mgcp', ['']))
400 self.assertEquals(self.vty.node(), 'config-mgcp')
401 self.checkForEndAndExit()
402 self.assertTrue(self.vty.verify('exit', ['']))
403 self.assertEquals(self.vty.node(), 'config')
404 self.assertTrue(self.vty.verify('nat', ['']))
405 self.assertEquals(self.vty.node(), 'config-nat')
406 self.checkForEndAndExit()
407 self.assertTrue(self.vty.verify('bsc 0', ['']))
408 self.assertEquals(self.vty.node(), 'config-nat-bsc')
409 self.checkForEndAndExit()
410 self.assertTrue(self.vty.verify('exit', ['']))
411 self.assertEquals(self.vty.node(), 'config-nat')
412 self.assertTrue(self.vty.verify('exit', ['']))
413 self.assertEquals(self.vty.node(), 'config')
414 self.assertTrue(self.vty.verify('exit', ['']))
415 self.assertTrue(self.vty.node() is None)
416
417 # Check searching for outer node's commands
418 self.vty.command('configure terminal')
419 self.vty.command('mgcp')
420 self.vty.command('nat')
421 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200422 self.vty.command('mgcp')
423 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200424 self.vty.command('nat')
425 self.assertEquals(self.vty.node(), 'config-nat')
426 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200427 self.vty.command('mgcp')
428 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200429
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200430 def testRewriteNoRewrite(self):
431 self.vty.enable()
432 res = self.vty.command("configure terminal")
433 res = self.vty.command("nat")
434 res = self.vty.command("number-rewrite rewrite.cfg")
435 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200436
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200437 def testRewritePostNoRewrite(self):
438 self.vty.enable()
439 self.vty.command("configure terminal")
440 self.vty.command("nat")
441 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
442 self.vty.verify("no number-rewrite-post", [''])
443
444
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200445 def testPrefixTreeLoading(self):
446 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
447
448 self.vty.enable()
449 self.vty.command("configure terminal")
450 self.vty.command("nat")
451 res = self.vty.command("prefix-tree %s" % cfg)
452 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
453 self.vty.command("end")
454
455 res = self.vty.command("show prefix-tree")
456 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')
457
458 self.vty.command("configure terminal")
459 self.vty.command("nat")
460 self.vty.command("no prefix-tree")
461 self.vty.command("end")
462
463 res = self.vty.command("show prefix-tree")
464 self.assertEqual(res, "% there is now prefix tree loaded.")
465
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200466 def testUssdSideChannelProvider(self):
467 self.vty.command("end")
468 self.vty.enable()
469 self.vty.command("configure terminal")
470 self.vty.command("nat")
471 self.vty.command("ussd-token key")
472 self.vty.command("end")
473
474 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
475 self.assertTrue(res)
476
477 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
478 ussdSocket.connect(('127.0.0.1', 5001))
479 ussdSocket.settimeout(2.0)
480 print "Connected to %s:%d" % ussdSocket.getpeername()
481
482 print "Expecting ID_GET request"
483 data = ussdSocket.recv(4)
484 self.assertEqual(data, "\x00\x01\xfe\x04")
485
486 print "Going to send ID_RESP response"
487 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
488 self.assertEqual(res, 10)
489
490 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
491
492 print "Going to send PING request"
493 res = ussdSocket.send("\x00\x01\xfe\x00")
494 self.assertEqual(res, 4)
495
496 print "Expecting PONG response"
497 data = ussdSocket.recv(4)
498 self.assertEqual(data, "\x00\x01\xfe\x01")
499
500 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
501 self.assertTrue(res)
502
503 print "Going to shut down connection"
504 ussdSocket.shutdown(socket.SHUT_WR)
505
506 print "Expecting EOF"
507 data = ussdSocket.recv(4)
508 self.assertEqual(data, "")
509
510 ussdSocket.close()
511
512 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
513 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200514
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100515 def testAccessList(self):
516 """
517 Verify that the imsi-deny can have a reject cause or no reject cause
518 """
519 self.vty.enable()
520 self.vty.command("configure terminal")
521 self.vty.command("nat")
522
523 # Old default
524 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
525 res = self.vty.command("show running-config").split("\r\n")
526 asserted = False
527 for line in res:
528 if line.startswith(" access-list"):
529 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
530 asserted = True
531 self.assert_(asserted)
532
533 # Check the optional CM Service Reject Cause
534 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
535 res = self.vty.command("show running-config").split("\r\n")
536 asserted = False
537 for line in res:
538 if line.startswith(" access-list test-cm"):
539 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
540 asserted = True
541 self.assert_(asserted)
542
543 # Check the optional LU Reject Cause
544 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
545 res = self.vty.command("show running-config").split("\r\n")
546 asserted = False
547 for line in res:
548 if line.startswith(" access-list test-lu"):
549 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
550 asserted = True
551 self.assert_(asserted)
552
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200553class TestVTYGbproxy(TestVTYGenericBSC):
554
555 def vty_command(self):
556 return ["./src/gprs/osmo-gbproxy", "-c",
557 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
558
559 def vty_app(self):
560 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
561
562 def testVtyTree(self):
563 self.vty.enable()
564 self.assertTrue(self.vty.verify('configure terminal', ['']))
565 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100566 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200567 self.assertTrue(self.vty.verify('ns', ['']))
568 self.assertEquals(self.vty.node(), 'config-ns')
569 self.checkForEndAndExit()
570 self.assertTrue(self.vty.verify('exit', ['']))
571 self.assertEquals(self.vty.node(), 'config')
572 self.assertTrue(self.vty.verify('gbproxy', ['']))
573 self.assertEquals(self.vty.node(), 'config-gbproxy')
574 self.checkForEndAndExit()
575 self.assertTrue(self.vty.verify('exit', ['']))
576 self.assertEquals(self.vty.node(), 'config')
577
578 def testVtyShow(self):
579 res = self.vty.command("show ns")
580 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
581
582 res = self.vty.command("show gbproxy stats")
583 self.assert_(res.find('GBProxy Global Statistics') >= 0)
584
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200585 def testVtyDeletePeer(self):
586 self.vty.enable()
587 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
588 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
589 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
590 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
591 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
592 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
593 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
594 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
595 self.assert_(res.find('Not Deleted 0 BVC') < 0)
596 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
597 res = self.vty.command("delete-gbproxy-peer 9999 all")
598 self.assert_(res.find('Deleted 0 BVC') >= 0)
599 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
600
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200601def add_nat_test(suite, workdir):
602 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
603 print("Skipping the NAT test")
604 return
605 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
606 suite.addTest(test)
607
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200608def add_bsc_test(suite, workdir):
609 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
610 print("Skipping the BSC test")
611 return
612 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
613 suite.addTest(test)
614
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200615def add_gbproxy_test(suite, workdir):
616 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
617 print("Skipping the Gb-Proxy test")
618 return
619 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
620 suite.addTest(test)
621
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200622if __name__ == '__main__':
623 import argparse
624 import sys
625
626 workdir = '.'
627
628 parser = argparse.ArgumentParser()
629 parser.add_argument("-v", "--verbose", dest="verbose",
630 action="store_true", help="verbose mode")
631 parser.add_argument("-p", "--pythonconfpath", dest="p",
632 help="searchpath for config")
633 parser.add_argument("-w", "--workdir", dest="w",
634 help="Working directory")
635 args = parser.parse_args()
636
637 verbose_level = 1
638 if args.verbose:
639 verbose_level = 2
640
641 if args.w:
642 workdir = args.w
643
644 if args.p:
645 confpath = args.p
646
647 print "confpath %s, workdir %s" % (confpath, workdir)
648 os.chdir(workdir)
649 print "Running tests for specific VTY commands"
650 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200651 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200652 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200653 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200654 add_gbproxy_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200655 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
656 sys.exit(len(res.errors) + len(res.failures))