1 #!/usr/bin/env python 2 3 # Copyright (c) 2012 Google Inc. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 """ 8 Make sure we generate a manifest file when linking binaries, including 9 handling AdditionalManifestFiles. 10 """ 11 12 import TestGyp 13 14 import sys 15 16 if sys.platform == 'win32': 17 import pywintypes 18 import win32api 19 import winerror 20 21 RT_MANIFEST = 24 22 23 class LoadLibrary(object): 24 """Context manager for loading and releasing binaries in Windows. 25 Yields the handle of the binary loaded.""" 26 def __init__(self, path): 27 self._path = path 28 self._handle = None 29 30 def __enter__(self): 31 self._handle = win32api.LoadLibrary(self._path) 32 return self._handle 33 34 def __exit__(self, type, value, traceback): 35 win32api.FreeLibrary(self._handle) 36 37 def extract_manifest(path, resource_name): 38 """Reads manifest from |path| and returns it as a string. 39 Returns None is there is no such manifest.""" 40 with LoadLibrary(path) as handle: 41 try: 42 return win32api.LoadResource(handle, RT_MANIFEST, resource_name) 43 except pywintypes.error as error: 44 if error.args[0] == winerror.ERROR_RESOURCE_DATA_NOT_FOUND: 45 return None 46 else: 47 raise 48 49 test = TestGyp.TestGyp(formats=['msvs', 'ninja']) 50 51 CHDIR = 'linker-flags' 52 test.run_gyp('generate-manifest.gyp', chdir=CHDIR) 53 test.build('generate-manifest.gyp', test.ALL, chdir=CHDIR) 54 55 # Make sure that generation of .generated.manifest does not cause a relink. 56 test.run_gyp('generate-manifest.gyp', chdir=CHDIR) 57 test.up_to_date('generate-manifest.gyp', test.ALL, chdir=CHDIR) 58 59 def test_manifest(filename, generate_manifest, embedded_manifest, 60 extra_manifest): 61 exe_file = test.built_file_path(filename, chdir=CHDIR) 62 if not generate_manifest: 63 test.must_not_exist(exe_file + '.manifest') 64 manifest = extract_manifest(exe_file, 1) 65 test.fail_test(manifest) 66 return 67 if embedded_manifest: 68 manifest = extract_manifest(exe_file, 1) 69 test.fail_test(not manifest) 70 else: 71 test.must_exist(exe_file + '.manifest') 72 manifest = test.read(exe_file + '.manifest') 73 test.fail_test(not manifest) 74 test.fail_test(extract_manifest(exe_file, 1)) 75 if generate_manifest: 76 test.must_contain_any_line(manifest, 'requestedExecutionLevel') 77 if extra_manifest: 78 test.must_contain_any_line(manifest, 79 '35138b9a-5d96-4fbd-8e2d-a2440225f93a') 80 test.must_contain_any_line(manifest, 81 'e2011457-1546-43c5-a5fe-008deee3d3f0') 82 83 test_manifest('test_generate_manifest_true.exe', 84 generate_manifest=True, 85 embedded_manifest=False, 86 extra_manifest=False) 87 test_manifest('test_generate_manifest_false.exe', 88 generate_manifest=False, 89 embedded_manifest=False, 90 extra_manifest=False) 91 test_manifest('test_generate_manifest_default.exe', 92 generate_manifest=True, 93 embedded_manifest=False, 94 extra_manifest=False) 95 test_manifest('test_generate_manifest_true_as_embedded.exe', 96 generate_manifest=True, 97 embedded_manifest=True, 98 extra_manifest=False) 99 test_manifest('test_generate_manifest_false_as_embedded.exe', 100 generate_manifest=False, 101 embedded_manifest=True, 102 extra_manifest=False) 103 test_manifest('test_generate_manifest_default_as_embedded.exe', 104 generate_manifest=True, 105 embedded_manifest=True, 106 extra_manifest=False) 107 test_manifest('test_generate_manifest_true_with_extra_manifest.exe', 108 generate_manifest=True, 109 embedded_manifest=False, 110 extra_manifest=True) 111 test_manifest('test_generate_manifest_false_with_extra_manifest.exe', 112 generate_manifest=False, 113 embedded_manifest=False, 114 extra_manifest=True) 115 test_manifest('test_generate_manifest_true_with_extra_manifest_list.exe', 116 generate_manifest=True, 117 embedded_manifest=False, 118 extra_manifest=True) 119 test_manifest('test_generate_manifest_false_with_extra_manifest_list.exe', 120 generate_manifest=False, 121 embedded_manifest=False, 122 extra_manifest=True) 123 test_manifest('test_generate_manifest_default_embed_default.exe', 124 generate_manifest=True, 125 embedded_manifest=True, 126 extra_manifest=False) 127 test.pass_test() 128