1 #!/usr/bin/env python 2 # 3 # Copyright 2017 - The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import os 18 19 from metrics.metric import Metric 20 21 22 class AdbHashMetric(Metric): 23 """Gathers metrics on environment variable and a hash of that directory. 24 25 This class will verify that $ADB_VENDOR_KEYS is in the environment variables, 26 return True or False, and then verify that the hash of that directory 27 matches the 'golden' directory. 28 """ 29 _ADV_ENV_VAR = 'ADB_VENDOR_KEYS' 30 _MD5_COMMAND = ('find $ADB_VENDOR_KEYS -not -path \'*/\.*\' -type f ' 31 '-exec md5sum {} + | awk \'{print $1}\' | sort | md5sum') 32 KEYS_PATH = 'keys_path' 33 KEYS_HASH = 'hash' 34 35 def _verify_env(self): 36 """Determines the path of ADB_VENDOR_KEYS. 37 38 Returns: 39 The path to $ADB_VENDOR_KEYS location, or None if env variable isn't 40 set. 41 """ 42 try: 43 return os.environ[self._ADV_ENV_VAR] 44 except KeyError: 45 return None 46 47 def _find_hash(self): 48 """Determines the hash of keys in $ADB_VENDOR_KEYS folder. 49 50 As of now, it just gets the hash, and returns it. 51 52 Returns: 53 The hash of the $ADB_VENDOR_KEYS directory excluding hidden files. 54 """ 55 return self._shell.run(self._MD5_COMMAND).stdout.split(' ')[0] 56 57 def gather_metric(self): 58 """Gathers data on adb keys environment variable, and the hash of the 59 directory. 60 61 Returns: 62 A dictionary with 'env' set to the location of adb_vendor_keys, and 63 key 'hash' with an md5sum as value. 64 """ 65 adb_keys_path = self._verify_env() 66 if adb_keys_path is not None: 67 adb_keys_hash = self._find_hash() 68 else: 69 adb_keys_hash = None 70 71 return {self.KEYS_PATH: adb_keys_path, self.KEYS_HASH: adb_keys_hash} 72