1# Copyright 2019 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
5import logging
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros.network import iw_runner
10
11class network_WiFiHECaps(test.test):
12    """Test the specified HE client capabilities."""
13    version = 1
14
15    def run_once(self, phy=None, features=None):
16        """
17        Check for support of the features specified in control file.
18
19        Features are passed in as a list of lists containing string constants.
20        The test will pass if the DUT supports at least one string in each
21        list. Essentially, all the elements in the list are joined with AND
22        while all the elements of a list are joined with OR.
23
24        @param phy string name of wifi phy to use, or None to allow the
25                test to choose.
26        @param features list of lists of string constants from iw_runner
27                specifying the HE features to check on the DUT.
28
29        """
30        iw = iw_runner.IwRunner()
31        if not phy:
32            phys = iw.list_phys()
33            if not phys:
34                raise error.TestError('No valid WiFi phy found')
35            phy = phys[0].name
36        if not iw.he_supported():
37            raise error.TestNAError('HE not supported by DUT')
38
39        phy_info = iw.get_info()
40        if not phy_info:
41            raise error.TestError('Could not get phy info using iw_runner')
42
43        if not features:
44            features = []
45
46        featurelist = [f for inner_list in features for f in inner_list]
47        is_supported = {f : False for f in featurelist}
48        values = {}
49
50        for line in phy_info.splitlines():
51            line = line.strip()
52            for f in featurelist:
53                if not is_supported[f] and f in line:
54                    is_supported[f] = True
55                    l = line.split(':', 1)
56                    if len(l) > 1:
57                        values[f] = l[1].strip()
58                    break
59
60        supported = ['These features are supported by the DUT:']
61        not_supported = ['These features are NOT supported by the DUT:']
62        for f in featurelist:
63            if is_supported[f]:
64                if values.get(f, None):
65                    f += ('; has value %s' % values[f])
66                supported.append(f)
67            else:
68                not_supported.append(f)
69        logging.info(' '.join(supported))
70        logging.info(' '.join(not_supported))
71
72        for inner_list in features:
73            list_passed = False
74            for f in inner_list:
75                if is_supported[f]:
76                    list_passed = True
77                    break
78            if not list_passed:
79                raise error.TestError('Test failed because none of %r are '
80                                      'supported by the DUT.' % inner_list)
81