File: test_debfile.py

package info (click to toggle)
python-debian 1.0.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,260 kB
  • sloc: python: 12,905; makefile: 239; sh: 25
file content (666 lines) | stat: -rwxr-xr-x 27,318 bytes parent folder | download
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#! /usr/bin/python

# Tests for ArFile/DebFile
# Copyright (C) 2007    Stefano Zacchiroli  <zack@debian.org>
# Copyright (C) 2007    Filippo Giunchedi   <filippo@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import contextlib
import os
import os.path
from pathlib import Path
import re
import shutil
import stat
import subprocess
import sys
import tempfile

import pytest

from _md5 import md5   # type: ignore

from debian import arfile
from debian import debfile


from typing import (
    Any,
    Callable,
    Dict,
    Generator,
    IO,
    Iterator,
    List,
    Optional,
    Union,
    Text,
    Tuple,
    Type,
    TypeVar,
)



# Only run tests that rely on ar to make archives if it installed.
_ar_path = shutil.which('ar') or ""
# Only run tests that rely on zstd to make archives if it installed.
_zstd_path = shutil.which('zstd') or ""
# Only run tests that rely on dpkg-deb to make archives if it installed.
_dpkg_deb_path = shutil.which('dpkg-deb') or ""


# Deterministic tests are good; automatically skipping tests because optional
# dependencies are not available is a way of accidentally missing problems.
# Here, we control whether missing dependencies result in skipping tests
# or if instead, missing dependencies cause test failures.
#
# FORBID_MISSING_AR:
#   any non-empty value for the environment variable FORBID_MISSING_AR
#   will mean that tests fail if ar (from binutils) can't be found
FORBID_MISSING_AR = os.environ.get("FORBID_MISSING_AR", None)
#
# FORBID_MISSING_ZSTD:
#   any non-empty value for the environment variable FORBID_MISSING_ZSTD
#   will mean that tests fail if zstd (from zstd) can't be found
FORBID_MISSING_ZSTD = os.environ.get("FORBID_MISSING_ZSTD", None)
#
# FORBID_MISSING_DPKG_DEB:
#   any non-empty value for the environment variable FORBID_MISSING_DPKG_DEB
#   will mean that tests fail if dpkg-deb (from dpkg) can't be found
FORBID_MISSING_DPKG_DEB = os.environ.get("FORBID_MISSING_DPKG_DEB", None)


CONTROL_FILE = r"""\
Package: hello
Version: 2.10-2
Architecture: amd64
Maintainer: Santiago Vila <sanvila@debian.org>
Installed-Size: 280
Depends: libc6 (>= 2.14)
Conflicts: hello-traditional
Breaks: hello-debhelper (<< 2.9)
Replaces: hello-debhelper (<< 2.9), hello-traditional
Section: devel
Priority: optional
Homepage: http://www.gnu.org/software/hello/
Description: example package based on GNU hello
 The GNU hello program produces a familiar, friendly greeting.  It
 allows non-programmers to use a classic computer science tool which
 would otherwise be unavailable to them.
 .
 Seriously, though: this is an example of how to do a Debian package.
 It is the Debian version of the GNU Project's `hello world' program
 (which is itself an example for the GNU Project).
"""


def find_test_file(filename: str) -> str:
    """ find a test file that is located within the test suite """
    return os.path.join(os.path.dirname(__file__), filename)


class TestToolsInstalled:

    def test_ar_installed(self) -> None:
        """ test that ar is available from binutils (e.g. /usr/bin/ar) """
        # If test suite is running in FORBID_MISSING_AR mode where
        # having ar is mandatory, explicitly include a failing test to
        # highlight this problem.
        if FORBID_MISSING_AR and not _ar_path:
            pytest.fail("Required ar executable is not installed (tests run in FORBID_MISSING_AR mode)")

    def test_dpkg_deb_installed(self) -> None:
        """ test that dpkg-deb is available from dpkg (e.g. /usr/bin/dpkg-deb) """
        # If test suite is running in FORBID_MISSING_DPKG_DEB mode where
        # having dpkg-deb is mandatory, explicitly include a failing test to
        # highlight this problem.
        if FORBID_MISSING_DPKG_DEB and not _dpkg_deb_path:
            pytest.fail("Required dpkg-deb executable is not installed (tests run in FORBID_MISSING_DPKG_DEB mode)")

    def test_zstd_installed(self) -> None:
        """ test that zstd is available from zstd (e.g. /usr/bin/zstd) """
        # If test suite is running in FORBID_MISSING_ZSTD mode where
        # having zstd is mandatory, explicitly include a failing test to
        # highlight this problem.
        if FORBID_MISSING_ZSTD and not _zstd_path:
            pytest.fail("Required zstd executable is not installed (tests run in FORBID_MISSING_ZSTD mode)")


