File: test_miscutils.py

package info (click to toggle)
pyinstaller 6.13.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,520 kB
  • sloc: python: 41,347; ansic: 11,334; makefile: 176; sh: 136; xml: 19
file content (230 lines) | stat: -rw-r--r-- 8,414 bytes parent folder | download | duplicates (3)
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
#-----------------------------------------------------------------------------
# Copyright (c) 2005-2023, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------

import os
import array
import shutil

import pytest

from PyInstaller import HOMEPATH, PLATFORM
from PyInstaller.utils.misc import load_py_data_struct, save_py_data_struct


@pytest.mark.win32
def test_versioninfo(tmp_path):
    from PyInstaller.utils.win32.versioninfo import VSVersionInfo, \
        FixedFileInfo, StringFileInfo, StringTable, StringStruct, \
        VarFileInfo, VarStruct

    vsinfo = VSVersionInfo(
        ffi=FixedFileInfo(
            filevers=(1, 2, 3, 4),
            prodvers=(5, 6, 7, 8),
            mask=0x3f,
            flags=0x1,
            OS=0x40004,
            fileType=0x42,
            subtype=0x42,
            date=(0, 0)
        ),
        kids=[
            StringFileInfo([StringTable('040904b0', [StringStruct('FileDescription', 'versioninfo test')])]),
            VarFileInfo([VarStruct('Translation', [1033, 1200])])
        ]
    )

    file = str(tmp_path / 'versioninfo')
    save_py_data_struct(file, vsinfo)

    assert vsinfo == load_py_data_struct(file)


@pytest.mark.win32
def test_versioninfo_str(tmp_path):
    from PyInstaller.utils.win32.versioninfo import VSVersionInfo, \
        FixedFileInfo, StringFileInfo, StringTable, StringStruct, \
        VarFileInfo, VarStruct

    vsinfo = VSVersionInfo(
        ffi=FixedFileInfo(
            filevers=(1, 2, 3, 4),
            prodvers=(5, 6, 7, 8),
            mask=0x3f,
            flags=0x1,
            OS=0x40004,
            fileType=0x42,
            subtype=0x42,
            date=(0, 0)
        ),
        kids=[
            StringFileInfo([StringTable('040904b0', [StringStruct('FileDescription', 'versioninfo test')])]),
            VarFileInfo([VarStruct('Translation', [1033, 1200])])
        ]
    )

    # "Serialize" to string. This is what grab_version.py utility does to write VsVersionInfo to output text file.
    vs_info_str = str(vsinfo)

    # "Deserialize" via eval. This is what `versioninfo.load_version_info_from_text_file` does to read `VsVersionInfo`
    # from text file.
    vsinfo2 = eval(vs_info_str)

    assert vsinfo == vsinfo2


# Test that we properly serialize and deserialize VersionInfo that contains strings with quotes, such as the
# `FileDescription`  of `winrshost.exe` being `Host Process for WinRM's Remote Shell plugin`. See #7630.
@pytest.mark.win32
def test_versioninfo_str_quotes(tmp_path):
    from PyInstaller.utils.win32.versioninfo import VSVersionInfo, \
        FixedFileInfo, StringFileInfo, StringTable, StringStruct, \
        VarFileInfo, VarStruct

    FILE_DESCRIPTION = """versioninfo with quotes (' and ") test"""

    vsinfo = VSVersionInfo(
        ffi=FixedFileInfo(
            filevers=(1, 2, 3, 4),
            prodvers=(5, 6, 7, 8),
            mask=0x3f,
            flags=0x1,
            OS=0x40004,
            fileType=0x42,
            subtype=0x42,
            date=(0, 0)
        ),
        kids=[
            StringFileInfo([StringTable('040904b0', [StringStruct('FileDescription', FILE_DESCRIPTION)])]),
            VarFileInfo([VarStruct('Translation', [1033, 1200])])
        ]
    )

    # "Serialize" to string. This is what grab_version.py utility does to write VsVersionInfo to output text file.
    vs_info_str = str(vsinfo)

    # "Deserialize" via eval. This is what `versioninfo.load_version_info_from_text_file` does to read `VsVersionInfo`
    # from text file.
    vsinfo2 = eval(vs_info_str)

    assert vsinfo == vsinfo2


