1 #!/usr/bin/env python 2 # 3 # Copyright (C) 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 18 import logging 19 import re 20 21 from vts.runners.host import asserts 22 from vts.runners.host import base_test 23 from vts.runners.host import test_runner 24 from vts.utils.python.controllers import android_device 25 from vts.utils.python.file import file_utils 26 27 28 class KernelApiSysfsTest(base_test.BaseTestClass): 29 '''Test cases which check sysfs files.''' 30 31 def setUpClass(self): 32 self.dut = self.registerController(android_device)[0] 33 self.dut.shell.InvokeTerminal( 34 'default') # creates a remote shell instance. 35 self.shell = self.dut.shell.default 36 37 def ConvertToInteger(self, text): 38 '''Check whether a given text is interger. 39 40 Args: 41 text: object, usually a string representing the content of a file 42 43 Returns: 44 bool, True if is integer 45 ''' 46 try: 47 return int(text) 48 except ValueError as e: 49 logging.exception(e) 50 asserts.fail('Content "%s" is not integer' % text) 51 52 def MatchRegex(self, regex, string): 53 '''Check whether a string completely matches a given regex. 54 55 Assertions will fail if given string is not a complete match. 56 57 Args: 58 regex: string, regex pattern to match 59 string: string, given string for matching 60 ''' 61 pattern = re.compile(regex) 62 match = pattern.match(string) 63 message = 'String "%s" is not a complete match of regex "%s".' % ( 64 string, regex) 65 asserts.assertTrue(match is not None, message) 66 asserts.assertEqual(match.start(), 0, message) 67 asserts.assertEqual(match.end(), len(string), message) 68 69 def IsReadOnly(self, path): 70 '''Check whether a given path is read only. 71 72 Assertion will fail if given path does not exist or is not read only. 73 ''' 74 permission = '' 75 try: 76 permission = file_utils.GetPermission(path, self.shell) 77 except IOError as e: 78 logging.exception(e) 79 asserts.fail('Path "%s" does not exist or has invalid ' 80 'permission bits' % path) 81 82 try: 83 asserts.assertTrue( 84 file_utils.IsReadOnly(permission), 85 'path %s is not read only' % path) 86 except IOError as e: 87 logging.exception(e) 88 asserts.fail('Got invalid permission bits "%s" for path "%s"' % 89 (permission, path)) 90 91 def testCpuOnlineFormat(self): 92 '''Check the format of cpu online file. 93 94 Confirm /sys/devices/system/cpu/online exists and is read-only. 95 Parse contents to ensure it is a comma-separated series of ranges 96 (%d-%d) and/or integers. 97 ''' 98 filepath = '/sys/devices/system/cpu/online' 99 self.IsReadOnly(filepath) 100 content = file_utils.ReadFileContent(filepath, self.shell) 101 regex = '(\d+(-\d+)?)(,\d+(-\d+)?)*' 102 if content.endswith('\n'): 103 content = content[:-1] 104 self.MatchRegex(regex, content) 105 106 def testLastResumeReason(self): 107 '''Check /sys/kernel/wakeup_reasons/last_resume_reason.''' 108 filepath = '/sys/kernel/wakeup_reasons/last_resume_reason' 109 self.IsReadOnly(filepath) 110 111 def testKernelMax(self): 112 '''Check the value of /sys/devices/system/cpu/kernel_max.''' 113 filepath = '/sys/devices/system/cpu/kernel_max' 114 self.IsReadOnly(filepath) 115 content = file_utils.ReadFileContent(filepath, self.shell) 116 self.ConvertToInteger(content) 117 118 119 if __name__ == "__main__": 120 test_runner.main() 121