1 # Copyright 2015 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import re 6 7 from autotest_lib.client.bin import test, utils 8 from autotest_lib.client.common_lib import error 9 10 11 class _Matcher(object): 12 """Extends regular expression with a match/do not match bit and 13 a saner definition of "match". 14 """ 15 16 17 def __init__(self, pattern): 18 19 self._pattern = pattern 20 # If the pattern starts with !, it means "do not match". 21 if pattern[0] == '!': 22 self._positive_match = False 23 pattern = pattern[1:] 24 else: 25 self._positive_match = True 26 27 # re.match() forces the RE to match from the beginning, but doesn't 28 # require that the RE matches the entire string, so wrap with ^$ even 29 # though the ^ is not strictly needed. 30 self._regexp = re.compile("^" + pattern + "$") 31 32 33 def match(self, string): 34 return bool(self._regexp.match(string)) == self._positive_match 35 36 37 _ALPHANUM = _Matcher("[\d\w]+") 38 _NUM = _Matcher("[\d]+") 39 _HEXNUM = _Matcher("0x[\da-fA-F]+") 40 _BIT = _Matcher("[01]") 41 _ANYTHING = _Matcher("!(\(error\))|") # anything but "(error)" or "" 42 43 def check(var, matcher): 44 """ 45 Runs "crossystem @var" and raises an error 46 if the output does not match @matcher 47 48 @param var: the name of a crossystem variable 49 @param matcher: a matcher that must match the output of crossystem @var 50 51 """ 52 output = utils.system_output("crossystem %s" % var).strip() 53 if not matcher.match(output): 54 raise error.TestFail("crossystem %s = \"%s\", does not match \"%s\"" % 55 (var, output, matcher._pattern)) 56 57 58 class platform_Crossystem(test.test): 59 """See control file for doc""" 60 version = 2 61 62 def run_once(self): 63 """Checks that crossystem works and returns plausible values for 64 a set of variables that are implemented on all platforms. 65 """ 66 67 for var, matcher in ( 68 ("arch", _ALPHANUM), 69 ("cros_debug", _BIT), 70 ("debug_build", _BIT), 71 ("devsw_boot", _BIT), 72 ("devsw_cur", _BIT), 73 ("fwid", _ANYTHING), 74 ("hwid", _ANYTHING), 75 ("loc_idx", _NUM), 76 ("mainfw_act", _ALPHANUM), 77 ("mainfw_type", _ALPHANUM), 78 ("ro_fwid", _ANYTHING), 79 ("tpm_fwver", _HEXNUM), 80 ("tpm_kernver", _HEXNUM), 81 ("wpsw_boot", _BIT), 82 ("wpsw_cur", _BIT), 83 ): 84 check(var, matcher) 85