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 |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 23 | from .util import is_dict, is_list, str2bool |
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): |
| 42 | if val in ('GSM-1800', 'GSM-1900'): |
| 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): |
| 65 | if val in ('none', 'xor', 'comp128v1'): |
| 66 | return |
| 67 | raise ValueError('Unknown Authentication Algorithm: %r' % val) |
| 68 | |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 69 | def uint(val): |
| 70 | n = int(val) |
| 71 | if n < 0: |
| 72 | raise ValueError('Positive value expected instead of %d' % n) |
| 73 | |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 74 | def uint16(val): |
| 75 | n = int(val) |
| 76 | if n < 0: |
| 77 | raise ValueError('Positive value expected instead of %d' % n) |
| 78 | if n > 65535: # 2^16 - 1 |
| 79 | raise ValueError('Value %d too big, max value is 65535' % n) |
| 80 | |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 81 | def times(val): |
| 82 | n = int(val) |
| 83 | if n < 1: |
| 84 | raise ValueError('Positive value >0 expected instead of %d' % n) |
| 85 | |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 86 | def cipher(val): |
| 87 | if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'): |
| 88 | return |
| 89 | raise ValueError('Unknown Cipher value: %r' % val) |
| 90 | |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 91 | def modem_feature(val): |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 92 | if val in ('sms', 'gprs', 'voice', 'ussd'): |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 93 | return |
| 94 | raise ValueError('Unknown Modem Feature: %r' % val) |
| 95 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 96 | INT = 'int' |
| 97 | STR = 'str' |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 98 | UINT = 'uint' |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 99 | BOOL_STR = 'bool_str' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 100 | BAND = 'band' |
| 101 | IPV4 = 'ipv4' |
| 102 | HWADDR = 'hwaddr' |
| 103 | IMSI = 'imsi' |
| 104 | KI = 'ki' |
| 105 | MSISDN = 'msisdn' |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 106 | AUTH_ALGO = 'auth_algo' |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 107 | TIMES='times' |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 108 | CIPHER = 'cipher' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 109 | MODEM_FEATURE = 'modem_feature' |
| 110 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 111 | SCHEMA_TYPES = { |
| 112 | INT: int, |
| 113 | STR: str, |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 114 | UINT: uint, |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 115 | BOOL_STR: str2bool, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 116 | BAND: band, |
| 117 | IPV4: ipv4, |
| 118 | HWADDR: hwaddr, |
| 119 | IMSI: imsi, |
| 120 | KI: ki, |
| 121 | MSISDN: msisdn, |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 122 | AUTH_ALGO: auth_algo, |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 123 | TIMES: times, |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 124 | CIPHER: cipher, |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 125 | MODEM_FEATURE: modem_feature, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | def validate(config, schema): |
| 129 | '''Make sure the given config dict adheres to the schema. |
| 130 | The schema is a dict of 'dict paths' in dot-notation with permitted |
| 131 | value type. All leaf nodes are validated, nesting dicts are implicit. |
| 132 | |
| 133 | validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } }, |
| 134 | { 'a': int, |
| 135 | 'b.b1': str, |
| 136 | 'b.b2[]': int } ) |
| 137 | |
| 138 | Raise a ValueError in case the schema is violated. |
| 139 | ''' |
| 140 | |
| 141 | def validate_item(path, value, schema): |
| 142 | want_type = schema.get(path) |
| 143 | |
| 144 | if is_list(value): |
| 145 | if want_type: |
| 146 | raise ValueError('config item is a list, should be %r: %r' % (want_type, path)) |
| 147 | path = path + '[]' |
| 148 | want_type = schema.get(path) |
| 149 | |
| 150 | if not want_type: |
| 151 | if is_dict(value): |
| 152 | nest(path, value, schema) |
| 153 | return |
| 154 | if is_list(value) and value: |
| 155 | for list_v in value: |
| 156 | validate_item(path, list_v, schema) |
| 157 | return |
| 158 | raise ValueError('config item not known: %r' % path) |
| 159 | |
| 160 | if want_type not in SCHEMA_TYPES: |
| 161 | raise ValueError('unknown type %r at %r' % (want_type, path)) |
| 162 | |
| 163 | if is_dict(value): |
| 164 | raise ValueError('config item is dict but should be a leaf node of type %r: %r' |
| 165 | % (want_type, path)) |
| 166 | |
| 167 | if is_list(value): |
| 168 | for list_v in value: |
| 169 | validate_item(path, list_v, schema) |
| 170 | return |
| 171 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 172 | log.ctx(path) |
| 173 | type_validator = SCHEMA_TYPES.get(want_type) |
| 174 | type_validator(value) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 175 | |
| 176 | def nest(parent_path, config, schema): |
| 177 | if parent_path: |
| 178 | parent_path = parent_path + '.' |
| 179 | else: |
| 180 | parent_path = '' |
| 181 | for k,v in config.items(): |
| 182 | if not KEY_RE.fullmatch(k): |
| 183 | raise ValueError('invalid config key: %r' % k) |
| 184 | path = parent_path + k |
| 185 | validate_item(path, v, schema) |
| 186 | |
| 187 | nest(None, config, schema) |
| 188 | |
| 189 | # vim: expandtab tabstop=4 shiftwidth=4 |