summaryrefslogtreecommitdiff
path: root/tests/run_cgpt_tests.sh
blob: 8b792b4e1b261445b0a72dff9cc0d8aae705a0f5 (plain)
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
#!/bin/bash -eu

# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Run tests for CGPT.

# Load common constants and variables.
. "$(dirname "$0")/common.sh"

GPT=$(readlink -f "$1")
[ -x "$GPT" ] || error "Can't execute $GPT"

# Run tests in a dedicated directory for easy cleanup or debugging.
DIR="${TEST_DIR}/cgpt_test_dir"
[ -d "$DIR" ] || mkdir -p "$DIR"
warning "testing $GPT in $DIR"
cd "$DIR"

echo "Create an empty file to use as the device..."
NUM_SECTORS=1000
DEV=fake_dev.bin
rm -f ${DEV}
dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null


echo "Create a bunch of partitions, using the real GUID types..."
DATA_START=100
DATA_SIZE=20
DATA_LABEL="data stuff"
DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
DATA_NUM=1

KERN_START=200
KERN_SIZE=30
KERN_LABEL="kernel stuff"
KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309'
KERN_NUM=2

ROOTFS_START=300
ROOTFS_SIZE=40
ROOTFS_LABEL="rootfs stuff"
ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
ROOTFS_NUM=3

ESP_START=400
ESP_SIZE=50
ESP_LABEL="ESP stuff"
ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
ESP_NUM=4

FUTURE_START=500
FUTURE_SIZE=60
FUTURE_LABEL="future stuff"
FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e'
FUTURE_NUM=5

RANDOM_START=600
RANDOM_SIZE=70
RANDOM_LABEL="random stuff"
RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5'
RANDOM_NUM=6

$GPT create ${DEV}

$GPT add -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \
  -l "${DATA_LABEL}" ${DEV}
$GPT add -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \
  -l "${KERN_LABEL}" ${DEV}
$GPT add -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \
  -l "${ROOTFS_LABEL}" ${DEV}
$GPT add -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \
  -l "${ESP_LABEL}" ${DEV}
$GPT add -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \
  -l "${FUTURE_LABEL}" ${DEV}
$GPT add -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \
  -l "${RANDOM_LABEL}" ${DEV}


echo "Extract the start and size of given partitions..."

