1#
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17from proc_tests import KernelProcFileTestBase
18from proc_tests.KernelProcFileTestBase import repeat_rule
19
20
21class ProcCpuInfoTest(KernelProcFileTestBase.KernelProcFileTestBase):
22    '''/proc/cpuinfo displays a collection of cpu and architecture specific items.
23
24    The file consists of multiple lines of 'identifier : values' where
25    'identifier' is a space separated name that can end in some tabs, and
26    'values' are space separated items that can also in in a space.
27    Extra newlines are allowed between normal lines.
28    '''
29
30    EXPECTED_FIELDS = [
31        ['processor', ['\t']],
32    ]
33
34    start = 'lines'
35
36    # Any character except for ':' and whitespace is allowed
37    t_STRING = r'[^:^ ^\t^\n]+'
38
39    # Numbers and literals are tokenized as strings instead
40    t_NUMBER = r'x'
41    t_HEX_LITERAL = r'x'
42    t_FLOAT =  r'x'
43
44    p_lines = repeat_rule('line')
45    p_string_spaces = repeat_rule('string_space', zero_ok=True)
46    p_space_items = repeat_rule('space_item', zero_ok=True)
47    p_TABs = repeat_rule('TAB', zero_ok=True)
48
49    def p_line(self, p):
50        '''line : string_spaces STRING TABs COLON space_items SPACE NEWLINE
51                | string_spaces STRING TABs COLON space_items NEWLINE
52                | NEWLINE'''
53        if len(p) == 2:
54            p[0] = []
55            return
56        p[0] = [p[1] + [p[2], p[3]], p[5], p[6]]
57
58    def p_space_item(self, p):
59        'space_item : SPACE STRING'
60        p[0] = p[2]
61
62    def p_string_space(self, p):
63        'string_space : STRING SPACE'
64        p[0] = p[1]
65
66    def result_correct(self, parse_result):
67        expected_fields = self.EXPECTED_FIELDS[:]
68        for line in parse_result:
69            if len(line) > 0:
70                if line[0] in expected_fields:
71                    expected_fields.remove(line[0])
72        return len(expected_fields) == 0
73
74    def get_path(self):
75        return "/proc/cpuinfo"
76
77
78class ProcLoadavgTest(KernelProcFileTestBase.KernelProcFileTestBase):
79    '''/proc/loadavg displays CPU and IO load average over time.'''
80
81    def parse_contents(self, contents):
82        return self.parse_line("{:f} {:f} {:f} {:d}/{:d} {:d}\n", contents)
83
84    def get_path(self):
85        return "/proc/loadavg"
86