lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
#!/bin/bash
#
# Copyright (C) 2013 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
 
# A test script for paycheck.py and the update_payload.py library.
#
# This script requires three payload files, along with a metadata signature for
# each, and a public key for verifying signatures. Payload include:
#
# - A full payload for release X (old_full_payload)
#
# - A full payload for release Y (new_full_payload), where Y > X
#
# - A delta payload from X to Y (delta_payload)
#
# The test performs the following:
#
# - It verifies each payload against its metadata signature, also asserting the
#   payload type. Another artifact is a human-readable payload report, which
#   is output to stdout to be inspected by the user.
#
# - It applies old_full_payload to yield old kernel (old_kern.part) and rootfs
#   (old_root.part) partitions.
#
# - It applies delta_payload to old_{kern,root}.part to yield new kernel
#   (new_delta_kern.part) and rootfs (new_delta_root.part) partitions.
#
# - It applies new_full_payload to yield reference new kernel
#   (new_full_kern.part) and rootfs (new_full_root.part) partitions.
#
# - It compares new_{delta,full}_kern.part and new_{delta,full}_root.part to
#   ensure that they are binary identical.
#
# If all steps have completed successfully we know with high certainty that
# paycheck.py (and hence update_payload.py) correctly parses both full and delta
# payloads, and applies them to yield the expected result. Finally, each
# paycheck.py execution is timed.
 
 
# Stop on errors, unset variables.
set -e
set -u
 
# Temporary image files.
OLD_KERN_PART=old_kern.part
OLD_ROOT_PART=old_root.part
NEW_DELTA_KERN_PART=new_delta_kern.part
NEW_DELTA_ROOT_PART=new_delta_root.part
NEW_FULL_KERN_PART=new_full_kern.part
NEW_FULL_ROOT_PART=new_full_root.part
CROS_PARTS="kernel root"
 
 
log() {
  echo "$@" >&2
}
 
die() {
  log "$@"
  exit 1
}
 
usage_and_exit() {
  cat >&2 <<EOF
Usage: ${0##*/} old_full_payload delta_payload new_full_payload
EOF
  exit
}
 
check_payload() {
  payload_file=$1
  payload_type=$2
 
  time ${paycheck} -t ${payload_type} ${payload_file}
}
 
apply_full_payload() {
  payload_file=$1
  out_dst_kern_part="$2/$3"
  out_dst_root_part="$2/$4"
 
  time ${paycheck} ${payload_file} \
    --part_names ${CROS_PARTS} \
    --out_dst_part_paths ${out_dst_kern_part} ${out_dst_root_part}
}
 
apply_delta_payload() {
  payload_file=$1
  out_dst_kern_part="$2/$3"
  out_dst_root_part="$2/$4"
  dst_kern_part="$2/$5"
  dst_root_part="$2/$6"
  src_kern_part="$2/$7"
  src_root_part="$2/$8"
 
  time ${paycheck} ${payload_file} \
    --part_names ${CROS_PARTS} \
    --out_dst_part_paths ${out_dst_kern_part} ${out_dst_root_part} \
    --dst_part_paths ${dst_kern_part} ${dst_root_part} \
    --src_part_paths ${src_kern_part} ${src_root_part}
}
 
main() {
  # Read command-line arguments.
  if [ $# == 1 ] && [ "$1" == "-h" ]; then
    usage_and_exit
  elif [ $# != 3 ]; then
    die "Error: unexpected number of arguments"
  fi
  old_full_payload="$1"
  delta_payload="$2"
  new_full_payload="$3"
 
  # Find paycheck.py
  paycheck=${0%/*}/paycheck.py
  if [ -z "${paycheck}" ] || [ ! -x ${paycheck} ]; then
    die "cannot find ${paycheck} or file is not executable"
  fi
 
  # Check the payloads statically.
  log "Checking payloads..."
  check_payload "${old_full_payload}" full
  check_payload "${new_full_payload}" full
  check_payload "${delta_payload}" delta
  log "Done"
 
  # Apply full/delta payloads and verify results are identical.
  tmpdir="$(mktemp -d --tmpdir test_paycheck.XXXXXXXX)"
  log "Initiating application of payloads at $tmpdir"
 
  log "Applying old full payload..."
  apply_full_payload "${old_full_payload}" "${tmpdir}" "${OLD_KERN_PART}" \
    "${OLD_ROOT_PART}"
  log "Done"
 
  log "Applying new full payload..."
  apply_full_payload "${new_full_payload}" "${tmpdir}" "${NEW_FULL_KERN_PART}" \
    "${NEW_FULL_ROOT_PART}"
  log "Done"
 
  log "Applying delta payload to old partitions..."
  apply_delta_payload "${delta_payload}" "${tmpdir}" "${NEW_DELTA_KERN_PART}" \
    "${NEW_DELTA_ROOT_PART}" "${NEW_FULL_KERN_PART}" \
    "${NEW_FULL_ROOT_PART}" "${OLD_KERN_PART}" "${OLD_ROOT_PART}"
  log "Done"
 
  log "Comparing results of delta and new full updates..."
  diff "${tmpdir}/${NEW_FULL_KERN_PART}" "${tmpdir}/${NEW_DELTA_KERN_PART}"
  diff "${tmpdir}/${NEW_FULL_ROOT_PART}" "${tmpdir}/${NEW_DELTA_ROOT_PART}"
  log "Done"
 
  log "Cleaning up"
  rm -fr "${tmpdir}"
}
 
main "$@"