gyptest-link-enable-uac.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # Copyright 2013 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Verifies that embedding UAC information into the manifest works.
  7. """
  8. import TestGyp
  9. import sys
  10. from xml.dom.minidom import parseString
  11. if sys.platform == 'win32':
  12. import pywintypes
  13. import win32api
  14. import winerror
  15. RT_MANIFEST = 24
  16. class LoadLibrary(object):
  17. """Context manager for loading and releasing binaries in Windows.
  18. Yields the handle of the binary loaded."""
  19. def __init__(self, path):
  20. self._path = path
  21. self._handle = None
  22. def __enter__(self):
  23. self._handle = win32api.LoadLibrary(self._path)
  24. return self._handle
  25. def __exit__(self, type, value, traceback):
  26. win32api.FreeLibrary(self._handle)
  27. def extract_manifest(path, resource_name):
  28. """Reads manifest from |path| and returns it as a string.
  29. Returns None is there is no such manifest."""
  30. with LoadLibrary(path) as handle:
  31. try:
  32. return win32api.LoadResource(handle, RT_MANIFEST, resource_name)
  33. except pywintypes.error as error:
  34. if error.args[0] == winerror.ERROR_RESOURCE_DATA_NOT_FOUND:
  35. return None
  36. else:
  37. raise
  38. test = TestGyp.TestGyp(formats=['msvs', 'ninja'])
  39. CHDIR = 'linker-flags'
  40. test.run_gyp('enable-uac.gyp', chdir=CHDIR)
  41. test.build('enable-uac.gyp', test.ALL, chdir=CHDIR)
  42. # The following binaries must contain a manifest embedded.
  43. test.fail_test(not extract_manifest(test.built_file_path(
  44. 'enable_uac.exe', chdir=CHDIR), 1))
  45. test.fail_test(not extract_manifest(test.built_file_path(
  46. 'enable_uac_no.exe', chdir=CHDIR), 1))
  47. test.fail_test(not extract_manifest(test.built_file_path(
  48. 'enable_uac_admin.exe', chdir=CHDIR), 1))
  49. # Verify that <requestedExecutionLevel level="asInvoker" uiAccess="false" />
  50. # is present.
  51. manifest = parseString(extract_manifest(
  52. test.built_file_path('enable_uac.exe', chdir=CHDIR), 1))
  53. execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  54. test.fail_test(len(execution_level) != 1)
  55. execution_level = execution_level[0].attributes
  56. test.fail_test(not (
  57. execution_level.has_key('level') and
  58. execution_level.has_key('uiAccess') and
  59. execution_level['level'].nodeValue == 'asInvoker' and
  60. execution_level['uiAccess'].nodeValue == 'false'))
  61. # Verify that <requestedExecutionLevel> is not in the menifest.
  62. manifest = parseString(extract_manifest(
  63. test.built_file_path('enable_uac_no.exe', chdir=CHDIR), 1))
  64. execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  65. test.fail_test(len(execution_level) != 0)
  66. # Verify that <requestedExecutionLevel level="requireAdministrator"
  67. # uiAccess="true" /> is present.
  68. manifest = parseString(extract_manifest(
  69. test.built_file_path('enable_uac_admin.exe', chdir=CHDIR), 1))
  70. execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  71. test.fail_test(len(execution_level) != 1)
  72. execution_level = execution_level[0].attributes
  73. test.fail_test(not (
  74. execution_level.has_key('level') and
  75. execution_level.has_key('uiAccess') and
  76. execution_level['level'].nodeValue == 'requireAdministrator' and
  77. execution_level['uiAccess'].nodeValue == 'true'))
  78. test.pass_test()