gyptest-link-embed-manifest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # Copyright (c) 2013 Yandex LLC. 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. Make sure manifests are embedded in binaries properly. Handling of
  7. AdditionalManifestFiles is tested too.
  8. """
  9. import TestGyp
  10. import sys
  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('embed-manifest.gyp', chdir=CHDIR)
  41. test.build('embed-manifest.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. 'test_manifest_exe.exe', chdir=CHDIR), 1))
  45. test.fail_test(not extract_manifest(test.built_file_path(
  46. 'test_manifest_exe_inc.exe', chdir=CHDIR), 1))
  47. test.fail_test(not extract_manifest(test.built_file_path(
  48. 'test_manifest_dll.dll', chdir=CHDIR), 2))
  49. test.fail_test(not extract_manifest(test.built_file_path(
  50. 'test_manifest_dll_inc.dll', chdir=CHDIR), 2))
  51. # Must contain the Win7 support GUID, but not the Vista one (from
  52. # extra2.manifest).
  53. test.fail_test(
  54. '35138b9a-5d96-4fbd-8e2d-a2440225f93a' not in
  55. extract_manifest(test.built_file_path('test_manifest_extra1.exe',
  56. chdir=CHDIR), 1))
  57. test.fail_test(
  58. 'e2011457-1546-43c5-a5fe-008deee3d3f0' in
  59. extract_manifest(test.built_file_path('test_manifest_extra1.exe',
  60. chdir=CHDIR), 1))
  61. # Must contain both.
  62. test.fail_test(
  63. '35138b9a-5d96-4fbd-8e2d-a2440225f93a' not in
  64. extract_manifest(test.built_file_path('test_manifest_extra2.exe',
  65. chdir=CHDIR), 1))
  66. test.fail_test(
  67. 'e2011457-1546-43c5-a5fe-008deee3d3f0' not in
  68. extract_manifest(test.built_file_path('test_manifest_extra2.exe',
  69. chdir=CHDIR), 1))
  70. # Same as extra2, but using list syntax instead.
  71. test.fail_test(
  72. '35138b9a-5d96-4fbd-8e2d-a2440225f93a' not in
  73. extract_manifest(test.built_file_path('test_manifest_extra_list.exe',
  74. chdir=CHDIR), 1))
  75. test.fail_test(
  76. 'e2011457-1546-43c5-a5fe-008deee3d3f0' not in
  77. extract_manifest(test.built_file_path('test_manifest_extra_list.exe',
  78. chdir=CHDIR), 1))
  79. # Test that incremental linking doesn't force manifest embedding.
  80. test.fail_test(extract_manifest(test.built_file_path(
  81. 'test_manifest_exe_inc_no_embed.exe', chdir=CHDIR), 1))
  82. test.pass_test()