blob: 9dc0d1340c94f10a04e17f71c9fb68b2c2957a1c [file] [log] [blame]
Holger Hans Peter Freythered0d4f62014-03-06 23:43:13 +01001#!/usr/bin/env python
2
3# (C) 2014 by Holger Hans Peter Freyther
4# based on vty_test_runner.py:
5# (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
6# (C) 2013 by Holger Hans Peter Freyther
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import time
22import unittest
23import socket
24
25import osmopy.obscvty as obscvty
26import osmopy.osmoutil as osmoutil
27
28confpath = '.'
29
30class TestVTYBase(unittest.TestCase):
31
32 def vty_command(self):
33 raise Exception("Needs to be implemented by a subclass")
34
35 def vty_app(self):
36 raise Exception("Needs to be implemented by a subclass")
37
38 def setUp(self):
39 osmo_vty_cmd = self.vty_command()[:]
40 config_index = osmo_vty_cmd.index('-c')
41 if config_index:
42 cfi = config_index + 1
43 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
44
45 try:
46 print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())
47 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
48 except OSError:
49 print >> sys.stderr, "Current directory: %s" % os.getcwd()
50 print >> sys.stderr, "Consider setting -b"
51 time.sleep(1)
52
53 appstring = self.vty_app()[2]
54 appport = self.vty_app()[0]
55 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
56
57 def tearDown(self):
58 self.vty = None
59 osmoutil.end_proc(self.proc)
60
61
62class TestSMPPNITB(TestVTYBase):
63
64 def vty_command(self):
65 return ["./src/osmo-nitb/osmo-nitb", "-c",
66 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
67
68 def vty_app(self):
69 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
70
71 def testSMPPCrashes(self):
72 # Enable the configuration
73 self.vty.enable()
74 self.assertTrue(self.vty.verify("configure terminal", ['']))
75 self.assertEquals(self.vty.node(), 'config')
76
77 self.assertTrue(self.vty.verify('smpp', ['']))
78 self.assertEquals(self.vty.node(), 'config-smpp')
79 self.assertTrue(self.vty.verify('system-id test', ['']))
80 self.assertTrue(self.vty.verify('local-tcp-port 2775', ['']))
81 self.assertTrue(self.vty.verify('esme test', ['']))
82 self.assertEquals(self.vty.node(), 'config-smpp-esme')
83 self.assertTrue(self.vty.verify('default-route', ['']))
84 self.assertTrue(self.vty.verify('end', ['']))
85
86 # NITB should listen to 2775 now!
87 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
88 sck.setblocking(1)
89 sck.connect(('0.0.0.0', 2775))
90 sck.sendall('\x00\x00\x00\x02\x00')
91 sck.close()
92
93 # Check if the VTY is still there
94 self.vty.verify('disable',[''])
95
96 # Now for the second packet
97 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
98 sck.setblocking(1)
99 sck.connect(('0.0.0.0', 2775))
100 sck.sendall('\x00\x01\x00\x01\x01')
101 sck.close()
102
103 self.vty.verify('enable',[''])
104
105if __name__ == '__main__':
106 import argparse
107 import sys
108
109 workdir = '.'
110
111 parser = argparse.ArgumentParser()
112 parser.add_argument("-v", "--verbose", dest="verbose",
113 action="store_true", help="verbose mode")
114 parser.add_argument("-p", "--pythonconfpath", dest="p",
115 help="searchpath for config")
116 parser.add_argument("-w", "--workdir", dest="w",
117 help="Working directory")
118 args = parser.parse_args()
119
120 verbose_level = 1
121 if args.verbose:
122 verbose_level = 2
123
124 if args.w:
125 workdir = args.w
126
127 if args.p:
128 confpath = args.p
129
130 print "confpath %s, workdir %s" % (confpath, workdir)
131 os.chdir(workdir)
132 print "Running tests for specific SMPP"
133 suite = unittest.TestSuite()
134 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestSMPPNITB))
135 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
136 sys.exit(len(res.errors) + len(res.failures))