blob: f92d1db4802890a0a4cdd0192348db0e979cb3e7 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# 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 Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +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 Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import re
21
22from . import log
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020023from .util import is_dict, is_list, str2bool
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024
25KEY_RE = re.compile('[a-zA-Z][a-zA-Z0-9_]*')
26IPV4_RE = re.compile('([0-9]{1,3}.){3}[0-9]{1,3}')
27HWADDR_RE = re.compile('([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}')
28IMSI_RE = re.compile('[0-9]{6,15}')
29KI_RE = re.compile('[0-9a-fA-F]{32}')
30MSISDN_RE = re.compile('[0-9]{1,15}')
31
32def 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
41def band(val):
42 if val in ('GSM-1800', 'GSM-1900'):
43 return
44 raise ValueError('Unknown GSM band: %r' % val)
45
46def 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
52def hwaddr(val):
53 match_re('hardware address', HWADDR_RE, val)
54
55def imsi(val):
56 match_re('IMSI', IMSI_RE, val)
57
58def ki(val):
59 match_re('KI', KI_RE, val)
60
61def msisdn(val):
62 match_re('MSISDN', MSISDN_RE, val)
63
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +020064def auth_algo(val):
65 if val in ('none', 'xor', 'comp128v1'):
66 return
67 raise ValueError('Unknown Authentication Algorithm: %r' % val)
68
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +020069def uint(val):
70 n = int(val)
71 if n < 0:
72 raise ValueError('Positive value expected instead of %d' % n)
73
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +010074def uint8(val):
75 n = int(val)
76 if n < 0:
77 raise ValueError('Positive value expected instead of %d' % n)
78 if n > 255: # 2^8 - 1
79 raise ValueError('Value %d too big, max value is 255' % n)
80
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010081def uint16(val):
82 n = int(val)
83 if n < 0:
84 raise ValueError('Positive value expected instead of %d' % n)
85 if n > 65535: # 2^16 - 1
86 raise ValueError('Value %d too big, max value is 65535' % n)
87
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +020088def times(val):
89 n = int(val)
90 if n < 1:
91 raise ValueError('Positive value >0 expected instead of %d' % n)
92
Pau Espin Pedrol57497a62017-08-28 14:21:15 +020093def cipher(val):
94 if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'):
95 return
96 raise ValueError('Unknown Cipher value: %r' % val)
97
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +020098def modem_feature(val):
Pau Espin Pedrold71edd12017-10-06 13:53:54 +020099 if val in ('sms', 'gprs', 'voice', 'ussd'):
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200100 return
101 raise ValueError('Unknown Modem Feature: %r' % val)
102
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200103INT = 'int'
104STR = 'str'
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200105UINT = 'uint'
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200106BOOL_STR = 'bool_str'
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200107BAND = 'band'
108IPV4 = 'ipv4'
109HWADDR = 'hwaddr'
110IMSI = 'imsi'
111KI = 'ki'
112MSISDN = 'msisdn'
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200113AUTH_ALGO = 'auth_algo'
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200114TIMES='times'
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200115CIPHER = 'cipher'
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200116MODEM_FEATURE = 'modem_feature'
117
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200118SCHEMA_TYPES = {
119 INT: int,
120 STR: str,
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200121 UINT: uint,
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200122 BOOL_STR: str2bool,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200123 BAND: band,
124 IPV4: ipv4,
125 HWADDR: hwaddr,
126 IMSI: imsi,
127 KI: ki,
128 MSISDN: msisdn,
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200129 AUTH_ALGO: auth_algo,
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200130 TIMES: times,
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200131 CIPHER: cipher,
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200132 MODEM_FEATURE: modem_feature,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200133 }
134
135def validate(config, schema):
136 '''Make sure the given config dict adheres to the schema.
137 The schema is a dict of 'dict paths' in dot-notation with permitted
138 value type. All leaf nodes are validated, nesting dicts are implicit.
139
140 validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } },
141 { 'a': int,
142 'b.b1': str,
143 'b.b2[]': int } )
144
145 Raise a ValueError in case the schema is violated.
146 '''
147
148 def validate_item(path, value, schema):
149 want_type = schema.get(path)
150
151 if is_list(value):
152 if want_type:
153 raise ValueError('config item is a list, should be %r: %r' % (want_type, path))
154 path = path + '[]'
155 want_type = schema.get(path)
156
157 if not want_type:
158 if is_dict(value):
159 nest(path, value, schema)
160 return
161 if is_list(value) and value:
162 for list_v in value:
163 validate_item(path, list_v, schema)
164 return
165 raise ValueError('config item not known: %r' % path)
166
167 if want_type not in SCHEMA_TYPES:
168 raise ValueError('unknown type %r at %r' % (want_type, path))
169
170 if is_dict(value):
171 raise ValueError('config item is dict but should be a leaf node of type %r: %r'
172 % (want_type, path))
173
174 if is_list(value):
175 for list_v in value:
176 validate_item(path, list_v, schema)
177 return
178
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200179 log.ctx(path)
180 type_validator = SCHEMA_TYPES.get(want_type)
181 type_validator(value)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200182
183 def nest(parent_path, config, schema):
184 if parent_path:
185 parent_path = parent_path + '.'
186 else:
187 parent_path = ''
188 for k,v in config.items():
189 if not KEY_RE.fullmatch(k):
190 raise ValueError('invalid config key: %r' % k)
191 path = parent_path + k
192 validate_item(path, v, schema)
193
194 nest(None, config, schema)
195
196# vim: expandtab tabstop=4 shiftwidth=4