blob: db8294d3acb038c9a75f0fd5bda404dd63e00ac8 [file] [log] [blame]
Holger Hans Peter Freyther65397522013-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 Erlbeck4684eb62013-08-14 11:10:34 +020021import socket
Holger Hans Peter Freyther65397522013-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 Freytherb30b3aa2014-07-04 20:23:56 +020059class TestVTYMGCP(TestVTYBase):
60 def vty_command(self):
61 return ["./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "-c",
62 "doc/examples/osmo-bsc_mgcp/mgcp.cfg"]
63
64 def vty_app(self):
65 return (4243, "./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "OpenBSC MGCP", "mgcp")
66
67 def testForcePtime(self):
68 self.vty.enable()
69 res = self.vty.command("show running-config")
70 self.assert_(res.find(' rtp force-ptime 20\r') > 0)
71 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
72
73 self.vty.command("configure terminal")
74 self.vty.command("mgcp")
75 self.vty.command("no rtp force-ptime")
76 res = self.vty.command("show running-config")
77 self.assertEquals(res.find(' rtp force-ptime 20\r'), -1)
78 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
79
Jacob Erlbeck768a7c32013-09-02 13:17:14 +020080
81class TestVTYGenericBSC(TestVTYBase):
82
83 def checkForEndAndExit(self):
84 res = self.vty.command("list")
85 #print ('looking for "exit"\n')
86 self.assert_(res.find(' exit\r') > 0)
87 #print 'found "exit"\nlooking for "end"\n'
88 self.assert_(res.find(' end\r') > 0)
89 #print 'found "end"\n'
90
Jacob Erlbeck768a7c32013-09-02 13:17:14 +020091 def _testConfigNetworkTree(self):
92 self.vty.enable()
93 self.assertTrue(self.vty.verify("configure terminal",['']))
94 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +010095 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +020096 self.assertTrue(self.vty.verify("network",['']))
97 self.assertEquals(self.vty.node(), 'config-net')
98 self.checkForEndAndExit()
99 self.assertTrue(self.vty.verify("bts 0",['']))
100 self.assertEquals(self.vty.node(), 'config-net-bts')
101 self.checkForEndAndExit()
102 self.assertTrue(self.vty.verify("trx 0",['']))
103 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
104 self.checkForEndAndExit()
Jacob Erlbeck5e229112013-09-11 10:46:56 +0200105 self.vty.command("write terminal")
106 self.assertTrue(self.vty.verify("exit",['']))
107 self.assertEquals(self.vty.node(), 'config-net-bts')
108 self.assertTrue(self.vty.verify("exit",['']))
109 self.assertTrue(self.vty.verify("bts 1",['']))
110 self.assertEquals(self.vty.node(), 'config-net-bts')
111 self.checkForEndAndExit()
112 self.assertTrue(self.vty.verify("trx 1",['']))
113 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
114 self.checkForEndAndExit()
115 self.vty.command("write terminal")
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200116 self.assertTrue(self.vty.verify("exit",['']))
117 self.assertEquals(self.vty.node(), 'config-net-bts')
118 self.assertTrue(self.vty.verify("exit",['']))
119 self.assertEquals(self.vty.node(), 'config-net')
120 self.assertTrue(self.vty.verify("exit",['']))
121 self.assertEquals(self.vty.node(), 'config')
122 self.assertTrue(self.vty.verify("exit",['']))
123 self.assertTrue(self.vty.node() is None)
124
125class TestVTYNITB(TestVTYGenericBSC):
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200126
127 def vty_command(self):
128 return ["./src/osmo-nitb/osmo-nitb", "-c",
129 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
130
131 def vty_app(self):
132 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
133
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200134 def testConfigNetworkTree(self):
Jacob Erlbeck5c46e932013-10-23 11:24:14 +0200135 self._testConfigNetworkTree()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200136
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200137 def checkForSmpp(self):
138 """SMPP is not always enabled, check if it is"""
139 res = self.vty.command("list")
140 return "smpp" in res
141
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200142 def testVtyTree(self):
143 self.vty.enable()
144 self.assertTrue(self.vty.verify("configure terminal", ['']))
145 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +0100146 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200147 self.assertTrue(self.vty.verify('mncc-int', ['']))
148 self.assertEquals(self.vty.node(), 'config-mncc-int')
149 self.checkForEndAndExit()
150 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200151
152 if self.checkForSmpp():
153 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck5c46e932013-10-23 11:24:14 +0200154 self.assertTrue(self.vty.verify('smpp', ['']))
155 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbecka4235912013-10-29 09:30:31 +0100156 self.checkForEndAndExit()
Jacob Erlbeck5c46e932013-10-23 11:24:14 +0200157 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200158
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200159 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200160 self.assertTrue(self.vty.verify("exit", ['']))
161 self.assertTrue(self.vty.node() is None)
162
163 # Check searching for outer node's commands
164 self.vty.command("configure terminal")
165 self.vty.command('mncc-int')
Holger Hans Peter Freytherabb54ae2013-09-02 20:58:38 +0200166
167 if self.checkForSmpp():
168 self.vty.command('smpp')
169 self.assertEquals(self.vty.node(), 'config-smpp')
170 self.vty.command('mncc-int')
171
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200172 self.assertEquals(self.vty.node(), 'config-mncc-int')
173
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200174 def testEnableDisablePeriodicLU(self):
175 self.vty.enable()
176 self.vty.command("configure terminal")
177 self.vty.command("network")
178 self.vty.command("bts 0")
179
180 # Test invalid input
181 self.vty.verify("periodic location update 0", ['% Unknown command.'])
182 self.vty.verify("periodic location update 5", ['% Unknown command.'])
183 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
184
185 # Enable periodic lu..
186 self.vty.verify("periodic location update 60", [''])
187 res = self.vty.command("write terminal")
Holger Hans Peter Freythereda08672013-07-27 22:23:25 +0200188 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200189 self.assertEquals(res.find('no periodic location update'), -1)
190
191 # Now disable it..
192 self.vty.verify("no periodic location update", [''])
193 res = self.vty.command("write terminal")
194 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freythereda08672013-07-27 22:23:25 +0200195 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200196
Jacob Erlbeck8f8e5bf2014-01-16 11:02:14 +0100197 def testEnableDisableSiHacks(self):
198 self.vty.enable()
199 self.vty.command("configure terminal")
200 self.vty.command("network")
201 self.vty.command("bts 0")
202
203 # Enable periodic lu..
204 self.vty.verify("force-combined-si", [''])
205 res = self.vty.command("write terminal")
206 self.assert_(res.find(' force-combined-si') > 0)
207 self.assertEquals(res.find('no force-combined-si'), -1)
208
209 # Now disable it..
210 self.vty.verify("no force-combined-si", [''])
211 res = self.vty.command("write terminal")
212 self.assertEquals(res.find(' force-combined-si'), -1)
213 self.assert_(res.find('no force-combined-si') > 0)
214
Ivan Kluchnikov80d58432013-09-16 13:13:04 +0400215 def testRachAccessControlClass(self):
216 self.vty.enable()
217 self.vty.command("configure terminal")
218 self.vty.command("network")
219 self.vty.command("bts 0")
220
221 # Test invalid input
222 self.vty.verify("rach access-control-class", ['% Command incomplete.'])
223 self.vty.verify("rach access-control-class 1", ['% Command incomplete.'])
224 self.vty.verify("rach access-control-class -1", ['% Unknown command.'])
225 self.vty.verify("rach access-control-class 10", ['% Unknown command.'])
226 self.vty.verify("rach access-control-class 16", ['% Unknown command.'])
227
228 # Barred rach access control classes
229 for classNum in range(16):
230 if classNum != 10:
231 self.vty.verify("rach access-control-class " + str(classNum) + " barred", [''])
232
233 # Verify settings
234 res = self.vty.command("write terminal")
235 for classNum in range(16):
236 if classNum != 10:
237 self.assert_(res.find("rach access-control-class " + str(classNum) + " barred") > 0)
238
239 # Allowed rach access control classes
240 for classNum in range(16):
241 if classNum != 10:
242 self.vty.verify("rach access-control-class " + str(classNum) + " allowed", [''])
243
244 # Verify settings
245 res = self.vty.command("write terminal")
246 for classNum in range(16):
247 if classNum != 10:
248 self.assertEquals(res.find("rach access-control-class " + str(classNum) + " barred"), -1)
249
Alexander Chemerise092dec2013-10-04 23:54:17 +0200250 def testSubscriberCreate(self):
251 self.vty.enable()
252
253 imsi = "204300854013739"
254
255 # Initially we don't have this subscriber
256 self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi])
257
258 # Lets create one
259 res = self.vty.command('subscriber create imsi '+imsi)
260 self.assert_(res.find(" IMSI: "+imsi) > 0)
261
262 # Now we have it
263 res = self.vty.command('show subscriber imsi '+imsi)
264 self.assert_(res.find(" IMSI: "+imsi) > 0)
265
Holger Hans Peter Freyther35e6fbb2013-02-05 09:39:09 +0100266 def testShowPagingGroup(self):
267 res = self.vty.command("show paging-group 255 1234567")
268 self.assertEqual(res, "% can't find BTS 255")
269 res = self.vty.command("show paging-group 0 1234567")
270 self.assertEquals(res, "%Paging group for IMSI 1234567 on BTS #0 is 7")
271
Ciaby38abd7b2014-03-06 17:20:55 +0100272 def testShowNetwork(self):
273 res = self.vty.command("show network")
274 self.assert_(res.startswith('BSC is on Country Code') >= 0)
275
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200276class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200277
278 def vty_command(self):
279 return ["./src/osmo-bsc/osmo-bsc", "-c",
280 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
281
282 def vty_app(self):
283 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
284
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200285 def testConfigNetworkTree(self):
Jacob Erlbeck5c46e932013-10-23 11:24:14 +0200286 self._testConfigNetworkTree()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200287
288 def testVtyTree(self):
289 self.vty.enable()
290 self.assertTrue(self.vty.verify("configure terminal", ['']))
291 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +0100292 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200293 self.assertTrue(self.vty.verify("msc 0", ['']))
294 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200295 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200296 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200297 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200298 self.assertTrue(self.vty.verify("bsc", ['']))
299 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200300 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200301 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeckbf8eec72013-09-02 13:17:16 +0200302 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200303 self.assertTrue(self.vty.verify("exit", ['']))
304 self.assertTrue(self.vty.node() is None)
305
306 # Check searching for outer node's commands
307 self.vty.command("configure terminal")
308 self.vty.command('msc 0')
309 self.vty.command("bsc")
310 self.assertEquals(self.vty.node(), 'config-bsc')
311 self.vty.command("msc 0")
312 self.assertEquals(self.vty.node(), 'config-msc')
313
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200314 def testUssdNotificationsMsc(self):
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200315 self.vty.enable()
316 self.vty.command("configure terminal")
317 self.vty.command("msc")
318
319 # Test invalid input
320 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200321 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200322 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200323
324 # Enable USSD notifications
325 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200326 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200327 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200328
329 # Verify settings
330 res = self.vty.command("write terminal")
331 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
332 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200333 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
334 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200335 self.assert_(res.find('bsc-grace-text In grace period') > 0)
336 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200337
338 # Now disable it..
339 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200340 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200341 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200342
343 # Verify settings
344 res = self.vty.command("write terminal")
345 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
346 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeckb62092a2013-08-28 10:16:55 +0200347 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck3ccb86b2013-09-11 10:46:55 +0200348 self.assert_(res.find('no bsc-welcome-text') > 0)
349 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
350 self.assert_(res.find('no bsc-grace-text') > 0)
351
352 def testUssdNotificationsBsc(self):
353 self.vty.enable()
354 self.vty.command("configure terminal")
355 self.vty.command("bsc")
356
357 # Test invalid input
358 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
359
360 # Enable USSD notifications
361 self.vty.verify("missing-msc-text No MSC found", [''])
362
363 # Verify settings
364 res = self.vty.command("write terminal")
365 self.assert_(res.find('missing-msc-text No MSC found') > 0)
366 self.assertEquals(res.find('no missing-msc-text'), -1)
367
368 # Now disable it..
369 self.vty.verify("no missing-msc-text", [''])
370
371 # Verify settings
372 res = self.vty.command("write terminal")
373 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
374 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200375
Jacob Erlbeckcc0d8842013-09-17 13:59:29 +0200376 def testNetworkTimezone(self):
377 self.vty.enable()
378 self.vty.verify("configure terminal", [''])
379 self.vty.verify("network", [''])
380 self.vty.verify("bts 0", [''])
381
382 # Test invalid input
383 self.vty.verify("timezone", ['% Command incomplete.'])
384 self.vty.verify("timezone 20 0", ['% Unknown command.'])
385 self.vty.verify("timezone 0 11", ['% Unknown command.'])
386 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
387
388 # Set time zone without DST
389 self.vty.verify("timezone 2 30", [''])
390
391 # Verify settings
392 res = self.vty.command("write terminal")
393 self.assert_(res.find('timezone 2 30') > 0)
394 self.assertEquals(res.find('timezone 2 30 '), -1)
395
396 # Set time zone with DST
397 self.vty.verify("timezone 2 30 1", [''])
398
399 # Verify settings
400 res = self.vty.command("write terminal")
401 self.assert_(res.find('timezone 2 30 1') > 0)
402
403 # Now disable it..
404 self.vty.verify("no timezone", [''])
405
406 # Verify settings
407 res = self.vty.command("write terminal")
408 self.assertEquals(res.find(' timezone'), -1)
409
Ciaby38abd7b2014-03-06 17:20:55 +0100410 def testShowNetwork(self):
411 res = self.vty.command("show network")
412 self.assert_(res.startswith('BSC is on Country Code') >= 0)
413
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200414class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200415
416 def vty_command(self):
417 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
418 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
419
420 def vty_app(self):
421 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
422
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200423 def testVtyTree(self):
424 self.vty.enable()
425 self.assertTrue(self.vty.verify('configure terminal', ['']))
426 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +0100427 self.checkForEndAndExit()
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200428 self.assertTrue(self.vty.verify('mgcp', ['']))
429 self.assertEquals(self.vty.node(), 'config-mgcp')
430 self.checkForEndAndExit()
431 self.assertTrue(self.vty.verify('exit', ['']))
432 self.assertEquals(self.vty.node(), 'config')
433 self.assertTrue(self.vty.verify('nat', ['']))
434 self.assertEquals(self.vty.node(), 'config-nat')
435 self.checkForEndAndExit()
436 self.assertTrue(self.vty.verify('bsc 0', ['']))
437 self.assertEquals(self.vty.node(), 'config-nat-bsc')
438 self.checkForEndAndExit()
439 self.assertTrue(self.vty.verify('exit', ['']))
440 self.assertEquals(self.vty.node(), 'config-nat')
441 self.assertTrue(self.vty.verify('exit', ['']))
442 self.assertEquals(self.vty.node(), 'config')
443 self.assertTrue(self.vty.verify('exit', ['']))
444 self.assertTrue(self.vty.node() is None)
445
446 # Check searching for outer node's commands
447 self.vty.command('configure terminal')
448 self.vty.command('mgcp')
449 self.vty.command('nat')
450 self.assertEquals(self.vty.node(), 'config-nat')
Jacob Erlbeckcdf18572013-09-02 13:17:17 +0200451 self.vty.command('mgcp')
452 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200453 self.vty.command('nat')
454 self.assertEquals(self.vty.node(), 'config-nat')
455 self.vty.command('bsc 0')
Jacob Erlbeckcdf18572013-09-02 13:17:17 +0200456 self.vty.command('mgcp')
457 self.assertEquals(self.vty.node(), 'config-mgcp')
Jacob Erlbeck768a7c32013-09-02 13:17:14 +0200458
Holger Hans Peter Freytherbf123a12013-06-25 09:08:02 +0200459 def testRewriteNoRewrite(self):
460 self.vty.enable()
461 res = self.vty.command("configure terminal")
462 res = self.vty.command("nat")
463 res = self.vty.command("number-rewrite rewrite.cfg")
464 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200465
Holger Hans Peter Freythere9c7e272013-06-25 15:38:31 +0200466 def testRewritePostNoRewrite(self):
467 self.vty.enable()
468 self.vty.command("configure terminal")
469 self.vty.command("nat")
470 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
471 self.vty.verify("no number-rewrite-post", [''])
472
473
Holger Hans Peter Freyther367390b2013-06-25 11:44:01 +0200474 def testPrefixTreeLoading(self):
475 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
476
477 self.vty.enable()
478 self.vty.command("configure terminal")
479 self.vty.command("nat")
480 res = self.vty.command("prefix-tree %s" % cfg)
481 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
482 self.vty.command("end")
483
484 res = self.vty.command("show prefix-tree")
485 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')
486
487 self.vty.command("configure terminal")
488 self.vty.command("nat")
489 self.vty.command("no prefix-tree")
490 self.vty.command("end")
491
492 res = self.vty.command("show prefix-tree")
493 self.assertEqual(res, "% there is now prefix tree loaded.")
494
Jacob Erlbeck4684eb62013-08-14 11:10:34 +0200495 def testUssdSideChannelProvider(self):
496 self.vty.command("end")
497 self.vty.enable()
498 self.vty.command("configure terminal")
499 self.vty.command("nat")
500 self.vty.command("ussd-token key")
501 self.vty.command("end")
502
503 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
504 self.assertTrue(res)
505
506 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
507 ussdSocket.connect(('127.0.0.1', 5001))
508 ussdSocket.settimeout(2.0)
509 print "Connected to %s:%d" % ussdSocket.getpeername()
510
511 print "Expecting ID_GET request"
512 data = ussdSocket.recv(4)
513 self.assertEqual(data, "\x00\x01\xfe\x04")
514
515 print "Going to send ID_RESP response"
516 res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79")
517 self.assertEqual(res, 10)
518
519 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
520
521 print "Going to send PING request"
522 res = ussdSocket.send("\x00\x01\xfe\x00")
523 self.assertEqual(res, 4)
524
525 print "Expecting PONG response"
526 data = ussdSocket.recv(4)
527 self.assertEqual(data, "\x00\x01\xfe\x01")
528
529 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
530 self.assertTrue(res)
531
532 print "Going to shut down connection"
533 ussdSocket.shutdown(socket.SHUT_WR)
534
535 print "Expecting EOF"
536 data = ussdSocket.recv(4)
537 self.assertEqual(data, "")
538
539 ussdSocket.close()
540
541 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
542 self.assertTrue(res)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200543
Holger Hans Peter Freytherd8afce92014-01-20 10:14:05 +0100544 def testAccessList(self):
545 """
546 Verify that the imsi-deny can have a reject cause or no reject cause
547 """
548 self.vty.enable()
549 self.vty.command("configure terminal")
550 self.vty.command("nat")
551
552 # Old default
553 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
554 res = self.vty.command("show running-config").split("\r\n")
555 asserted = False
556 for line in res:
Holger Hans Peter Freyther842137a2014-03-04 15:38:00 +0100557 if line.startswith(" access-list test-default"):
Holger Hans Peter Freytherd8afce92014-01-20 10:14:05 +0100558 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
559 asserted = True
560 self.assert_(asserted)
561
562 # Check the optional CM Service Reject Cause
563 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
564 res = self.vty.command("show running-config").split("\r\n")
565 asserted = False
566 for line in res:
567 if line.startswith(" access-list test-cm"):
568 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
569 asserted = True
570 self.assert_(asserted)
571
572 # Check the optional LU Reject Cause
573 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
574 res = self.vty.command("show running-config").split("\r\n")
575 asserted = False
576 for line in res:
577 if line.startswith(" access-list test-lu"):
578 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
579 asserted = True
580 self.assert_(asserted)
581
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200582class TestVTYGbproxy(TestVTYGenericBSC):
583
584 def vty_command(self):
585 return ["./src/gprs/osmo-gbproxy", "-c",
586 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
587
588 def vty_app(self):
589 return (4246, "./src/gprs/osmo-gbproxy", "OsmoGbProxy", "bsc")
590
591 def testVtyTree(self):
592 self.vty.enable()
593 self.assertTrue(self.vty.verify('configure terminal', ['']))
594 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +0100595 self.checkForEndAndExit()
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200596 self.assertTrue(self.vty.verify('ns', ['']))
597 self.assertEquals(self.vty.node(), 'config-ns')
598 self.checkForEndAndExit()
599 self.assertTrue(self.vty.verify('exit', ['']))
600 self.assertEquals(self.vty.node(), 'config')
601 self.assertTrue(self.vty.verify('gbproxy', ['']))
602 self.assertEquals(self.vty.node(), 'config-gbproxy')
603 self.checkForEndAndExit()
604 self.assertTrue(self.vty.verify('exit', ['']))
605 self.assertEquals(self.vty.node(), 'config')
606
607 def testVtyShow(self):
608 res = self.vty.command("show ns")
609 self.assert_(res.find('Encapsulation NS-UDP-IP') >= 0)
610
611 res = self.vty.command("show gbproxy stats")
612 self.assert_(res.find('GBProxy Global Statistics') >= 0)
613
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200614 def testVtyDeletePeer(self):
615 self.vty.enable()
616 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['BVC not found']))
617 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
618 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
619 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
620 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
621 self.assert_(res.find('Not Deleted 0 BVC') >= 0)
622 self.assert_(res.find('Not Deleted 0 NS-VC') < 0)
623 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
624 self.assert_(res.find('Not Deleted 0 BVC') < 0)
625 self.assert_(res.find('Not Deleted 0 NS-VC') >= 0)
626 res = self.vty.command("delete-gbproxy-peer 9999 all")
627 self.assert_(res.find('Deleted 0 BVC') >= 0)
628 self.assert_(res.find('Deleted 0 NS-VC') >= 0)
629
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200630def add_nat_test(suite, workdir):
631 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
632 print("Skipping the NAT test")
633 return
634 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
635 suite.addTest(test)
636
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200637def add_bsc_test(suite, workdir):
638 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
639 print("Skipping the BSC test")
640 return
641 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
642 suite.addTest(test)
643
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200644def add_gbproxy_test(suite, workdir):
645 if not os.path.isfile(os.path.join(workdir, "src/gprs/osmo-gbproxy")):
646 print("Skipping the Gb-Proxy test")
647 return
648 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
649 suite.addTest(test)
650
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200651if __name__ == '__main__':
652 import argparse
653 import sys
654
655 workdir = '.'
656
657 parser = argparse.ArgumentParser()
658 parser.add_argument("-v", "--verbose", dest="verbose",
659 action="store_true", help="verbose mode")
660 parser.add_argument("-p", "--pythonconfpath", dest="p",
661 help="searchpath for config")
662 parser.add_argument("-w", "--workdir", dest="w",
663 help="Working directory")
664 args = parser.parse_args()
665
666 verbose_level = 1
667 if args.verbose:
668 verbose_level = 2
669
670 if args.w:
671 workdir = args.w
672
673 if args.p:
674 confpath = args.p
675
676 print "confpath %s, workdir %s" % (confpath, workdir)
677 os.chdir(workdir)
678 print "Running tests for specific VTY commands"
679 suite = unittest.TestSuite()
Holger Hans Peter Freytherb30b3aa2014-07-04 20:23:56 +0200680 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Holger Hans Peter Freyther2fb8ebf2013-07-27 21:07:57 +0200681 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Jacob Erlbeck058b1e52013-08-28 10:16:54 +0200682 add_bsc_test(suite, workdir)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200683 add_nat_test(suite, workdir)
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200684 add_gbproxy_test(suite, workdir)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200685 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
686 sys.exit(len(res.errors) + len(res.failures))