Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: validate dict structures |
| 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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import re |
| 21 | |
| 22 | from . import log |
Neels Hofmeyr | 0af893c | 2017-12-14 15:18:05 +0100 | [diff] [blame] | 23 | from .util import is_dict, is_list, str2bool, ENUM_OSMO_AUTH_ALGO |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 24 | |
| 25 | KEY_RE = re.compile('[a-zA-Z][a-zA-Z0-9_]*') |
| 26 | IPV4_RE = re.compile('([0-9]{1,3}.){3}[0-9]{1,3}') |
| 27 | HWADDR_RE = re.compile('([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}') |
| 28 | IMSI_RE = re.compile('[0-9]{6,15}') |
| 29 | KI_RE = re.compile('[0-9a-fA-F]{32}') |
| 30 | MSISDN_RE = re.compile('[0-9]{1,15}') |
| 31 | |
| 32 | def match_re(name, regex, val): |
| 33 | while True: |
| 34 | if not isinstance(val, str): |
| 35 | break; |
| 36 | if not regex.fullmatch(val): |
| 37 | break; |
| 38 | return |
| 39 | raise ValueError('Invalid %s: %r' % (name, val)) |
| 40 | |
| 41 | def band(val): |
Pau Espin Pedrol | 05a838e | 2018-03-27 19:15:41 +0200 | [diff] [blame] | 42 | if val in ('GSM-900', 'GSM-1800', 'GSM-1900'): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 43 | return |
| 44 | raise ValueError('Unknown GSM band: %r' % val) |
| 45 | |
| 46 | def ipv4(val): |
| 47 | match_re('IPv4 address', IPV4_RE, val) |
| 48 | els = [int(el) for el in val.split('.')] |
| 49 | if not all([el >= 0 and el <= 255 for el in els]): |
| 50 | raise ValueError('Invalid IPv4 address: %r' % val) |
| 51 | |
| 52 | def hwaddr(val): |
| 53 | match_re('hardware address', HWADDR_RE, val) |
| 54 | |
| 55 | def imsi(val): |
| 56 | match_re('IMSI', IMSI_RE, val) |
| 57 | |
| 58 | def ki(val): |
| 59 | match_re('KI', KI_RE, val) |
| 60 | |
| 61 | def msisdn(val): |
| 62 | match_re('MSISDN', MSISDN_RE, val) |
| 63 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 64 | def auth_algo(val): |
Neels Hofmeyr | 0af893c | 2017-12-14 15:18:05 +0100 | [diff] [blame] | 65 | if val not in ENUM_OSMO_AUTH_ALGO: |
| 66 | raise ValueError('Unknown Authentication Algorithm: %r' % val) |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 67 | |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 68 | def uint(val): |
| 69 | n = int(val) |
| 70 | if n < 0: |
| 71 | raise ValueError('Positive value expected instead of %d' % n) |
| 72 | |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 73 | def uint8(val): |
| 74 | n = int(val) |
| 75 | if n < 0: |
| 76 | raise ValueError('Positive value expected instead of %d' % n) |
| 77 | if n > 255: # 2^8 - 1 |
| 78 | raise ValueError('Value %d too big, max value is 255' % n) |
| 79 | |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 80 | def uint16(val): |
| 81 | n = int(val) |
| 82 | if n < 0: |
| 83 | raise ValueError('Positive value expected instead of %d' % n) |
| 84 | if n > 65535: # 2^16 - 1 |
| 85 | raise ValueError('Value %d too big, max value is 65535' % n) |
| 86 | |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 87 | def times(val): |
| 88 | n = int(val) |
| 89 | if n < 1: |
| 90 | raise ValueError('Positive value >0 expected instead of %d' % n) |
| 91 | |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 92 | def cipher(val): |
| 93 | if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'): |
| 94 | return |
| 95 | raise ValueError('Unknown Cipher value: %r' % val) |
| 96 | |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 97 | def modem_feature(val): |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 98 | if val in ('sms', 'gprs', 'voice', 'ussd', 'sim'): |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 99 | return |
| 100 | raise ValueError('Unknown Modem Feature: %r' % val) |
| 101 | |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 102 | def phy_channel_config(val): |
| 103 | if val in ('CCCH', 'CCCH+SDCCH4', 'TCH/F', 'TCH/H', 'SDCCH8', 'PDCH', |
| 104 | 'TCH/F_PDCH', 'CCCH+SDCCH4+CBCH', 'SDCCH8+CBCH','TCH/F_TCH/H_PDCH'): |
| 105 | return |
| 106 | raise ValueError('Unknown Physical channel config: %r' % val) |
| 107 | |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 108 | def channel_allocator(val): |
| 109 | if val in ('ascending', 'descending'): |
| 110 | return |
| 111 | raise ValueError('Unknown Channel Allocator Policy %r' % val) |
| 112 | |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 113 | def gprs_mode(val): |
| 114 | if val in ('none', 'gprs', 'egprs'): |
| 115 | return |
| 116 | raise ValueError('Unknown GPRS mode %r' % val) |
| 117 | |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 118 | def codec(val): |
| 119 | if val in ('hr1', 'hr2', 'hr3', 'fr1', 'fr2', 'fr3'): |
| 120 | return |
| 121 | raise ValueError('Unknown Codec value: %r' % val) |
| 122 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 123 | def osmo_trx_clock_ref(val): |
| 124 | if val in ('internal', 'external', 'gspdo'): |
| 125 | return |
| 126 | raise ValueError('Unknown OsmoTRX clock reference value: %r' % val) |
| 127 | |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 128 | def lte_transmission_mode(val): |
| 129 | n = int(val) |
| 130 | if n <= 4: |
| 131 | return |
| 132 | raise ValueError('LTE Transmission Mode %d not in expected range' % n) |
| 133 | |
| 134 | def lte_rlc_drb_mode(val): |
| 135 | if val.upper() in ('UM', 'AM'): |
| 136 | return |
| 137 | raise ValueError('Unknown LTE RLC DRB Mode value: %r' % val) |
| 138 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 139 | INT = 'int' |
| 140 | STR = 'str' |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 141 | UINT = 'uint' |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 142 | BOOL_STR = 'bool_str' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 143 | BAND = 'band' |
| 144 | IPV4 = 'ipv4' |
| 145 | HWADDR = 'hwaddr' |
| 146 | IMSI = 'imsi' |
| 147 | KI = 'ki' |
| 148 | MSISDN = 'msisdn' |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 149 | AUTH_ALGO = 'auth_algo' |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 150 | TIMES='times' |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 151 | CIPHER = 'cipher' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 152 | MODEM_FEATURE = 'modem_feature' |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 153 | PHY_CHAN = 'chan' |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 154 | CHAN_ALLOCATOR = 'chan_allocator' |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 155 | GPRS_MODE = 'gprs_mode' |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 156 | CODEC = 'codec' |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 157 | OSMO_TRX_CLOCK_REF = 'osmo_trx_clock_ref' |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 158 | LTE_TRANSMISSION_MODE = 'lte_transmission_mode' |
| 159 | LTE_RLC_DRB_MODE = 'lte_rlc_drb_mode' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 160 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 161 | SCHEMA_TYPES = { |
| 162 | INT: int, |
| 163 | STR: str, |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 164 | UINT: uint, |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 165 | BOOL_STR: str2bool, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 166 | BAND: band, |
| 167 | IPV4: ipv4, |
| 168 | HWADDR: hwaddr, |
| 169 | IMSI: imsi, |
| 170 | KI: ki, |
| 171 | MSISDN: msisdn, |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 172 | AUTH_ALGO: auth_algo, |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 173 | TIMES: times, |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 174 | CIPHER: cipher, |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 175 | MODEM_FEATURE: modem_feature, |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 176 | PHY_CHAN: phy_channel_config, |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 177 | CHAN_ALLOCATOR: channel_allocator, |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 178 | GPRS_MODE: gprs_mode, |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 179 | CODEC: codec, |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 180 | OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref, |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 181 | LTE_TRANSMISSION_MODE: lte_transmission_mode, |
| 182 | LTE_RLC_DRB_MODE: lte_rlc_drb_mode, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | def validate(config, schema): |
| 186 | '''Make sure the given config dict adheres to the schema. |
| 187 | The schema is a dict of 'dict paths' in dot-notation with permitted |
| 188 | value type. All leaf nodes are validated, nesting dicts are implicit. |
| 189 | |
| 190 | validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } }, |
| 191 | { 'a': int, |
| 192 | 'b.b1': str, |
| 193 | 'b.b2[]': int } ) |
| 194 | |
| 195 | Raise a ValueError in case the schema is violated. |
| 196 | ''' |
| 197 | |
| 198 | def validate_item(path, value, schema): |
| 199 | want_type = schema.get(path) |
| 200 | |
| 201 | if is_list(value): |
| 202 | if want_type: |
| 203 | raise ValueError('config item is a list, should be %r: %r' % (want_type, path)) |
| 204 | path = path + '[]' |
| 205 | want_type = schema.get(path) |
| 206 | |
| 207 | if not want_type: |
| 208 | if is_dict(value): |
| 209 | nest(path, value, schema) |
| 210 | return |
| 211 | if is_list(value) and value: |
| 212 | for list_v in value: |
| 213 | validate_item(path, list_v, schema) |
| 214 | return |
| 215 | raise ValueError('config item not known: %r' % path) |
| 216 | |
| 217 | if want_type not in SCHEMA_TYPES: |
| 218 | raise ValueError('unknown type %r at %r' % (want_type, path)) |
| 219 | |
| 220 | if is_dict(value): |
| 221 | raise ValueError('config item is dict but should be a leaf node of type %r: %r' |
| 222 | % (want_type, path)) |
| 223 | |
| 224 | if is_list(value): |
| 225 | for list_v in value: |
| 226 | validate_item(path, list_v, schema) |
| 227 | return |
| 228 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 229 | log.ctx(path) |
| 230 | type_validator = SCHEMA_TYPES.get(want_type) |
| 231 | type_validator(value) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 232 | |
| 233 | def nest(parent_path, config, schema): |
| 234 | if parent_path: |
| 235 | parent_path = parent_path + '.' |
| 236 | else: |
| 237 | parent_path = '' |
| 238 | for k,v in config.items(): |
| 239 | if not KEY_RE.fullmatch(k): |
| 240 | raise ValueError('invalid config key: %r' % k) |
| 241 | path = parent_path + k |
| 242 | validate_item(path, v, schema) |
| 243 | |
| 244 | nest(None, config, schema) |
| 245 | |
| 246 | # vim: expandtab tabstop=4 shiftwidth=4 |