blob: e91d0182124b426c435bbf4d9e7b32f373be7da8 [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
77 def notIgnoredTest(self):
78 sys.stderr.write('Going to ignore the next assertion(s) due to known bugs\n')
79 return False
80
81
82 def _testConfigNetworkTree(self):
83 self.vty.enable()
84 self.assertTrue(self.vty.verify("configure terminal",['']))
85 self.assertEquals(self.vty.node(), 'config')
86 self.ignoredCheckForEndAndExit()
87 self.assertTrue(self.vty.verify("network",['']))
88 self.assertEquals(self.vty.node(), 'config-net')
89 self.checkForEndAndExit()
90 self.assertTrue(self.vty.verify("bts 0",['']))
91 self.assertEquals(self.vty.node(), 'config-net-bts')
92 self.checkForEndAndExit()
93 self.assertTrue(self.vty.verify("trx 0",['']))
94 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
95 self.checkForEndAndExit()
96 self.assertTrue(self.vty.verify("exit",['']))
97 self.assertEquals(self.vty.node(), 'config-net-bts')
98 self.assertTrue(self.vty.verify("exit",['']))
99 self.assertEquals(self.vty.node(), 'config-net')
100 self.assertTrue(self.vty.verify("exit",['']))
101 self.assertEquals(self.vty.node(), 'config')
102 self.assertTrue(self.vty.verify("exit",['']))
103 self.assertTrue(self.vty.node() is None)
104
105class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200106
107 def vty_command(self):
108 return ["./src/osmo-nitb/osmo-nitb", "-c",
109 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
110
111 def vty_app(self):
112 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
113
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200114 def testConfigNetworkTree(self):
115 self._testConfigNetworkTree()
116
117 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', ['']))
126 self.assertEquals(self.vty.node(), 'config')
127 self.assertTrue(self.vty.verify('smpp', ['']))
128 self.assertEquals(self.vty.node(), 'config-smpp')
129 self.ignoredCheckForEndAndExit()
130 self.assertTrue(self.vty.verify("exit", ['']))
131 if self.notIgnoredTest():
132 self.assertEquals(self.vty.node(), 'config')
133 else:
134 self.assertTrue(self.vty.verify("configure terminal", ['']))
135 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')
141 self.vty.command('smpp')
142 self.assertEquals(self.vty.node(), 'config-smpp')
143 self.vty.command('mncc-int')
144 self.assertEquals(self.vty.node(), 'config-mncc-int')
145
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200146 def testEnableDisablePeriodicLU(self):
147 self.vty.enable()
148 self.vty.command("configure terminal")
149 self.vty.command("network")
150 self.vty.command("bts 0")
151
152 # Test invalid input
153 self.vty.verify("periodic location update 0", ['% Unknown command.'])
154 self.vty.verify("periodic location update 5", ['% Unknown command.'])
155 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
156
157 # Enable periodic lu..
158 self.vty.verify("periodic location update 60", [''])
159 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200160 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200161 self.assertEquals(res.find('no periodic location update'), -1)
162
163 # Now disable it..
164 self.vty.verify("no periodic location update", [''])
165 res = self.vty.command("write terminal")
166 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200167 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200168
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200169class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200170
171 def vty_command(self):
172 return ["./src/osmo-bsc/osmo-bsc", "-c",
173 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
174
175 def vty_app(self):
176 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
177
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200178 def testConfigNetworkTree(self):
179 self._testConfigNetworkTree()
180
181 def testVtyTree(self):
182 self.vty.enable()
183 self.assertTrue(self.vty.verify("configure terminal", ['']))
184 self.assertEquals(self.vty.node(), 'config')
185 self.ignoredCheckForEndAndExit()
186 self.assertTrue(self.vty.verify("msc 0", ['']))
187 self.assertEquals(self.vty.node(), 'config-msc')
188 self.ignoredCheckForEndAndExit()
189 self.assertTrue(self.vty.verify("exit", ['']))
190 if self.notIgnoredTest():
191 self.assertEquals(self.vty.node(), 'config')
192 else:
193 self.assertTrue(self.vty.verify("configure terminal", ['']))
194 self.assertTrue(self.vty.verify("bsc", ['']))
195 self.assertEquals(self.vty.node(), 'config-bsc')
196 self.ignoredCheckForEndAndExit()
197 self.assertTrue(self.vty.verify("exit", ['']))
198 if self.notIgnoredTest():
199 self.assertEquals(self.vty.node(), 'config')
200 else:
201 self.assertTrue(self.vty.verify("configure terminal", ['']))
202 self.assertTrue(self.vty.verify("exit", ['']))
203 self.assertTrue(self.vty.node() is None)
204
205 # Check searching for outer node's commands
206 self.vty.command("configure terminal")
207 self.vty.command('msc 0')
208 self.vty.command("bsc")
209 self.assertEquals(self.vty.node(), 'config-bsc')
210 self.vty.command("msc 0")
211 self.assertEquals(self.vty.node(), 'config-msc')
212
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200213 def testUssdNotifications(self):
214 self.vty.enable()
215 self.vty.command("configure terminal")
216 self.vty.command("msc")
217
218 # Test invalid input
219 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200220 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200221
222 # Enable USSD notifications
223 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200224 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200225
226 # Verify settings
227 res = self.vty.command("write terminal")
228 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
229 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200230 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
231 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200232
233 # Now disable it..
234 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200235 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200236
237 # Verify settings
238 res = self.vty.command("write terminal")
239 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
240 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200241 self.assert_(res.find('no bsc-welcome-text') > 0)
242 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200243
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200244class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200245
246 def vty_command(self):
247 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
248 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
249
250 def vty_app(self):
251 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
252
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200253 def testVtyTree(self):
254 self.vty.enable()
255 self.assertTrue(self.vty.verify('configure terminal', ['']))
256 self.assertEquals(self.vty.node(), 'config')
257 self.ignoredCheckForEndAndExit()
258 self.assertTrue(self.vty.verify('mgcp', ['']))
259 self.assertEquals(self.vty.node(), 'config-mgcp')
260 self.checkForEndAndExit()
261 self.assertTrue(self.vty.verify('exit', ['']))
262 self.assertEquals(self.vty.node(), 'config')
263 self.assertTrue(self.vty.verify('nat', ['']))
264 self.assertEquals(self.vty.node(), 'config-nat')
265 self.checkForEndAndExit()
266 self.assertTrue(self.vty.verify('bsc 0', ['']))
267 self.assertEquals(self.vty.node(), 'config-nat-bsc')
268 self.checkForEndAndExit()
269 self.assertTrue(self.vty.verify('exit', ['']))
270 self.assertEquals(self.vty.node(), 'config-nat')
271 self.assertTrue(self.vty.verify('exit', ['']))
272 self.assertEquals(self.vty.node(), 'config')
273 self.assertTrue(self.vty.verify('exit', ['']))
274 self.assertTrue(self.vty.node() is None)
275
276 # Check searching for outer node's commands
277 self.vty.command('configure terminal')
278 self.vty.command('mgcp')
279 self.vty.command('nat')
280 self.assertEquals(self.vty.node(), 'config-nat')
281 self.vty.command('line vty')
282 self.assertEquals(self.vty.node(), 'config-line')
283 self.vty.command('nat')
284 self.assertEquals(self.vty.node(), 'config-nat')
285 self.vty.command('bsc 0')
286 self.vty.command('line vty')
287 self.assertEquals(self.vty.node(), 'config-line')
288
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200289 def testRewriteNoRewrite(self):
290 self.vty.enable()
291 res = self.vty.command("configure terminal")
292 res = self.vty.command("nat")
293 res = self.vty.command("number-rewrite rewrite.cfg")
294 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200295
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200296 def testRewritePostNoRewrite(self):
297 self.vty.enable()
298 self.vty.command("configure terminal")
299 self.vty.command("nat")
300 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
301 self.vty.verify("no number-rewrite-post", [''])
302
303
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200304 def testPrefixTreeLoading(self):
305 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
306
307 self.vty.enable()
308 self.vty.command("configure terminal")
309 self.vty.command("nat")
310 res = self.vty.command("prefix-tree %s" % cfg)
311 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
312 self.vty.command("end")
313
314 res = self.vty.command("show prefix-tree")
315 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')
316
317 self.vty.command("configure terminal")
318 self.vty.command("nat")
319 self.vty.command("no prefix-tree")
320 self.vty.command("end")
321
322 res = self.vty.command("show prefix-tree")
323 self.assertEqual(res, "% there is now prefix tree loaded.")
324
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200325 def testUssdSideChannelProvider(self):
326 self.vty.command("end")
327 self.vty.enable()
328 self.vty.command("configure terminal")
329 self.vty.command("nat")
330 self.vty.command("ussd-token key")
331 self.vty.command("end")
332
333 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
334 self.assertTrue(res)
335
336 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
337 ussdSocket.connect(('127.0.0.1', 5001))
338 ussdSocket.settimeout(2.0)
339 print "Connected to %s:%d" % ussdSocket.getpeername()
340
341 print "Expecting ID_GET request"
342 data = ussdSocket.recv(4)
343 self.assertEqual(data, "\x00\x01\xfe\x04")
344
345 print "Going to send ID_RESP response"
346 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
347 self.assertEqual(res, 10)
348
349 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
350
351 print "Going to send PING request"
352 res = ussdSocket.send("\x00\x01\xfe\x00")
353 self.assertEqual(res, 4)
354
355 print "Expecting PONG response"
356 data = ussdSocket.recv(4)
357 self.assertEqual(data, "\x00\x01\xfe\x01")
358
359 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
360 self.assertTrue(res)
361
362 print "Going to shut down connection"
363 ussdSocket.shutdown(socket.SHUT_WR)
364
365 print "Expecting EOF"
366 data = ussdSocket.recv(4)
367 self.assertEqual(data, "")
368
369 ussdSocket.close()
370
371 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
372 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200373
374def add_nat_test(suite, workdir):
375 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
376 print("Skipping the NAT test")
377 return
378 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
379 suite.addTest(test)
380
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200381def add_bsc_test(suite, workdir):
382 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
383 print("Skipping the BSC test")
384 return
385 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
386 suite.addTest(test)
387
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200388if __name__ == '__main__':
389 import argparse
390 import sys
391
392 workdir = '.'
393
394 parser = argparse.ArgumentParser()
395 parser.add_argument("-v", "--verbose", dest="verbose",
396 action="store_true", help="verbose mode")
397 parser.add_argument("-p", "--pythonconfpath", dest="p",
398 help="searchpath for config")
399 parser.add_argument("-w", "--workdir", dest="w",
400 help="Working directory")
401 args = parser.parse_args()
402
403 verbose_level = 1
404 if args.verbose:
405 verbose_level = 2
406
407 if args.w:
408 workdir = args.w
409
410 if args.p:
411 confpath = args.p
412
413 print "confpath %s, workdir %s" % (confpath, workdir)
414 os.chdir(workdir)
415 print "Running tests for specific VTY commands"
416 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200417 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200418 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200419 add_nat_test(suite, workdir)
420 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
421 sys.exit(len(res.errors) + len(res.failures))