1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
4# ex: ts=8 sw=4 sts=4 et filetype=sh
5set -eux
6set -o pipefail
7
8SYSUPDATE=/lib/systemd/systemd-sysupdate
9
10if ! test -x "$SYSUPDATE"; then
11    echo "no systemd-sysupdate" >/skipped
12    exit 0
13fi
14
15export SYSTEMD_PAGER=cat
16export SYSTEMD_LOG_LEVEL=debug
17
18rm -f /var/tmp/72-joined.raw
19truncate -s 10M /var/tmp/72-joined.raw
20
21sfdisk /var/tmp/72-joined.raw <<EOF
22label: gpt
23unit: sectors
24sector-size: 512
25
26size=2048, type=4f68bce3-e8cd-4db1-96e7-fbcaf984b709, name=_empty
27size=2048, type=4f68bce3-e8cd-4db1-96e7-fbcaf984b709, name=_empty
28size=2048, type=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5, name=_empty
29size=2048, type=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5, name=_empty
30EOF
31
32rm -rf /var/tmp/72-dirs
33
34rm -rf /var/tmp/72-defs
35mkdir -p /var/tmp/72-defs
36
37cat >/var/tmp/72-defs/01-first.conf <<"EOF"
38[Source]
39Type=regular-file
40Path=/var/tmp/72-source
41MatchPattern=part1-@v.raw
42
43[Target]
44Type=partition
45Path=/var/tmp/72-joined.raw
46MatchPattern=part1-@v
47MatchPartitionType=root-x86-64
48EOF
49
50cat >/var/tmp/72-defs/02-second.conf <<"EOF"
51[Source]
52Type=regular-file
53Path=/var/tmp/72-source
54MatchPattern=part2-@v.raw.gz
55
56[Target]
57Type=partition
58Path=/var/tmp/72-joined.raw
59MatchPattern=part2-@v
60MatchPartitionType=root-x86-64-verity
61EOF
62
63cat >/var/tmp/72-defs/03-third.conf <<"EOF"
64[Source]
65Type=directory
66Path=/var/tmp/72-source
67MatchPattern=dir-@v
68
69[Target]
70Type=directory
71Path=/var/tmp/72-dirs
72CurrentSymlink=/var/tmp/72-dirs/current
73MatchPattern=dir-@v
74InstancesMax=3
75EOF
76
77rm -rf /var/tmp/72-source
78mkdir -p /var/tmp/72-source
79
80new_version() {
81    # Create a pair of random partition payloads, and compress one
82    dd if=/dev/urandom of="/var/tmp/72-source/part1-$1.raw" bs=1024 count=1024
83    dd if=/dev/urandom of="/var/tmp/72-source/part2-$1.raw" bs=1024 count=1024
84    gzip -k -f "/var/tmp/72-source/part2-$1.raw"
85
86    mkdir -p "/var/tmp/72-source/dir-$1"
87    echo $RANDOM >"/var/tmp/72-source/dir-$1/foo.txt"
88    echo $RANDOM >"/var/tmp/72-source/dir-$1/bar.txt"
89
90    tar --numeric-owner -C "/var/tmp/72-source/dir-$1/" -czf "/var/tmp/72-source/dir-$1.tar.gz" .
91
92    ( cd /var/tmp/72-source/ && sha256sum part* dir-*.tar.gz >SHA256SUMS )
93}
94
95update_now() {
96    # Update to newest version. First there should be an update ready, then we
97    # do the update, and then there should not be any ready anymore
98
99    "$SYSUPDATE" --definitions=/var/tmp/72-defs --verify=no check-new
100    "$SYSUPDATE" --definitions=/var/tmp/72-defs --verify=no update
101    ( ! "$SYSUPDATE" --definitions=/var/tmp/72-defs --verify=no check-new )
102}
103
104verify_version() {
105    # Expects: version ID + sector offset of both partitions to compare
106    dd if=/var/tmp/72-joined.raw bs=1024 skip="$2" count=1024 | cmp "/var/tmp/72-source/part1-$1.raw"
107    dd if=/var/tmp/72-joined.raw bs=1024 skip="$3" count=1024 | cmp "/var/tmp/72-source/part2-$1.raw"
108    cmp "/var/tmp/72-source/dir-$1/foo.txt" /var/tmp/72-dirs/current/foo.txt
109    cmp "/var/tmp/72-source/dir-$1/bar.txt" /var/tmp/72-dirs/current/bar.txt
110}
111
112# Install initial version and verify
113new_version v1
114update_now
115verify_version v1 1024 3072
116
117# Create second version, update and verify that it is added
118new_version v2
119update_now
120verify_version v2 2048 4096
121
122# Create third version, update and verify it replaced the first version
123new_version v3
124update_now
125verify_version v3 1024 3072
126
127# Create fourth version, and update through a file:// URL. This should be
128# almost as good as testing HTTP, but is simpler for us to set up. file:// is
129# abstracted in curl for us, and since our main goal is to test our own code
130# (and not curl) this test should be quite good even if not comprehensive. This
131# will test the SHA256SUMS logic at least (we turn off GPG validation though,
132# see above)
133new_version v4
134
135cat >/var/tmp/72-defs/02-second.conf <<"EOF"
136[Source]
137Type=url-file
138Path=file:///var/tmp/72-source
139MatchPattern=part2-@v.raw.gz
140
141[Target]
142Type=partition
143Path=/var/tmp/72-joined.raw
144MatchPattern=part2-@v
145MatchPartitionType=root-x86-64-verity
146EOF
147
148cat >/var/tmp/72-defs/03-third.conf <<"EOF"
149[Source]
150Type=url-tar
151Path=file:///var/tmp/72-source
152MatchPattern=dir-@v.tar.gz
153
154[Target]
155Type=directory
156Path=/var/tmp/72-dirs
157CurrentSymlink=/var/tmp/72-dirs/current
158MatchPattern=dir-@v
159InstancesMax=3
160EOF
161
162update_now
163verify_version v4 2048 4096
164
165rm  /var/tmp/72-joined.raw
166rm -r /var/tmp/72-dirs /var/tmp/72-defs /var/tmp/72-source
167
168echo OK >/testok
169
170exit 0
171