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 |
| 8 | # it under the terms of the GNU Affero General Public License as |
| 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 |
| 15 | # GNU Affero General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Affero General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import re |
| 21 | |
| 22 | from . import log |
| 23 | from .util import is_dict, is_list |
| 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 | |
| 64 | INT = 'int' |
| 65 | STR = 'str' |
| 66 | BAND = 'band' |
| 67 | IPV4 = 'ipv4' |
| 68 | HWADDR = 'hwaddr' |
| 69 | IMSI = 'imsi' |
| 70 | KI = 'ki' |
| 71 | MSISDN = 'msisdn' |
| 72 | SCHEMA_TYPES = { |
| 73 | INT: int, |
| 74 | STR: str, |
| 75 | BAND: band, |
| 76 | IPV4: ipv4, |
| 77 | HWADDR: hwaddr, |
| 78 | IMSI: imsi, |
| 79 | KI: ki, |
| 80 | MSISDN: msisdn, |
| 81 | } |
| 82 | |
| 83 | def validate(config, schema): |
| 84 | '''Make sure the given config dict adheres to the schema. |
| 85 | The schema is a dict of 'dict paths' in dot-notation with permitted |
| 86 | value type. All leaf nodes are validated, nesting dicts are implicit. |
| 87 | |
| 88 | validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } }, |
| 89 | { 'a': int, |
| 90 | 'b.b1': str, |
| 91 | 'b.b2[]': int } ) |
| 92 | |
| 93 | Raise a ValueError in case the schema is violated. |
| 94 | ''' |
| 95 | |
| 96 | def validate_item(path, value, schema): |
| 97 | want_type = schema.get(path) |
| 98 | |
| 99 | if is_list(value): |
| 100 | if want_type: |
| 101 | raise ValueError('config item is a list, should be %r: %r' % (want_type, path)) |
| 102 | path = path + '[]' |
| 103 | want_type = schema.get(path) |
| 104 | |
| 105 | if not want_type: |
| 106 | if is_dict(value): |
| 107 | nest(path, value, schema) |
| 108 | return |
| 109 | if is_list(value) and value: |
| 110 | for list_v in value: |
| 111 | validate_item(path, list_v, schema) |
| 112 | return |
| 113 | raise ValueError('config item not known: %r' % path) |
| 114 | |
| 115 | if want_type not in SCHEMA_TYPES: |
| 116 | raise ValueError('unknown type %r at %r' % (want_type, path)) |
| 117 | |
| 118 | if is_dict(value): |
| 119 | raise ValueError('config item is dict but should be a leaf node of type %r: %r' |
| 120 | % (want_type, path)) |
| 121 | |
| 122 | if is_list(value): |
| 123 | for list_v in value: |
| 124 | validate_item(path, list_v, schema) |
| 125 | return |
| 126 | |
| 127 | with log.Origin(item=path): |
| 128 | type_validator = SCHEMA_TYPES.get(want_type) |
| 129 | type_validator(value) |
| 130 | |
| 131 | def nest(parent_path, config, schema): |
| 132 | if parent_path: |
| 133 | parent_path = parent_path + '.' |
| 134 | else: |
| 135 | parent_path = '' |
| 136 | for k,v in config.items(): |
| 137 | if not KEY_RE.fullmatch(k): |
| 138 | raise ValueError('invalid config key: %r' % k) |
| 139 | path = parent_path + k |
| 140 | validate_item(path, v, schema) |
| 141 | |
| 142 | nest(None, config, schema) |
| 143 | |
| 144 | # vim: expandtab tabstop=4 shiftwidth=4 |