1# Copyright 2014 The Chromium 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 subprocess
6
7from telemetry.internal.util import binary_manager
8from telemetry.core import platform
9from telemetry.core import os_version
10
11def _PathForExecutable(executable_name):
12  """Fetches the executable from cloud storage, and returns its path."""
13  arch_name = platform.GetHostPlatform().GetArchName()
14  return binary_manager.FetchPath(executable_name, arch_name, 'mac')
15
16def IsKeychainLocked():
17  """
18  Returns True if the keychain is locked, or if there is an error determining
19  the keychain state.
20  """
21  path = _PathForExecutable('determine_if_keychain_is_locked')
22
23  child = subprocess.Popen(path, stdout=subprocess.PIPE)
24  child.communicate()
25  return child.returncode != 0
26
27def DoesKeychainHaveTimeout():
28  """
29  Returns True if the keychain will lock itself have a period of time.
30
31  This method will trigger a blocking, modal dialog if the keychain is
32  locked.
33  """
34  command = ("/usr/bin/security", "show-keychain-info")
35  child = subprocess.Popen(command, stderr=subprocess.PIPE)
36  stderr = child.communicate()[1]
37  return "no-timeout" not in stderr
38
39def _IsKeychainConfiguredForBots(service_name, account_name):
40  """
41  Returns True if the keychain entry associated with |service_name| and
42  |account_name| is correctly configured for running telemetry tests on bots.
43
44  This method will trigger a blocking, modal dialog if the keychain is
45  locked.
46  """
47  # The executable requires OSX 10.7+ APIs.
48  if (platform.GetHostPlatform().GetOSVersionName() <
49      os_version.LION):
50    return False
51
52  path = _PathForExecutable('determine_if_keychain_entry_is_decryptable')
53
54  command = (path, service_name, account_name)
55  child = subprocess.Popen(command)
56  child.communicate()
57  return child.returncode == 0
58
59def IsKeychainConfiguredForBotsWithChrome():
60  return _IsKeychainConfiguredForBots("Chrome Safe Storage",
61      "Chrome")
62
63def IsKeychainConfiguredForBotsWithChromium():
64  return _IsKeychainConfiguredForBots("Chromium Safe Storage",
65      "Chromium")
66