X=$($GPT show -b -i $DATA_NUM ${DEV})
Y=$($GPT show -s -i $DATA_NUM ${DEV})
[ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error

X=$($GPT show -b -i $KERN_NUM ${DEV})
Y=$($GPT show -s -i $KERN_NUM ${DEV})
[ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error

X=$($GPT show -b -i $ROOTFS_NUM ${DEV})
Y=$($GPT show -s -i $ROOTFS_NUM ${DEV})
[ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error

X=$($GPT show -b -i $ESP_NUM ${DEV})
Y=$($GPT show -s -i $ESP_NUM ${DEV})
[ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error

X=$($GPT show -b -i $FUTURE_NUM ${DEV})
Y=$($GPT show -s -i $FUTURE_NUM ${DEV})
[ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error

X=$($GPT show -b -i $RANDOM_NUM ${DEV})
Y=$($GPT show -s -i $RANDOM_NUM ${DEV})
[ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error


echo "Set the boot partition.."
$GPT boot -i ${KERN_NUM} ${DEV} >/dev/null

echo "Check the PMBR's idea of the boot partition..."
X=$($GPT boot ${DEV})
Y=$($GPT show -u -i $KERN_NUM $DEV)
[ "$X" = "$Y" ] || error


echo "Test the cgpt prioritize command..."

# Input: sequence of priorities
# Output: ${DEV} has kernel partitions with the given priorities
make_pri() {
  local idx=0
  $GPT create ${DEV}
  for pri in "$@"; do
    idx=$((idx+1))
    $GPT add -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV}
  done
}

# Output: returns string containing priorities of all kernels
get_pri() {
  echo $(
  for idx in $($GPT find -t kernel ${DEV} | sed -e s@${DEV}@@); do
    $GPT show -i $idx -P ${DEV}
  done
  )
}

# Input: list of priorities
# Operation: expects ${DEV} to contain those kernel priorities
assert_pri() {
  local expected="$*"
  local actual=$(get_pri)
  [ "$actual" = "$expected" ] || \
    error 1 "expected priority \"$expected\", actual priority \"$actual\""
}


# no kernels at all. This should do nothing.
$GPT create ${DEV}
$GPT add -t rootfs -b 100 -s 1 ${DEV}
$GPT prioritize ${DEV}
assert_pri ""

# common install/upgrade sequence
make_pri   2 0 0
$GPT prioritize -i 1 ${DEV}
assert_pri 1 0 0
$GPT prioritize -i 2 ${DEV}
assert_pri 1 2 0
$GPT prioritize -i 1 ${DEV}
assert_pri 2 1 0
$GPT prioritize -i 2 ${DEV}
assert_pri 1 2 0

# lots of kernels, all same starting priority, should go to priority 1
make_pri   8 8 8 8 8 8 8 8 8 8 8 0 0 8
$GPT prioritize ${DEV}
assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1

# now raise them all up again
$GPT prioritize -P 4 ${DEV}
assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4

# set one of them higher, should leave the rest alone
$GPT prioritize -P 5 -i 3 ${DEV}
assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4

# set one of them lower, should bring the rest down
$GPT prioritize -P 3 -i 4 ${DEV}
assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1

# raise a group by including the friends of one partition
$GPT prioritize -P 6 -i 1 -f ${DEV}
assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6

# resurrect one, should not affect the others
make_pri   0 0 0 0 0 0 0 0 0 0 0 0 0 0
$GPT prioritize -i 2 ${DEV}
assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0

# resurrect one and all its friends
make_pri   0 0 0 0 0 0 0 0 1 2 0 0 0 0
$GPT prioritize -P 5 -i 2 -f ${DEV}
assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5

# no options should maintain the same order
$GPT prioritize ${DEV}
assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3

# squish all the ranks
make_pri   1 1 2 2 3 3 4 4 5 5 0 6 7 7
$GPT prioritize -P 6 ${DEV}
assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6

# squish the ranks by not leaving room
make_pri   1 1 2 2 3 3 4 4 5 5 0 6 7 7
$GPT prioritize -P 7 -i 3 ${DEV}
assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6

# squish the ranks while bringing the friends along
make_pri   1 1 2 2 3 3 4 4 5 5 0 6 7 7
$GPT prioritize -P 6 -i 3 -f ${DEV}
assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5

# squish them pretty hard
make_pri   1 1 2 2 3 3 4 4 5 5 0 6 7 7
$GPT prioritize -P 2 ${DEV}
assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2

# squish them really really hard (nobody gets reduced to zero, though)
make_pri   1 1 2 2 3 3 4 4 5 5 0 6 7 7
$GPT prioritize -P 1 -i 3 ${DEV}
assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1

# squish if we try to go too high
make_pri   15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0
$GPT prioritize -i 3 ${DEV}
assert_pri 14 14 15 13 12 12 11 11 10 10  9  9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 0
$GPT prioritize -i 5 ${DEV}
assert_pri 13 13 14 12 15 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
# but if I bring friends I don't have to squish
$GPT prioritize -i 1 -f ${DEV}
assert_pri 15 15 13 12 14 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0


# Now make sure that we don't need write access if we're just looking.
echo "Test read vs read-write access..."
chmod 0444 ${DEV}

# These should fail
$GPT create -z ${DEV} 2>/dev/null && error
$GPT add -i 2 -P 3 ${DEV} 2>/dev/null && error
$GPT repair ${DEV} 2>/dev/null && error
$GPT prioritize -i 3 ${DEV} 2>/dev/null && error

# Most 'boot' usage should fail too.
$GPT boot -p ${DEV} 2>/dev/null && error
dd if=/dev/zero of=fake_mbr.bin bs=100 count=1 2>/dev/null
$GPT boot -b fake_mbr.bin ${DEV} 2>/dev/null && error
$GPT boot -i 2 ${DEV} 2>/dev/null && error

# These should pass
$GPT boot ${DEV} >/dev/null
$GPT show ${DEV} >/dev/null
$GPT find -t kernel ${DEV} >/dev/null

echo "Done."

happy "All tests passed."