blob: fd200c850bbacb4c05858df965caa2709a6a66d7 [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.'])
107
108 # Enable USSD notifications
109 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
110
111 # Verify settings
112 res = self.vty.command("write terminal")
113 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
114 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
115
116 # Now disable it..
117 self.vty.verify("no bsc-msc-lost-text", [''])
118
119 # Verify settings
120 res = self.vty.command("write terminal")
121 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
122 self.assert_(res.find('no bsc-msc-lost-text') > 0)
123
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200124class TestVTYNAT(TestVTYBase):
125
126 def vty_command(self):
127 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
128 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
129
130 def vty_app(self):
131 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
132
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200133 def testRewriteNoRewrite(self):
134 self.vty.enable()
135 res = self.vty.command("configure terminal")
136 res = self.vty.command("nat")
137 res = self.vty.command("number-rewrite rewrite.cfg")
138 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200139
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200140 def testRewritePostNoRewrite(self):
141 self.vty.enable()
142 self.vty.command("configure terminal")
143 self.vty.command("nat")
144 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
145 self.vty.verify("no number-rewrite-post", [''])
146
147
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200148 def testPrefixTreeLoading(self):
149 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
150
151 self.vty.enable()
152 self.vty.command("configure terminal")
153 self.vty.command("nat")
154 res = self.vty.command("prefix-tree %s" % cfg)
155 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
156 self.vty.command("end")
157
158 res = self.vty.command("show prefix-tree")
159 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')
160
161 self.vty.command("configure terminal")
162 self.vty.command("nat")
163 self.vty.command("no prefix-tree")
164 self.vty.command("end")
165
166 res = self.vty.command("show prefix-tree")
167 self.assertEqual(res, "% there is now prefix tree loaded.")
168
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200169 def testUssdSideChannelProvider(self):
170 self.vty.command("end")
171 self.vty.enable()
172 self.vty.command("configure terminal")
173 self.vty.command("nat")
174 self.vty.command("ussd-token key")
175 self.vty.command("end")
176
177 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
178 self.assertTrue(res)
179
180 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
181 ussdSocket.connect(('127.0.0.1', 5001))
182 ussdSocket.settimeout(2.0)
183 print "Connected to %s:%d" % ussdSocket.getpeername()
184
185 print "Expecting ID_GET request"
186 data = ussdSocket.recv(4)
187 self.assertEqual(data, "\x00\x01\xfe\x04")
188
189 print "Going to send ID_RESP response"
190 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
191 self.assertEqual(res, 10)
192
193 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
194
195 print "Going to send PING request"
196 res = ussdSocket.send("\x00\x01\xfe\x00")
197 self.assertEqual(res, 4)
198
199 print "Expecting PONG response"
200 data = ussdSocket.recv(4)
201 self.assertEqual(data, "\x00\x01\xfe\x01")
202
203 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
204 self.assertTrue(res)
205
206 print "Going to shut down connection"
207 ussdSocket.shutdown(socket.SHUT_WR)
208
209 print "Expecting EOF"
210 data = ussdSocket.recv(4)
211 self.assertEqual(data, "")
212
213 ussdSocket.close()
214
215 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
216 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200217
218def add_nat_test(suite, workdir):
219 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
220 print("Skipping the NAT test")
221 return
222 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
223 suite.addTest(test)
224
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200225def add_bsc_test(suite, workdir):
226 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
227 print("Skipping the BSC test")
228 return
229 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
230 suite.addTest(test)
231
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200232if __name__ == '__main__':
233 import argparse
234 import sys
235
236 workdir = '.'
237
238 parser = argparse.ArgumentParser()
239 parser.add_argument("-v", "--verbose", dest="verbose",
240 action="store_true", help="verbose mode")
241 parser.add_argument("-p", "--pythonconfpath", dest="p",
242 help="searchpath for config")
243 parser.add_argument("-w", "--workdir", dest="w",
244 help="Working directory")
245 args = parser.parse_args()
246
247 verbose_level = 1
248 if args.verbose:
249 verbose_level = 2
250
251 if args.w:
252 workdir = args.w
253
254 if args.p:
255 confpath = args.p
256
257 print "confpath %s, workdir %s" % (confpath, workdir)
258 os.chdir(workdir)
259 print "Running tests for specific VTY commands"
260 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200261 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200262 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200263 add_nat_test(suite, workdir)
264 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
265 sys.exit(len(res.errors) + len(res.failures))