OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import re | 9 import re |
10 | 10 |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 def testAlwaysAuthorizePlugins(self): | 318 def testAlwaysAuthorizePlugins(self): |
319 """Verify plugins are always allowed to run when policy is set.""" | 319 """Verify plugins are always allowed to run when policy is set.""" |
320 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': | 320 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
321 return | 321 return |
322 url = self.GetFileURLForDataPath('plugin', 'java_new.html') | 322 url = self.GetFileURLForDataPath('plugin', 'java_new.html') |
323 self.NavigateToURL(url) | 323 self.NavigateToURL(url) |
324 self.assertFalse(self.WaitForInfobarCount(1)) | 324 self.assertFalse(self.WaitForInfobarCount(1)) |
325 pid = self._GetPluginPID('Java') | 325 pid = self._GetPluginPID('Java') |
326 self.assertTrue(pid, 'No plugin process for java') | 326 self.assertTrue(pid, 'No plugin process for java') |
327 | 327 |
| 328 class EnterpriseTestReverse(pyauto.PyUITest): |
| 329 """Test for the Enterprise features that uses the opposite values of the |
| 330 policies used by above test class 'EnterpriseTest'. |
| 331 |
| 332 Browser preferences will be managed using policies. These managed preferences |
| 333 cannot be modified by user. This works only for Google Chrome, not Chromium. |
| 334 |
| 335 On Linux, assume that 'suid-python' (a python binary setuid root) is |
| 336 available on the machine under /usr/local/bin directory. |
| 337 """ |
| 338 assert pyauto.PyUITest.IsWin() or pyauto.PyUITest.IsLinux(), \ |
| 339 'Only runs on Win or Linux' |
| 340 |
| 341 def Debug(self): |
| 342 """Test method for experimentation. |
| 343 |
| 344 This method will not run automatically. |
| 345 """ |
| 346 import pprint |
| 347 pp = pprint.PrettyPrinter(indent=2) |
| 348 while True: |
| 349 raw_input('Interact with the browser and hit <enter> to dump prefs... ') |
| 350 pp.pprint(self.GetPrefsInfo().Prefs()) |
| 351 |
| 352 @staticmethod |
| 353 def _Cleanup(): |
| 354 """Removes the registry key and its subkeys(if they exist). |
| 355 |
| 356 Win: Registry Key being deleted: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google |
| 357 Linux: Removes the chrome directory from /etc/opt |
| 358 """ |
| 359 if pyauto.PyUITest.IsWin(): |
| 360 if subprocess.call( |
| 361 r'reg query HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google') == 0: |
| 362 logging.debug(r'Removing HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google') |
| 363 subprocess.call(r'reg delete HKLM\Software\Policies\Google /f') |
| 364 elif pyauto.PyUITest.IsLinux(): |
| 365 sudo_cmd_file = os.path.join(os.path.dirname(__file__), |
| 366 'enterprise_helper_linux.py') |
| 367 if os.path.isdir ('/etc/opt/chrome'): |
| 368 logging.debug('Removing directory /etc/opt/chrome/') |
| 369 subprocess.call(['suid-python', sudo_cmd_file, |
| 370 'remove_dir', '/etc/opt/chrome']) |
| 371 |
| 372 @staticmethod |
| 373 def _SetUp(): |
| 374 """Win: Add the registry keys from the .reg file. |
| 375 |
| 376 Removes the registry key and its subkeys if they exist. |
| 377 Adding HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google. |
| 378 |
| 379 Linux: Copy the chrome.json file to the managed directory. |
| 380 Remove /etc/opt/chrome directory if it exists. |
| 381 """ |
| 382 EnterpriseTestReverse._Cleanup() |
| 383 if pyauto.PyUITest.IsWin(): |
| 384 registry_location = os.path.join(EnterpriseTestReverse.DataDir(), |
| 385 'enterprise', 'chrome-add-reverse.reg') |
| 386 # Add the registry keys |
| 387 subprocess.call('reg import %s' % registry_location) |
| 388 elif pyauto.PyUITest.IsLinux(): |
| 389 chrome_json = os.path.join(EnterpriseTestReverse.DataDir(), |
| 390 'enterprise', 'chrome-reverse.json') |
| 391 sudo_cmd_file = os.path.join(os.path.dirname(__file__), |
| 392 'enterprise_helper_linux.py') |
| 393 policies_location = '/etc/opt/chrome/policies/managed' |
| 394 subprocess.call(['suid-python', sudo_cmd_file, |
| 395 'setup_dir', policies_location]) |
| 396 # Copy chrome.json file to the managed directory |
| 397 subprocess.call(['suid-python', sudo_cmd_file, |
| 398 'copy', chrome_json, policies_location]) |
| 399 |
| 400 def setUp(self): |
| 401 # Add policies through registry or json file. |
| 402 self._SetUp() |
| 403 # Check if registries are created in Win. |
| 404 if pyauto.PyUITest.IsWin(): |
| 405 registry_query_code = subprocess.call( |
| 406 r'reg query HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google') |
| 407 assert registry_query_code == 0, 'Could not create registries.' |
| 408 # Check if json file is copied under correct location in Linux. |
| 409 elif pyauto.PyUITest.IsLinux(): |
| 410 policy_file_check = os.path.isfile( |
| 411 '/etc/opt/chrome/policies/managed/chrome-reverse.json') |
| 412 assert policy_file_check, 'Policy file(s) not set up.' |
| 413 pyauto.PyUITest.setUp(self) |
| 414 |
| 415 def tearDown(self): |
| 416 pyauto.PyUITest.tearDown(self) |
| 417 EnterpriseTestReverse._Cleanup() |
| 418 |
| 419 def _CheckIfPrefCanBeModified(self, key, defaultval, newval): |
| 420 """Check if the managed preferences can be modified. |
| 421 |
| 422 Args: |
| 423 key: The preference key that you want to modify |
| 424 defaultval: Default value of the preference that we are trying to modify |
| 425 newval: New value that we are trying to set |
| 426 """ |
| 427 # Check if the default value of the preference is set as expected. |
| 428 self.assertEqual(self.GetPrefsInfo().Prefs(key), defaultval, |
| 429 msg='Default value of the preference is wrong.') |
| 430 self.assertRaises(pyauto.JSONInterfaceError, |
| 431 lambda: self.SetPrefs(key, newval)) |
| 432 |
| 433 def _GetPluginPID(self, plugin_name): |
| 434 """Fetch the pid of the plugin process with name |plugin_name|.""" |
| 435 child_processes = self.GetBrowserInfo()['child_processes'] |
| 436 plugin_type = 'Plug-in' |
| 437 for x in child_processes: |
| 438 if x['type'] == plugin_type and re.search(plugin_name, x['name']): |
| 439 return x['pid'] |
| 440 return None |
| 441 |
| 442 # Tests for options in Basics |
| 443 def testStartupPages(self): |
| 444 """Verify that user cannot modify the startup page options.""" |
| 445 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 446 return |
| 447 # Verify startup option |
| 448 self.assertEquals(0, self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
| 449 self.assertRaises(pyauto.JSONInterfaceError, |
| 450 lambda: self.SetPrefs(pyauto.kRestoreOnStartup, 1)) |
| 451 |
| 452 def testHomePageOptions(self): |
| 453 """Verify that we cannot modify Homepage settings.""" |
| 454 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 455 return |
| 456 # Try to configure home page URL |
| 457 self.assertEquals('http://chromium.org', |
| 458 self.GetPrefsInfo().Prefs('homepage')) |
| 459 self.assertRaises(pyauto.JSONInterfaceError, |
| 460 lambda: self.SetPrefs('homepage', 'http://www.google.com')) |
| 461 |
| 462 # Try to reconfigure NTP as home page |
| 463 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kHomePageIsNewTabPage)) |
| 464 self.assertRaises(pyauto.JSONInterfaceError, |
| 465 lambda: self.SetPrefs(pyauto.kHomePageIsNewTabPage, True)) |
| 466 |
| 467 def testShowHomeButton(self): |
| 468 """Verify that home button option cannot be modified when it's managed.""" |
| 469 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 470 return |
| 471 self._CheckIfPrefCanBeModified(pyauto.kShowHomeButton, False, True) |
| 472 |
| 473 def testInstant(self): |
| 474 """Verify that Instant option cannot be modified.""" |
| 475 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 476 return |
| 477 self._CheckIfPrefCanBeModified(pyauto.kInstantEnabled, False, True) |
| 478 |
| 479 # Tests for options in Personal Stuff |
| 480 def testPasswordManagerEnabled(self): |
| 481 """Verify that password manager preference cannot be modified.""" |
| 482 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 483 return |
| 484 self._CheckIfPrefCanBeModified(pyauto.kPasswordManagerEnabled, False, True) |
| 485 |
| 486 # Tests for options in Under the Hood |
| 487 def testPrivacyPrefs(self): |
| 488 """Verify that the managed preferences cannot be modified.""" |
| 489 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 490 return |
| 491 prefs_list = [ |
| 492 # (preference key, default value, new value) |
| 493 (pyauto.kAlternateErrorPagesEnabled, False, True), |
| 494 (pyauto.kNetworkPredictionEnabled, False, True), |
| 495 (pyauto.kSafeBrowsingEnabled, False, True), |
| 496 (pyauto.kSearchSuggestEnabled, False, True), |
| 497 ] |
| 498 # Check if the policies got applied by trying to modify |
| 499 for key, defaultval, newval in prefs_list: |
| 500 logging.info('Checking pref %s', key) |
| 501 self._CheckIfPrefCanBeModified(key, defaultval, newval) |
| 502 |
| 503 def testNotClearSiteDataOnExit(self): |
| 504 """Verify that clear data on exit preference cannot be modified.""" |
| 505 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 506 return |
| 507 self._CheckIfPrefCanBeModified(pyauto.kClearSiteDataOnExit, False, True) |
| 508 |
| 509 def testUnblockThirdPartyCookies(self): |
| 510 """Verify that block third party cookies preference cannot be modified.""" |
| 511 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 512 return |
| 513 self._CheckIfPrefCanBeModified(pyauto.kBlockThirdPartyCookies, False, True) |
| 514 |
| 515 def testEnableDevTools(self): |
| 516 """Verify that devtools window can be launched.""" |
| 517 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 518 return |
| 519 # DevTools process can be seen by PyAuto only when it's undocked. |
| 520 self.SetPrefs(pyauto.kDevToolsOpenDocked, False) |
| 521 self.ApplyAccelerator(pyauto.IDC_DEV_TOOLS) |
| 522 self.assertEquals(2, len(self.GetBrowserInfo()['windows']), |
| 523 msg='Devtools window not launched.') |
| 524 |
| 525 def testEnableSPDY(self): |
| 526 """Verify that SPDY is enabled.""" |
| 527 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 528 return |
| 529 self.NavigateToURL('chrome://net-internals/#spdy') |
| 530 self.assertEquals(0, |
| 531 self.FindInPage('encrypted.google.com')['match_count']) |
| 532 self.AppendTab(pyauto.GURL('https://encrypted.google.com')) |
| 533 self.assertEquals('Google', self.GetActiveTabTitle()) |
| 534 self.GetBrowserWindow(0).GetTab(0).Reload() |
| 535 self.assertEquals(1, |
| 536 self.FindInPage('encrypted.google.com', tab_index=0)['match_count'], |
| 537 msg='SPDY is not enabled.') |
| 538 |
| 539 def testSetDownloadDirectory(self): |
| 540 """Verify that the downloads directory and prompt for download preferences |
| 541 can be modified. |
| 542 """ |
| 543 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 544 return |
| 545 if self.IsWin(): |
| 546 download_default_dir = os.path.join(os.getenv('USERPROFILE'),'Downloads') |
| 547 self.assertEqual(download_default_dir, |
| 548 self.GetPrefsInfo().Prefs()['download']['default_directory'], |
| 549 msg='Downloads directory is not set correctly.') |
| 550 # Check for changing the download directory location |
| 551 self.SetPrefs(pyauto.kDownloadDefaultDirectory, |
| 552 os.getenv('USERPROFILE')) |
| 553 elif self.IsLinux(): |
| 554 download_default_dir = os.path.join(os.getenv('HOME'), 'Downloads') |
| 555 self.assertEqual(download_default_dir, |
| 556 self.GetPrefsInfo().Prefs()['download']['default_directory'], |
| 557 msg='Downloads directory is not set correctly.') |
| 558 self.SetPrefs(pyauto.kDownloadDefaultDirectory, |
| 559 os.getenv('HOME')) |
| 560 # Check for changing the option 'Ask for each download' |
| 561 self.SetPrefs(pyauto.kPromptForDownload, False) |
| 562 |
| 563 def testIncognitoDisabled(self): |
| 564 """Verify that incognito window can be launched.""" |
| 565 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 566 return |
| 567 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) |
| 568 self.assertEquals(1, self.GetBrowserWindowCount()) |
| 569 |
| 570 def testEnableBrowsingHistory(self): |
| 571 """Verify that browsing history is being saved.""" |
| 572 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 573 return |
| 574 url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'empty.html')) |
| 575 self.NavigateToURL(url) |
| 576 self.assertTrue(self.GetHistoryInfo().History(), |
| 577 msg='History not is being saved.') |
| 578 |
| 579 def testAlwaysAuthorizePluginsDisabled(self): |
| 580 """Verify plugins are always not allowed to run when policy is set.""" |
| 581 if self.GetBrowserInfo()['properties']['branding'] != 'Google Chrome': |
| 582 return |
| 583 url = self.GetFileURLForDataPath('plugin', 'java_new.html') |
| 584 self.NavigateToURL(url) |
| 585 self.assertTrue(self.WaitForInfobarCount(1)) |
| 586 pid = self._GetPluginPID('Java') |
| 587 self.assertFalse(pid, 'There is a plugin process for java') |
| 588 |
328 | 589 |
329 if __name__ == '__main__': | 590 if __name__ == '__main__': |
330 pyauto_functional.Main() | 591 pyauto_functional.Main() |
OLD | NEW |