blob: b6da86ca2794bbb053cee5f887ec525608b23e8c [file] [log] [blame]
Holger Hans Peter Freyther4a93f572014-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:
Holger Hans Peter Freyther4a93f572014-03-06 23:43:13 +010046 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
47 except OSError:
48 print >> sys.stderr, "Current directory: %s" % os.getcwd()
49 print >> sys.stderr, "Consider setting -b"
50 time.sleep(1)
51
52 appstring = self.vty_app()[2]
53 appport = self.vty_app()[0]
54 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
55
56 def tearDown(self):
57 self.vty = None
58 osmoutil.end_proc(self.proc)
59
60
61class TestSMPPNITB(TestVTYBase):
62
63 def vty_command(self):
64 return ["./src/osmo-nitb/osmo-nitb", "-c",
65 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
66
67 def vty_app(self):
68 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
69
70 def testSMPPCrashes(self):
71 # Enable the configuration
72 self.vty.enable()
73 self.assertTrue(self.vty.verify("configure terminal", ['']))
74 self.assertEquals(self.vty.node(), 'config')
75
76 self.assertTrue(self.vty.verify('smpp', ['']))
77 self.assertEquals(self.vty.node(), 'config-smpp')
78 self.assertTrue(self.vty.verify('system-id test', ['']))
79 self.assertTrue(self.vty.verify('local-tcp-port 2775', ['']))
80 self.assertTrue(self.vty.verify('esme test', ['']))
81 self.assertEquals(self.vty.node(), 'config-smpp-esme')
82 self.assertTrue(self.vty.verify('default-route', ['']))
83 self.assertTrue(self.vty.verify('end', ['']))
84
85 # NITB should listen to 2775 now!
86 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
87 sck.setblocking(1)
88 sck.connect(('0.0.0.0', 2775))
89 sck.sendall('\x00\x00\x00\x02\x00')
90 sck.close()
91
92 # Check if the VTY is still there
93 self.vty.verify('disable',[''])
94
95 # Now for the second packet
96 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97 sck.setblocking(1)
98 sck.connect(('0.0.0.0', 2775))
99 sck.sendall('\x00\x01\x00\x01\x01')
100 sck.close()
101
102 self.vty.verify('enable',[''])
103
104if __name__ == '__main__':
105 import argparse
106 import sys
107
108 workdir = '.'
109
110 parser = argparse.ArgumentParser()
111 parser.add_argument("-v", "--verbose", dest="verbose",
112 action="store_true", help="verbose mode")
113 parser.add_argument("-p", "--pythonconfpath", dest="p",
114 help="searchpath for config")
115 parser.add_argument("-w", "--workdir", dest="w",
116 help="Working directory")
117 args = parser.parse_args()
118
119 verbose_level = 1
120 if args.verbose:
121 verbose_level = 2
122
123 if args.w:
124 workdir = args.w
125
126 if args.p:
127 confpath = args.p
128
129 print "confpath %s, workdir %s" % (confpath, workdir)
130 os.chdir(workdir)
131 print "Running tests for specific SMPP"
132 suite = unittest.TestSuite()
133 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestSMPPNITB))
134 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
135 sys.exit(len(res.errors) + len(res.failures))