blob: ab9670ce00740b6cbfafcd815ea1139cce506780 [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()
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 Freytherc63f6f12013-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 Erlbeck96903c42013-09-02 13:17:14 +0200109 def testConfigNetworkTree(self):
110 self._testConfigNetworkTree()
111
Holger Hans Peter Freyther0df1ab92013-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 Erlbeck96903c42013-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 Freyther0df1ab92013-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 Erlbeck0ae92a92013-09-02 13:17:16 +0200134 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-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 Freyther0df1ab92013-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 Erlbeck96903c42013-09-02 13:17:14 +0200147 self.assertEquals(self.vty.node(), 'config-mncc-int')
148
Holger Hans Peter Freytherc63f6f12013-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 Freytherc0438e32013-07-27 22:23:25 +0200163 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-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 Freytherc0438e32013-07-27 22:23:25 +0200170 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200171
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200172class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-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 Erlbeck96903c42013-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 Erlbeck0ae92a92013-09-02 13:17:16 +0200191 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200192 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200193 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200194 self.assertTrue(self.vty.verify("bsc", ['']))
195 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200196 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200197 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200198 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-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 Erlbeck1b894022013-08-28 10:16:54 +0200210 def testUssdNotifications(self):
211 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 Erlbeck97e139f2013-08-28 10:16:55 +0200217 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200218
219 # Enable USSD notifications
220 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200221 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200222
223 # Verify settings
224 res = self.vty.command("write terminal")
225 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
226 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200227 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
228 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200229
230 # Now disable it..
231 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200232 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200233
234 # Verify settings
235 res = self.vty.command("write terminal")
236 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
237 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200238 self.assert_(res.find('no bsc-welcome-text') > 0)
239 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200240
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200241class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200242
243 def vty_command(self):
244 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
245 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
246
247 def vty_app(self):
248 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
249
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200250 def testVtyTree(self):
251 self.vty.enable()
252 self.assertTrue(self.vty.verify('configure terminal', ['']))
253 self.assertEquals(self.vty.node(), 'config')
254 self.ignoredCheckForEndAndExit()
255 self.assertTrue(self.vty.verify('mgcp', ['']))
256 self.assertEquals(self.vty.node(), 'config-mgcp')
257 self.checkForEndAndExit()
258 self.assertTrue(self.vty.verify('exit', ['']))
259 self.assertEquals(self.vty.node(), 'config')
260 self.assertTrue(self.vty.verify('nat', ['']))
261 self.assertEquals(self.vty.node(), 'config-nat')
262 self.checkForEndAndExit()
263 self.assertTrue(self.vty.verify('bsc 0', ['']))
264 self.assertEquals(self.vty.node(), 'config-nat-bsc')
265 self.checkForEndAndExit()
266 self.assertTrue(self.vty.verify('exit', ['']))
267 self.assertEquals(self.vty.node(), 'config-nat')
268 self.assertTrue(self.vty.verify('exit', ['']))
269 self.assertEquals(self.vty.node(), 'config')
270 self.assertTrue(self.vty.verify('exit', ['']))
271 self.assertTrue(self.vty.node() is None)
272
273 # Check searching for outer node's commands
274 self.vty.command('configure terminal')
275 self.vty.command('mgcp')
276 self.vty.command('nat')
277 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200278 self.vty.command('mgcp')
279 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200280 self.vty.command('nat')
281 self.assertEquals(self.vty.node(), 'config-nat')
282 self.vty.command('bsc 0')
Jacob Erlbeck4c9dff52013-09-02 13:17:17 +0200283 self.vty.command('mgcp')
284 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200285
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200286 def testRewriteNoRewrite(self):
287 self.vty.enable()
288 res = self.vty.command("configure terminal")
289 res = self.vty.command("nat")
290 res = self.vty.command("number-rewrite rewrite.cfg")
291 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200292
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200293 def testRewritePostNoRewrite(self):
294 self.vty.enable()
295 self.vty.command("configure terminal")
296 self.vty.command("nat")
297 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
298 self.vty.verify("no number-rewrite-post", [''])
299
300
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200301 def testPrefixTreeLoading(self):
302 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
303
304 self.vty.enable()
305 self.vty.command("configure terminal")
306 self.vty.command("nat")
307 res = self.vty.command("prefix-tree %s" % cfg)
308 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
309 self.vty.command("end")
310
311 res = self.vty.command("show prefix-tree")
312 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')
313
314 self.vty.command("configure terminal")
315 self.vty.command("nat")
316 self.vty.command("no prefix-tree")
317 self.vty.command("end")
318
319 res = self.vty.command("show prefix-tree")
320 self.assertEqual(res, "% there is now prefix tree loaded.")
321
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200322 def testUssdSideChannelProvider(self):
323 self.vty.command("end")
324 self.vty.enable()
325 self.vty.command("configure terminal")
326 self.vty.command("nat")
327 self.vty.command("ussd-token key")
328 self.vty.command("end")
329
330 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
331 self.assertTrue(res)
332
333 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
334 ussdSocket.connect(('127.0.0.1', 5001))
335 ussdSocket.settimeout(2.0)
336 print "Connected to %s:%d" % ussdSocket.getpeername()
337
338 print "Expecting ID_GET request"
339 data = ussdSocket.recv(4)
340 self.assertEqual(data, "\x00\x01\xfe\x04")
341
342 print "Going to send ID_RESP response"
343 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
344 self.assertEqual(res, 10)
345
346 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
347
348 print "Going to send PING request"
349 res = ussdSocket.send("\x00\x01\xfe\x00")
350 self.assertEqual(res, 4)
351
352 print "Expecting PONG response"
353 data = ussdSocket.recv(4)
354 self.assertEqual(data, "\x00\x01\xfe\x01")
355
356 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
357 self.assertTrue(res)
358
359 print "Going to shut down connection"
360 ussdSocket.shutdown(socket.SHUT_WR)
361
362 print "Expecting EOF"
363 data = ussdSocket.recv(4)
364 self.assertEqual(data, "")
365
366 ussdSocket.close()
367
368 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
369 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200370
371def add_nat_test(suite, workdir):
372 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
373 print("Skipping the NAT test")
374 return
375 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
376 suite.addTest(test)
377
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200378def add_bsc_test(suite, workdir):
379 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
380 print("Skipping the BSC test")
381 return
382 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
383 suite.addTest(test)
384
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200385if __name__ == '__main__':
386 import argparse
387 import sys
388
389 workdir = '.'
390
391 parser = argparse.ArgumentParser()
392 parser.add_argument("-v", "--verbose", dest="verbose",
393 action="store_true", help="verbose mode")
394 parser.add_argument("-p", "--pythonconfpath", dest="p",
395 help="searchpath for config")
396 parser.add_argument("-w", "--workdir", dest="w",
397 help="Working directory")
398 args = parser.parse_args()
399
400 verbose_level = 1
401 if args.verbose:
402 verbose_level = 2
403
404 if args.w:
405 workdir = args.w
406
407 if args.p:
408 confpath = args.p
409
410 print "confpath %s, workdir %s" % (confpath, workdir)
411 os.chdir(workdir)
412 print "Running tests for specific VTY commands"
413 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200414 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200415 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200416 add_nat_test(suite, workdir)
417 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
418 sys.exit(len(res.errors) + len(res.failures))