Index: chrome/test/functional/chromoting/mock_pref_pane.py |
=================================================================== |
--- chrome/test/functional/chromoting/mock_pref_pane.py (revision 0) |
+++ chrome/test/functional/chromoting/mock_pref_pane.py (revision 0) |
@@ -0,0 +1,99 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Mock pref pane for testing purpose.""" |
+ |
+import Foundation |
+import os |
+import signal |
+import subprocess |
+import sys |
+import tempfile |
+import time |
+ |
+ |
+_SERVICE_NAME = "org.chromium.chromoting" |
Nirnimesh
2012/08/06 19:13:53
use ' instead of " for consistency here and everyw
yihongg
2012/08/08 01:04:48
Done.
|
+ |
+ |
+def _GetJobPid(): |
+ """Gets the org.chromium.chromoting job id.""" |
+ process = subprocess.Popen(["launchctl", "list"], stdout=subprocess.PIPE) |
+ pid = None |
+ for line in process.stdout: |
+ # Format is: |
+ # 12345 - my.job (if my.job is running, number is job's PID) |
+ # - 0 my.other.job (if my.other.job is not running) |
+ fields = line.strip().split('\t') |
+ if fields[2] == _SERVICE_NAME and fields[0] != "-": |
+ pid = fields[0] |
+ break |
+ process.wait() |
+ return pid |
+ |
+ |
+def main(): |
+ """Handles the mock pref pane actions.""" |
+ print "*** Started mock pref pane ***" |
+ print "*** EUID=%d, UID=%d ***" % (os.geteuid(), os.getuid()) |
+ |
+ real_user_id = os.getuid() |
+ config_file = os.path.join(tempfile.gettempdir(), "%s.json" % _SERVICE_NAME) |
+ tool_script = "/Library/PrivilegedHelperTools/%s.me2me.sh" % _SERVICE_NAME |
Nirnimesh
2012/08/06 19:13:53
This is for mac only. what about other platforms?
yihongg
2012/08/08 01:04:48
This is mac only.
|
+ |
+ if sys.argv[1] == 'enable': |
+ # Elevate privileges, otherwise tool_script executes with EUID != 0. |
+ os.setuid(0) |
+ subprocess.call([tool_script, "--enable"], stdin=open(config_file)) |
+ |
+ # Drop privileges, start the launchd job as the logged-in user. |
+ os.setuid(real_user_id) |
+ subprocess.call(["launchctl", "start", _SERVICE_NAME]) |
+ |
+ # Starting a launchd job is an asynchronous operation that typically takes |
+ # a couple of seconds, so poll until the job has started. |
+ for i in range(1, 10): |
+ if _GetJobPid(): |
+ print "*** org.chromium.chromoting is running ***" |
+ break |
+ time.sleep(2) |
+ elif sys.argv[1] == 'disable': |
+ # Elevate privileges, otherwise tool_script executes with EUID != 0. |
+ os.setuid(0) |
+ subprocess.call([tool_script, "--disable"], stdin=open(config_file)) |
+ |
+ # Drop privileges, stop the launchd job as the logged-in user. |
+ os.setuid(real_user_id) |
+ subprocess.call(["launchctl", "stop", _SERVICE_NAME]) |
+ |
+ # Stopping a launchd job is an asynchronous operation that typically takes |
+ # a couple of seconds, so poll until the job has stopped. |
+ for i in range(1, 10): |
Nirnimesh
2012/08/06 19:13:53
i -> _
since i is unused
yihongg
2012/08/08 01:04:48
Done.
|
+ if not _GetJobPid(): |
+ print "*** org.chromium.chromoting is not running ***" |
+ break |
+ time.sleep(2) |
Nirnimesh
2012/08/06 19:13:53
Sleeping is not allowed, since it makes tests flak
yihongg
2012/08/08 01:04:48
Agree sleep will cause flaky test in general. For
|
+ elif sys.argv[1] == 'changepin': |
+ # Elevate privileges, otherwise tool_script executes with EUID != 0. |
+ os.setuid(0) |
+ subprocess.call([tool_script, "--save-config"], stdin=open(config_file)) |
+ |
+ # Drop privileges and send SIGHUP to org.chromium.chromoting |
+ os.setuid(real_user_id) |
+ os.kill(int(_GetJobPid()), signal.SIGHUP) |
+ else: |
+ print >> sys.stderr, ( |
+ 'Invalid syntax') |
+ return 1 |
+ |
+ notif_center = Foundation.NSDistributedNotificationCenter.defaultCenter() |
+ notif_center.postNotificationName_object_userInfo_( |
+ _SERVICE_NAME + ".update_succeeded", None, None) |
+ |
+ time.sleep(10) |
Nirnimesh
2012/08/06 19:13:53
what is this sleep for?
yihongg
2012/08/08 01:04:48
This is put here to wait until webapp gets notific
|
+ |
+ return 0 |
Nirnimesh
2012/08/06 19:13:53
remove
yihongg
2012/08/08 01:04:48
Done.
|
+ |
+if __name__ == '__main__': |
+ sys.exit(main()) |
+ |