Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 1 | #!/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 | |
| 18 | import os |
| 19 | import time |
| 20 | import unittest |
Jacob Erlbeck | 6cb2ccc | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 21 | import socket |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 22 | |
| 23 | import osmopy.obscvty as obscvty |
| 24 | import osmopy.osmoutil as osmoutil |
| 25 | |
| 26 | confpath = '.' |
| 27 | |
| 28 | class 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 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 59 | |
| 60 | class TestVTYGenericBSC(TestVTYBase): |
| 61 | |
| 62 | def checkForEndAndExit(self): |
| 63 | res = self.vty.command("list") |
| 64 | #print ('looking for "exit"\n') |
| 65 | self.assert_(res.find(' exit\r') > 0) |
| 66 | #print 'found "exit"\nlooking for "end"\n' |
| 67 | self.assert_(res.find(' end\r') > 0) |
| 68 | #print 'found "end"\n' |
| 69 | |
| 70 | def ignoredCheckForEndAndExit(self): |
| 71 | sys.stderr.write('Going to ignore the next assertion(s) due to known bugs\n') |
| 72 | try: |
| 73 | self.checkForEndAndExit() |
| 74 | except BaseException as e: |
| 75 | sys.stderr.write('Expected and ignored failure: %s\n' % (str(e))) |
| 76 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 77 | def _testConfigNetworkTree(self): |
| 78 | self.vty.enable() |
| 79 | self.assertTrue(self.vty.verify("configure terminal",[''])) |
| 80 | self.assertEquals(self.vty.node(), 'config') |
| 81 | self.ignoredCheckForEndAndExit() |
| 82 | 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 Erlbeck | 733bec8 | 2013-09-11 10:46:56 +0200 | [diff] [blame] | 91 | 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 Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 102 | 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 | |
| 111 | class TestVTYNITB(TestVTYGenericBSC): |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 112 | |
| 113 | def vty_command(self): |
| 114 | return ["./src/osmo-nitb/osmo-nitb", "-c", |
| 115 | "doc/examples/osmo-nitb/nanobts/openbsc.cfg"] |
| 116 | |
| 117 | def vty_app(self): |
| 118 | return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb") |
| 119 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 120 | def testConfigNetworkTree(self): |
| 121 | self._testConfigNetworkTree() |
| 122 | |
Holger Hans Peter Freyther | 0df1ab9 | 2013-09-02 20:58:38 +0200 | [diff] [blame] | 123 | def checkForSmpp(self): |
| 124 | """SMPP is not always enabled, check if it is""" |
| 125 | res = self.vty.command("list") |
| 126 | return "smpp" in res |
| 127 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 128 | def testVtyTree(self): |
| 129 | self.vty.enable() |
| 130 | self.assertTrue(self.vty.verify("configure terminal", [''])) |
| 131 | self.assertEquals(self.vty.node(), 'config') |
| 132 | self.ignoredCheckForEndAndExit() |
| 133 | self.assertTrue(self.vty.verify('mncc-int', [''])) |
| 134 | self.assertEquals(self.vty.node(), 'config-mncc-int') |
| 135 | self.checkForEndAndExit() |
| 136 | self.assertTrue(self.vty.verify('exit', [''])) |
Holger Hans Peter Freyther | 0df1ab9 | 2013-09-02 20:58:38 +0200 | [diff] [blame] | 137 | |
| 138 | if self.checkForSmpp(): |
| 139 | self.assertEquals(self.vty.node(), 'config') |
| 140 | self.assertTrue(self.vty.verify('smpp', [''])) |
| 141 | self.assertEquals(self.vty.node(), 'config-smpp') |
| 142 | self.ignoredCheckForEndAndExit() |
| 143 | self.assertTrue(self.vty.verify("exit", [''])) |
| 144 | |
Jacob Erlbeck | 0ae92a9 | 2013-09-02 13:17:16 +0200 | [diff] [blame] | 145 | self.assertEquals(self.vty.node(), 'config') |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 146 | self.assertTrue(self.vty.verify("exit", [''])) |
| 147 | self.assertTrue(self.vty.node() is None) |
| 148 | |
| 149 | # Check searching for outer node's commands |
| 150 | self.vty.command("configure terminal") |
| 151 | self.vty.command('mncc-int') |
Holger Hans Peter Freyther | 0df1ab9 | 2013-09-02 20:58:38 +0200 | [diff] [blame] | 152 | |
| 153 | if self.checkForSmpp(): |
| 154 | self.vty.command('smpp') |
| 155 | self.assertEquals(self.vty.node(), 'config-smpp') |
| 156 | self.vty.command('mncc-int') |
| 157 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 158 | self.assertEquals(self.vty.node(), 'config-mncc-int') |
| 159 | |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 160 | def testEnableDisablePeriodicLU(self): |
| 161 | self.vty.enable() |
| 162 | self.vty.command("configure terminal") |
| 163 | self.vty.command("network") |
| 164 | self.vty.command("bts 0") |
| 165 | |
| 166 | # Test invalid input |
| 167 | self.vty.verify("periodic location update 0", ['% Unknown command.']) |
| 168 | self.vty.verify("periodic location update 5", ['% Unknown command.']) |
| 169 | self.vty.verify("periodic location update 1531", ['% Unknown command.']) |
| 170 | |
| 171 | # Enable periodic lu.. |
| 172 | self.vty.verify("periodic location update 60", ['']) |
| 173 | res = self.vty.command("write terminal") |
Holger Hans Peter Freyther | c0438e3 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 174 | self.assert_(res.find('periodic location update 60') > 0) |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 175 | self.assertEquals(res.find('no periodic location update'), -1) |
| 176 | |
| 177 | # Now disable it.. |
| 178 | self.vty.verify("no periodic location update", ['']) |
| 179 | res = self.vty.command("write terminal") |
| 180 | self.assertEquals(res.find('periodic location update 60'), -1) |
Holger Hans Peter Freyther | c0438e3 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 181 | self.assert_(res.find('no periodic location update') > 0) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 182 | |
Ivan Kluchnikov | 6792059 | 2013-09-16 13:13:04 +0400 | [diff] [blame] | 183 | def testRachAccessControlClass(self): |
| 184 | self.vty.enable() |
| 185 | self.vty.command("configure terminal") |
| 186 | self.vty.command("network") |
| 187 | self.vty.command("bts 0") |
| 188 | |
| 189 | # Test invalid input |
| 190 | self.vty.verify("rach access-control-class", ['% Command incomplete.']) |
| 191 | self.vty.verify("rach access-control-class 1", ['% Command incomplete.']) |
| 192 | self.vty.verify("rach access-control-class -1", ['% Unknown command.']) |
| 193 | self.vty.verify("rach access-control-class 10", ['% Unknown command.']) |
| 194 | self.vty.verify("rach access-control-class 16", ['% Unknown command.']) |
| 195 | |
| 196 | # Barred rach access control classes |
| 197 | for classNum in range(16): |
| 198 | if classNum != 10: |
| 199 | self.vty.verify("rach access-control-class " + str(classNum) + " barred", ['']) |
| 200 | |
| 201 | # Verify settings |
| 202 | res = self.vty.command("write terminal") |
| 203 | for classNum in range(16): |
| 204 | if classNum != 10: |
| 205 | self.assert_(res.find("rach access-control-class " + str(classNum) + " barred") > 0) |
| 206 | |
| 207 | # Allowed rach access control classes |
| 208 | for classNum in range(16): |
| 209 | if classNum != 10: |
| 210 | self.vty.verify("rach access-control-class " + str(classNum) + " allowed", ['']) |
| 211 | |
| 212 | # Verify settings |
| 213 | res = self.vty.command("write terminal") |
| 214 | for classNum in range(16): |
| 215 | if classNum != 10: |
| 216 | self.assertEquals(res.find("rach access-control-class " + str(classNum) + " barred"), -1) |
| 217 | |
Alexander Chemeris | bd6d40f | 2013-10-04 23:54:17 +0200 | [diff] [blame] | 218 | def testSubscriberCreate(self): |
| 219 | self.vty.enable() |
| 220 | |
| 221 | imsi = "204300854013739" |
| 222 | |
| 223 | # Initially we don't have this subscriber |
| 224 | self.vty.verify('show subscriber imsi '+imsi, ['% No subscriber found for imsi '+imsi]) |
| 225 | |
| 226 | # Lets create one |
| 227 | res = self.vty.command('subscriber create imsi '+imsi) |
| 228 | self.assert_(res.find(" IMSI: "+imsi) > 0) |
| 229 | |
| 230 | # Now we have it |
| 231 | res = self.vty.command('show subscriber imsi '+imsi) |
| 232 | self.assert_(res.find(" IMSI: "+imsi) > 0) |
| 233 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 234 | class TestVTYBSC(TestVTYGenericBSC): |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 235 | |
| 236 | def vty_command(self): |
| 237 | return ["./src/osmo-bsc/osmo-bsc", "-c", |
| 238 | "doc/examples/osmo-bsc/osmo-bsc.cfg"] |
| 239 | |
| 240 | def vty_app(self): |
| 241 | return (4242, "./src/osmo-bsc/osmo-bsc", "OsmoBSC", "bsc") |
| 242 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 243 | def testConfigNetworkTree(self): |
| 244 | self._testConfigNetworkTree() |
| 245 | |
| 246 | def testVtyTree(self): |
| 247 | self.vty.enable() |
| 248 | self.assertTrue(self.vty.verify("configure terminal", [''])) |
| 249 | self.assertEquals(self.vty.node(), 'config') |
| 250 | self.ignoredCheckForEndAndExit() |
| 251 | self.assertTrue(self.vty.verify("msc 0", [''])) |
| 252 | self.assertEquals(self.vty.node(), 'config-msc') |
Jacob Erlbeck | 0ae92a9 | 2013-09-02 13:17:16 +0200 | [diff] [blame] | 253 | self.checkForEndAndExit() |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 254 | self.assertTrue(self.vty.verify("exit", [''])) |
Jacob Erlbeck | 0ae92a9 | 2013-09-02 13:17:16 +0200 | [diff] [blame] | 255 | self.assertEquals(self.vty.node(), 'config') |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 256 | self.assertTrue(self.vty.verify("bsc", [''])) |
| 257 | self.assertEquals(self.vty.node(), 'config-bsc') |
Jacob Erlbeck | 0ae92a9 | 2013-09-02 13:17:16 +0200 | [diff] [blame] | 258 | self.checkForEndAndExit() |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 259 | self.assertTrue(self.vty.verify("exit", [''])) |
Jacob Erlbeck | 0ae92a9 | 2013-09-02 13:17:16 +0200 | [diff] [blame] | 260 | self.assertEquals(self.vty.node(), 'config') |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 261 | self.assertTrue(self.vty.verify("exit", [''])) |
| 262 | self.assertTrue(self.vty.node() is None) |
| 263 | |
| 264 | # Check searching for outer node's commands |
| 265 | self.vty.command("configure terminal") |
| 266 | self.vty.command('msc 0') |
| 267 | self.vty.command("bsc") |
| 268 | self.assertEquals(self.vty.node(), 'config-bsc') |
| 269 | self.vty.command("msc 0") |
| 270 | self.assertEquals(self.vty.node(), 'config-msc') |
| 271 | |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 272 | def testUssdNotificationsMsc(self): |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 273 | self.vty.enable() |
| 274 | self.vty.command("configure terminal") |
| 275 | self.vty.command("msc") |
| 276 | |
| 277 | # Test invalid input |
| 278 | self.vty.verify("bsc-msc-lost-text", ['% Command incomplete.']) |
Jacob Erlbeck | 97e139f | 2013-08-28 10:16:55 +0200 | [diff] [blame] | 279 | self.vty.verify("bsc-welcome-text", ['% Command incomplete.']) |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 280 | self.vty.verify("bsc-grace-text", ['% Command incomplete.']) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 281 | |
| 282 | # Enable USSD notifications |
| 283 | self.vty.verify("bsc-msc-lost-text MSC disconnected", ['']) |
Jacob Erlbeck | 97e139f | 2013-08-28 10:16:55 +0200 | [diff] [blame] | 284 | self.vty.verify("bsc-welcome-text Hello MS", ['']) |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 285 | self.vty.verify("bsc-grace-text In grace period", ['']) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 286 | |
| 287 | # Verify settings |
| 288 | res = self.vty.command("write terminal") |
| 289 | self.assert_(res.find('bsc-msc-lost-text MSC disconnected') > 0) |
| 290 | self.assertEquals(res.find('no bsc-msc-lost-text'), -1) |
Jacob Erlbeck | 97e139f | 2013-08-28 10:16:55 +0200 | [diff] [blame] | 291 | self.assert_(res.find('bsc-welcome-text Hello MS') > 0) |
| 292 | self.assertEquals(res.find('no bsc-welcome-text'), -1) |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 293 | self.assert_(res.find('bsc-grace-text In grace period') > 0) |
| 294 | self.assertEquals(res.find('no bsc-grace-text'), -1) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 295 | |
| 296 | # Now disable it.. |
| 297 | self.vty.verify("no bsc-msc-lost-text", ['']) |
Jacob Erlbeck | 97e139f | 2013-08-28 10:16:55 +0200 | [diff] [blame] | 298 | self.vty.verify("no bsc-welcome-text", ['']) |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 299 | self.vty.verify("no bsc-grace-text", ['']) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 300 | |
| 301 | # Verify settings |
| 302 | res = self.vty.command("write terminal") |
| 303 | self.assertEquals(res.find('bsc-msc-lost-text MSC disconnected'), -1) |
| 304 | self.assert_(res.find('no bsc-msc-lost-text') > 0) |
Jacob Erlbeck | 97e139f | 2013-08-28 10:16:55 +0200 | [diff] [blame] | 305 | self.assertEquals(res.find('bsc-welcome-text Hello MS'), -1) |
Jacob Erlbeck | 56595f8 | 2013-09-11 10:46:55 +0200 | [diff] [blame] | 306 | self.assert_(res.find('no bsc-welcome-text') > 0) |
| 307 | self.assertEquals(res.find('bsc-grace-text In grace period'), -1) |
| 308 | self.assert_(res.find('no bsc-grace-text') > 0) |
| 309 | |
| 310 | def testUssdNotificationsBsc(self): |
| 311 | self.vty.enable() |
| 312 | self.vty.command("configure terminal") |
| 313 | self.vty.command("bsc") |
| 314 | |
| 315 | # Test invalid input |
| 316 | self.vty.verify("missing-msc-text", ['% Command incomplete.']) |
| 317 | |
| 318 | # Enable USSD notifications |
| 319 | self.vty.verify("missing-msc-text No MSC found", ['']) |
| 320 | |
| 321 | # Verify settings |
| 322 | res = self.vty.command("write terminal") |
| 323 | self.assert_(res.find('missing-msc-text No MSC found') > 0) |
| 324 | self.assertEquals(res.find('no missing-msc-text'), -1) |
| 325 | |
| 326 | # Now disable it.. |
| 327 | self.vty.verify("no missing-msc-text", ['']) |
| 328 | |
| 329 | # Verify settings |
| 330 | res = self.vty.command("write terminal") |
| 331 | self.assertEquals(res.find('missing-msc-text No MSC found'), -1) |
| 332 | self.assert_(res.find('no missing-msc-text') > 0) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 333 | |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 334 | def testNetworkTimezone(self): |
| 335 | self.vty.enable() |
| 336 | self.vty.verify("configure terminal", ['']) |
| 337 | self.vty.verify("network", ['']) |
| 338 | self.vty.verify("bts 0", ['']) |
| 339 | |
| 340 | # Test invalid input |
| 341 | self.vty.verify("timezone", ['% Command incomplete.']) |
| 342 | self.vty.verify("timezone 20 0", ['% Unknown command.']) |
| 343 | self.vty.verify("timezone 0 11", ['% Unknown command.']) |
| 344 | self.vty.verify("timezone 0 0 99", ['% Unknown command.']) |
| 345 | |
| 346 | # Set time zone without DST |
| 347 | self.vty.verify("timezone 2 30", ['']) |
| 348 | |
| 349 | # Verify settings |
| 350 | res = self.vty.command("write terminal") |
| 351 | self.assert_(res.find('timezone 2 30') > 0) |
| 352 | self.assertEquals(res.find('timezone 2 30 '), -1) |
| 353 | |
| 354 | # Set time zone with DST |
| 355 | self.vty.verify("timezone 2 30 1", ['']) |
| 356 | |
| 357 | # Verify settings |
| 358 | res = self.vty.command("write terminal") |
| 359 | self.assert_(res.find('timezone 2 30 1') > 0) |
| 360 | |
| 361 | # Now disable it.. |
| 362 | self.vty.verify("no timezone", ['']) |
| 363 | |
| 364 | # Verify settings |
| 365 | res = self.vty.command("write terminal") |
| 366 | self.assertEquals(res.find(' timezone'), -1) |
| 367 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 368 | class TestVTYNAT(TestVTYGenericBSC): |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 369 | |
| 370 | def vty_command(self): |
| 371 | return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c", |
| 372 | "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"] |
| 373 | |
| 374 | def vty_app(self): |
| 375 | return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat") |
| 376 | |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 377 | def testVtyTree(self): |
| 378 | self.vty.enable() |
| 379 | self.assertTrue(self.vty.verify('configure terminal', [''])) |
| 380 | self.assertEquals(self.vty.node(), 'config') |
| 381 | self.ignoredCheckForEndAndExit() |
| 382 | self.assertTrue(self.vty.verify('mgcp', [''])) |
| 383 | self.assertEquals(self.vty.node(), 'config-mgcp') |
| 384 | self.checkForEndAndExit() |
| 385 | self.assertTrue(self.vty.verify('exit', [''])) |
| 386 | self.assertEquals(self.vty.node(), 'config') |
| 387 | self.assertTrue(self.vty.verify('nat', [''])) |
| 388 | self.assertEquals(self.vty.node(), 'config-nat') |
| 389 | self.checkForEndAndExit() |
| 390 | self.assertTrue(self.vty.verify('bsc 0', [''])) |
| 391 | self.assertEquals(self.vty.node(), 'config-nat-bsc') |
| 392 | self.checkForEndAndExit() |
| 393 | self.assertTrue(self.vty.verify('exit', [''])) |
| 394 | self.assertEquals(self.vty.node(), 'config-nat') |
| 395 | self.assertTrue(self.vty.verify('exit', [''])) |
| 396 | self.assertEquals(self.vty.node(), 'config') |
| 397 | self.assertTrue(self.vty.verify('exit', [''])) |
| 398 | self.assertTrue(self.vty.node() is None) |
| 399 | |
| 400 | # Check searching for outer node's commands |
| 401 | self.vty.command('configure terminal') |
| 402 | self.vty.command('mgcp') |
| 403 | self.vty.command('nat') |
| 404 | self.assertEquals(self.vty.node(), 'config-nat') |
Jacob Erlbeck | 4c9dff5 | 2013-09-02 13:17:17 +0200 | [diff] [blame] | 405 | self.vty.command('mgcp') |
| 406 | self.assertEquals(self.vty.node(), 'config-mgcp') |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 407 | self.vty.command('nat') |
| 408 | self.assertEquals(self.vty.node(), 'config-nat') |
| 409 | self.vty.command('bsc 0') |
Jacob Erlbeck | 4c9dff5 | 2013-09-02 13:17:17 +0200 | [diff] [blame] | 410 | self.vty.command('mgcp') |
| 411 | self.assertEquals(self.vty.node(), 'config-mgcp') |
Jacob Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 412 | |
Holger Hans Peter Freyther | b718ad3 | 2013-06-25 09:08:02 +0200 | [diff] [blame] | 413 | def testRewriteNoRewrite(self): |
| 414 | self.vty.enable() |
| 415 | res = self.vty.command("configure terminal") |
| 416 | res = self.vty.command("nat") |
| 417 | res = self.vty.command("number-rewrite rewrite.cfg") |
| 418 | res = self.vty.command("no number-rewrite") |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 419 | |
Holger Hans Peter Freyther | 67e423c | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 420 | def testRewritePostNoRewrite(self): |
| 421 | self.vty.enable() |
| 422 | self.vty.command("configure terminal") |
| 423 | self.vty.command("nat") |
| 424 | self.vty.verify("number-rewrite-post rewrite.cfg", ['']) |
| 425 | self.vty.verify("no number-rewrite-post", ['']) |
| 426 | |
| 427 | |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 428 | def testPrefixTreeLoading(self): |
| 429 | cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv") |
| 430 | |
| 431 | self.vty.enable() |
| 432 | self.vty.command("configure terminal") |
| 433 | self.vty.command("nat") |
| 434 | res = self.vty.command("prefix-tree %s" % cfg) |
| 435 | self.assertEqual(res, "% prefix-tree loaded 17 rules.") |
| 436 | self.vty.command("end") |
| 437 | |
| 438 | res = self.vty.command("show prefix-tree") |
| 439 | 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') |
| 440 | |
| 441 | self.vty.command("configure terminal") |
| 442 | self.vty.command("nat") |
| 443 | self.vty.command("no prefix-tree") |
| 444 | self.vty.command("end") |
| 445 | |
| 446 | res = self.vty.command("show prefix-tree") |
| 447 | self.assertEqual(res, "% there is now prefix tree loaded.") |
| 448 | |
Jacob Erlbeck | 6cb2ccc | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 449 | def testUssdSideChannelProvider(self): |
| 450 | self.vty.command("end") |
| 451 | self.vty.enable() |
| 452 | self.vty.command("configure terminal") |
| 453 | self.vty.command("nat") |
| 454 | self.vty.command("ussd-token key") |
| 455 | self.vty.command("end") |
| 456 | |
| 457 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.']) |
| 458 | self.assertTrue(res) |
| 459 | |
| 460 | ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 461 | ussdSocket.connect(('127.0.0.1', 5001)) |
| 462 | ussdSocket.settimeout(2.0) |
| 463 | print "Connected to %s:%d" % ussdSocket.getpeername() |
| 464 | |
| 465 | print "Expecting ID_GET request" |
| 466 | data = ussdSocket.recv(4) |
| 467 | self.assertEqual(data, "\x00\x01\xfe\x04") |
| 468 | |
| 469 | print "Going to send ID_RESP response" |
| 470 | res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79") |
| 471 | self.assertEqual(res, 10) |
| 472 | |
| 473 | # initiating PING/PONG cycle to know, that the ID_RESP message has been processed |
| 474 | |
| 475 | print "Going to send PING request" |
| 476 | res = ussdSocket.send("\x00\x01\xfe\x00") |
| 477 | self.assertEqual(res, 4) |
| 478 | |
| 479 | print "Expecting PONG response" |
| 480 | data = ussdSocket.recv(4) |
| 481 | self.assertEqual(data, "\x00\x01\xfe\x01") |
| 482 | |
| 483 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.']) |
| 484 | self.assertTrue(res) |
| 485 | |
| 486 | print "Going to shut down connection" |
| 487 | ussdSocket.shutdown(socket.SHUT_WR) |
| 488 | |
| 489 | print "Expecting EOF" |
| 490 | data = ussdSocket.recv(4) |
| 491 | self.assertEqual(data, "") |
| 492 | |
| 493 | ussdSocket.close() |
| 494 | |
| 495 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.']) |
| 496 | self.assertTrue(res) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 497 | |
| 498 | def add_nat_test(suite, workdir): |
| 499 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")): |
| 500 | print("Skipping the NAT test") |
| 501 | return |
| 502 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT) |
| 503 | suite.addTest(test) |
| 504 | |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 505 | def add_bsc_test(suite, workdir): |
| 506 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc/osmo-bsc")): |
| 507 | print("Skipping the BSC test") |
| 508 | return |
| 509 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYBSC) |
| 510 | suite.addTest(test) |
| 511 | |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 512 | if __name__ == '__main__': |
| 513 | import argparse |
| 514 | import sys |
| 515 | |
| 516 | workdir = '.' |
| 517 | |
| 518 | parser = argparse.ArgumentParser() |
| 519 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 520 | action="store_true", help="verbose mode") |
| 521 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 522 | help="searchpath for config") |
| 523 | parser.add_argument("-w", "--workdir", dest="w", |
| 524 | help="Working directory") |
| 525 | args = parser.parse_args() |
| 526 | |
| 527 | verbose_level = 1 |
| 528 | if args.verbose: |
| 529 | verbose_level = 2 |
| 530 | |
| 531 | if args.w: |
| 532 | workdir = args.w |
| 533 | |
| 534 | if args.p: |
| 535 | confpath = args.p |
| 536 | |
| 537 | print "confpath %s, workdir %s" % (confpath, workdir) |
| 538 | os.chdir(workdir) |
| 539 | print "Running tests for specific VTY commands" |
| 540 | suite = unittest.TestSuite() |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 541 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB)) |
Jacob Erlbeck | 1b89402 | 2013-08-28 10:16:54 +0200 | [diff] [blame] | 542 | add_bsc_test(suite, workdir) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 543 | add_nat_test(suite, workdir) |
| 544 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
| 545 | sys.exit(len(res.errors) + len(res.failures)) |