Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-hlr |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import re |
| 22 | import pprint |
Pau Espin Pedrol | 6610dfe | 2017-08-23 17:02:38 +0200 | [diff] [blame] | 23 | import sqlite3 |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 24 | |
| 25 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder |
| 26 | |
| 27 | class OsmoHlr(log.Origin): |
| 28 | suite_run = None |
| 29 | ip_address = None |
| 30 | run_dir = None |
| 31 | config_file = None |
| 32 | process = None |
| 33 | next_subscriber_id = 1 |
| 34 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame] | 35 | AUTH_ALGO_NONE = 0 |
| 36 | AUTH_ALGO_XOR = 1 |
| 37 | AUTH_ALGO_COMP128v1 = 2 |
| 38 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 39 | def __init__(self, suite_run, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 40 | super().__init__(log.C_RUN, 'osmo-hlr_%s' % ip_address.get('addr')) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 41 | self.suite_run = suite_run |
| 42 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 43 | self.bts = [] |
| 44 | |
| 45 | def start(self): |
| 46 | self.log('Starting osmo-hlr') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 47 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 48 | self.configure() |
| 49 | |
| 50 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-hlr'))) |
| 51 | |
| 52 | binary = inst.child('bin', 'osmo-hlr') |
| 53 | if not os.path.isfile(binary): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 54 | raise log.Error('Binary missing:', binary) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 55 | lib = inst.child('lib') |
| 56 | if not os.path.isdir(lib): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 57 | raise log.Error('No lib/ in', inst) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 58 | |
| 59 | # bootstrap an empty hlr.db |
| 60 | self.db_file = self.run_dir.new_file('hlr.db') |
| 61 | sql_input = inst.child('share/doc/osmo-hlr/hlr.sql') |
| 62 | if not os.path.isfile(sql_input): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 63 | raise log.Error('hlr.sql missing:', sql_input) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 64 | self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input))) |
| 65 | |
| 66 | iface = util.ip_to_iface(self.addr()) |
| 67 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface, |
| 68 | 'host %s' % self.addr()) |
| 69 | |
| 70 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 71 | |
| 72 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 73 | self.process = process.Process(self.name(), self.run_dir, |
| 74 | (binary, |
| 75 | '-c', os.path.abspath(self.config_file), |
| 76 | '--database', self.db_file), |
| 77 | env=env) |
| 78 | self.suite_run.remember_to_stop(self.process) |
| 79 | self.process.launch() |
| 80 | |
| 81 | def configure(self): |
| 82 | self.config_file = self.run_dir.new_file('osmo-hlr.cfg') |
| 83 | self.dbg(config_file=self.config_file) |
| 84 | |
| 85 | values = dict(hlr=config.get_defaults('hlr')) |
| 86 | config.overlay(values, self.suite_run.config()) |
| 87 | config.overlay(values, dict(hlr=dict(ip_address=self.ip_address))) |
| 88 | |
| 89 | self.dbg('HLR CONFIG:\n' + pprint.pformat(values)) |
| 90 | |
| 91 | with open(self.config_file, 'w') as f: |
| 92 | r = template.render('osmo-hlr.cfg', values) |
| 93 | self.dbg(r) |
| 94 | f.write(r) |
| 95 | |
| 96 | def addr(self): |
| 97 | return self.ip_address.get('addr') |
| 98 | |
| 99 | def running(self): |
| 100 | return not self.process.terminated() |
| 101 | |
| 102 | def run_local(self, name, popen_args): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 103 | run_dir = self.run_dir.new_dir(name) |
| 104 | proc = process.Process(name, run_dir, popen_args) |
| 105 | proc.launch() |
| 106 | proc.wait() |
| 107 | if proc.result != 0: |
| 108 | log.ctx(proc) |
| 109 | raise log.Error('Exited in error') |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 110 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame] | 111 | def subscriber_add(self, modem, msisdn=None, algo=None): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 112 | if msisdn is None: |
| 113 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 114 | modem.set_msisdn(msisdn) |
| 115 | subscriber_id = self.next_subscriber_id |
| 116 | self.next_subscriber_id += 1 |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 117 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame] | 118 | if not algo: |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 119 | alg_str = modem.auth_algo() |
| 120 | if alg_str is None or alg_str == 'none': |
| 121 | algo = self.AUTH_ALGO_NONE |
| 122 | elif alg_str == 'comp128v1': |
| 123 | algo = self.AUTH_ALGO_COMP128v1 |
| 124 | elif alg_str == 'xor': |
| 125 | algo = self.AUTH_ALGO_XOR |
| 126 | if algo != self.AUTH_ALGO_NONE and not modem.ki(): |
| 127 | raise log.Error("Auth algo %r selected and no KI specified" % algo) |
| 128 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame] | 129 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id, algo=algo) |
Pau Espin Pedrol | 6610dfe | 2017-08-23 17:02:38 +0200 | [diff] [blame] | 130 | conn = sqlite3.connect(self.db_file) |
| 131 | try: |
| 132 | c = conn.cursor() |
| 133 | c.execute('insert into subscriber (id, imsi, msisdn) values (?, ?, ?)', |
| 134 | (subscriber_id, modem.imsi(), modem.msisdn,)) |
| 135 | c.execute('insert into auc_2g (subscriber_id, algo_id_2g, ki) values (?, ?, ?)', |
| 136 | (subscriber_id, algo, modem.ki(),)) |
| 137 | conn.commit() |
| 138 | finally: |
| 139 | conn.close() |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 140 | return subscriber_id |
| 141 | |
Pau Espin Pedrol | 0755cdb | 2017-08-23 17:03:48 +0200 | [diff] [blame] | 142 | def subscriber_delete(self, modem): |
| 143 | self.log('Add subscriber', imsi=modem.imsi()) |
| 144 | conn = sqlite3.connect(self.db_file) |
| 145 | try: |
| 146 | c = conn.cursor() |
| 147 | c.execute('select id from subscriber where imsi = ?', (modem.imsi(),)) |
| 148 | subscriber_id = c.fetchone()[0] |
| 149 | c.execute('delete from subscriber where id = ?', (subscriber_id,)) |
| 150 | c.execute('delete from auc_2g where subscriber_id = ?', (subscriber_id,)) |
| 151 | conn.commit() |
| 152 | finally: |
| 153 | conn.close() |
| 154 | |
Neels Hofmeyr | 7b02ed0 | 2017-06-08 23:08:59 +0200 | [diff] [blame] | 155 | def conf_for_msc(self): |
| 156 | return dict(hlr=dict(ip_address=self.ip_address)) |
| 157 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 158 | # vim: expandtab tabstop=4 shiftwidth=4 |