blob: 460b70e77855a29d3214f481db5d714284ba7857 [file] [log] [blame]
Holger Hans Peter Freyther65397522013-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 Erlbeck4684eb62013-08-14 11:10:34 +020021import socket
Holger Hans Peter Freyther65397522013-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 Erlbeck768a7c32013-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 Erlbeck768a7c32013-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()
91 self.assertTrue(self.vty.verify("exit",['']))
92 self.assertEquals(self.vty.node(), 'config-net-bts')
93 self.assertTrue(self.vty.verify("exit",['']))
94 self.assertEquals(self.vty.node(), 'config-net')
95 self.assertTrue(self.vty.verify("exit",['']))
96 self.assertEquals(self.vty.node(), 'config')
97 self.assertTrue(self.vty.verify("exit",['']))
98 self.assertTrue(self.vty.node() is None)
99
100class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200101
102 def vty_command(self):
103 return ["./src/osmo-nitb/osmo-nitb", "-c",
104 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
105
106 def vty_app(self):
107 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
108
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200109 def testConfigNetworkTree(self):
110 self._testConfigNetworkTree()
111
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200112 def checkForSmpp(self):
113 """SMPP is not always enabled, check if it is"""
114 res = self.vty.command("list")
115 return "smpp" in res
116
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200117 def testVtyTree(self):
118 self.vty.enable()
119 self.assertTrue(self.vty.verify("configure terminal", ['']))
120 self.assertEquals(self.vty.node(), 'config')
121 self.ignoredCheckForEndAndExit()
122 self.assertTrue(self.vty.verify('mncc-int', ['']))
123 self.assertEquals(self.vty.node(), 'config-mncc-int')
124 self.checkForEndAndExit()
125 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200126
127 if self.checkForSmpp():
128 self.assertEquals(self.vty.node(), 'config')
129 self.assertTrue(self.vty.verify('smpp', ['']))
130 self.assertEquals(self.vty.node(), 'config-smpp')
131 self.ignoredCheckForEndAndExit()
132 self.assertTrue(self.vty.verify("exit", ['']))
133
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200134 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200135 self.assertTrue(self.vty.verify("exit", ['']))
136 self.assertTrue(self.vty.node() is None)
137
138 # Check searching for outer node's commands
139 self.vty.command("configure terminal")
140 self.vty.command('mncc-int')
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200141
142 if self.checkForSmpp():
143 self.vty.command('smpp')
144 self.assertEquals(self.vty.node(), 'config-smpp')
145 self.vty.command('mncc-int')
146
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200147 self.assertEquals(self.vty.node(), 'config-mncc-int')
148
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200149 def testEnableDisablePeriodicLU(self):
150 self.vty.enable()
151 self.vty.command("configure terminal")
152 self.vty.command("network")
153 self.vty.command("bts 0")
154
155 # Test invalid input
156 self.vty.verify("periodic location update 0", ['% Unknown command.'])
157 self.vty.verify("periodic location update 5", ['% Unknown command.'])
158 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
159
160 # Enable periodic lu..
161 self.vty.verify("periodic location update 60", [''])
162 res = self.vty.command("write terminal")
Holger Hans Peter Freythereda08672013-07-27 22:23:25 +0200163 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200164 self.assertEquals(res.find('no periodic location update'), -1)
165
166 # Now disable it..
167 self.vty.verify("no periodic location update", [''])
168 res = self.vty.command("write terminal")
169 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freythereda08672013-07-27 22:23:25 +0200170 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200171
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200172class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200173
174 def vty_command(self):
175 return ["./src/osmo-bsc/osmo-bsc", "-c",
176 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
177
178 def vty_app(self):
179 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
180
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200181 def testConfigNetworkTree(self):
182 self._testConfigNetworkTree()
183
184 def testVtyTree(self):
185 self.vty.enable()
186 self.assertTrue(self.vty.verify("configure terminal", ['']))
187 self.assertEquals(self.vty.node(), 'config')
188 self.ignoredCheckForEndAndExit()
189 self.assertTrue(self.vty.verify("msc 0", ['']))
190 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200191 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200192 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200193 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200194 self.assertTrue(self.vty.verify("bsc", ['']))
195 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200196 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200197 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200198 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200199 self.assertTrue(self.vty.verify("exit", ['']))
200 self.assertTrue(self.vty.node() is None)
201
202 # Check searching for outer node's commands
203 self.vty.command("configure terminal")
204 self.vty.command('msc 0')
205 self.vty.command("bsc")
206 self.assertEquals(self.vty.node(), 'config-bsc')
207 self.vty.command("msc 0")
208 self.assertEquals(self.vty.node(), 'config-msc')
209
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200210 def testUssdNotificationsMsc(self):
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200211 self.vty.enable()
212 self.vty.command("configure terminal")
213 self.vty.command("msc")
214
215 # Test invalid input
216 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200217 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200218 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200219
220 # Enable USSD notifications
221 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200222 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200223 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200224
225 # Verify settings
226 res = self.vty.command("write terminal")
227 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
228 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200229 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
230 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200231 self.assert_(res.find('bsc-grace-text In grace period') > 0)
232 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200233
234 # Now disable it..
235 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200236 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200237 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200238
239 # Verify settings
240 res = self.vty.command("write terminal")
241 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
242 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200243 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200244 self.assert_(res.find('no bsc-welcome-text') > 0)
245 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
246 self.assert_(res.find('no bsc-grace-text') > 0)
247
248 def testUssdNotificationsBsc(self):
249 self.vty.enable()
250 self.vty.command("configure terminal")
251 self.vty.command("bsc")
252
253 # Test invalid input
254 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
255
256 # Enable USSD notifications
257 self.vty.verify("missing-msc-text No MSC found", [''])
258
259 # Verify settings
260 res = self.vty.command("write terminal")
261 self.assert_(res.find('missing-msc-text No MSC found') > 0)
262 self.assertEquals(res.find('no missing-msc-text'), -1)
263
264 # Now disable it..
265 self.vty.verify("no missing-msc-text", [''])
266
267 # Verify settings
268 res = self.vty.command("write terminal")
269 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
270 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200271
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200272class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200273
274 def vty_command(self):
275 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
276 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
277
278 def vty_app(self):
279 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
280
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200281 def testVtyTree(self):
282 self.vty.enable()
283 self.assertTrue(self.vty.verify('configure terminal', ['']))
284 self.assertEquals(self.vty.node(), 'config')
285 self.ignoredCheckForEndAndExit()
286 self.assertTrue(self.vty.verify('mgcp', ['']))
287 self.assertEquals(self.vty.node(), 'config-mgcp')
288 self.checkForEndAndExit()
289 self.assertTrue(self.vty.verify('exit', ['']))
290 self.assertEquals(self.vty.node(), 'config')
291 self.assertTrue(self.vty.verify('nat', ['']))
292 self.assertEquals(self.vty.node(), 'config-nat')
293 self.checkForEndAndExit()
294 self.assertTrue(self.vty.verify('bsc 0', ['']))
295 self.assertEquals(self.vty.node(), 'config-nat-bsc')
296 self.checkForEndAndExit()
297 self.assertTrue(self.vty.verify('exit', ['']))
298 self.assertEquals(self.vty.node(), 'config-nat')
299 self.assertTrue(self.vty.verify('exit', ['']))
300 self.assertEquals(self.vty.node(), 'config')
301 self.assertTrue(self.vty.verify('exit', ['']))
302 self.assertTrue(self.vty.node() is None)
303
304 # Check searching for outer node's commands
305 self.vty.command('configure terminal')
306 self.vty.command('mgcp')
307 self.vty.command('nat')
308 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeckcdf18572013-09-02 13:17:17 +0200309 self.vty.command('mgcp')
310 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200311 self.vty.command('nat')
312 self.assertEquals(self.vty.node(), 'config-nat')
313 self.vty.command('bsc 0')
Jacob Erlbeckcdf18572013-09-02 13:17:17 +0200314 self.vty.command('mgcp')
315 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200316
Holger Hans Peter Freytherbf123a12013-06-25 09:08:02 +0200317 def testRewriteNoRewrite(self):
318 self.vty.enable()
319 res = self.vty.command("configure terminal")
320 res = self.vty.command("nat")
321 res = self.vty.command("number-rewrite rewrite.cfg")
322 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200323
Holger Hans Peter Freythere9c7e272013-06-25 15:38:31 +0200324 def testRewritePostNoRewrite(self):
325 self.vty.enable()
326 self.vty.command("configure terminal")
327 self.vty.command("nat")
328 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
329 self.vty.verify("no number-rewrite-post", [''])
330
331
Holger Hans Peter Freyther367390b2013-06-25 11:44:01 +0200332 def testPrefixTreeLoading(self):
333 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
334
335 self.vty.enable()
336 self.vty.command("configure terminal")
337 self.vty.command("nat")
338 res = self.vty.command("prefix-tree %s" % cfg)
339 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
340 self.vty.command("end")
341
342 res = self.vty.command("show prefix-tree")
343 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')
344
345 self.vty.command("configure terminal")
346 self.vty.command("nat")
347 self.vty.command("no prefix-tree")
348 self.vty.command("end")
349
350 res = self.vty.command("show prefix-tree")
351 self.assertEqual(res, "% there is now prefix tree loaded.")
352
Jacob Erlbeck4684eb62013-08-14 11:10:34 +0200353 def testUssdSideChannelProvider(self):
354 self.vty.command("end")
355 self.vty.enable()
356 self.vty.command("configure terminal")
357 self.vty.command("nat")
358 self.vty.command("ussd-token key")
359 self.vty.command("end")
360
361 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
362 self.assertTrue(res)
363
364 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
365 ussdSocket.connect(('127.0.0.1', 5001))
366 ussdSocket.settimeout(2.0)
367 print "Connected to %s:%d" % ussdSocket.getpeername()
368
369 print "Expecting ID_GET request"
370 data = ussdSocket.recv(4)
371 self.assertEqual(data, "\x00\x01\xfe\x04")
372
373 print "Going to send ID_RESP response"
374 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
375 self.assertEqual(res, 10)
376
377 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
378
379 print "Going to send PING request"
380 res = ussdSocket.send("\x00\x01\xfe\x00")
381 self.assertEqual(res, 4)
382
383 print "Expecting PONG response"
384 data = ussdSocket.recv(4)
385 self.assertEqual(data, "\x00\x01\xfe\x01")
386
387 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
388 self.assertTrue(res)
389
390 print "Going to shut down connection"
391 ussdSocket.shutdown(socket.SHUT_WR)
392
393 print "Expecting EOF"
394 data = ussdSocket.recv(4)
395 self.assertEqual(data, "")
396
397 ussdSocket.close()
398
399 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
400 self.assertTrue(res)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200401
402def add_nat_test(suite, workdir):
403 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
404 print("Skipping the NAT test")
405 return
406 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
407 suite.addTest(test)
408
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200409def add_bsc_test(suite, workdir):
410 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
411 print("Skipping the BSC test")
412 return
413 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
414 suite.addTest(test)
415
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200416if __name__ == '__main__':
417 import argparse
418 import sys
419
420 workdir = '.'
421
422 parser = argparse.ArgumentParser()
423 parser.add_argument("-v", "--verbose", dest="verbose",
424 action="store_true", help="verbose mode")
425 parser.add_argument("-p", "--pythonconfpath", dest="p",
426 help="searchpath for config")
427 parser.add_argument("-w", "--workdir", dest="w",
428 help="Working directory")
429 args = parser.parse_args()
430
431 verbose_level = 1
432 if args.verbose:
433 verbose_level = 2
434
435 if args.w:
436 workdir = args.w
437
438 if args.p:
439 confpath = args.p
440
441 print "confpath %s, workdir %s" % (confpath, workdir)
442 os.chdir(workdir)
443 print "Running tests for specific VTY commands"
444 suite = unittest.TestSuite()
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200445 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200446 add_bsc_test(suite, workdir)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200447 add_nat_test(suite, workdir)
448 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
449 sys.exit(len(res.errors) + len(res.failures))