blob: 7f71288ff0309726da878052ad8926ffb19d4d43 [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
70 def ignoredCheckForEndAndExit(self):
71 sys.stderr.write('Going to ignore the next assertion(s) due to known bugs\n')
72 try:
73 self.checkForEndAndExit()
74 except BaseException as e:
75 sys.stderr.write('Expected and ignored failure: %s\n' % (str(e)))
76
Jacob Erlbeck96903c42013-09-02 13:17:14 +020077 def _testConfigNetworkTree(self):
78 self.vty.enable()
79 self.assertTrue(self.vty.verify("configure terminal",['']))
80 self.assertEquals(self.vty.node(), 'config')
81 self.ignoredCheckForEndAndExit()
82 self.assertTrue(self.vty.verify("network",['']))
83 self.assertEquals(self.vty.node(), 'config-net')
84 self.checkForEndAndExit()
85 self.assertTrue(self.vty.verify("bts 0",['']))
86 self.assertEquals(self.vty.node(), 'config-net-bts')
87 self.checkForEndAndExit()
88 self.assertTrue(self.vty.verify("trx 0",['']))
89 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
90 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +020091 self.vty.command("write terminal")
92 self.assertTrue(self.vty.verify("exit",['']))
93 self.assertEquals(self.vty.node(), 'config-net-bts')
94 self.assertTrue(self.vty.verify("exit",['']))
95 self.assertTrue(self.vty.verify("bts 1",['']))
96 self.assertEquals(self.vty.node(), 'config-net-bts')
97 self.checkForEndAndExit()
98 self.assertTrue(self.vty.verify("trx 1",['']))
99 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
100 self.checkForEndAndExit()
101 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200102 self.assertTrue(self.vty.verify("exit",['']))
103 self.assertEquals(self.vty.node(), 'config-net-bts')
104 self.assertTrue(self.vty.verify("exit",['']))
105 self.assertEquals(self.vty.node(), 'config-net')
106 self.assertTrue(self.vty.verify("exit",['']))
107 self.assertEquals(self.vty.node(), 'config')
108 self.assertTrue(self.vty.verify("exit",['']))
109 self.assertTrue(self.vty.node() is None)
110
111class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200112
113 def vty_command(self):
114 return ["./src/osmo-nitb/osmo-nitb", "-c",
115 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
116
117 def vty_app(self):
118 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
119
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200120 def testConfigNetworkTree(self):
121 self._testConfigNetworkTree()
122
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200123 def checkForSmpp(self):
124 """SMPP is not always enabled, check if it is"""
125 res = self.vty.command("list")
126 return "smpp" in res
127
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200128 def testVtyTree(self):
129 self.vty.enable()
130 self.assertTrue(self.vty.verify("configure terminal", ['']))
131 self.assertEquals(self.vty.node(), 'config')
132 self.ignoredCheckForEndAndExit()
133 self.assertTrue(self.vty.verify('mncc-int', ['']))
134 self.assertEquals(self.vty.node(), 'config-mncc-int')
135 self.checkForEndAndExit()
136 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200137
138 if self.checkForSmpp():
139 self.assertEquals(self.vty.node(), 'config')
140 self.assertTrue(self.vty.verify('smpp', ['']))
141 self.assertEquals(self.vty.node(), 'config-smpp')
142 self.ignoredCheckForEndAndExit()
143 self.assertTrue(self.vty.verify("exit", ['']))
144
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200145 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200146 self.assertTrue(self.vty.verify("exit", ['']))
147 self.assertTrue(self.vty.node() is None)
148
149 # Check searching for outer node's commands
150 self.vty.command("configure terminal")
151 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200152
153 if self.checkForSmpp():
154 self.vty.command('smpp')
155 self.assertEquals(self.vty.node(), 'config-smpp')
156 self.vty.command('mncc-int')
157
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200158 self.assertEquals(self.vty.node(), 'config-mncc-int')
159
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200160 def testEnableDisablePeriodicLU(self):
161 self.vty.enable()
162 self.vty.command("configure terminal")
163 self.vty.command("network")
164 self.vty.command("bts 0")
165
166 # Test invalid input
167 self.vty.verify("periodic location update 0", ['% Unknown command.'])
168 self.vty.verify("periodic location update 5", ['% Unknown command.'])
169 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
170
171 # Enable periodic lu..
172 self.vty.verify("periodic location update 60", [''])
173 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200174 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200175 self.assertEquals(res.find('no periodic location update'), -1)
176
177 # Now disable it..
178 self.vty.verify("no periodic location update", [''])
179 res = self.vty.command("write terminal")
180 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200181 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200182
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200183class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200184
185 def vty_command(self):
186 return ["./src/osmo-bsc/osmo-bsc", "-c",
187 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
188
189 def vty_app(self):
190 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
191
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200192 def testConfigNetworkTree(self):
193 self._testConfigNetworkTree()
194
195 def testVtyTree(self):
196 self.vty.enable()
197 self.assertTrue(self.vty.verify("configure terminal", ['']))
198 self.assertEquals(self.vty.node(), 'config')
199 self.ignoredCheckForEndAndExit()
200 self.assertTrue(self.vty.verify("msc 0", ['']))
201 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200202 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200203 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200204 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200205 self.assertTrue(self.vty.verify("bsc", ['']))
206 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200207 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200208 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200209 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200210 self.assertTrue(self.vty.verify("exit", ['']))
211 self.assertTrue(self.vty.node() is None)
212
213 # Check searching for outer node's commands
214 self.vty.command("configure terminal")
215 self.vty.command('msc 0')
216 self.vty.command("bsc")
217 self.assertEquals(self.vty.node(), 'config-bsc')
218 self.vty.command("msc 0")
219 self.assertEquals(self.vty.node(), 'config-msc')
220
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200221 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200222 self.vty.enable()
223 self.vty.command("configure terminal")
224 self.vty.command("msc")
225
226 # Test invalid input
227 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200228 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200229 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200230
231 # Enable USSD notifications
232 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200233 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200234 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200235
236 # Verify settings
237 res = self.vty.command("write terminal")
238 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
239 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200240 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
241 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200242 self.assert_(res.find('bsc-grace-text In grace period') > 0)
243 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200244
245 # Now disable it..
246 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200247 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200248 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200249
250 # Verify settings
251 res = self.vty.command("write terminal")
252 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
253 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200254 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200255 self.assert_(res.find('no bsc-welcome-text') > 0)
256 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
257 self.assert_(res.find('no bsc-grace-text') > 0)
258
259 def testUssdNotificationsBsc(self):
260 self.vty.enable()
261 self.vty.command("configure terminal")
262 self.vty.command("bsc")
263
264 # Test invalid input
265 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
266
267 # Enable USSD notifications
268 self.vty.verify("missing-msc-text No MSC found", [''])
269
270 # Verify settings
271 res = self.vty.command("write terminal")
272 self.assert_(res.find('missing-msc-text No MSC found') > 0)
273 self.assertEquals(res.find('no missing-msc-text'), -1)
274
275 # Now disable it..
276 self.vty.verify("no missing-msc-text", [''])
277
278 # Verify settings
279 res = self.vty.command("write terminal")
280 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
281 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200282
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200283class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200284
285 def vty_command(self):
286 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
287 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
288
289 def vty_app(self):
290 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
291
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200292 def testVtyTree(self):
293 self.vty.enable()
294 self.assertTrue(self.vty.verify('configure terminal', ['']))
295 self.assertEquals(self.vty.node(), 'config')
296 self.ignoredCheckForEndAndExit()
297 self.assertTrue(self.vty.verify('mgcp', ['']))
298 self.assertEquals(self.vty.node(), 'config-mgcp')
299 self.checkForEndAndExit()
300 self.assertTrue(self.vty.verify('exit', ['']))
301 self.assertEquals(self.vty.node(), 'config')
302 self.assertTrue(self.vty.verify('nat', ['']))
303 self.assertEquals(self.vty.node(), 'config-nat')
304 self.checkForEndAndExit()
305 self.assertTrue(self.vty.verify('bsc 0', ['']))
306 self.assertEquals(self.vty.node(), 'config-nat-bsc')
307 self.checkForEndAndExit()
308 self.assertTrue(self.vty.verify('exit', ['']))
309 self.assertEquals(self.vty.node(), 'config-nat')
310 self.assertTrue(self.vty.verify('exit', ['']))
311 self.assertEquals(self.vty.node(), 'config')
312 self.assertTrue(self.vty.verify('exit', ['']))
313 self.assertTrue(self.vty.node() is None)
314
315 # Check searching for outer node's commands
316 self.vty.command('configure terminal')
317 self.vty.command('mgcp')
318 self.vty.command('nat')
319 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200320 self.vty.command('mgcp')
321 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200322 self.vty.command('nat')
323 self.assertEquals(self.vty.node(), 'config-nat')
324 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200325 self.vty.command('mgcp')
326 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200327
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200328 def testRewriteNoRewrite(self):
329 self.vty.enable()
330 res = self.vty.command("configure terminal")
331 res = self.vty.command("nat")
332 res = self.vty.command("number-rewrite rewrite.cfg")
333 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200334
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200335 def testRewritePostNoRewrite(self):
336 self.vty.enable()
337 self.vty.command("configure terminal")
338 self.vty.command("nat")
339 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
340 self.vty.verify("no number-rewrite-post", [''])
341
342
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200343 def testPrefixTreeLoading(self):
344 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
345
346 self.vty.enable()
347 self.vty.command("configure terminal")
348 self.vty.command("nat")
349 res = self.vty.command("prefix-tree %s" % cfg)
350 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
351 self.vty.command("end")
352
353 res = self.vty.command("show prefix-tree")
354 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')
355
356 self.vty.command("configure terminal")
357 self.vty.command("nat")
358 self.vty.command("no prefix-tree")
359 self.vty.command("end")
360
361 res = self.vty.command("show prefix-tree")
362 self.assertEqual(res, "% there is now prefix tree loaded.")
363
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200364 def testUssdSideChannelProvider(self):
365 self.vty.command("end")
366 self.vty.enable()
367 self.vty.command("configure terminal")
368 self.vty.command("nat")
369 self.vty.command("ussd-token key")
370 self.vty.command("end")
371
372 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
373 self.assertTrue(res)
374
375 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
376 ussdSocket.connect(('127.0.0.1', 5001))
377 ussdSocket.settimeout(2.0)
378 print "Connected to %s:%d" % ussdSocket.getpeername()
379
380 print "Expecting ID_GET request"
381 data = ussdSocket.recv(4)
382 self.assertEqual(data, "\x00\x01\xfe\x04")
383
384 print "Going to send ID_RESP response"
385 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
386 self.assertEqual(res, 10)
387
388 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
389
390 print "Going to send PING request"
391 res = ussdSocket.send("\x00\x01\xfe\x00")
392 self.assertEqual(res, 4)
393
394 print "Expecting PONG response"
395 data = ussdSocket.recv(4)
396 self.assertEqual(data, "\x00\x01\xfe\x01")
397
398 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
399 self.assertTrue(res)
400
401 print "Going to shut down connection"
402 ussdSocket.shutdown(socket.SHUT_WR)
403
404 print "Expecting EOF"
405 data = ussdSocket.recv(4)
406 self.assertEqual(data, "")
407
408 ussdSocket.close()
409
410 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
411 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200412
413def add_nat_test(suite, workdir):
414 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
415 print("Skipping the NAT test")
416 return
417 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
418 suite.addTest(test)
419
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200420def add_bsc_test(suite, workdir):
421 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
422 print("Skipping the BSC test")
423 return
424 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
425 suite.addTest(test)
426
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200427if __name__ == '__main__':
428 import argparse
429 import sys
430
431 workdir = '.'
432
433 parser = argparse.ArgumentParser()
434 parser.add_argument("-v", "--verbose", dest="verbose",
435 action="store_true", help="verbose mode")
436 parser.add_argument("-p", "--pythonconfpath", dest="p",
437 help="searchpath for config")
438 parser.add_argument("-w", "--workdir", dest="w",
439 help="Working directory")
440 args = parser.parse_args()
441
442 verbose_level = 1
443 if args.verbose:
444 verbose_level = 2
445
446 if args.w:
447 workdir = args.w
448
449 if args.p:
450 confpath = args.p
451
452 print "confpath %s, workdir %s" % (confpath, workdir)
453 os.chdir(workdir)
454 print "Running tests for specific VTY commands"
455 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200456 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200457 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200458 add_nat_test(suite, workdir)
459 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
460 sys.exit(len(res.errors) + len(res.failures))