zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#
# (C) Copyright 2014 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#
import os
import sys
import unittest
import contextlib
import tempfile
import shutil
 
import win32api
 
from win32ctypes import pywin32
from win32ctypes.pywin32.pywintypes import error
from win32ctypes.tests import compat
 
 
skip_on_wine = 'SKIP_WINE_KNOWN_FAILURES' in os.environ
 
 
class TestWin32API(compat.TestCase):
 
    # the pywin32ctypes implementation
    module = pywin32.win32api
 
    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        shutil.copy(sys.executable, self.tempdir)
 
    def tearDown(self):
        shutil.rmtree(self.tempdir)
 
    @contextlib.contextmanager
    def load_library(self, module, library=sys.executable, flags=0x2):
        handle = module.LoadLibraryEx(library, 0, flags)
        try:
            yield handle
        finally:
            module.FreeLibrary(handle)
 
    @contextlib.contextmanager
    def resource_update(self, module, library=sys.executable):
        handle = module.BeginUpdateResource(library, False)
        try:
            yield handle
        finally:
            module.EndUpdateResource(handle, False)
 
    def test_load_library_ex(self):
        with self.load_library(win32api) as expected:
            with self.load_library(self.module) as handle:
                self.assertEqual(handle, expected)
 
        with self.assertRaises(error):
            self.module.LoadLibraryEx(u'ttt.dll', 0, 0x2)
 
    def test_free_library(self):
        with self.load_library(win32api) as handle:
            self.assertTrue(win32api.FreeLibrary(handle) is None)
            self.assertNotEqual(self.module.FreeLibrary(handle), 0)
 
        with self.assertRaises(error):
            self.module.FreeLibrary(-3)
 
    def test_enum_resource_types(self):
        with self.load_library(win32api, u'shell32.dll') as handle:
            expected = win32api.EnumResourceTypes(handle)
 
        with self.load_library(pywin32.win32api, u'shell32.dll') as handle:
            resource_types = self.module.EnumResourceTypes(handle)
 
        self.assertEqual(resource_types, expected)
 
        with self.assertRaises(error):
            self.module.EnumResourceTypes(-3)
 
    def test_enum_resource_names(self):
        with self.load_library(win32api, u'shell32.dll') as handle:
            resource_types = win32api.EnumResourceTypes(handle)
            for resource_type in resource_types:
                expected = win32api.EnumResourceNames(handle, resource_type)
                resource_names = self.module.EnumResourceNames(
                    handle, resource_type)
                self.assertEqual(resource_names, expected)
                # check that the #<index> format works
                resource_names = self.module.EnumResourceNames(
                    handle, self._id2str(resource_type))
                self.assertEqual(resource_names, expected)
 
        with self.assertRaises(error):
            self.module.EnumResourceNames(2, 3)
 
    def test_enum_resource_languages(self):
        with self.load_library(win32api, u'shell32.dll') as handle:
            resource_types = win32api.EnumResourceTypes(handle)
            for resource_type in resource_types:
                resource_names = win32api.EnumResourceNames(
                    handle, resource_type)
                for resource_name in resource_names:
                    expected = win32api.EnumResourceLanguages(
                        handle, resource_type, resource_name)
                    resource_languages = self.module.EnumResourceLanguages(
                        handle, resource_type, resource_name)
                    self.assertEqual(resource_languages, expected)
                    # check that the #<index> format works
                    resource_languages = self.module.EnumResourceLanguages(
                        handle, self._id2str(resource_type),
                        self._id2str(resource_name))
                    self.assertEqual(resource_languages, expected)
 
        with self.assertRaises(error):
            self.module.EnumResourceLanguages(handle, resource_type, 2235)
 
    def test_load_resource(self):
        with self.load_library(win32api, u'explorer.exe') as handle:
            resource_types = win32api.EnumResourceTypes(handle)
            for resource_type in resource_types:
                resource_names = win32api.EnumResourceNames(
                    handle, resource_type)
                for resource_name in resource_names:
                    resource_languages = win32api.EnumResourceLanguages(
                        handle, resource_type, resource_name)
                    for resource_language in resource_languages:
                        expected = win32api.LoadResource(
                            handle, resource_type, resource_name,
                            resource_language)
                        resource = self.module.LoadResource(
                            handle, resource_type, resource_name,
                            resource_language)
                        # check that the #<index> format works
                        resource = self.module.LoadResource(
                            handle, self._id2str(resource_type),
                            self._id2str(resource_name),
                            resource_language)
                        self.assertEqual(resource, expected)
 
        with self.assertRaises(error):
            self.module.LoadResource(
                handle, resource_type, resource_name, 12435)
 
    def test_get_tick_count(self):
        self.assertGreater(self.module.GetTickCount(), 0.0)
 
    def test_begin_and_end_update_resource(self):
        # given
        module = self.module
        filename = os.path.join(self.tempdir, 'python.exe')
        with self.load_library(module, filename) as handle:
            count = len(module.EnumResourceTypes(handle))
 
        # when
        handle = module.BeginUpdateResource(filename, False)
        module.EndUpdateResource(handle, False)
 
        # then
        with self.load_library(module, filename) as handle:
            self.assertEqual(len(module.EnumResourceTypes(handle)), count)
 
        # when
        handle = module.BeginUpdateResource(filename, True)
        module.EndUpdateResource(handle, True)
 
        # then
        with self.load_library(module, filename) as handle:
            self.assertEqual(len(module.EnumResourceTypes(handle)), count)
 
    def test_begin_removing_all_resources(self):
        if skip_on_wine:
            self.skipTest('EnumResourceTypes known failure on wine, see #59')
 
        # given
        module = self.module
        filename = os.path.join(self.tempdir, 'python.exe')
 
        # when
        handle = module.BeginUpdateResource(filename, True)
        module.EndUpdateResource(handle, False)
 
        # then
        with self.load_library(module, filename) as handle:
            self.assertEqual(len(module.EnumResourceTypes(handle)), 0)
 
    def test_begin_update_resource_with_invalid(self):
        if skip_on_wine:
            self.skipTest('BeginUpdateResource known failure on wine, see #59')
 
            # when/then
        with self.assertRaises(error) as context:
            self.module.BeginUpdateResource('invalid', False)
        # the errno cannot be 0 (i.e. success)
        self.assertNotEqual(context.exception.winerror, 0)
 
    def test_end_update_resource_with_invalid(self):
        if skip_on_wine:
            self.skipTest('EndUpdateResource known failure on wine, see #59')
 
        # when/then
        with self.assertRaises(error) as context:
            self.module.EndUpdateResource(-3, False)
        # the errno cannot be 0 (i.e. success)
        self.assertNotEqual(context.exception.winerror, 0)
 
    def test_update_resource(self):
        # given
        module = self.module
        filename = os.path.join(self.tempdir, 'python.exe')
        with self.load_library(self.module, filename) as handle:
            resource_type = module.EnumResourceTypes(handle)[-1]
            resource_name = module.EnumResourceNames(handle, resource_type)[-1]
            resource_language = module.EnumResourceLanguages(
                handle, resource_type, resource_name)[-1]
            resource = module.LoadResource(
                handle, resource_type, resource_name, resource_language)
 
        # when
        with self.resource_update(self.module, filename) as handle:
            module.UpdateResource(
                handle, resource_type, resource_name, resource[:-2],
                resource_language)
 
        # then
        with self.load_library(self.module, filename) as handle:
            updated = module.LoadResource(
                handle, resource_type, resource_name, resource_language)
        self.assertEqual(len(updated), len(resource) - 2)
        self.assertEqual(updated, resource[:-2])
 
    def test_update_resource_with_unicode(self):
        # given
        module = self.module
        filename = os.path.join(self.tempdir, 'python.exe')
        with self.load_library(module, filename) as handle:
            resource_type = module.EnumResourceTypes(handle)[-1]
            resource_name = module.EnumResourceNames(handle, resource_type)[-1]
            resource_language = module.EnumResourceLanguages(
                handle, resource_type, resource_name)[-1]
        resource = u"\N{GREEK CAPITAL LETTER DELTA}"
 
        # when
        with self.resource_update(module, filename) as handle:
            with self.assertRaises(TypeError):
                module.UpdateResource(
                    handle, resource_type, resource_name, resource,
                    resource_language)
 
    def test_get_windows_directory(self):
        # given
        expected = win32api.GetWindowsDirectory()
 
        # when
        result = self.module.GetWindowsDirectory()
 
        # then
        # note: pywin32 returns str on py27, unicode (which is str) on py3
        self.assertIsInstance(result, str)
        self.assertEqual(result.lower(), r"c:\windows")
        self.assertEqual(result, expected)
 
    def test_get_system_directory(self):
        # given
        expected = win32api.GetSystemDirectory()
 
        # when
        result = self.module.GetSystemDirectory()
 
        # then
        # note: pywin32 returns str on py27, unicode (which is str) on py3
        self.assertIsInstance(result, str)
        self.assertEqual(result.lower(), r"c:\windows\system32")
        self.assertEqual(result, expected)
 
    def _id2str(self, type_id):
        if hasattr(type_id, 'index'):
            return type_id
        else:
            return u'#{0}'.format(type_id)
 
 
if __name__ == '__main__':
    unittest.main()