[autotest] Ensure that ownership state is properly cleaned during test setup

The session_manager persists signed settings back to disk on graceful exit.  As such, removing the signed settings file and then killing the session manager does nothing.  The right thing to do is take down the UI, fiddle with the on-disk state, and then bring the UI back up again.

BUG=14008
TEST=suite_Smoke a couple times in a row.
STATUS=Fixed

Change-Id: Iec2dffc98849bd62229ade645509aaf5439c6fff

Revert "Disable OwnershipTaken, OwnershipNotRetaken"

This reverts commit 136bbe09befb71b2c5e10d706908f6bdb8989987.

R=davemoore@chromium.org

Review URL: http://codereview.chromium.org/6825028
diff --git a/client/cros/cros_ui.py b/client/cros/cros_ui.py
index ad40759..a5f2a7a 100644
--- a/client/cros/cros_ui.py
+++ b/client/cros/cros_ui.py
@@ -65,6 +65,14 @@
     return autox.AutoX()
 
 
+def stop():
+    return utils.system("stop ui")
+
+
+def start():
+    return utils.system("start ui")
+
+
 class ChromeSession(object):
     """
     A class to open a tab within the running browser process.
diff --git a/client/cros/cros_ui_test.py b/client/cros/cros_ui_test.py
index 7659b55..a182444 100644
--- a/client/cros/cros_ui_test.py
+++ b/client/cros/cros_ui_test.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
@@ -168,16 +168,11 @@
         if creds:
             self.start_authserver()
 
-        # Fake ownership unless the test is explicitly testing owner creation.
-        if not is_creating_owner and not os.access(
-            constants.OWNER_KEY_FILE, os.F_OK):
-            logging.info('Owner credentials not found. Faking ownership...')
-            self.__fake_ownership()
-            self.fake_owner = True
-
         if login.logged_in():
             login.attempt_logout()
 
+        # The UI must be taken down to ensure that no stale state persists.
+        cros_ui.stop()
         (self.username, self.password) = self.__resolve_creds(creds)
         # Ensure there's no stale cryptohome from previous tests.
         try:
@@ -185,10 +180,16 @@
         except cryptohome.ChromiumOSError as err:
             logging.error(err)
 
-        if is_creating_owner:
+        # Fake ownership unless the test is explicitly testing owner creation.
+        if not is_creating_owner:
+            logging.info('Faking ownership...')
+            self.__fake_ownership()
+            self.fake_owner = True
+        else:
             logging.info('Erasing stale owner state.')
             ownership.clear_ownership()
             self.fake_owner = False
+        cros_ui.start()
 
         login.refresh_login_screen()
         if self.auto_login:
diff --git a/client/site_tests/suite_Smoke/control b/client/site_tests/suite_Smoke/control
index c3d0212..0ea574e 100644
--- a/client/site_tests/suite_Smoke/control
+++ b/client/site_tests/suite_Smoke/control
@@ -28,9 +28,8 @@
 job.run_test('login_CryptohomeUnmounted')
 job.run_test('login_LoginSuccess', tag='default')
 job.run_test('login_LoginSuccess', creds='$apps', tag='apps')
-# http://crosbug.com/
-#job.run_test('login_OwnershipTaken')
-#job.run_test('login_OwnershipNotRetaken')
+job.run_test('login_OwnershipTaken')
+job.run_test('login_OwnershipNotRetaken')
 job.run_test('platform_FilePerms')
 job.run_test('platform_OSLimits')
 
diff --git a/server/site_tests/suites/control.bvt b/server/site_tests/suites/control.bvt
index 057402a..fa1e23b 100644
--- a/server/site_tests/suites/control.bvt
+++ b/server/site_tests/suites/control.bvt
@@ -46,9 +46,8 @@
   ('login_CryptohomeIncognitoUnmounted', {}),
   ('login_LoginSuccess', {'tag': 'default'}),
   ('login_LoginSuccess', {'creds': '$apps', 'tag': 'apps'}),
-# TODO(cmasone): http://crosbug.com/14008
-#  ('login_OwnershipNotRetaken', {}),
-#  ('login_OwnershipTaken', {}),
+  ('login_OwnershipNotRetaken', {}),
+  ('login_OwnershipTaken', {}),
   ('login_RemoteLogin', {}),
   ('network_DisableInterface', {'iface_name': 'wlan0',
                                 'tag': 'wlan0'}),