Holger Hans Peter Freyther | 6539752 | 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 | 4684eb6 | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 21 | import socket |
Holger Hans Peter Freyther | 6539752 | 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 | |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 59 | class 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 Freyther | eda0867 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 82 | self.assert_(res.find('periodic location update 60') > 0) |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 83 | 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 Freyther | eda0867 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 89 | self.assert_(res.find('no periodic location update') > 0) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 90 | |
| 91 | class TestVTYNAT(TestVTYBase): |
| 92 | |
| 93 | def vty_command(self): |
| 94 | return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c", |
| 95 | "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"] |
| 96 | |
| 97 | def vty_app(self): |
| 98 | return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat") |
| 99 | |
Holger Hans Peter Freyther | bf123a1 | 2013-06-25 09:08:02 +0200 | [diff] [blame] | 100 | def testRewriteNoRewrite(self): |
| 101 | self.vty.enable() |
| 102 | res = self.vty.command("configure terminal") |
| 103 | res = self.vty.command("nat") |
| 104 | res = self.vty.command("number-rewrite rewrite.cfg") |
| 105 | res = self.vty.command("no number-rewrite") |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 106 | |
Holger Hans Peter Freyther | e9c7e27 | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 107 | def testRewritePostNoRewrite(self): |
| 108 | self.vty.enable() |
| 109 | self.vty.command("configure terminal") |
| 110 | self.vty.command("nat") |
| 111 | self.vty.verify("number-rewrite-post rewrite.cfg", ['']) |
| 112 | self.vty.verify("no number-rewrite-post", ['']) |
| 113 | |
| 114 | |
Holger Hans Peter Freyther | 367390b | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 115 | def testPrefixTreeLoading(self): |
| 116 | cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv") |
| 117 | |
| 118 | self.vty.enable() |
| 119 | self.vty.command("configure terminal") |
| 120 | self.vty.command("nat") |
| 121 | res = self.vty.command("prefix-tree %s" % cfg) |
| 122 | self.assertEqual(res, "% prefix-tree loaded 17 rules.") |
| 123 | self.vty.command("end") |
| 124 | |
| 125 | res = self.vty.command("show prefix-tree") |
| 126 | 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') |
| 127 | |
| 128 | self.vty.command("configure terminal") |
| 129 | self.vty.command("nat") |
| 130 | self.vty.command("no prefix-tree") |
| 131 | self.vty.command("end") |
| 132 | |
| 133 | res = self.vty.command("show prefix-tree") |
| 134 | self.assertEqual(res, "% there is now prefix tree loaded.") |
| 135 | |
Jacob Erlbeck | 4684eb6 | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 136 | def testUssdSideChannelProvider(self): |
| 137 | self.vty.command("end") |
| 138 | self.vty.enable() |
| 139 | self.vty.command("configure terminal") |
| 140 | self.vty.command("nat") |
| 141 | self.vty.command("ussd-token key") |
| 142 | self.vty.command("end") |
| 143 | |
| 144 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.']) |
| 145 | self.assertTrue(res) |
| 146 | |
| 147 | ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 148 | ussdSocket.connect(('127.0.0.1', 5001)) |
| 149 | ussdSocket.settimeout(2.0) |
| 150 | print "Connected to %s:%d" % ussdSocket.getpeername() |
| 151 | |
| 152 | print "Expecting ID_GET request" |
| 153 | data = ussdSocket.recv(4) |
| 154 | self.assertEqual(data, "\x00\x01\xfe\x04") |
| 155 | |
| 156 | print "Going to send ID_RESP response" |
| 157 | res = ussdSocket.send("\x00\x07\xfe\x05\x00\x04\x01\x6b\x65\x79") |
| 158 | self.assertEqual(res, 10) |
| 159 | |
| 160 | # initiating PING/PONG cycle to know, that the ID_RESP message has been processed |
| 161 | |
| 162 | print "Going to send PING request" |
| 163 | res = ussdSocket.send("\x00\x01\xfe\x00") |
| 164 | self.assertEqual(res, 4) |
| 165 | |
| 166 | print "Expecting PONG response" |
| 167 | data = ussdSocket.recv(4) |
| 168 | self.assertEqual(data, "\x00\x01\xfe\x01") |
| 169 | |
| 170 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.']) |
| 171 | self.assertTrue(res) |
| 172 | |
| 173 | print "Going to shut down connection" |
| 174 | ussdSocket.shutdown(socket.SHUT_WR) |
| 175 | |
| 176 | print "Expecting EOF" |
| 177 | data = ussdSocket.recv(4) |
| 178 | self.assertEqual(data, "") |
| 179 | |
| 180 | ussdSocket.close() |
| 181 | |
| 182 | res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is not connected and not authorized.']) |
| 183 | self.assertTrue(res) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 184 | |
| 185 | def add_nat_test(suite, workdir): |
| 186 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")): |
| 187 | print("Skipping the NAT test") |
| 188 | return |
| 189 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT) |
| 190 | suite.addTest(test) |
| 191 | |
| 192 | if __name__ == '__main__': |
| 193 | import argparse |
| 194 | import sys |
| 195 | |
| 196 | workdir = '.' |
| 197 | |
| 198 | parser = argparse.ArgumentParser() |
| 199 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 200 | action="store_true", help="verbose mode") |
| 201 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 202 | help="searchpath for config") |
| 203 | parser.add_argument("-w", "--workdir", dest="w", |
| 204 | help="Working directory") |
| 205 | args = parser.parse_args() |
| 206 | |
| 207 | verbose_level = 1 |
| 208 | if args.verbose: |
| 209 | verbose_level = 2 |
| 210 | |
| 211 | if args.w: |
| 212 | workdir = args.w |
| 213 | |
| 214 | if args.p: |
| 215 | confpath = args.p |
| 216 | |
| 217 | print "confpath %s, workdir %s" % (confpath, workdir) |
| 218 | os.chdir(workdir) |
| 219 | print "Running tests for specific VTY commands" |
| 220 | suite = unittest.TestSuite() |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 221 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB)) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 222 | add_nat_test(suite, workdir) |
| 223 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
| 224 | sys.exit(len(res.errors) + len(res.failures)) |