summaryrefslogtreecommitdiffstats
path: root/third_party/python/gyp/test/win/gyptest-link-enable-uac.py
blob: 0ddbde5fa5509b6f2999f8c4ef402c195a18f514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python

# Copyright 2013 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Verifies that embedding UAC information into the manifest works.
"""

import TestGyp

import sys
from xml.dom.minidom import parseString

if sys.platform == 'win32':
  import pywintypes
  import win32api
  import winerror

  RT_MANIFEST = 24

  class LoadLibrary(object):
    """Context manager for loading and releasing binaries in Windows.
    Yields the handle of the binary loaded."""
    def __init__(self, path):
      self._path = path
      self._handle = None

    def __enter__(self):
      self._handle = win32api.LoadLibrary(self._path)
      return self._handle

    def __exit__(self, type, value, traceback):
      win32api.FreeLibrary(self._handle)


  def extract_manifest(path, resource_name):
    """Reads manifest from |path| and returns it as a string.
    Returns None is there is no such manifest."""
    with LoadLibrary(path) as handle:
      try:
        return win32api.LoadResource(handle, RT_MANIFEST, resource_name)
      except pywintypes.error as error:
        if error.args[0] == winerror.ERROR_RESOURCE_DATA_NOT_FOUND:
          return None
        else:
          raise

  test = TestGyp.TestGyp(formats=['msvs', 'ninja'])
  CHDIR = 'linker-flags'
  test.run_gyp('enable-uac.gyp', chdir=CHDIR)
  test.build('enable-uac.gyp', test.ALL, chdir=CHDIR)

  # The following binaries must contain a manifest embedded.
  test.fail_test(not extract_manifest(test.built_file_path(
    'enable_uac.exe', chdir=CHDIR), 1))
  test.fail_test(not extract_manifest(test.built_file_path(
    'enable_uac_no.exe', chdir=CHDIR), 1))
  test.fail_test(not extract_manifest(test.built_file_path(
    'enable_uac_admin.exe', chdir=CHDIR), 1))

  # Verify that <requestedExecutionLevel level="asInvoker" uiAccess="false" />
  # is present.
  manifest = parseString(extract_manifest(
      test.built_file_path('enable_uac.exe', chdir=CHDIR), 1))
  execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  test.fail_test(len(execution_level) != 1)
  execution_level = execution_level[0].attributes

  def _has_key(node, key):
    # 'in' doesn't work with the NamedNodeMap interface in Python2,
    # but 'has_key' was removed from it in Python3, so we need to
    # shim things :(.
    if hasattr(node, 'has_key'):
      return node.has_key(key)
    return key in node

  test.fail_test(not (
      _has_key(execution_level, 'level') and
      _has_key(execution_level, 'uiAccess') and
      execution_level['level'].nodeValue == 'asInvoker' and
      execution_level['uiAccess'].nodeValue == 'false'))

  # Verify that <requestedExecutionLevel> is not in the menifest.
  manifest = parseString(extract_manifest(
      test.built_file_path('enable_uac_no.exe', chdir=CHDIR), 1))
  execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  test.fail_test(len(execution_level) != 0)

  # Verify that <requestedExecutionLevel level="requireAdministrator"
  # uiAccess="true" /> is present.
  manifest = parseString(extract_manifest(
      test.built_file_path('enable_uac_admin.exe', chdir=CHDIR), 1))
  execution_level = manifest.getElementsByTagName('requestedExecutionLevel')
  test.fail_test(len(execution_level) != 1)
  execution_level = execution_level[0].attributes
  test.fail_test(not (
      _has_key(execution_level, 'level') and
      _has_key(execution_level, 'uiAccess') and
      execution_level['level'].nodeValue == 'requireAdministrator' and
      execution_level['uiAccess'].nodeValue == 'true'))

  test.pass_test()