File: test_fit.py

package info (click to toggle)
diffoscope 240%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,036 kB
  • sloc: python: 16,298; sh: 113; makefile: 97; xml: 36; javascript: 2
file content (146 lines) | stat: -rw-r--r-- 4,650 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2020 Conrad Ratschan <ratschance@gmail.com>
# Copyright © 2021 Chris Lamb <lamby@debian.org>
#
# diffoscope 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.
#
# diffoscope 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 diffoscope.  If not, see <https://www.gnu.org/licenses/>.
import os
import subprocess

import pytest

from diffoscope.comparators.binary import FilesystemFile
from diffoscope.comparators.fit import FlattenedImageTreeFile
from diffoscope.comparators.utils.specialize import specialize

from ..utils.data import data, assert_diff, load_fixture
from ..utils.tools import skip_unless_tools_exist, skip_unless_tool_is_at_least
from ..utils.nonexisting import assert_non_existing

cpio1 = load_fixture("test1.cpio")
cpio2 = load_fixture("test2.cpio")


def dumpimage_version():
    return (
        subprocess.check_output(("dumpimage", "-V"))
        .decode("utf-8")
        .strip()
        .split(" ")[-1]
    )


def fit_fixture(prefix, entrypoint):
    @pytest.fixture
    def uboot_fit(tmp_path):
        cpio = data("{}.cpio".format(prefix))
        fit_out = tmp_path / "{}.itb".format(prefix)

        # Time must be controlled for reproducible FIT images
        time_env = os.environ.copy()
        time_env["SOURCE_DATE_EPOCH"] = "1609459200"
        time_env["TZ"] = "UTC"
        _ = subprocess.run(
            [
                "mkimage",
                "-f",
                "auto",
                "-A",
                "arm",
                "-O",
                "linux",
                "-T",
                "ramdisk",
                "-C",
                "none",
                "-e",
                entrypoint,
                "-d",
                cpio,
                fit_out,
            ],
            capture_output=True,
            env=time_env,
        )
        return specialize(FilesystemFile(str(fit_out)))

    return uboot_fit


uboot_fit1 = fit_fixture("test1", "0x1000")
uboot_fit2 = fit_fixture("test2", "0x2000")


@skip_unless_tools_exist("dumpimage")
@skip_unless_tools_exist("fdtdump")
def test_identification(uboot_fit1, uboot_fit2):
    assert isinstance(uboot_fit1, FlattenedImageTreeFile)
    assert isinstance(uboot_fit2, FlattenedImageTreeFile)


@skip_unless_tools_exist("dumpimage")
@skip_unless_tools_exist("fdtdump")
def test_no_differences(uboot_fit1):
    difference = uboot_fit1.compare(uboot_fit1)
    assert difference is None


@pytest.fixture
def differences(uboot_fit1, uboot_fit2):
    return uboot_fit1.compare(uboot_fit2).details


@pytest.fixture
def nested_differences(uboot_fit1, uboot_fit2):
    return uboot_fit1.compare(uboot_fit2).details[1].details


@skip_unless_tool_is_at_least("dumpimage", dumpimage_version, "2021.01")
@skip_unless_tools_exist("fdtdump")
def test_file_differences(differences):
    assert_diff(differences[0], "fit_expected_diff")


@skip_unless_tools_exist("cpio")
@skip_unless_tool_is_at_least("dumpimage", dumpimage_version, "2021.01")
@skip_unless_tools_exist("fdtdump")
def test_nested_listing(nested_differences):
    assert_diff(nested_differences[0], "cpio_listing_expected_diff")


@skip_unless_tools_exist("cpio")
@skip_unless_tool_is_at_least("dumpimage", dumpimage_version, "2021.01")
@skip_unless_tools_exist("fdtdump")
def test_nested_symlink(nested_differences):
    assert nested_differences[1].source1 == "dir/link"
    assert nested_differences[1].comment == "symlink"
    assert_diff(nested_differences[1], "symlink_expected_diff")


@skip_unless_tools_exist("cpio")
@skip_unless_tool_is_at_least("dumpimage", dumpimage_version, "2021.01")
@skip_unless_tools_exist("fdtdump")
def test_nested_compressed_files(nested_differences):
    assert nested_differences[2].source1 == "dir/text"
    assert nested_differences[2].source2 == "dir/text"
    assert_diff(nested_differences[2], "text_ascii_expected_diff")


@skip_unless_tools_exist("cpio")
@skip_unless_tool_is_at_least("dumpimage", dumpimage_version, "2021.01")
@skip_unless_tools_exist("fdtdump")
def test_compare_non_existing(monkeypatch, uboot_fit1):
    assert_non_existing(monkeypatch, uboot_fit1)