@pytest.mark.win32
def test_versioninfo_written_to_exe(tmp_path):
    from PyInstaller.utils.win32 import versioninfo
    from PyInstaller.utils.win32.versioninfo import VSVersionInfo, \
        FixedFileInfo, StringFileInfo, StringTable, StringStruct, \
        VarFileInfo, VarStruct

    # Test/expected values
    FILE_DESCRIPTION = 'versioninfo test'
    PRODUCT_NAME = 'Test Product'
    PRODUCT_VERSION = '2021.09.18.00'

    # Create a version info structure...
    vsinfo = VSVersionInfo(
        ffi=FixedFileInfo(
            filevers=(1, 2, 3, 4),
            prodvers=(5, 6, 7, 8),
            mask=0x3f,
            flags=0x1,
            OS=0x40004,
            fileType=0x42,
            subtype=0x42,
            date=(0, 0)
        ),
        kids=[
            StringFileInfo([
                StringTable(
                    '040904b0', [
                        StringStruct('FileDescription', FILE_DESCRIPTION),
                        StringStruct('ProductName', PRODUCT_NAME),
                        StringStruct('ProductVersion', PRODUCT_VERSION)
                    ]
                )
            ]),
            VarFileInfo([VarStruct('Translation', [1033, 1200])])
        ]
    )

    # Locate bootloader executable
    bootloader_file = os.path.join(HOMEPATH, 'PyInstaller', 'bootloader', PLATFORM, 'run.exe')

    # Create a local copy
    test_file = str(tmp_path / 'test_file.exe')
    shutil.copyfile(bootloader_file, test_file)

    # Embed version info
    versioninfo.write_version_info_to_executable(test_file, vsinfo)

    # Read back the values from the string table.
    def read_file_version_info(filename, *attributes):
        import ctypes
        winver = ctypes.WinDLL('version.dll')

        # Read the version information.
        info_len = winver.GetFileVersionInfoSizeW(filename, None)
        assert info_len, "No version information found!"
        info_buf = ctypes.create_string_buffer(info_len)
        rc = winver.GetFileVersionInfoW(filename, None, info_len, info_buf)
        assert rc, "Failed to read version information!"

        # Look for the codepages and take the first one.
        entry_ptr = ctypes.c_void_p()
        entry_len = ctypes.c_uint()
        winver.VerQueryValueW(info_buf, r'\VarFileInfo\Translation', ctypes.byref(entry_ptr), ctypes.byref(entry_len))
        assert entry_len.value, "No codepages found!"
        codepage = array.array('H', ctypes.string_at(entry_ptr.value, 4))
        codepage = "%04x%04x" % tuple(codepage)

        # Query attributes.
        attr_values = []
        for attr in attributes:
            entry_name = fr'\StringFileInfo\{codepage}\{attr}'
            rc = winver.VerQueryValueW(info_buf, entry_name, ctypes.byref(entry_ptr), ctypes.byref(entry_len))
            if not rc:
                entry_value = None
            else:
                entry_value = ctypes.wstring_at(entry_ptr.value, entry_len.value - 1)
            attr_values.append(entry_value)

        return attr_values

    values = read_file_version_info(test_file, 'FileDescription', 'ProductName', 'ProductVersion')
    assert values == [FILE_DESCRIPTION, PRODUCT_NAME, PRODUCT_VERSION]


CHINESE_LOREM_IPSUM = """
索感歴新策理通幻月続本初迷検価泉属速棄者。料流重者県作軽渡獲県行選見。意鑑民供変介画同表後院苦面例紙約度電心
時盤止野断希茶術同懲上権策全崎査応校匹国。真村辺名下周一場庭戸原図会正前対校米利。岩録開教純止表乗都偉別際政難然始京国京
校県水路中旅続球是校高約止性振下上派若福。案神整細番公流関閲元弊導説大実枝際確導
"""


def test_decode():
    """
    Test PyInstaller.utils.misc.decode().
    """
    import codecs
    from PyInstaller.utils.misc import decode

    # Test UTF8 with or without the BOM.
    assert decode(CHINESE_LOREM_IPSUM.encode()) == CHINESE_LOREM_IPSUM
    assert decode(codecs.BOM_UTF8 + CHINESE_LOREM_IPSUM.encode()) == CHINESE_LOREM_IPSUM

    # Test non-default UTF variants specified via a BOM.
    assert decode(codecs.BOM_UTF32_LE + codecs.utf_32_le_encode(CHINESE_LOREM_IPSUM)[0]) == CHINESE_LOREM_IPSUM
    assert decode(codecs.BOM_UTF16_BE + codecs.utf_16_be_encode(CHINESE_LOREM_IPSUM)[0]) == CHINESE_LOREM_IPSUM

    # Test using the encoding comment.
    with_cookie = "# encoding: gb18030\n" + CHINESE_LOREM_IPSUM
    assert decode(with_cookie.encode("GB18030")) == with_cookie