OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Base class for tests that need to update the policies enforced by Chrome. | 5 """Base class for tests that need to update the policies enforced by Chrome. |
6 | 6 |
7 Subclasses can call SetUserPolicy (ChromeOS, Linux, Windows) and | 7 Subclasses can call SetUserPolicy (ChromeOS, Linux, Windows) and |
8 SetDevicePolicy (ChromeOS only) with a dictionary of the policies to install. | 8 SetDevicePolicy (ChromeOS only) with a dictionary of the policies to install. |
9 | 9 |
10 The current implementation depends on the platform. The implementations might | 10 The current implementation depends on the platform. The implementations might |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 # TODO(bartfab): Extend the C++ wrapper that starts the mock DMServer so | 95 # TODO(bartfab): Extend the C++ wrapper that starts the mock DMServer so |
96 # that an owner can be passed in. Without this, the server will assume that | 96 # that an owner can be passed in. Without this, the server will assume that |
97 # the owner is user@example.com and for consistency, so must we. | 97 # the owner is user@example.com and for consistency, so must we. |
98 owner = 'user@example.com' | 98 owner = 'user@example.com' |
99 # Subclasses may override these credentials to fake enrollment into another | 99 # Subclasses may override these credentials to fake enrollment into another |
100 # mode or use different device and machine IDs. | 100 # mode or use different device and machine IDs. |
101 mode = 'enterprise' | 101 mode = 'enterprise' |
102 device_id = string.upper(str(uuid.uuid4())) | 102 device_id = string.upper(str(uuid.uuid4())) |
103 machine_id = 'CROSTEST' | 103 machine_id = 'CROSTEST' |
104 | 104 |
| 105 _auth_server = None |
| 106 _dns_server = None |
| 107 |
105 @staticmethod | 108 @staticmethod |
106 def _Call(command, check=False): | 109 def _Call(command, check=False): |
107 """Invokes a subprocess and optionally asserts the return value is zero.""" | 110 """Invokes a subprocess and optionally asserts the return value is zero.""" |
108 with open(os.devnull, 'w') as devnull: | 111 with open(os.devnull, 'w') as devnull: |
109 if check: | 112 if check: |
110 return subprocess.check_call(command.split(' '), stdout=devnull) | 113 return subprocess.check_call(command.split(' '), stdout=devnull) |
111 else: | 114 else: |
112 return subprocess.call(command.split(' '), stdout=devnull) | 115 return subprocess.call(command.split(' '), stdout=devnull) |
113 | 116 |
114 def _WriteFile(self, path, content): | 117 def _WriteFile(self, path, content): |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 # Restart session manager and Chrome. | 317 # Restart session manager and Chrome. |
315 self._StartSessionManagerAndChrome() | 318 self._StartSessionManagerAndChrome() |
316 | 319 |
317 def _tearDownWithSessionManagerStopped(self): | 320 def _tearDownWithSessionManagerStopped(self): |
318 """Resets the test environment after stopping the session manager.""" | 321 """Resets the test environment after stopping the session manager.""" |
319 assert self.IsChromeOS() | 322 assert self.IsChromeOS() |
320 logging.debug('Stopping session manager') | 323 logging.debug('Stopping session manager') |
321 cros_ui.stop(allow_fail=True) | 324 cros_ui.stop(allow_fail=True) |
322 | 325 |
323 # Stop mock GAIA server. | 326 # Stop mock GAIA server. |
324 self._auth_server.stop() | 327 if self._auth_server: |
| 328 self._auth_server.stop() |
325 | 329 |
326 # Reenable TPM if present. | 330 # Reenable TPM if present. |
327 if os.path.exists(TPM_SYSFS_PATH): | 331 if os.path.exists(TPM_SYSFS_PATH): |
328 self._Call('umount %s' % os.path.realpath(TPM_SYSFS_PATH)) | 332 self._Call('umount %s' % os.path.realpath(TPM_SYSFS_PATH)) |
329 | 333 |
330 # Clear install attributes and restart cryptohomed to pick up the change. | 334 # Clear install attributes and restart cryptohomed to pick up the change. |
331 self._ClearInstallAttributesOnChromeOS() | 335 self._ClearInstallAttributesOnChromeOS() |
332 | 336 |
333 # Stop mock DNS server. | 337 # Stop mock DNS server. |
334 self._dns_server.stop() | 338 if self._dns_server: |
| 339 self._dns_server.stop() |
335 | 340 |
336 # Stop mock DMServer. | 341 # Stop mock DMServer. |
337 self.StopHTTPServer(self._http_server) | 342 self.StopHTTPServer(self._http_server) |
338 | 343 |
339 # Clear the policy served. | 344 # Clear the policy served. |
340 pyauto_utils.RemovePath(self._temp_data_dir) | 345 pyauto_utils.RemovePath(self._temp_data_dir) |
341 | 346 |
342 # Remove the device policy blob. | 347 # Remove the device policy blob. |
343 self._RemoveIfExists(constants.OWNER_KEY_FILE) | 348 self._RemoveIfExists(constants.OWNER_KEY_FILE) |
344 self._RemoveIfExists(constants.SIGNED_POLICY_FILE) | 349 self._RemoveIfExists(constants.SIGNED_POLICY_FILE) |
345 | 350 |
346 # Remove any existing vaults. | 351 # Remove any existing vaults. |
347 self.RemoveAllCryptohomeVaultsOnChromeOS() | 352 self.RemoveAllCryptohomeVaultsOnChromeOS() |
348 | 353 |
349 # Restart session manager and Chrome. | 354 # Restart session manager and Chrome. |
350 self._StartSessionManagerAndChrome() | 355 self._StartSessionManagerAndChrome() |
351 | 356 |
352 def setUp(self): | 357 def setUp(self): |
353 """Sets up the platform for policy testing. | 358 """Sets up the platform for policy testing. |
354 | 359 |
355 On ChromeOS, part of the setup involves restarting the session manager to | 360 On ChromeOS, part of the setup involves restarting the session manager to |
356 inject an initial device policy blob. | 361 inject an initial device policy blob. |
357 """ | 362 """ |
358 if self.IsChromeOS(): | 363 if self.IsChromeOS(): |
359 # Perform the remainder of the setup with the device manager stopped. | 364 # Perform the remainder of the setup with the device manager stopped. |
360 self.WaitForSessionManagerRestart( | 365 try: |
361 self._SetUpWithSessionManagerStopped) | 366 self.WaitForSessionManagerRestart( |
| 367 self._SetUpWithSessionManagerStopped) |
| 368 except: |
| 369 # Destroy the non re-entrant services. |
| 370 if self._auth_server: |
| 371 self._auth_server.stop() |
| 372 if self._dns_server: |
| 373 self._dns_server.stop() |
| 374 raise |
362 | 375 |
363 pyauto.PyUITest.setUp(self) | 376 pyauto.PyUITest.setUp(self) |
364 self._branding = self.GetBrowserInfo()['properties']['branding'] | 377 self._branding = self.GetBrowserInfo()['properties']['branding'] |
365 | 378 |
366 def tearDown(self): | 379 def tearDown(self): |
367 """Cleans up the policies and related files created in tests.""" | 380 """Cleans up the policies and related files created in tests.""" |
368 if self.IsChromeOS(): | 381 if self.IsChromeOS(): |
369 # Perform the cleanup with the device manager stopped. | 382 # Perform the cleanup with the device manager stopped. |
370 self.WaitForSessionManagerRestart(self._tearDownWithSessionManagerStopped) | 383 self.WaitForSessionManagerRestart(self._tearDownWithSessionManagerStopped) |
371 else: | 384 else: |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 Args: | 574 Args: |
562 device_policy: The device policy to set. None clears it. | 575 device_policy: The device policy to set. None clears it. |
563 refresh: If True, Chrome will refresh and apply the new policy. | 576 refresh: If True, Chrome will refresh and apply the new policy. |
564 Requires Chrome to be alive for it. | 577 Requires Chrome to be alive for it. |
565 """ | 578 """ |
566 assert self.IsChromeOS() | 579 assert self.IsChromeOS() |
567 self._device_policy = device_policy or {} | 580 self._device_policy = device_policy or {} |
568 self._WritePolicyOnChromeOS() | 581 self._WritePolicyOnChromeOS() |
569 if refresh: | 582 if refresh: |
570 self.RefreshPolicies() | 583 self.RefreshPolicies() |
OLD | NEW |