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. 4from autotest_lib.client.common_lib import error 5from autotest_lib.client.common_lib import utils 6from autotest_lib.client.cros.enterprise import enterprise_policy_base 7 8 9class policy_DeviceDockMacAddressSource( 10 enterprise_policy_base.EnterprisePolicyTest): 11 """Test for setting the DeviceDockMacAddressSource policy. 12 13 This test checks which MAC address will be used when a 14 dock is connected to the device. 15 16 """ 17 version = 1 18 _POLICY = 'DeviceDockMacAddressSource' 19 20 21 def _get_device_name(self): 22 """Figure out which ethernet port is the dut. 23 24 Since dut is the one plugged into the internet it's the one 25 that has 'BROADCAST' and 'state UP' in 'ip link'. 26 Example: "2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500" 27 " qdisc mq state UP". 28 """ 29 active_ethernet = utils.run("ip link | grep 'BROADCAST.*state UP'") 30 device_name = active_ethernet.stdout.split(":") 31 device_name = device_name[1].lstrip() 32 return device_name 33 34 35 def _get_dock_mac(self, device_name): 36 """Determine the dock's mac address. 37 38 This is done via looking for an "eth" interface in /sys/class/net, 39 that is NOT the interface currently in use by the device. E.g. if 40 the "device_name" is "eth0", we are looking for an interface that 41 has the name "eth" but not "eth0" (such as eth1"). 42 """ 43 dock_ethernet = utils.run( 44 "ls /sys/class/net/ | grep -v {} | grep 'eth'".format( 45 device_name)) 46 dock_ethernet = dock_ethernet.stdout.rstrip() 47 dock_mac = utils.run( 48 'cat /sys/class/net/{}/address'.format(dock_ethernet)) 49 dock_mac = dock_mac.stdout.lower().rstrip() 50 return dock_mac 51 52 53 def _get_dut_mac(self, device_name): 54 """Grab duts's mac address.""" 55 dut_mac = utils.run( 56 'cat /sys/class/net/{}/address'.format(device_name)) 57 dut_mac = dut_mac.stdout.lower().rstrip() 58 return dut_mac 59 60 61 def _get_designated_mac(self): 62 """Device's designated dock MAC address.""" 63 desig_mac = utils.run('vpd -g dock_mac') 64 desig_mac = desig_mac.stdout.lower().rstrip() 65 return desig_mac 66 67 68 def run_once(self, case, enroll=True, check_mac=False): 69 """ 70 Entry point of this test. 71 72 @param case: True, False, or None for the value of the policy. 73 74 """ 75 76 TEST_CASES = { 77 'designated_mac': 1, 78 'device_mac': 2, 79 'dock_mac': 3 80 } 81 82 if enroll: 83 case_value = TEST_CASES[case] 84 self.setup_case( 85 device_policies={self._POLICY: case_value}, enroll=enroll) 86 87 if check_mac: 88 device_name = self._get_device_name() 89 dock_mac = self._get_dock_mac(device_name) 90 dut_mac = self._get_dut_mac(device_name) 91 desig_mac = self._get_designated_mac() 92 93 if case is 'designated_mac': 94 if dock_mac != desig_mac: 95 raise error.TestFail( 96 'Dock MAC address should match the designated MAC ' 97 'address and it does not.') 98 elif case is 'device_mac': 99 if dut_mac != dock_mac: 100 raise error.TestFail( 101 'Dock MAC address should match the device MAC ' 102 'address and it does not.') 103 else: 104 if dock_mac == dut_mac or dock_mac == desig_mac: 105 raise error.TestFail( 106 'Dock MAC should not match any other MAC addresses ' 107 'but it does.') 108