blob: 6bbc606cf10b42483253856999efcfb754ce7e89 [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
Ciabyec6e4f82014-03-06 17:20:55 +0100251 def testShowNetwork(self):
252 res = self.vty.command("show network")
253 self.assert_(res.startswith('BSC is on Country Code') >= 0)
254
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200255class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200256
257 def vty_command(self):
258 return ["./src/osmo-bsc/osmo-bsc", "-c",
259 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
260
261 def vty_app(self):
262 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
263
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200264 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200265 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200266
267 def testVtyTree(self):
268 self.vty.enable()
269 self.assertTrue(self.vty.verify("configure terminal", ['']))
270 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100271 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200272 self.assertTrue(self.vty.verify("msc 0", ['']))
273 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200274 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200275 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200276 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200277 self.assertTrue(self.vty.verify("bsc", ['']))
278 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200279 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200280 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200281 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200282 self.assertTrue(self.vty.verify("exit", ['']))
283 self.assertTrue(self.vty.node() is None)
284
285 # Check searching for outer node's commands
286 self.vty.command("configure terminal")
287 self.vty.command('msc 0')
288 self.vty.command("bsc")
289 self.assertEquals(self.vty.node(), 'config-bsc')
290 self.vty.command("msc 0")
291 self.assertEquals(self.vty.node(), 'config-msc')
292
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200293 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200294 self.vty.enable()
295 self.vty.command("configure terminal")
296 self.vty.command("msc")
297
298 # Test invalid input
299 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200300 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200301 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200302
303 # Enable USSD notifications
304 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200305 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200306 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200307
308 # Verify settings
309 res = self.vty.command("write terminal")
310 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
311 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200312 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
313 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200314 self.assert_(res.find('bsc-grace-text In grace period') > 0)
315 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200316
317 # Now disable it..
318 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200319 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200320 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200321
322 # Verify settings
323 res = self.vty.command("write terminal")
324 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
325 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200326 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200327 self.assert_(res.find('no bsc-welcome-text') > 0)
328 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
329 self.assert_(res.find('no bsc-grace-text') > 0)
330
331 def testUssdNotificationsBsc(self):
332 self.vty.enable()
333 self.vty.command("configure terminal")
334 self.vty.command("bsc")
335
336 # Test invalid input
337 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
338
339 # Enable USSD notifications
340 self.vty.verify("missing-msc-text No MSC found", [''])
341
342 # Verify settings
343 res = self.vty.command("write terminal")
344 self.assert_(res.find('missing-msc-text No MSC found') > 0)
345 self.assertEquals(res.find('no missing-msc-text'), -1)
346
347 # Now disable it..
348 self.vty.verify("no missing-msc-text", [''])
349
350 # Verify settings
351 res = self.vty.command("write terminal")
352 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
353 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200354
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200355 def testNetworkTimezone(self):
356 self.vty.enable()
357 self.vty.verify("configure terminal", [''])
358 self.vty.verify("network", [''])
359 self.vty.verify("bts 0", [''])
360
361 # Test invalid input
362 self.vty.verify("timezone", ['% Command incomplete.'])
363 self.vty.verify("timezone 20 0", ['% Unknown command.'])
364 self.vty.verify("timezone 0 11", ['% Unknown command.'])
365 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
366
367 # Set time zone without DST
368 self.vty.verify("timezone 2 30", [''])
369
370 # Verify settings
371 res = self.vty.command("write terminal")
372 self.assert_(res.find('timezone 2 30') > 0)
373 self.assertEquals(res.find('timezone 2 30 '), -1)
374
375 # Set time zone with DST
376 self.vty.verify("timezone 2 30 1", [''])
377
378 # Verify settings
379 res = self.vty.command("write terminal")
380 self.assert_(res.find('timezone 2 30 1') > 0)
381
382 # Now disable it..
383 self.vty.verify("no timezone", [''])
384
385 # Verify settings
386 res = self.vty.command("write terminal")
387 self.assertEquals(res.find(' timezone'), -1)
388
Ciabyec6e4f82014-03-06 17:20:55 +0100389 def testShowNetwork(self):
390 res = self.vty.command("show network")
391 self.assert_(res.startswith('BSC is on Country Code') >= 0)
392
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200393class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200394
395 def vty_command(self):
396 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
397 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
398
399 def vty_app(self):
400 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
401
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200402 def testVtyTree(self):
403 self.vty.enable()
404 self.assertTrue(self.vty.verify('configure terminal', ['']))
405 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100406 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200407 self.assertTrue(self.vty.verify('mgcp', ['']))
408 self.assertEquals(self.vty.node(), 'config-mgcp')
409 self.checkForEndAndExit()
410 self.assertTrue(self.vty.verify('exit', ['']))
411 self.assertEquals(self.vty.node(), 'config')
412 self.assertTrue(self.vty.verify('nat', ['']))
413 self.assertEquals(self.vty.node(), 'config-nat')
414 self.checkForEndAndExit()
415 self.assertTrue(self.vty.verify('bsc 0', ['']))
416 self.assertEquals(self.vty.node(), 'config-nat-bsc')
417 self.checkForEndAndExit()
418 self.assertTrue(self.vty.verify('exit', ['']))
419 self.assertEquals(self.vty.node(), 'config-nat')
420 self.assertTrue(self.vty.verify('exit', ['']))
421 self.assertEquals(self.vty.node(), 'config')
422 self.assertTrue(self.vty.verify('exit', ['']))
423 self.assertTrue(self.vty.node() is None)
424
425 # Check searching for outer node's commands
426 self.vty.command('configure terminal')
427 self.vty.command('mgcp')
428 self.vty.command('nat')
429 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200430 self.vty.command('mgcp')
431 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200432 self.vty.command('nat')
433 self.assertEquals(self.vty.node(), 'config-nat')
434 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200435 self.vty.command('mgcp')
436 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200437
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200438 def testRewriteNoRewrite(self):
439 self.vty.enable()
440 res = self.vty.command("configure terminal")
441 res = self.vty.command("nat")
442 res = self.vty.command("number-rewrite rewrite.cfg")
443 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200444
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200445 def testRewritePostNoRewrite(self):
446 self.vty.enable()
447 self.vty.command("configure terminal")
448 self.vty.command("nat")
449 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
450 self.vty.verify("no number-rewrite-post", [''])
451
452
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200453 def testPrefixTreeLoading(self):
454 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
455
456 self.vty.enable()
457 self.vty.command("configure terminal")
458 self.vty.command("nat")
459 res = self.vty.command("prefix-tree %s" % cfg)
460 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
461 self.vty.command("end")
462
463 res = self.vty.command("show prefix-tree")
464 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')
465
466 self.vty.command("configure terminal")
467 self.vty.command("nat")
468 self.vty.command("no prefix-tree")
469 self.vty.command("end")
470
471 res = self.vty.command("show prefix-tree")
472 self.assertEqual(res, "% there is now prefix tree loaded.")
473
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200474 def testUssdSideChannelProvider(self):
475 self.vty.command("end")
476 self.vty.enable()
477 self.vty.command("configure terminal")
478 self.vty.command("nat")
479 self.vty.command("ussd-token key")
480 self.vty.command("end")
481
482 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
483 self.assertTrue(res)
484
485 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
486 ussdSocket.connect(('127.0.0.1', 5001))
487 ussdSocket.settimeout(2.0)
488 print "Connected to %s:%d" % ussdSocket.getpeername()
489
490 print "Expecting ID_GET request"
491 data = ussdSocket.recv(4)
492 self.assertEqual(data, "\x00\x01\xfe\x04")
493
494 print "Going to send ID_RESP response"
495 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
496 self.assertEqual(res, 10)
497
498 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
499
500 print "Going to send PING request"
501 res = ussdSocket.send("\x00\x01\xfe\x00")
502 self.assertEqual(res, 4)
503
504 print "Expecting PONG response"
505 data = ussdSocket.recv(4)
506 self.assertEqual(data, "\x00\x01\xfe\x01")
507
508 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
509 self.assertTrue(res)
510
511 print "Going to shut down connection"
512 ussdSocket.shutdown(socket.SHUT_WR)
513
514 print "Expecting EOF"
515 data = ussdSocket.recv(4)
516 self.assertEqual(data, "")
517
518 ussdSocket.close()
519
520 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
521 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200522
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100523 def testAccessList(self):
524 """
525 Verify that the imsi-deny can have a reject cause or no reject cause
526 """
527 self.vty.enable()
528 self.vty.command("configure terminal")
529 self.vty.command("nat")
530
531 # Old default
532 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
533 res = self.vty.command("show running-config").split("\r\n")
534 asserted = False
535 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100536 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100537 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
538 asserted = True
539 self.assert_(asserted)
540
541 # Check the optional CM Service Reject Cause
542 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
543 res = self.vty.command("show running-config").split("\r\n")
544 asserted = False
545 for line in res:
546 if line.startswith(" access-list test-cm"):
547 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
548 asserted = True
549 self.assert_(asserted)
550
551 # Check the optional LU Reject Cause
552 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
553 res = self.vty.command("show running-config").split("\r\n")
554 asserted = False
555 for line in res:
556 if line.startswith(" access-list test-lu"):
557 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
558 asserted = True
559 self.assert_(asserted)
560
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200561class TestVTYGbproxy(TestVTYGenericBSC):
562
563 def vty_command(self):
564 return ["./src/gprs/osmo-gbproxy", "-c",
565 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
566
567 def vty_app(self):
568 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
569
570 def testVtyTree(self):
571 self.vty.enable()
572 self.assertTrue(self.vty.verify('configure terminal', ['']))
573 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100574 self.checkForEndAndExit()
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200575 self.assertTrue(self.vty.verify('ns', ['']))
576 self.assertEquals(self.vty.node(), 'config-ns')
577 self.checkForEndAndExit()
578 self.assertTrue(self.vty.verify('exit', ['']))
579 self.assertEquals(self.vty.node(), 'config')
580 self.assertTrue(self.vty.verify('gbproxy', ['']))
581 self.assertEquals(self.vty.node(), 'config-gbproxy')
582 self.checkForEndAndExit()
583 self.assertTrue(self.vty.verify('exit', ['']))
584 self.assertEquals(self.vty.node(), 'config')
585
586 def testVtyShow(self):
587 res = self.vty.command("show ns")
588 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
589
590 res = self.vty.command("show gbproxy stats")
591 self.assert_(res.find('GBProxy Global Statistics') >= 0)
592
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200593 def testVtyDeletePeer(self):
594 self.vty.enable()
595 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
596 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
597 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
598 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
599 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
600 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
601 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
602 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
603 self.assert_(res.find('Not Deleted 0 BVC') < 0)
604 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
605 res = self.vty.command("delete-gbproxy-peer 9999 all")
606 self.assert_(res.find('Deleted 0 BVC') >= 0)
607 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
608
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200609def add_nat_test(suite, workdir):
610 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
611 print("Skipping the NAT test")
612 return
613 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
614 suite.addTest(test)
615
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200616def add_bsc_test(suite, workdir):
617 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
618 print("Skipping the BSC test")
619 return
620 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
621 suite.addTest(test)
622
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200623def add_gbproxy_test(suite, workdir):
624 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
625 print("Skipping the Gb-Proxy test")
626 return
627 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
628 suite.addTest(test)
629
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200630if __name__ == '__main__':
631 import argparse
632 import sys
633
634 workdir = '.'
635
636 parser = argparse.ArgumentParser()
637 parser.add_argument("-v", "--verbose", dest="verbose",
638 action="store_true", help="verbose mode")
639 parser.add_argument("-p", "--pythonconfpath", dest="p",
640 help="searchpath for config")
641 parser.add_argument("-w", "--workdir", dest="w",
642 help="Working directory")
643 args = parser.parse_args()
644
645 verbose_level = 1
646 if args.verbose:
647 verbose_level = 2
648
649 if args.w:
650 workdir = args.w
651
652 if args.p:
653 confpath = args.p
654
655 print "confpath %s, workdir %s" % (confpath, workdir)
656 os.chdir(workdir)
657 print "Running tests for specific VTY commands"
658 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200659 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200660 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200661 add_nat_test(suite, workdir)
Jacob Erlbeck6d233712013-10-23 11:24:15 +0200662 add_gbproxy_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200663 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
664 sys.exit(len(res.errors) + len(res.failures))