blob: 3ecfd39876a6af6971ea9966f53372736f1d61b6 [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
Max3e676892016-11-16 14:55:21 +010018import os, sys
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020019import time
20import unittest
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020021import socket
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +010022import subprocess
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020023
24import osmopy.obscvty as obscvty
25import osmopy.osmoutil as osmoutil
26
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010027# add $top_srcdir/contrib to find ipa.py
28sys.path.append(os.path.join(sys.path[0], '..', 'contrib'))
29
Max3e676892016-11-16 14:55:21 +010030from ipa import IPA
31
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010032# to be able to find $top_srcdir/doc/...
33confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020034
35class TestVTYBase(unittest.TestCase):
36
Neels Hofmeyrc29505e2016-05-20 21:59:55 +020037 def checkForEndAndExit(self):
38 res = self.vty.command("list")
39 #print ('looking for "exit"\n')
40 self.assert_(res.find(' exit\r') > 0)
41 #print 'found "exit"\nlooking for "end"\n'
42 self.assert_(res.find(' end\r') > 0)
43 #print 'found "end"\n'
44
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020045 def vty_command(self):
46 raise Exception("Needs to be implemented by a subclass")
47
48 def vty_app(self):
49 raise Exception("Needs to be implemented by a subclass")
50
51 def setUp(self):
52 osmo_vty_cmd = self.vty_command()[:]
53 config_index = osmo_vty_cmd.index('-c')
54 if config_index:
55 cfi = config_index + 1
56 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
57
58 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020059 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
60 except OSError:
61 print >> sys.stderr, "Current directory: %s" % os.getcwd()
62 print >> sys.stderr, "Consider setting -b"
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020063
64 appstring = self.vty_app()[2]
65 appport = self.vty_app()[0]
66 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
67
68 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010069 if self.vty:
70 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020071 self.vty = None
72 osmoutil.end_proc(self.proc)
73
Jacob Erlbeck96903c42013-09-02 13:17:14 +020074
75class TestVTYGenericBSC(TestVTYBase):
76
Neels Hofmeyrc29505e2016-05-20 21:59:55 +020077 def _testConfigNetworkTree(self, include_bsc_items=True):
Jacob Erlbeck96903c42013-09-02 13:17:14 +020078 self.vty.enable()
79 self.assertTrue(self.vty.verify("configure terminal",['']))
80 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +010081 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +020082 self.assertTrue(self.vty.verify("network",['']))
83 self.assertEquals(self.vty.node(), 'config-net')
84 self.checkForEndAndExit()
85 self.assertTrue(self.vty.verify("bts 0",['']))
86 self.assertEquals(self.vty.node(), 'config-net-bts')
87 self.checkForEndAndExit()
88 self.assertTrue(self.vty.verify("trx 0",['']))
89 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
90 self.checkForEndAndExit()
Jacob Erlbeck733bec82013-09-11 10:46:56 +020091 self.vty.command("write terminal")
92 self.assertTrue(self.vty.verify("exit",['']))
93 self.assertEquals(self.vty.node(), 'config-net-bts')
94 self.assertTrue(self.vty.verify("exit",['']))
95 self.assertTrue(self.vty.verify("bts 1",['']))
96 self.assertEquals(self.vty.node(), 'config-net-bts')
97 self.checkForEndAndExit()
98 self.assertTrue(self.vty.verify("trx 1",['']))
99 self.assertEquals(self.vty.node(), 'config-net-bts-trx')
100 self.checkForEndAndExit()
101 self.vty.command("write terminal")
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200102 self.assertTrue(self.vty.verify("exit",['']))
103 self.assertEquals(self.vty.node(), 'config-net-bts')
104 self.assertTrue(self.vty.verify("exit",['']))
105 self.assertEquals(self.vty.node(), 'config-net')
106 self.assertTrue(self.vty.verify("exit",['']))
107 self.assertEquals(self.vty.node(), 'config')
108 self.assertTrue(self.vty.verify("exit",['']))
109 self.assertTrue(self.vty.node() is None)
110
Holger Hans Peter Freyther86573262015-01-31 09:47:37 +0100111
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200112class TestVTYBSC(TestVTYGenericBSC):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200113
114 def vty_command(self):
115 return ["./src/osmo-bsc/osmo-bsc", "-c",
116 "doc/examples/osmo-bsc/osmo-bsc.cfg"]
117
118 def vty_app(self):
119 return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc")
120
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200121 def testConfigNetworkTree(self):
Jacob Erlbeck75877272013-10-23 11:24:14 +0200122 self._testConfigNetworkTree()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200123
124 def testVtyTree(self):
125 self.vty.enable()
126 self.assertTrue(self.vty.verify("configure terminal", ['']))
127 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100128 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200129 self.assertTrue(self.vty.verify("msc 0", ['']))
130 self.assertEquals(self.vty.node(), 'config-msc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200131 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200132 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200133 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200134 self.assertTrue(self.vty.verify("bsc", ['']))
135 self.assertEquals(self.vty.node(), 'config-bsc')
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200136 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200137 self.assertTrue(self.vty.verify("exit", ['']))
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200138 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200139 self.assertTrue(self.vty.verify("exit", ['']))
140 self.assertTrue(self.vty.node() is None)
141
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200142 def testUssdNotificationsMsc(self):
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200143 self.vty.enable()
144 self.vty.command("configure terminal")
145 self.vty.command("msc")
146
147 # Test invalid input
148 self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.'])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200149 self.vty.verify("bsc-welcome-text", ['% Command incomplete.'])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200150 self.vty.verify("bsc-grace-text", ['% Command incomplete.'])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200151
152 # Enable USSD notifications
153 self.vty.verify("bsc-msc-lost-text MSC disconnected", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200154 self.vty.verify("bsc-welcome-text Hello MS", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200155 self.vty.verify("bsc-grace-text In grace period", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200156
157 # Verify settings
158 res = self.vty.command("write terminal")
159 self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0)
160 self.assertEquals(res.find('no bsc-msc-lost-text'), -1)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200161 self.assert_(res.find('bsc-welcome-text Hello MS') > 0)
162 self.assertEquals(res.find('no bsc-welcome-text'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200163 self.assert_(res.find('bsc-grace-text In grace period') > 0)
164 self.assertEquals(res.find('no bsc-grace-text'), -1)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200165
166 # Now disable it..
167 self.vty.verify("no bsc-msc-lost-text", [''])
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200168 self.vty.verify("no bsc-welcome-text", [''])
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200169 self.vty.verify("no bsc-grace-text", [''])
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200170
171 # Verify settings
172 res = self.vty.command("write terminal")
173 self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1)
174 self.assert_(res.find('no bsc-msc-lost-text') > 0)
Jacob Erlbeck97e139f2013-08-28 10:16:55 +0200175 self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1)
Jacob Erlbeck56595f82013-09-11 10:46:55 +0200176 self.assert_(res.find('no bsc-welcome-text') > 0)
177 self.assertEquals(res.find('bsc-grace-text In grace period'), -1)
178 self.assert_(res.find('no bsc-grace-text') > 0)
179
180 def testUssdNotificationsBsc(self):
181 self.vty.enable()
182 self.vty.command("configure terminal")
183 self.vty.command("bsc")
184
185 # Test invalid input
186 self.vty.verify("missing-msc-text", ['% Command incomplete.'])
187
188 # Enable USSD notifications
189 self.vty.verify("missing-msc-text No MSC found", [''])
190
191 # Verify settings
192 res = self.vty.command("write terminal")
193 self.assert_(res.find('missing-msc-text No MSC found') > 0)
194 self.assertEquals(res.find('no missing-msc-text'), -1)
195
196 # Now disable it..
197 self.vty.verify("no missing-msc-text", [''])
198
199 # Verify settings
200 res = self.vty.command("write terminal")
201 self.assertEquals(res.find('missing-msc-text No MSC found'), -1)
202 self.assert_(res.find('no missing-msc-text') > 0)
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200203
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200204 def testNetworkTimezone(self):
205 self.vty.enable()
206 self.vty.verify("configure terminal", [''])
207 self.vty.verify("network", [''])
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200208
209 # Test invalid input
210 self.vty.verify("timezone", ['% Command incomplete.'])
211 self.vty.verify("timezone 20 0", ['% Unknown command.'])
212 self.vty.verify("timezone 0 11", ['% Unknown command.'])
213 self.vty.verify("timezone 0 0 99", ['% Unknown command.'])
214
215 # Set time zone without DST
216 self.vty.verify("timezone 2 30", [''])
217
218 # Verify settings
219 res = self.vty.command("write terminal")
220 self.assert_(res.find('timezone 2 30') > 0)
221 self.assertEquals(res.find('timezone 2 30 '), -1)
222
223 # Set time zone with DST
224 self.vty.verify("timezone 2 30 1", [''])
225
226 # Verify settings
227 res = self.vty.command("write terminal")
228 self.assert_(res.find('timezone 2 30 1') > 0)
229
230 # Now disable it..
231 self.vty.verify("no timezone", [''])
232
233 # Verify settings
234 res = self.vty.command("write terminal")
235 self.assertEquals(res.find(' timezone'), -1)
236
Ciabyec6e4f82014-03-06 17:20:55 +0100237 def testShowNetwork(self):
238 res = self.vty.command("show network")
239 self.assert_(res.startswith('BSC is on Country Code') >= 0)
240
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100241 def testPingPongConfiguration(self):
242 self.vty.enable()
243 self.vty.verify("configure terminal", [''])
Holger Hans Peter Freytherdb64f2e2014-10-29 10:06:15 +0100244 self.vty.verify("msc 0", [''])
245
246 self.vty.verify("timeout-ping 12", [''])
247 self.vty.verify("timeout-pong 14", [''])
248 res = self.vty.command("show running-config")
249 self.assert_(res.find(" timeout-ping 12") > 0)
250 self.assert_(res.find(" timeout-pong 14") > 0)
251 self.assert_(res.find(" no timeout-ping advanced") > 0)
252
253 self.vty.verify("timeout-ping advanced", [''])
254 res = self.vty.command("show running-config")
255 self.assert_(res.find(" timeout-ping 12") > 0)
256 self.assert_(res.find(" timeout-pong 14") > 0)
257 self.assert_(res.find(" timeout-ping advanced") > 0)
258
259 self.vty.verify("no timeout-ping advanced", [''])
260 res = self.vty.command("show running-config")
261 self.assert_(res.find(" timeout-ping 12") > 0)
262 self.assert_(res.find(" timeout-pong 14") > 0)
263 self.assert_(res.find(" no timeout-ping advanced") > 0)
264
265 self.vty.verify("no timeout-ping", [''])
266 res = self.vty.command("show running-config")
267 self.assertEquals(res.find(" timeout-ping 12"), -1)
268 self.assertEquals(res.find(" timeout-pong 14"), -1)
269 self.assertEquals(res.find(" no timeout-ping advanced"), -1)
270 self.assert_(res.find(" no timeout-ping") > 0)
271
272 self.vty.verify("timeout-ping advanced", ['%ping handling is disabled. Enable it first.'])
273
274 # And back to enabling it
275 self.vty.verify("timeout-ping 12", [''])
276 self.vty.verify("timeout-pong 14", [''])
277 res = self.vty.command("show running-config")
278 self.assert_(res.find(" timeout-ping 12") > 0)
279 self.assert_(res.find(" timeout-pong 14") > 0)
280 self.assert_(res.find(" timeout-ping advanced") > 0)
281
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200282 def testMscDataCoreLACCI(self):
283 self.vty.enable()
284 res = self.vty.command("show running-config")
285 self.assertEquals(res.find("core-location-area-code"), -1)
286 self.assertEquals(res.find("core-cell-identity"), -1)
287
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200288 self.vty.command("configure terminal")
Holger Hans Peter Freyther32dd2f32015-04-01 18:15:48 +0200289 self.vty.command("msc 0")
290 self.vty.command("core-location-area-code 666")
291 self.vty.command("core-cell-identity 333")
292
293 res = self.vty.command("show running-config")
294 self.assert_(res.find("core-location-area-code 666") > 0)
295 self.assert_(res.find("core-cell-identity 333") > 0)
296
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200297class TestVTYNAT(TestVTYGenericBSC):
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200298
299 def vty_command(self):
Max49364482016-04-13 11:36:39 +0200300 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-l", "127.0.0.1", "-c",
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200301 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
302
303 def vty_app(self):
304 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
305
Max49364482016-04-13 11:36:39 +0200306 def testBSCreload(self):
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400307 # Use different port for the mock msc to avoid clashing with
308 # the osmo-bsc_nat itself
Holger Hans Peter Freytherf1a61bb2016-04-14 08:50:25 -0400309 ip = "127.0.0.1"
Holger Hans Peter Freythere98c9c72016-04-14 10:58:58 -0400310 port = 5522
Max49364482016-04-13 11:36:39 +0200311 self.vty.enable()
312 bscs1 = self.vty.command("show bscs-config")
313 nat_bsc_reload(self)
314 bscs2 = self.vty.command("show bscs-config")
315 # check that multiple calls to bscs-config-file give the same result
316 self.assertEquals(bscs1, bscs2)
317
318 # add new bsc
319 self.vty.command("configure terminal")
320 self.vty.command("nat")
321 self.vty.command("bsc 5")
322 self.vty.command("token key")
323 self.vty.command("location_area_code 666")
324 self.vty.command("end")
325
326 # update bsc token
327 self.vty.command("configure terminal")
328 self.vty.command("nat")
329 self.vty.command("bsc 1")
330 self.vty.command("token xyu")
331 self.vty.command("end")
332
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400333 nat_msc_ip(self, ip, port)
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100334 msc_socket, msc = nat_msc_test(self, ip, port, verbose=True)
335 try:
336 b0 = nat_bsc_sock_test(0, "lol", verbose=True, proc=self.proc)
337 b1 = nat_bsc_sock_test(1, "xyu", verbose=True, proc=self.proc)
338 b2 = nat_bsc_sock_test(5, "key", verbose=True, proc=self.proc)
Max49364482016-04-13 11:36:39 +0200339
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100340 self.assertEquals("3 BSCs configured", self.vty.command("show nat num-bscs-configured"))
341 self.assertTrue(3 == nat_bsc_num_con(self))
342 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
Max49364482016-04-13 11:36:39 +0200343
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100344 nat_bsc_reload(self)
345 bscs2 = self.vty.command("show bscs-config")
346 # check that the reset to initial config succeeded
347 self.assertEquals(bscs1, bscs2)
Max49364482016-04-13 11:36:39 +0200348
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100349 self.assertEquals("2 BSCs configured", self.vty.command("show nat num-bscs-configured"))
350 self.assertTrue(1 == nat_bsc_num_con(self))
351 rem = self.vty.command("show bsc connections").split(' ')
352 # remaining connection is for BSC0
353 self.assertEquals('0', rem[2])
354 # remaining connection is authorized
355 self.assertEquals('1', rem[4])
356 self.assertEquals("MSC is connected: 1", self.vty.command("show msc connection"))
357 finally:
358 msc.close()
359 msc_socket.close()
Max49364482016-04-13 11:36:39 +0200360
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200361 def testVtyTree(self):
362 self.vty.enable()
363 self.assertTrue(self.vty.verify('configure terminal', ['']))
364 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100365 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200366 self.assertTrue(self.vty.verify('mgcp', ['']))
367 self.assertEquals(self.vty.node(), 'config-mgcp')
368 self.checkForEndAndExit()
369 self.assertTrue(self.vty.verify('exit', ['']))
370 self.assertEquals(self.vty.node(), 'config')
371 self.assertTrue(self.vty.verify('nat', ['']))
372 self.assertEquals(self.vty.node(), 'config-nat')
373 self.checkForEndAndExit()
374 self.assertTrue(self.vty.verify('bsc 0', ['']))
375 self.assertEquals(self.vty.node(), 'config-nat-bsc')
376 self.checkForEndAndExit()
377 self.assertTrue(self.vty.verify('exit', ['']))
378 self.assertEquals(self.vty.node(), 'config-nat')
379 self.assertTrue(self.vty.verify('exit', ['']))
380 self.assertEquals(self.vty.node(), 'config')
381 self.assertTrue(self.vty.verify('exit', ['']))
382 self.assertTrue(self.vty.node() is None)
383
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +0200384 def testRewriteNoRewrite(self):
385 self.vty.enable()
386 res = self.vty.command("configure terminal")
387 res = self.vty.command("nat")
388 res = self.vty.command("number-rewrite rewrite.cfg")
389 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200390
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400391 def testEnsureNoEnsureModeSet(self):
392 self.vty.enable()
393 res = self.vty.command("configure terminal")
394 res = self.vty.command("nat")
395
396 # Ensure the default
397 res = self.vty.command("show running-config")
398 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
399
400 self.vty.command("sdp-ensure-amr-mode-set")
401 res = self.vty.command("show running-config")
402 self.assert_(res.find('\n sdp-ensure-amr-mode-set') > 0)
403
404 self.vty.command("no sdp-ensure-amr-mode-set")
405 res = self.vty.command("show running-config")
406 self.assert_(res.find('\n no sdp-ensure-amr-mode-set') > 0)
407
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200408 def testRewritePostNoRewrite(self):
409 self.vty.enable()
410 self.vty.command("configure terminal")
411 self.vty.command("nat")
412 self.vty.verify("number-rewrite-post rewrite.cfg", [''])
413 self.vty.verify("no number-rewrite-post", [''])
414
415
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200416 def testPrefixTreeLoading(self):
417 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
418
419 self.vty.enable()
420 self.vty.command("configure terminal")
421 self.vty.command("nat")
422 res = self.vty.command("prefix-tree %s" % cfg)
423 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
424 self.vty.command("end")
425
426 res = self.vty.command("show prefix-tree")
427 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')
428
429 self.vty.command("configure terminal")
430 self.vty.command("nat")
431 self.vty.command("no prefix-tree")
432 self.vty.command("end")
433
434 res = self.vty.command("show prefix-tree")
435 self.assertEqual(res, "% there is now prefix tree loaded.")
436
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200437 def testUssdSideChannelProvider(self):
438 self.vty.command("end")
439 self.vty.enable()
440 self.vty.command("configure terminal")
441 self.vty.command("nat")
442 self.vty.command("ussd-token key")
443 self.vty.command("end")
444
445 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
446 self.assertTrue(res)
447
448 ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
449 ussdSocket.connect(('127.0.0.1', 5001))
450 ussdSocket.settimeout(2.0)
451 print "Connected to %s:%d" % ussdSocket.getpeername()
452
453 print "Expecting ID_GET request"
454 data = ussdSocket.recv(4)
455 self.assertEqual(data, "\x00\x01\xfe\x04")
456
457 print "Going to send ID_RESP response"
Max3e676892016-11-16 14:55:21 +0100458 res = ussdSocket.send(IPA().id_resp(IPA().tag_name('key')))
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200459 self.assertEqual(res, 10)
460
461 # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
462
463 print "Going to send PING request"
Max3e676892016-11-16 14:55:21 +0100464 res = ussdSocket.send(IPA().ping())
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200465 self.assertEqual(res, 4)
466
467 print "Expecting PONG response"
468 data = ussdSocket.recv(4)
469 self.assertEqual(data, "\x00\x01\xfe\x01")
470
471 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
472 self.assertTrue(res)
473
474 print "Going to shut down connection"
475 ussdSocket.shutdown(socket.SHUT_WR)
476
477 print "Expecting EOF"
478 data = ussdSocket.recv(4)
479 self.assertEqual(data, "")
480
481 ussdSocket.close()
482
483 res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.'])
484 self.assertTrue(res)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200485
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100486 def testAccessList(self):
487 """
488 Verify that the imsi-deny can have a reject cause or no reject cause
489 """
490 self.vty.enable()
491 self.vty.command("configure terminal")
492 self.vty.command("nat")
493
494 # Old default
495 self.vty.command("access-list test-default imsi-deny ^123[0-9]*$")
496 res = self.vty.command("show running-config").split("\r\n")
497 asserted = False
498 for line in res:
Holger Hans Peter Freyther4ecc6872014-03-04 15:38:00 +0100499 if line.startswith(" access-list test-default"):
Holger Hans Peter Freyther64190182014-01-20 10:14:05 +0100500 self.assertEqual(line, " access-list test-default imsi-deny ^123[0-9]*$ 11 11")
501 asserted = True
502 self.assert_(asserted)
503
504 # Check the optional CM Service Reject Cause
505 self.vty.command("access-list test-cm-deny imsi-deny ^123[0-9]*$ 42").split("\r\n")
506 res = self.vty.command("show running-config").split("\r\n")
507 asserted = False
508 for line in res:
509 if line.startswith(" access-list test-cm"):
510 self.assertEqual(line, " access-list test-cm-deny imsi-deny ^123[0-9]*$ 42 11")
511 asserted = True
512 self.assert_(asserted)
513
514 # Check the optional LU Reject Cause
515 self.vty.command("access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42").split("\r\n")
516 res = self.vty.command("show running-config").split("\r\n")
517 asserted = False
518 for line in res:
519 if line.startswith(" access-list test-lu"):
520 self.assertEqual(line, " access-list test-lu-deny imsi-deny ^123[0-9]*$ 23 42")
521 asserted = True
522 self.assert_(asserted)
523
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +0200524
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200525def add_nat_test(suite, workdir):
526 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
527 print("Skipping the NAT test")
528 return
529 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
530 suite.addTest(test)
531
Max49364482016-04-13 11:36:39 +0200532def nat_bsc_reload(x):
533 x.vty.command("configure terminal")
534 x.vty.command("nat")
Neels Hofmeyr5809e1e2017-07-20 17:57:37 +0200535 x.vty.command("bscs-config-file bscs.cfg")
Max49364482016-04-13 11:36:39 +0200536 x.vty.command("end")
537
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400538def nat_msc_ip(x, ip, port):
Max49364482016-04-13 11:36:39 +0200539 x.vty.command("configure terminal")
540 x.vty.command("nat")
541 x.vty.command("msc ip " + ip)
Holger Hans Peter Freyther84ae27e2016-04-14 10:40:06 -0400542 x.vty.command("msc port " + str(port))
Max49364482016-04-13 11:36:39 +0200543 x.vty.command("end")
544
545def data2str(d):
Holger Hans Peter Freyther8bb62042016-04-14 21:40:04 -0400546 return d.encode('hex').lower()
Max49364482016-04-13 11:36:39 +0200547
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400548def nat_msc_test(x, ip, port, verbose = False):
Max49364482016-04-13 11:36:39 +0200549 msc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Neels Hofmeyr53403452017-02-28 02:38:43 +0100550 msc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +0200551 msc.settimeout(5)
Holger Hans Peter Freyther44ed4972016-04-14 10:05:13 -0400552 msc.bind((ip, port))
Max49364482016-04-13 11:36:39 +0200553 msc.listen(5)
554 if (verbose):
555 print "MSC is ready at " + ip
Neels Hofmeyr89d20b62016-09-26 12:59:36 +0200556 conn = None
Neels Hofmeyrcaeb62d2016-09-28 23:38:45 +0200557 while True:
558 vty_response = x.vty.command("show msc connection")
559 print "'show msc connection' says: %r" % vty_response
560 if vty_response == "MSC is connected: 1":
561 # success
562 break;
563 if vty_response != "MSC is connected: 0":
564 raise Exception("Unexpected response to 'show msc connection'"
565 " vty command: %r" % vty_response)
566
567 timeout_retries = 6
568 while timeout_retries > 0:
569 try:
570 conn, addr = msc.accept()
571 print "MSC got connection from ", addr
572 break
573 except socket.timeout:
574 print "socket timed out."
575 timeout_retries -= 1
576 continue
577
Neels Hofmeyr89d20b62016-09-26 12:59:36 +0200578 if not conn:
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200579 raise Exception("VTY reports MSC is connected, but I haven't"
580 " connected yet: %r %r" % (ip, port))
Neels Hofmeyr0b619322017-02-28 02:37:39 +0100581 return msc, conn
Max49364482016-04-13 11:36:39 +0200582
583def ipa_handle_small(x, verbose = False):
584 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +0100585 if len(s) != 4*2:
586 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +0200587 if "0001fe00" == s:
588 if (verbose):
589 print "\tBSC <- NAT: PING?"
Max3e676892016-11-16 14:55:21 +0100590 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +0200591 elif "0001fe06" == s:
592 if (verbose):
593 print "\tBSC <- NAT: IPA ID ACK"
Max3e676892016-11-16 14:55:21 +0100594 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +0200595 elif "0001fe00" == s:
596 if (verbose):
597 print "\tBSC <- NAT: PONG!"
598 else:
599 if (verbose):
600 print "\tBSC <- NAT: ", s
601
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100602def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200603 s = data2str(x.recv(38))
604 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100605 retries = 3
606 while True:
607 print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
608 try:
609 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
610 print "\tdone sending IPA identity(%s) at %s" % (tk,
611 time.strftime("%T"))
612 break
613 except:
614 print "\tfailed sending IPA identity at", time.strftime("%T")
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100615 if proc:
616 print "\tproc.poll() = %r" % proc.poll()
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100617 if retries < 1:
618 print "\tgiving up"
619 raise
Neels Hofmeyre02e1e72017-02-03 05:55:26 +0100620 print "\tretrying (%d attempts left)" % retries
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100621 retries -= 1
Max49364482016-04-13 11:36:39 +0200622 else:
623 if (verbose):
624 print "\tBSC <- NAT: ", s
625
626def nat_bsc_num_con(x):
627 return len(x.vty.command("show bsc connections").split('\n'))
628
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100629def nat_bsc_sock_test(nr, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200630 bsc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Holger Hans Peter Freyther2abf2b02016-04-14 21:13:51 -0400631 bsc.bind(('127.0.0.1', 0))
Max49364482016-04-13 11:36:39 +0200632 bsc.connect(('127.0.0.1', 5000))
633 if (verbose):
634 print "BSC%d " %nr
635 print "\tconnected to %s:%d" % bsc.getpeername()
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100636 if proc:
637 print "\tproc.poll() = %r" % proc.poll()
638 print "\tproc.pid = %r" % proc.pid
Max49364482016-04-13 11:36:39 +0200639 ipa_handle_small(bsc, verbose)
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100640 ipa_handle_resp(bsc, tk, verbose, proc=proc)
641 if proc:
642 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +0200643 bsc.recv(27) # MGCP msg
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100644 if proc:
645 print "\tproc.poll() = %r" % proc.poll()
Max49364482016-04-13 11:36:39 +0200646 ipa_handle_small(bsc, verbose)
647 return bsc
648
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200649def add_bsc_test(suite, workdir):
650 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")):
651 print("Skipping the BSC test")
652 return
653 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC)
654 suite.addTest(test)
655
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200656if __name__ == '__main__':
657 import argparse
658 import sys
659
660 workdir = '.'
661
662 parser = argparse.ArgumentParser()
663 parser.add_argument("-v", "--verbose", dest="verbose",
664 action="store_true", help="verbose mode")
665 parser.add_argument("-p", "--pythonconfpath", dest="p",
666 help="searchpath for config")
667 parser.add_argument("-w", "--workdir", dest="w",
668 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100669 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200670 args = parser.parse_args()
671
672 verbose_level = 1
673 if args.verbose:
674 verbose_level = 2
675
676 if args.w:
677 workdir = args.w
678
679 if args.p:
680 confpath = args.p
681
682 print "confpath %s, workdir %s" % (confpath, workdir)
683 os.chdir(workdir)
684 print "Running tests for specific VTY commands"
685 suite = unittest.TestSuite()
Jacob Erlbeck1b894022013-08-28 10:16:54 +0200686 add_bsc_test(suite, workdir)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200687 add_nat_test(suite, workdir)
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100688
689 if args.test_name:
690 osmoutil.pick_tests(suite, *args.test_name)
691
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +0200692 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200693 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +0200694
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200695# vim: shiftwidth=4 expandtab nocin ai