@pytest.mark.skipif(not _ar_path, reason="ar not installed")
class TestArFile:
    fromfp = False

    @pytest.fixture()
    def sample_archive(self) -> Generator[None, None, None]:
        subprocess.check_call(
            [
                _ar_path,
                "rU",
                "test.ar",
                find_test_file("test_debfile.py"),
                find_test_file("test_changelog"),
                find_test_file("test_deb822.py")
            ],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        assert os.path.exists("test.ar")
        with os.popen("ar t test.ar") as ar:
            self.testmembers = [x.strip() for x in ar.readlines()]
        self.fp = open("test.ar", "rb")
        if self.fromfp:
            self.a = arfile.ArFile(fileobj=self.fp)
        else:
            self.a = arfile.ArFile("test.ar")

        yield

        if hasattr(self, 'fp'):
            self.fp.close()
        if os.path.exists('test.ar'):
            os.unlink('test.ar')

    def test_getnames(self, sample_archive):
        # type: (None) -> None
        """ test for file list equality """
        assert self.a.getnames() == self.testmembers

    def test_getmember(self, sample_archive):
        # type: (None) -> None
        """ test for each member equality """
        for member in self.testmembers:
            m = self.a.getmember(member)
            assert m
            assert m.name == member

            mstat = os.stat(find_test_file(member))

            assert m.size == mstat[stat.ST_SIZE]
            assert m.owner == mstat[stat.ST_UID]
            assert m.group == mstat[stat.ST_GID]

    def test_file_seek(self, sample_archive):
        # type: (None) -> None
        """ test for faked seek """
        m = self.a.getmember(self.testmembers[0])

        for i in [10,100,10000,100000]:
            m.seek(i, 0)
            assert m.tell() == i, "failed tell()"

            m.seek(-i, 1)
            assert m.tell() == 0, "failed tell()"

        m.seek(0)
        with pytest.raises(IOError):
            m.seek(-1, 0)
        with pytest.raises(IOError):
            m.seek(-1, 1)
        m.seek(0)
        m.close()

    def test_file_read(self, sample_archive):
        # type: (None) -> None
        """ test for faked read """
        for m in self.a.getmembers():
            with open(find_test_file(m.name), 'rb') as f:

                for i in [10, 100, 10000]:
                    assert m.read(i) == f.read(i)

            m.close()

    def test_file_readlines(self, sample_archive):
        # type: (None) -> None
        """ test for faked readlines """

        for m in self.a.getmembers():
            f = open(find_test_file(m.name), 'rb')

            assert m.readlines() == f.readlines()

            m.close()
            f.close()


@pytest.mark.skipif(not _ar_path, reason="ar not installed")
class TestArFileFileObj(TestArFile):
    fromfp = True


def _make_archive(dir_path: str, compression: str) -> str:
    """ Create an archive from a directory with a given compression algorithm.

    :returns: the path to the created archive
    """

    if compression == "zsttar":
        if not _zstd_path:
            if FORBID_MISSING_ZSTD:
                pytest.fail("Required zstd is not installed (tests run in FORBID_MISSING_ZSTD mode")
            pytest.skip("zstd not installed")
        uncompressed_archive = shutil.make_archive(
            dir_path,
            "tar",
            root_dir=dir_path,
        )
        archive = uncompressed_archive + ".zst"
        with open(uncompressed_archive) as input:
            proc =subprocess.Popen([_zstd_path, "-q", "-o", archive], stdin=input)
            assert(proc.wait() == 0)
        os.remove(uncompressed_archive)
    else:
        archive = shutil.make_archive(
            dir_path,
            compression,
            root_dir=dir_path,
        )
    return archive


compressions = ["gztar", "bztar", "xztar", "tar", "zsttar"]

@pytest.mark.skipif(not _ar_path, reason="ar not installed")
class TestDebFile:

    # from this source package that will be included in the sample .deb
    # that is used for testing
    example_data_dir = Path("usr/share/doc/examples")
    example_data_files = [
        "test_debfile.py",
        "test_changelog",
        "test_deb822.py",
        "test_Changes",       # signed file so won't change
    ]
    # location for symlinks in the archive, relative to the example dir
    link_data_dirs = [
        "../links",
        "/var/tests",
    ]

    @pytest.fixture()
    def sample_deb_control(self, request: Any) -> Generator[str, None, None]:
        control = getattr(request, "param", "gztar")
        yield from self._generate_deb(control=control)

    @pytest.fixture()
    def sample_deb_data(self, request: Any) -> Generator[str, None, None]:
        data = getattr(request, "param", "gztar")
        yield from self._generate_deb(data=data)

    @pytest.fixture()
    def sample_deb_binnmu(self, request: Any) -> Generator[str, None, None]:
        def control_binnmu() -> str:
            return CONTROL_FILE.replace(
                "Version",
                "Source: hellosrc (1.2.3)\nVersion"
            )
        yield from self._generate_deb(control_generator=control_binnmu)

    @pytest.fixture()
    def sample_deb(self, request: Any) -> Generator[str, None, None]:
        config = getattr(request, "param", (None, None, None))
        control = config[0] or 'gztar'
        data = config[1] or 'gztar'
        control_generator = config[2] or None
        yield from self._generate_deb(
            control=control,
            data=data,
            control_generator=control_generator
        )

    def _generate_deb(
        self,
        filename: str = "test.deb",
        control: str = "gztar",
        data: str = "gztar",
        control_generator: Optional[Callable[[], str]] = None
    ) -> Generator[str, None, None]:
        """ Creates a test deb within a contextmanager for artefact cleanup

        :param filename:
            optionally specify the filename that will be used for the .deb
            file. If an absolute path is given, the .deb will be created
            outside of the TemporaryDirectory in which assembly is performed.
        :param control:
            optionally specify the compression format for the control member
            of the .deb file; allowable values are from
            `shutil.make_archive`: `gztar`, `bztar`, `xztar`, `zsttar`
        :param data:
            optionally specify the compression format for the data member
            of the .deb file; allowable values are from
            `shutil.make_archive`: `gztar`, `bztar`, `xztar`, `zsttar`
        """
        control_generator = control_generator or (lambda: CONTROL_FILE)
        with tempfile.TemporaryDirectory(prefix="test_debfile.") as tempdir:
            tpath = Path(tempdir)
            tempdeb = str(tpath / filename)

            # the debian-binary member
            info_member = str(tpath / "debian-binary")
            with open(info_member, "wt") as fh:
                fh.write("2.0\n")

            # the data.tar member
            datapath = tpath / "data"
            examplespath = datapath / self.example_data_dir
            examplespath.mkdir(parents=True)
            for f in self.example_data_files:
                shutil.copy(find_test_file(f), str(examplespath))

            # Make some symlinks for testing
            # a) symlink within a directory (relative path)
            dest = Path(self.example_data_files[0])
            link = Path(examplespath / ("link_" + self.example_data_files[0]))
            link.symlink_to(dest)

            # b) symlinks traversing directories
            for d in self.link_data_dirs:
                # dest dir for links
                linkspath = str(self.example_data_dir / d)
                tmplinkpath = Path(datapath) / (linkspath if not linkspath.startswith("/") else linkspath[1:])
                tmplinkpath.mkdir(parents=True)

                # Find the correct path for the symlink according to Policy
                # policy says to use relative paths unless the path traverses /
                if d.startswith("/"):
                    # it traverses root so use the absolute path
                    destpath = Path("/") / self.example_data_dir
                else:
                    # relative path is acceptable
                    # CRUFT: python < 3.6 doesn't support pathlib in os.path
                    destpath = Path(os.path.relpath(str(self.example_data_dir), linkspath))
                # finally, the destination where the link is supposed to point to
                dest = destpath / self.example_data_files[0]
                # and the actual filesystem location of the link
                # CRUFT: for python >= 3.6 this can be done with .resolve()
                link = Path(os.path.normpath(str(tmplinkpath / self.example_data_files[0])))
                link.symlink_to(dest)

                # c) also make a symlink to a directory
                # CRUFT: for python >= 3.6 this can be done with .resolve()
                link = Path(os.path.normpath(str(tmplinkpath / "dirlink")))
                link.symlink_to(destpath)

            data_member = _make_archive(str(datapath), data)

            # the control.tar member
            controlpath = tpath / "control"
            controlpath.mkdir()
            with open(str(controlpath / "control"), "w") as fh:
                fh.write(control_generator())
            with open(str(controlpath / "md5sums"), "w") as fh:
                for f in self.example_data_files:
                    with open(str(examplespath / f), 'rb') as hashfh:
                        h = md5(hashfh.read()).hexdigest()
                    fh.write("%s %s\n" % (h, str(self.example_data_dir / f)))

            control_member = _make_archive(str(controlpath), control)

            # Build the .deb file using `ar`
            make_deb_command = [
                _ar_path, "rU", tempdeb,
                info_member,
                control_member,
                data_member,
            ]
            subprocess.check_call(
                make_deb_command,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )
            assert os.path.exists(tempdeb)

            try:
                # provide the constructed .deb via the contextmanager
                yield tempdeb

            finally:
                # post contextmanager cleanup
                if os.path.exists(tempdeb):
                    os.unlink(tempdeb)
                # the contextmanager for the TemporaryDirectory will clean up
                # everything else that was left around

    @pytest.mark.parametrize('part', ['control.tar.gz', 'data.tar.gz'])
    def test_missing_members(self, sample_deb: str, part: str) -> None:
        """ test that broken .deb files raise exceptions """
        # break the .deb by deleting a required member
        subprocess.check_call(
            [_ar_path, 'd', sample_deb, part],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        with pytest.raises(debfile.DebError):
            debfile.DebFile(sample_deb)

    @pytest.mark.parametrize("sample_deb_data", compressions, indirect=["sample_deb_data"])
    def test_data_compression(self, sample_deb_data: str) -> None:
        """ test various compression schemes for the data member """
        with debfile.DebFile(sample_deb_data) as deb:
            # random test on the data part, just to check that content access
            # is OK
            all_files = [os.path.normpath(f) for f in deb.data.tgz().getnames()]
            for f in self.example_data_files:
                testfile = os.path.normpath(str(self.example_data_dir / f))
                assert testfile in all_files, \
                    "Data part failed on compression"
            assert os.path.normpath(str(self.example_data_dir)) in all_files, \
                "Data part failed on compression"

    @pytest.mark.parametrize("sample_deb_control", compressions, indirect=["sample_deb_control"])
    def test_control_compression(self, sample_deb_control: str) -> None:
        """ test various compression schemes for the control member """
        with debfile.DebFile(sample_deb_control) as deb:
            # random test on the control part
            assert 'control' in \
                [os.path.normpath(p) for p in deb.control.tgz().getnames()], \
                "Control part failed on compression"
            assert 'md5sums' in \
                [os.path.normpath(p) for p in deb.control.tgz().getnames()], \
                "Control part failed on compression"

    @pytest.mark.skipif(not _dpkg_deb_path, reason="dpkg-deb not installed")
    def test_data_names(self, sample_deb: str) -> None:
        """ test for file list equality """
        with debfile.DebFile(sample_deb) as deb:
            tgz = deb.data.tgz()
            with os.popen("dpkg-deb --fsys-tarfile %s | tar t" % sample_deb) as tar:
                dpkg_names = [os.path.normpath(x.strip()) for x in tar.readlines()]
            debfile_names = [os.path.normpath(name) for name in tgz.getnames()]

            # skip the root
            assert debfile_names[1:] == dpkg_names[1:]

    def _test_file_contents(self, debname, debfilename, origfilename, modes=None, follow_symlinks=False):
        # type: (str, Union[str, Path], Union[str, Path], Optional[List[str]], bool) -> None
        """ helper function to test that the deb file has the right contents """
        modes = modes or ["rb", "rt"]
        for mode in modes:
            with debfile.DebFile(debname) as deb:
                with open(origfilename, mode) as fh:
                    origdata = fh.read()
                encoding = None if not "t" in mode else "UTF-8"
                for prefix in "", "./":
                    dfh = deb.data.get_file(prefix + str(debfilename), encoding=encoding, follow_symlinks=follow_symlinks)
                    debdata = dfh.read()
                    assert origdata == debdata
                    dfh.close()

    def test_data_has_file(self, sample_deb: str) -> None:
        """ test for round-trip of a data file """
        # also test some variations on how the root directory is stored
        with debfile.DebFile(sample_deb) as deb:
            debdatafile = str(self.example_data_dir / self.example_data_files[-1])
            assert deb.data.has_file(debdatafile)
            assert deb.data.has_file("./" + debdatafile)

            assert deb.data.has_file("/")
            assert deb.data.has_file("./")
            assert deb.data.has_file(".")

            assert not deb.data.has_file("/usr/share/doc/nosuchfile")
            assert not deb.data.has_file("/nosuchdir/nosuchfile")

    def test_data_has_file_symlinks(self, sample_deb: str) -> None:
        """ test for round-trip of a data file """
        def path(*args):
            # type: (Union[str, Path]) -> str
            return os.path.normpath(os.path.join(
                str(self.example_data_dir), *args
            ))

        with debfile.DebFile(sample_deb) as deb:
            # link to file in same directory
            debdatafile = str(self.example_data_dir / ( "link_" + self.example_data_files[0]))
            assert deb.data.has_file(debdatafile)

            # link to file in different directory
            debdatafile = path(self.link_data_dirs[0], self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=False)

            debdatafile = path(self.link_data_dirs[0], self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=True)

            # link to file in different dir traversing /
            debdatafile = path(self.link_data_dirs[1], self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=False)

            debdatafile = path(self.link_data_dirs[1], self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=True)

            # file beyond a directory symlink
            debdatafile = path(self.link_data_dirs[0], "dirlink", self.example_data_files[0])
            assert not deb.data.has_file(debdatafile, follow_symlinks=False)

            debdatafile = path(self.link_data_dirs[0], "dirlink", self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=True)

            # file beyond a directory symlink traversing /
            debdatafile = path(self.link_data_dirs[1], "dirlink", self.example_data_files[0])
            assert not deb.data.has_file(debdatafile, follow_symlinks=False)

            debdatafile = path(self.link_data_dirs[1], "dirlink", self.example_data_files[0])
            assert deb.data.has_file(debdatafile, follow_symlinks=True)

            # non-existent files and directories
            assert not deb.data.has_file("/usr/share/doc/nosuchfile", follow_symlinks=False)
            assert not deb.data.has_file("/nosuchdir/nosuchfile", follow_symlinks=True)
            assert not deb.data.has_file("/usr/share/doc/nosuchfile", follow_symlinks=False)
            assert not deb.data.has_file("/nosuchdir/nosuchfile", follow_symlinks=True)

            # non-existent files beyond a symlink
            debdatafile = path(self.link_data_dirs[1], "dirlink", "nosuchfile")
            assert not deb.data.has_file(debdatafile, follow_symlinks=False)
            assert not deb.data.has_file(debdatafile, follow_symlinks=True)

    def test_data_get_file(self, sample_deb: str) -> None:
        """ test for round-trip of a data file """
        datafile = self.example_data_files[-1]
        debdatafile = self.example_data_dir / self.example_data_files[-1]

        self._test_file_contents(sample_deb, debdatafile, find_test_file(datafile))

        with pytest.raises(debfile.DebError):
            self._test_file_contents(sample_deb, "/usr/share/doc/nosuchfile", find_test_file(datafile))

        with pytest.raises(debfile.DebError):
            self._test_file_contents(sample_deb, "./usr/share/doc/nosuchfile", find_test_file(datafile))

        with pytest.raises(debfile.DebError):
            self._test_file_contents(sample_deb, "/nosuchdir/nosuchfile", find_test_file(datafile))

        with pytest.raises(debfile.DebError):
            self._test_file_contents(sample_deb, "./nosuchdir/nosuchfile", find_test_file(datafile))

    def test_data_get_file_symlinks(self, sample_deb: str) -> None:
        """ test for traversing symlinks in the package

        links that are within the same directory get automatically resolved
        by tarfile, but links that cross directories do not
        """
        basename = self.example_data_files[0]
        targetdata = find_test_file(basename)
        testlinkfiles = [
            # Format: (path in .deb, fails without symlinks)
            # relative symlink to a file within a directory
            (self.example_data_dir / ("link_" + basename), False),
            # relative symlink to a file
            (Path(self.example_data_dir / self.link_data_dirs[0]) / basename, True),
            # relative symlink to a directory
            (Path(self.example_data_dir / self.link_data_dirs[0]) / "dirlink" / basename, True),
            # absolute symlink to a file
            (Path(self.example_data_dir / self.link_data_dirs[1]) / basename, True),
            # absolute symlink to a directory
            (Path(self.example_data_dir / self.link_data_dirs[1]) / "dirlink" / basename, True),
        ]

        for linkname, fail_without_symlink in testlinkfiles:
            cleanlinkname = os.path.normpath(str(linkname))
            if fail_without_symlink:
                with pytest.raises(debfile.DebError):
                    self._test_file_contents(sample_deb, linkname, targetdata, follow_symlinks=False)
            else:
                self._test_file_contents(sample_deb, linkname, targetdata, follow_symlinks=False)
            self._test_file_contents(sample_deb, cleanlinkname, targetdata, follow_symlinks=True)

    @pytest.mark.skipif(not _dpkg_deb_path, reason="dpkg-deb not installed")
    def test_control(self, sample_deb: str) -> None:
        """ test for control contents equality """
        with os.popen("dpkg-deb -f %s" % sample_deb) as dpkg_deb:
            filecontrol = "".join(dpkg_deb.readlines())

        with debfile.DebFile(sample_deb) as deb:
            for control_file in ["control", "./control"]:
                ctrl = deb.control.get_content(control_file)
                assert ctrl is not None
                assert ctrl.decode("utf-8") == filecontrol
                assert deb.control.get_content("control", encoding="utf-8") == filecontrol

    def test_binnmu_control(self, sample_deb_binnmu: str) -> None:
        """ test for control contents equality """
        with debfile.DebFile(sample_deb_binnmu) as deb:
            ctrl = deb.debcontrol()
            assert ctrl.source == "hellosrc"
            assert ctrl.source_version == "1.2.3"
            assert ctrl['Version'] == "2.10-2"

    def test_md5sums(self, sample_deb: str) -> None:
        """test md5 extraction from .debs"""
        with debfile.DebFile(sample_deb) as deb:
            md5b = deb.md5sums()
            md5 = deb.md5sums(encoding="UTF-8")

            data = [
                (self.example_data_dir / "test_Changes", "73dbb291e900d8cd08e2bb76012a3829"),
            ]
            for f, h in data:
                assert md5b[str(f).encode('UTF-8')] == h
                assert md5[str(f)] == h

    def test_contextmanager(self, sample_deb: str) -> None:
        """test use of DebFile as a contextmanager"""
        with debfile.DebFile(sample_deb) as deb:
            all_files = deb.data.tgz().getnames()
            assert all_files
            assert deb.control.get_content("control")

    def test_open_directly(self, sample_deb: str) -> None:
        """test use of DebFile without the contextmanager"""
        with debfile.DebFile(sample_deb) as deb:
            all_files = deb.data.tgz().getnames()
            assert all_files
            assert deb.control.get_content("control")