1# Copyright 2015 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 os 6import subprocess 7import tempfile 8import time 9 10from autotest_lib.client.bin import test 11from autotest_lib.client.common_lib import error 12from autotest_lib.client.common_lib.cros import avahi_utils 13 14class platform_DBusMachineIdRotation(test.test): 15 """Verify that /var/lib/dbus/machine-id is properly rotated. 16 17 To avoid interference with existing rotation scripts on the DUT, 18 we actually don't use /var/lib/dbus/machine-id for 19 testing. Instead we allocate a file on test start. 20 """ 21 version = 1 22 23 def initialize(self): 24 """Allocates the machine-id file to use and initialize it.""" 25 fd, self._machine_id_file = tempfile.mkstemp(prefix='machine-id-rot-') 26 os.write(fd, '0123456789abcdef0123456789abcdef\n') 27 os.close(fd) 28 29 def cleanup(self): 30 """Cleans up the allocated machine-id file.""" 31 os.unlink(self._machine_id_file) 32 33 def _get_machine_id(self): 34 """Helper function to read the machine-id file.""" 35 with open(self._machine_id_file, 'r') as f: 36 return f.read().strip() 37 38 def _test_forced_rotation(self): 39 """Check that forced regeneration work.""" 40 machine_id_before = self._get_machine_id() 41 subprocess.check_call(['cros-machine-id-regen', '-r', 'network', 42 '-p', self._machine_id_file]) 43 machine_id_after = self._get_machine_id() 44 if machine_id_before == machine_id_after: 45 raise error.TestFail('Forced rotation failed.') 46 47 def _test_time_limit(self): 48 """Check that the machine-id is not regenerated unless a given amount 49 of time has passed.""" 50 machine_id_before = self._get_machine_id() 51 subprocess.check_call(['cros-machine-id-regen', '-r', 'network', 52 '-p', self._machine_id_file]) 53 machine_id_after = self._get_machine_id() 54 if machine_id_before == machine_id_after: 55 raise error.TestFail('Forced rotation failed.') 56 57 # Now request a very long time limit (1000 seconds) and check 58 # that the machine-id hasn't been regenerated. 59 machine_id_before = self._get_machine_id() 60 subprocess.check_call(['cros-machine-id-regen', '-r', 'periodic', 61 '-t', '1000', '-p', self._machine_id_file]) 62 machine_id_after = self._get_machine_id() 63 if machine_id_before != machine_id_after: 64 raise error.TestFail('Rotated despite timeout not reached.') 65 66 # Sleep ten seconds and request regeneration if ten seconds 67 # have passed. This should always result in regeneration. 68 machine_id_before = self._get_machine_id() 69 time.sleep(10) 70 subprocess.check_call(['cros-machine-id-regen', '-r', 'periodic', 71 '-t', '10', '-p', self._machine_id_file]) 72 machine_id_after = self._get_machine_id() 73 if machine_id_after == machine_id_before: 74 raise error.TestFail('Not rotated despite timeout reached.') 75 76 def _test_avahi_host_name(self): 77 """Check that the Avahi host name is set to the machine-id when 78 cros-machine-id-regen runs.""" 79 # Right now this throws if Avahi is running so manually 80 # catch and ignore any error. 81 try: 82 avahi_utils.avahi_start() 83 except: 84 pass 85 subprocess.check_call(['cros-machine-id-regen', '-r', 'network', 86 '-p', self._machine_id_file]) 87 machine_id = self._get_machine_id() 88 host_name = avahi_utils.avahi_get_hostname() 89 if host_name != machine_id: 90 raise error.TestFail('Avahi host name not updated as expected.') 91 92 def run_once(self): 93 """Run tests related to /var/lib/dbus/machine-id rotation.""" 94 self._test_forced_rotation() 95 self._test_time_limit() 96 self._test_avahi_host_name() 97