File: test_bundle.py

package info (click to toggle)
rauc 1.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,336 kB
  • sloc: ansic: 36,989; python: 3,354; sh: 1,391; xml: 53; makefile: 41
file content (256 lines) | stat: -rw-r--r-- 8,290 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
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
import os
import shutil

from helper import run


def test_bundle(tmp_path):
    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        "rauc bundle "
        "--cert openssl-ca/dev/autobuilder-1.cert.pem "
        "--key openssl-ca/dev/private/autobuilder-1.pem "
        f"{tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 0
    assert "Creating 'verity' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 0


def test_bundle_args_compat(tmp_path):
    "test compatibility for cert/key args before subcommand"

    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        f"rauc \
            --cert openssl-ca/dev/autobuilder-1.cert.pem \
            --key openssl-ca/dev/private/autobuilder-1.pem \
            bundle \
            {tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 0
    assert "Creating 'verity' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 0


def test_bundle_mksquashfs_extra_args(tmp_path):
    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        f'rauc \
            --cert openssl-ca/dev/autobuilder-1.cert.pem \
            --key openssl-ca/dev/private/autobuilder-1.pem \
            bundle \
            --mksquashfs-args="-comp xz -info" \
            {tmp_path}/install-content {tmp_path}/out.raucb'
    )

    assert exitcode == 0
    assert "Creating 'verity' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 0


def test_bundle_pkcs11_key1(tmp_path, pkcs11):
    "A bundle signed with autobuilder-1 key must verify against keyring"

    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        f"rauc bundle \
            --cert 'pkcs11:token=rauc;object=autobuilder-1' \
            --key 'pkcs11:token=rauc;object=autobuilder-1' \
            {tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 0
    assert "Creating 'verity' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 0


def test_bundle_pkcs11_key2_revoked(tmp_path, pkcs11):
    "A bundle signed with revoked autobuilder-2 key must NOT verify against keyring"

    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        f"rauc bundle \
            --cert 'pkcs11:token=rauc;object=autobuilder-2' \
            --key 'pkcs11:token=rauc;object=autobuilder-2' \
            {tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 0
    assert "Creating 'verity' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 1
    assert "certificate revoked" in err


def test_bundle_pkcs11_key_mismatch(tmp_path, pkcs11):
    "A bundle cannot be signed with mismatching key pair"

    shutil.copytree("install-content", tmp_path / "install-content")

    out, err, exitcode = run(
        f"rauc bundle \
            --cert 'pkcs11:token=rauc;object=autobuilder-1' \
            --key 'pkcs11:token=rauc;object=autobuilder-2' \
            {tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 1
    assert "Creating 'verity' format bundle" in out
    assert "key values mismatch" in err

    assert not os.path.exists(f"{tmp_path}/out.raucb")


def test_bundle_crypt(tmp_path):
    shutil.copytree("install-content", tmp_path / "install-content")
    shutil.copyfile("install-content/manifest.raucm.crypt", tmp_path / "install-content/manifest.raucm")

    out, err, exitcode = run(
        "rauc bundle "
        "--cert openssl-ca/dev/autobuilder-1.cert.pem "
        "--key openssl-ca/dev/private/autobuilder-1.pem "
        "--keyring openssl-ca/dev-ca.pem "
        f"{tmp_path}/install-content {tmp_path}/out.raucb"
    )

    assert exitcode == 0
    assert "Creating 'crypt' format bundle" in out

    assert os.path.exists(f"{tmp_path}/out.raucb")

    out, err, exitcode = run(f"rauc -c test.conf info {tmp_path}/out.raucb")
    assert exitcode == 0


def test_bundle_adaptive(tmp_path, bundle):
    bundle.manifest["image.rootfs"] = {
        "filename": "rootfs.img",
        "adaptive": "block-hash-index",
    }
    bundle.make_random_image("rootfs", 4097, "random rootfs")
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "Creating 'verity' format bundle" in out
    assert "not a multiple of 4096 bytes" in err
    assert not bundle.output.is_file()


def test_bundle_content_checks(tmp_path, bundle):
    bundle.build()
    assert bundle.output.exists()
    bundle.output.unlink()

    # absolute symlinks are not allowed
    test_file = bundle.content / "abs-symlink"
    test_file.symlink_to("/dev/null")
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "absolute symlinks are not supported as bundle contents (abs-symlink)" in err
    assert not bundle.output.is_file()
    test_file.unlink()

    # relative symlinks are not allowed
    test_file = bundle.content / "rel-symlink"
    test_file.symlink_to("../foo")
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "symlinks containing slashes are not supported as bundle contents (rel-symlink)" in err
    assert not bundle.output.is_file()
    test_file.unlink()

    # local symlinks are allowed
    test_file = bundle.content / "local-symlink"
    test_file.symlink_to("foo")
    bundle.build()
    assert bundle.output.is_file()
    bundle.output.unlink()
    test_file.unlink()

    # non-directories are allowed
    test_dir = bundle.content / "subdir"
    test_dir.mkdir()
    bundle.build()
    assert bundle.output.is_file()
    bundle.output.unlink()
    test_dir.rmdir()

    # hidden directories are not allowed
    test_dir = bundle.content / ".hidden_subdir"
    test_dir.mkdir()
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "hidden directories are not supported as bundle contents (.hidden_subdir)"
    assert not bundle.output.is_file()
    test_dir.rmdir()

    # fifos are not allowed
    test_fifo = bundle.content / "fifo"
    os.mkfifo(test_fifo)
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "only regular files are supported as bundle contents (fifo)"
    assert not bundle.output.is_file()
    test_fifo.unlink()


def test_bundle_min_rauc_version(bundle):
    bundle.manifest["update"]["min-rauc-version"] = "1.14-dev"
    bundle.build()
    bundle.output.unlink()

    bundle.manifest["update"]["min-rauc-version"] = "1000-rc.1+21000101"
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "Creating 'verity' format bundle" in out
    assert "Minimum RAUC version in manifest (1000-rc.1+21000101) is newer than current version" in err
    assert not bundle.output.is_file()

    bundle.manifest["update"]["min-rauc-version"] = "bad_version"
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "Creating 'verity' format bundle" in out
    assert (
        "Failed to parse 'min-rauc-version'. Expected 'Major[.Minor[.Patch]][-pre_release]]', got 'bad_version'" in err
    )
    assert not bundle.output.is_file()

    bundle.manifest["update"]["min-rauc-version"] = "1.13"
    out, err, exitcode = bundle.build_nocheck()
    assert exitcode == 1
    assert "Creating 'verity' format bundle" in out
    assert "Minimum RAUC version field in manifest is only supported since 1.14 (not '1.13')" in err
    assert not bundle.output.is_file()


def test_rollout_options(bundle):
    bundle.manifest["rollout"] = {
        "foo": "bar",
    }
    bundle.build()