summaryrefslogtreecommitdiff
path: root/tests/unit/misc/test_pakjoy.py
blob: 55a147269a9ceb48ecfe0cb725af02f088bb935e (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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# SPDX-FileCopyrightText: Florian Bruhin (The-Compiler) <mail@qutebrowser.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import io
import json
import struct
import pathlib
import logging

import pytest

from qutebrowser.misc import pakjoy, binparsing
from qutebrowser.utils import utils, version, standarddir


pytest.importorskip("qutebrowser.qt.webenginecore")


pytestmark = pytest.mark.usefixtures("cache_tmpdir")


versions = version.qtwebengine_versions(avoid_init=True)


# Used to skip happy path tests with the real resources file.
#
# Since we don't know how reliably the Google Meet hangouts extensions is
# reliably in the resource files, and this quirk is only targeting 6.6
# anyway.
skip_if_unsupported = pytest.mark.skipif(
    versions.webengine != utils.VersionNumber(6, 6),
    reason="Code under test only runs on 6.6",
)


@pytest.fixture(autouse=True)
def prepare_env(qapp, monkeypatch):
    monkeypatch.setattr(pakjoy.objects, "qapp", qapp)
    monkeypatch.delenv(pakjoy.RESOURCES_ENV_VAR, raising=False)


def patch_version(monkeypatch, *args):
    monkeypatch.setattr(
        pakjoy.version,
        "qtwebengine_versions",
        lambda **kwargs: version.WebEngineVersions(
            webengine=utils.VersionNumber(*args),
            chromium=None,
            source="unittest",
        ),
    )


@pytest.fixture
def unaffected_version(monkeypatch):
    patch_version(monkeypatch, 6, 6, 1)


@pytest.fixture
def affected_version(monkeypatch):
    patch_version(monkeypatch, 6, 6)


@pytest.mark.parametrize("workdir_exists", [True, False])
def test_version_gate(cache_tmpdir, unaffected_version, mocker, workdir_exists):
    workdir = cache_tmpdir / pakjoy.CACHE_DIR_NAME
    if workdir_exists:
        workdir.mkdir()
        (workdir / "some_patched_file.pak").ensure()
    fake_open = mocker.patch("qutebrowser.misc.pakjoy.open")

    pakjoy.patch_webengine()

    assert not fake_open.called
    assert not workdir.exists()


class TestFindWebengineResources:
    @pytest.fixture
    def qt_data_path(self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
        """Patch qtutils.library_path() to return a temp dir."""
        qt_data_path = tmp_path / "qt_data"
        qt_data_path.mkdir()
        monkeypatch.setattr(pakjoy.qtutils, "library_path", lambda _which: qt_data_path)
        return qt_data_path

    @pytest.fixture
    def application_dir_path(
        self,
        monkeypatch: pytest.MonkeyPatch,
        tmp_path: pathlib.Path,
        qt_data_path: pathlib.Path,  # needs patching
    ):
        """Patch QApplication.applicationDirPath() to return a temp dir."""
        app_dir_path = tmp_path / "app_dir"
        app_dir_path.mkdir()
        monkeypatch.setattr(
            pakjoy.objects.qapp, "applicationDirPath", lambda: app_dir_path
        )
        return app_dir_path

    @pytest.fixture
    def fallback_path(
        self,
        monkeypatch: pytest.MonkeyPatch,
        tmp_path: pathlib.Path,
        qt_data_path: pathlib.Path,  # needs patching
        application_dir_path: pathlib.Path,  # needs patching
    ):
        """Patch the fallback path to return a temp dir."""
        home_path = tmp_path / "home"
        monkeypatch.setattr(pakjoy.pathlib.Path, "home", lambda: home_path)

        app_path = home_path / f".{pakjoy.objects.qapp.applicationName()}"
        app_path.mkdir(parents=True)
        return app_path

    @pytest.mark.parametrize("create_file", [True, False])
    def test_overridden(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path, create_file: bool
    ):
        """Test the overridden path is used."""
        override_path = tmp_path / "override"
        override_path.mkdir()
        monkeypatch.setenv(pakjoy.RESOURCES_ENV_VAR, str(override_path))
        if create_file:  # should get this no matter if file exists or not
            (override_path / pakjoy.PAK_FILENAME).touch()
        assert pakjoy._find_webengine_resources() == override_path

    @pytest.mark.parametrize("with_subfolder", [True, False])
    def test_qt_data_path(self, qt_data_path: pathlib.Path, with_subfolder: bool):
        """Test qtutils.library_path() is used."""
        resources_path = qt_data_path
        if with_subfolder:
            resources_path /= "resources"
            resources_path.mkdir()
        (resources_path / pakjoy.PAK_FILENAME).touch()
        assert pakjoy._find_webengine_resources() == resources_path

    def test_application_dir_path(self, application_dir_path: pathlib.Path):
        """Test QApplication.applicationDirPath() is used."""
        (application_dir_path / pakjoy.PAK_FILENAME).touch()
        assert pakjoy._find_webengine_resources() == application_dir_path

    def test_fallback_path(self, fallback_path: pathlib.Path):
        """Test fallback path is used."""
        (fallback_path / pakjoy.PAK_FILENAME).touch()
        assert pakjoy._find_webengine_resources() == fallback_path

    def test_nowhere(self, fallback_path: pathlib.Path):
        """Test we raise if we can't find the resources."""
        with pytest.raises(
            binparsing.ParseError, match="Couldn't find webengine resources dir"
        ):
            pakjoy._find_webengine_resources()


def json_without_comments(bytestring):
    str_without_comments = "\n".join(
        [
            line
            for line in bytestring.decode("utf-8").split("\n")
            if not line.strip().startswith("//")
        ]
    )
    return json.loads(str_without_comments)


def read_patched_manifest():
    patched_resources = pathlib.Path(os.environ[pakjoy.RESOURCES_ENV_VAR])

    with open(patched_resources / pakjoy.PAK_FILENAME, "rb") as fd:
        reparsed = pakjoy.PakParser(fd)

    return json_without_comments(reparsed.manifest)


@pytest.mark.usefixtures("affected_version")
class TestWithRealResourcesFile:
    """Tests that use the real pak file form the Qt installation."""

    @skip_if_unsupported
    def test_happy_path(self):
        # Go through the full patching processes with the real resources file from
        # the current installation. Make sure our replacement string is in it
        # afterwards.
        pakjoy.patch_webengine()

        json_manifest = read_patched_manifest()
        assert (
            pakjoy.REPLACEMENT_URL.decode("utf-8")
            in json_manifest["externally_connectable"]["matches"]
        )

    def test_copying_resources(self):
        # Test we managed to copy some files over
        work_dir = pakjoy.copy_webengine_resources()

        assert work_dir is not None
        assert work_dir.exists()
        assert work_dir == pathlib.Path(standarddir.cache()) / pakjoy.CACHE_DIR_NAME
        assert (work_dir / pakjoy.PAK_FILENAME).exists()
        assert len(list(work_dir.glob("*"))) > 1

    def test_copying_resources_overwrites(self):
        work_dir = pakjoy.copy_webengine_resources()
        assert work_dir is not None
        tmpfile = work_dir / "tmp.txt"
        tmpfile.touch()

        # Set by first call to copy_webengine_resources()
        del os.environ[pakjoy.RESOURCES_ENV_VAR]

        pakjoy.copy_webengine_resources()
        assert not tmpfile.exists()

    @pytest.mark.parametrize("osfunc", ["copytree", "rmtree"])
    def test_copying_resources_oserror(self, monkeypatch, caplog, osfunc):
        # Test errors from the calls to shutil are handled
        pakjoy.copy_webengine_resources()  # run twice so we hit rmtree too
        caplog.clear()

        def raiseme(err):
            raise err

        monkeypatch.setattr(
            pakjoy.shutil, osfunc, lambda *_args: raiseme(PermissionError(osfunc))
        )
        with caplog.at_level(logging.ERROR, "misc"):
            pakjoy.patch_webengine()
        assert caplog.messages == [
            "Failed to copy webengine resources, not applying quirk"
        ]

    def test_expected_file_not_found(self, cache_tmpdir, monkeypatch, caplog):
        with caplog.at_level(logging.ERROR, "misc"):
            pakjoy._patch(pathlib.Path(cache_tmpdir) / "doesntexist")
        assert caplog.messages[-1].startswith(
            "Resource pak doesn't exist at expected location! "
            "Not applying quirks. Expected location: "
        )


def json_manifest_factory(extension_id=pakjoy.HANGOUTS_MARKER, url=pakjoy.TARGET_URL):
    assert isinstance(extension_id, bytes)
    assert isinstance(url, bytes)

    return f"""
    {{
      {extension_id.decode("utf-8")}
      "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAQt2ZDdPfoSe/JI6ID5bgLHRCnCu9T36aYczmhw/tnv6QZB2I6WnOCMZXJZlRdqWc7w9jo4BWhYS50Vb4weMfh/I0On7VcRwJUgfAxW2cHB+EkmtI1v4v/OU24OqIa1Nmv9uRVeX0GjhQukdLNhAE6ACWooaf5kqKlCeK+1GOkQIDAQAB",
      "name": "Google Hangouts",
      // Note: Always update the version number when this file is updated. Chrome
      // triggers extension preferences update on the version increase.
      "version": "1.3.21",
      "manifest_version": 2,
      "externally_connectable": {{
        "matches": [
          "{url.decode("utf-8")}",
          "http://localhost:*/*"
        ]
        }}
    }}
    """.strip().encode(
        "utf-8"
    )


def pak_factory(version=5, entries=None, encoding=1, sentinel_position=-1):
    if entries is None:
        entries = [json_manifest_factory()]

    buffer = io.BytesIO()
    buffer.write(struct.pack("<I", version))
    buffer.write(struct.pack(pakjoy.PakHeader._FORMAT, encoding, len(entries), 0))

    entry_headers_size = (len(entries) + 1) * 6
    start_of_data = buffer.tell() + entry_headers_size

    # Normally the sentinel sits between the headers and the data. But to get
    # full coverage we want to insert it in other positions.
    with_indices = list(enumerate(entries, 1))
    if sentinel_position == -1:
        with_indices.append((0, b""))
    elif sentinel_position is not None:
        with_indices.insert(sentinel_position, (0, b""))

    accumulated_data_offset = start_of_data
    for idx, entry in with_indices:
        buffer.write(struct.pack(pakjoy.PakEntry._FORMAT, idx, accumulated_data_offset))
        accumulated_data_offset += len(entry)

    for entry in entries:
        assert isinstance(entry, bytes)
        buffer.write(entry)

    buffer.seek(0)
    return buffer


@pytest.mark.usefixtures("affected_version")
class TestWithConstructedResourcesFile:
    """Tests that use a constructed pak file to give us more control over it."""

    @pytest.mark.parametrize(
        "offset",
        [0, 42, pakjoy.HANGOUTS_ID],  # test both slow search and fast path
    )
    def test_happy_path(self, offset):
        entries = [b""] * offset + [json_manifest_factory()]
        assert entries[offset] != b""
        buffer = pak_factory(entries=entries)

        parser = pakjoy.PakParser(buffer)

        json_manifest = json_without_comments(parser.manifest)

        assert (
            pakjoy.TARGET_URL.decode("utf-8")
            in json_manifest["externally_connectable"]["matches"]
        )

    def test_bad_version(self):
        buffer = pak_factory(version=99)

        with pytest.raises(
            binparsing.ParseError,
            match="Unsupported .pak version 99",
        ):
            pakjoy.PakParser(buffer)

    @pytest.mark.parametrize(
        "position, error",
        [
            (0, "Unexpected sentinel entry"),
            (None, "Missing sentinel entry"),
        ],
    )
    def test_bad_sentinal_position(self, position, error):
        buffer = pak_factory(sentinel_position=position)

        with pytest.raises(binparsing.ParseError):
            pakjoy.PakParser(buffer)

    @pytest.mark.parametrize(
        "entry",
        [
            b"{foo}",
            b"V2VsbCBoZWxsbyB0aGVyZQo=",
        ],
    )
    def test_marker_not_found(self, entry):
        buffer = pak_factory(entries=[entry])

        with pytest.raises(
            binparsing.ParseError,
            match="Couldn't find hangouts manifest",
        ):
            pakjoy.PakParser(buffer)

    def test_url_not_found(self):
        buffer = pak_factory(entries=[json_manifest_factory(url=b"example.com")])

        parser = pakjoy.PakParser(buffer)
        with pytest.raises(
            binparsing.ParseError,
            match="Couldn't find URL in manifest",
        ):
            parser.find_patch_offset()

    def test_url_not_found_high_level(self, cache_tmpdir, caplog, affected_version):
        buffer = pak_factory(entries=[json_manifest_factory(url=b"example.com")])

        # Write bytes to file so we can test pakjoy._patch()
        tmpfile = pathlib.Path(cache_tmpdir) / "bad.pak"
        with open(tmpfile, "wb") as fd:
            fd.write(buffer.read())

        with caplog.at_level(logging.ERROR, "misc"):
            pakjoy._patch(tmpfile)

        assert caplog.messages == ["Failed to apply quirk to resources pak."]

    def test_patching(self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
        """Go through the full patching processes with a fake resources file."""
        resources_path = tmp_path / "resources"
        resources_path.mkdir()

        buffer = pak_factory()
        with open(resources_path / pakjoy.PAK_FILENAME, "wb") as fd:
            fd.write(buffer.read())

        monkeypatch.setattr(pakjoy.qtutils, "library_path", lambda _which: tmp_path)
        pakjoy.patch_webengine()

        json_manifest = read_patched_manifest()
        assert (
            pakjoy.REPLACEMENT_URL.decode("utf-8")
            in json_manifest["externally_connectable"]["matches"]
        )