blob: b999363fceb994fcedb98a42f2d4c0fb2bf8c561 [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# 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 Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr798e5922017-05-18 15:24:02 +02009# 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 Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr798e5922017-05-18 15:24:02 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr798e5922017-05-18 15:24:02 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr798e5922017-05-18 15:24:02 +020021import pprint
Pau Espin Pedrol6610dfe2017-08-23 17:02:38 +020022import sqlite3
Neels Hofmeyr798e5922017-05-18 15:24:02 +020023
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020024from ..core import log, util, config, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020025from . import pcap_recorder
Neels Hofmeyr798e5922017-05-18 15:24:02 +020026
27class OsmoHlr(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020028 run_dir = None
29 config_file = None
30 process = None
31 next_subscriber_id = 1
32
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020033 def __init__(self, testenv, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020034 super().__init__(log.C_RUN, 'osmo-hlr_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020035 self.run_dir = None
36 self.config_file = None
37 self.process = None
38 self.next_subscriber_id = 1
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020039 self.testenv = testenv
Neels Hofmeyr798e5922017-05-18 15:24:02 +020040 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020041
42 def start(self):
43 self.log('Starting osmo-hlr')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020044 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020045 self.configure()
46
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020047 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-hlr')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020048
49 binary = inst.child('bin', 'osmo-hlr')
50 if not os.path.isfile(binary):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020051 raise log.Error('Binary missing:', binary)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020052 lib = inst.child('lib')
53 if not os.path.isdir(lib):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020054 raise log.Error('No lib/ in', inst)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020055
56 # bootstrap an empty hlr.db
57 self.db_file = self.run_dir.new_file('hlr.db')
Pau Espin Pedrolc81e2652018-07-03 11:56:03 +020058 sql_input = inst.child('share/doc/osmo-hlr/sql/hlr.sql')
Neels Hofmeyr798e5922017-05-18 15:24:02 +020059 if not os.path.isfile(sql_input):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020060 raise log.Error('hlr.sql missing:', sql_input)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020061 self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input)))
62
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020063 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None,
Neels Hofmeyr798e5922017-05-18 15:24:02 +020064 'host %s' % self.addr())
65
66 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
67
68 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
69 self.process = process.Process(self.name(), self.run_dir,
70 (binary,
71 '-c', os.path.abspath(self.config_file),
72 '--database', self.db_file),
73 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020074 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020075 self.process.launch()
76
77 def configure(self):
78 self.config_file = self.run_dir.new_file('osmo-hlr.cfg')
79 self.dbg(config_file=self.config_file)
80
81 values = dict(hlr=config.get_defaults('hlr'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020082 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020083 config.overlay(values, dict(hlr=dict(ip_address=self.ip_address)))
84
85 self.dbg('HLR CONFIG:\n' + pprint.pformat(values))
86
87 with open(self.config_file, 'w') as f:
88 r = template.render('osmo-hlr.cfg', values)
89 self.dbg(r)
90 f.write(r)
91
92 def addr(self):
93 return self.ip_address.get('addr')
94
95 def running(self):
96 return not self.process.terminated()
97
98 def run_local(self, name, popen_args):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020099 run_dir = self.run_dir.new_dir(name)
100 proc = process.Process(name, run_dir, popen_args)
101 proc.launch()
102 proc.wait()
103 if proc.result != 0:
104 log.ctx(proc)
105 raise log.Error('Exited in error')
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200106
Neels Hofmeyr0af893c2017-12-14 15:18:05 +0100107 def subscriber_add(self, modem, msisdn=None, algo_str=None):
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200108 if msisdn is None:
Pau Espin Pedrol83a2fdc2020-10-15 16:33:47 +0200109 msisdn = modem.msisdn()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200110 subscriber_id = self.next_subscriber_id
111 self.next_subscriber_id += 1
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200112
Neels Hofmeyr0af893c2017-12-14 15:18:05 +0100113 if algo_str is None:
114 algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200115
Neels Hofmeyr0af893c2017-12-14 15:18:05 +0100116 if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki():
117 raise log.Error("Auth algo %r selected but no KI specified" % algo_str)
118
119 algo = util.osmo_auth_algo_by_name(algo_str)
120
121 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id,
122 algo_str=algo_str, algo=algo)
Pau Espin Pedrol6610dfe2017-08-23 17:02:38 +0200123 conn = sqlite3.connect(self.db_file)
124 try:
125 c = conn.cursor()
126 c.execute('insert into subscriber (id, imsi, msisdn) values (?, ?, ?)',
Pau Espin Pedrol4b7c5852020-10-14 14:48:21 +0200127 (subscriber_id, modem.imsi(), modem.msisdn(),))
Pau Espin Pedrol6610dfe2017-08-23 17:02:38 +0200128 c.execute('insert into auc_2g (subscriber_id, algo_id_2g, ki) values (?, ?, ?)',
129 (subscriber_id, algo, modem.ki(),))
130 conn.commit()
131 finally:
132 conn.close()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200133 return subscriber_id
134
Pau Espin Pedrol0755cdb2017-08-23 17:03:48 +0200135 def subscriber_delete(self, modem):
136 self.log('Add subscriber', imsi=modem.imsi())
137 conn = sqlite3.connect(self.db_file)
138 try:
139 c = conn.cursor()
140 c.execute('select id from subscriber where imsi = ?', (modem.imsi(),))
141 subscriber_id = c.fetchone()[0]
142 c.execute('delete from subscriber where id = ?', (subscriber_id,))
143 c.execute('delete from auc_2g where subscriber_id = ?', (subscriber_id,))
144 conn.commit()
145 finally:
146 conn.close()
147
Pau Espin Pedrol5bbb3002017-11-23 11:07:02 +0100148 def conf_for_client(self):
Neels Hofmeyr7b02ed02017-06-08 23:08:59 +0200149 return dict(hlr=dict(ip_address=self.ip_address))
150
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200151# vim: expandtab tabstop=4 shiftwidth=4