blob: 87a45bbfa9b95ffc120f4c6f2a1d098d7cddfbc7 [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
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020059class TestVTYNITB(TestVTYBase):
60
61 def vty_command(self):
62 return ["./src/osmo-nitb/osmo-nitb", "-c",
63 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
64
65 def vty_app(self):
66 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
67
68 def testEnableDisablePeriodicLU(self):
69 self.vty.enable()
70 self.vty.command("configure terminal")
71 self.vty.command("network")
72 self.vty.command("bts 0")
73
74 # Test invalid input
75 self.vty.verify("periodic location update 0", ['% Unknown command.'])
76 self.vty.verify("periodic location update 5", ['% Unknown command.'])
77 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
78
79 # Enable periodic lu..
80 self.vty.verify("periodic location update 60", [''])
81 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +020082 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020083 self.assertEquals(res.find('no periodic location update'), -1)
84
85 # Now disable it..
86 self.vty.verify("no periodic location update", [''])
87 res = self.vty.command("write terminal")
88 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +020089 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020090
Jacob Erlbeck1b894022013-08-28 10:16:54 +020091class TestVTYBSC(TestVTYBase):
92
93 def vty_command(self):
94 return ["./src/osmo-bsc/osmo-bsc", "-c",
95 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
96
97 def vty_app(self):
98 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
99
100 def testUssdNotifications(self):
101 self.vty.enable()
102 self.vty.command("configure terminal")
103 self.vty.command("msc")
104
105 # Test invalid input
106 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200107 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200108
109 # Enable USSD notifications
110 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200111 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200112
113 # Verify settings
114 res = self.vty.command("write terminal")
115 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
116 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200117 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
118 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200119
120 # Now disable it..
121 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200122 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200123
124 # Verify settings
125 res = self.vty.command("write terminal")
126 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
127 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200128 self.assert_(res.find('no bsc-welcome-text') > 0)
129 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200130
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200131class TestVTYNAT(TestVTYBase):
132
133 def vty_command(self):
134 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
135 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
136
137 def vty_app(self):
138 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
139
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200140 def testRewriteNoRewrite(self):
141 self.vty.enable()
142 res = self.vty.command("configure terminal")
143 res = self.vty.command("nat")
144 res = self.vty.command("number-rewrite rewrite.cfg")
145 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200146
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200147 def testRewritePostNoRewrite(self):
148 self.vty.enable()
149 self.vty.command("configure terminal")
150 self.vty.command("nat")
151 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
152 self.vty.verify("no number-rewrite-post", [''])
153
154
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200155 def testPrefixTreeLoading(self):
156 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
157
158 self.vty.enable()
159 self.vty.command("configure terminal")
160 self.vty.command("nat")
161 res = self.vty.command("prefix-tree %s" % cfg)
162 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
163 self.vty.command("end")
164
165 res = self.vty.command("show prefix-tree")
166 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')
167
168 self.vty.command("configure terminal")
169 self.vty.command("nat")
170 self.vty.command("no prefix-tree")
171 self.vty.command("end")
172
173 res = self.vty.command("show prefix-tree")
174 self.assertEqual(res, "% there is now prefix tree loaded.")
175
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200176 def testUssdSideChannelProvider(self):
177 self.vty.command("end")
178 self.vty.enable()
179 self.vty.command("configure terminal")
180 self.vty.command("nat")
181 self.vty.command("ussd-token key")
182 self.vty.command("end")
183
184 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
185 self.assertTrue(res)
186
187 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
188 ussdSocket.connect(('127.0.0.1', 5001))
189 ussdSocket.settimeout(2.0)
190 print "Connected to %s:%d" % ussdSocket.getpeername()
191
192 print "Expecting ID_GET request"
193 data = ussdSocket.recv(4)
194 self.assertEqual(data, "\x00\x01\xfe\x04")
195
196 print "Going to send ID_RESP response"
197 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
198 self.assertEqual(res, 10)
199
200 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
201
202 print "Going to send PING request"
203 res = ussdSocket.send("\x00\x01\xfe\x00")
204 self.assertEqual(res, 4)
205
206 print "Expecting PONG response"
207 data = ussdSocket.recv(4)
208 self.assertEqual(data, "\x00\x01\xfe\x01")
209
210 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
211 self.assertTrue(res)
212
213 print "Going to shut down connection"
214 ussdSocket.shutdown(socket.SHUT_WR)
215
216 print "Expecting EOF"
217 data = ussdSocket.recv(4)
218 self.assertEqual(data, "")
219
220 ussdSocket.close()
221
222 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
223 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200224
225def add_nat_test(suite, workdir):
226 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
227 print("Skipping the NAT test")
228 return
229 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
230 suite.addTest(test)
231
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200232def add_bsc_test(suite, workdir):
233 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
234 print("Skipping the BSC test")
235 return
236 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
237 suite.addTest(test)
238
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200239if __name__ == '__main__':
240 import argparse
241 import sys
242
243 workdir = '.'
244
245 parser = argparse.ArgumentParser()
246 parser.add_argument("-v", "--verbose", dest="verbose",
247 action="store_true", help="verbose mode")
248 parser.add_argument("-p", "--pythonconfpath", dest="p",
249 help="searchpath for config")
250 parser.add_argument("-w", "--workdir", dest="w",
251 help="Working directory")
252 args = parser.parse_args()
253
254 verbose_level = 1
255 if args.verbose:
256 verbose_level = 2
257
258 if args.w:
259 workdir = args.w
260
261 if args.p:
262 confpath = args.p
263
264 print "confpath %s, workdir %s" % (confpath, workdir)
265 os.chdir(workdir)
266 print "Running tests for specific VTY commands"
267 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200268 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200269 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200270 add_nat_test(suite, workdir)
271 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
272 sys.exit(len(res.errors) + len(res.failures))