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
17import logging
18from proc_tests import KernelProcFileTestBase
19from proc_tests.KernelProcFileTestBase import repeat_rule
20
21
22class ProcDiskstatsTest(KernelProcFileTestBase.KernelProcFileTestBase):
23    '''/proc/diskstats displays I/O statistics of block devices.'''
24
25    t_ignore = ' '
26    start = 'lines'
27    p_lines = repeat_rule('line')
28
29    def p_line(self, p):
30        '''line : NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE
31                | NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE
32                | NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE'''
33        p[0] = p[1:]
34
35    def get_path(self):
36        return "/proc/diskstats"
37
38class ProcMountsTest(KernelProcFileTestBase.KernelProcFileTestBase):
39    '''/proc/self/mounts lists the mounted filesystems.
40
41    /proc/mounts must symlink to this file.'''
42
43    def parse_contents(self, contents):
44        if len(contents) == 0 or contents[-1] != '\n':
45            raise SyntaxError('Missing final newline')
46        result = []
47        for line in contents.split('\n')[:-1]:
48            parsed = line.split(' ')
49            parsed[3] = parsed[3].split(',')
50            result.append(parsed)
51        return result
52
53    def result_correct(self, parse_results):
54        for line in parse_results:
55            if len(line[3]) < 1 or line[3][0] not in {'rw', 'ro'}:
56                logging.error("First attribute must be rw or ro")
57                return False
58            if line[4] != '0' or line[5] != '0':
59                logging.error("Last 2 columns must be 0")
60                return False
61        return True
62
63    def prepare_test(self,  dut):
64        # Follow the symlink
65        out, err, r_code = dut.shell.Execute('readlink /proc/mounts')
66        if r_code != 0:
67            return False
68        return out == 'self/mounts\n'
69
70    def get_path(self):
71        return "/proc/self/mounts"
72
73class ProcFilesystemsTest(KernelProcFileTestBase.KernelProcFileTestBase):
74    '''/proc/filesystems lists filesystems currently supported by kernel.'''
75
76    def parse_contents(self, contents):
77        if len(contents) == 0 or contents[-1] != '\n':
78            raise SyntaxError('Missing final newline')
79
80        result = []
81        for line in contents.split('\n')[:-1]:
82            parsed = line.split('\t')
83            num_columns = len(parsed)
84            if num_columns != 2:
85                raise SyntaxError('Wrong number of columns.')
86            result.append(parsed)
87        return result
88
89    def get_path(self):
90        return "/proc/filesystems"
91
92class ProcSwapsTest(KernelProcFileTestBase.KernelProcFileTestBase):
93    '''/proc/swaps measures swap space utilization.'''
94
95    REQUIRED_COLUMNS = {
96        'Filename',
97        'Type',
98        'Size',
99        'Used',
100        'Priority'
101    }
102
103    def parse_contents(self, contents):
104        if len(contents) == 0 or contents[-1] != '\n':
105            raise SyntaxError('Missing final newline')
106        return list(map(lambda x: x.split(), contents.split('\n')[:-1]))
107
108    def result_correct(self, result):
109        return self.REQUIRED_COLUMNS.issubset(result[0])
110
111    def get_path(self):
112        return "/proc/swaps"
113
114    def file_optional(self, shell=None, dut=None):
115        # It is not mandatory to have this file present
116        return True
117