Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 1 | #!/bin/bash -eu |
| 2 | |
| 3 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
Gaurav Shah | 8b7baaf | 2010-09-07 18:19:21 -0700 | [diff] [blame] | 7 | # Run tests for CGPT. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 8 | |
| 9 | # Load common constants and variables. |
| 10 | . "$(dirname "$0")/common.sh" |
| 11 | |
Bill Richardson | 793e1b4 | 2010-08-20 07:58:43 -0700 | [diff] [blame] | 12 | GPT=$(readlink -f "$1") |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | [ -x "$GPT" ] || error "Can't execute $GPT" |
Bill Richardson | 793e1b4 | 2010-08-20 07:58:43 -0700 | [diff] [blame] | 14 | |
| 15 | # Run tests in a dedicated directory for easy cleanup or debugging. |
| 16 | DIR="${TEST_DIR}/cgpt_test_dir" |
| 17 | [ -d "$DIR" ] || mkdir -p "$DIR" |
| 18 | warning "testing $GPT in $DIR" |
| 19 | cd "$DIR" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 20 | |
| 21 | echo "Create an empty file to use as the device..." |
| 22 | NUM_SECTORS=1000 |
Bill Richardson | 793e1b4 | 2010-08-20 07:58:43 -0700 | [diff] [blame] | 23 | DEV=fake_dev.bin |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 24 | dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | echo "Create a bunch of partitions, using the real GUID types..." |
| 28 | DATA_START=100 |
| 29 | DATA_SIZE=20 |
| 30 | DATA_LABEL="data stuff" |
| 31 | DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7' |
| 32 | DATA_NUM=1 |
| 33 | |
| 34 | KERN_START=200 |
| 35 | KERN_SIZE=30 |
| 36 | KERN_LABEL="kernel stuff" |
| 37 | KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309' |
| 38 | KERN_NUM=2 |
| 39 | |
| 40 | ROOTFS_START=300 |
| 41 | ROOTFS_SIZE=40 |
| 42 | ROOTFS_LABEL="rootfs stuff" |
| 43 | ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec' |
| 44 | ROOTFS_NUM=3 |
| 45 | |
| 46 | ESP_START=400 |
| 47 | ESP_SIZE=50 |
| 48 | ESP_LABEL="ESP stuff" |
| 49 | ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b' |
| 50 | ESP_NUM=4 |
| 51 | |
| 52 | FUTURE_START=500 |
| 53 | FUTURE_SIZE=60 |
| 54 | FUTURE_LABEL="future stuff" |
| 55 | FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e' |
| 56 | FUTURE_NUM=5 |
| 57 | |
| 58 | RANDOM_START=600 |
| 59 | RANDOM_SIZE=70 |
| 60 | RANDOM_LABEL="random stuff" |
| 61 | RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5' |
| 62 | RANDOM_NUM=6 |
| 63 | |
| 64 | $GPT create ${DEV} |
| 65 | |
| 66 | $GPT add -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \ |
| 67 | -l "${DATA_LABEL}" ${DEV} |
| 68 | $GPT add -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \ |
| 69 | -l "${KERN_LABEL}" ${DEV} |
| 70 | $GPT add -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \ |
| 71 | -l "${ROOTFS_LABEL}" ${DEV} |
| 72 | $GPT add -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \ |
| 73 | -l "${ESP_LABEL}" ${DEV} |
| 74 | $GPT add -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \ |
| 75 | -l "${FUTURE_LABEL}" ${DEV} |
| 76 | $GPT add -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \ |
| 77 | -l "${RANDOM_LABEL}" ${DEV} |
| 78 | |
| 79 | |
| 80 | echo "Extract the start and size of given partitions..." |
| 81 | |
| 82 | X=$($GPT show -b -i $DATA_NUM ${DEV}) |
| 83 | Y=$($GPT show -s -i $DATA_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 84 | [ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 85 | |
| 86 | X=$($GPT show -b -i $KERN_NUM ${DEV}) |
| 87 | Y=$($GPT show -s -i $KERN_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 88 | [ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 89 | |
| 90 | X=$($GPT show -b -i $ROOTFS_NUM ${DEV}) |
| 91 | Y=$($GPT show -s -i $ROOTFS_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 92 | [ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 93 | |
| 94 | X=$($GPT show -b -i $ESP_NUM ${DEV}) |
| 95 | Y=$($GPT show -s -i $ESP_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 96 | [ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 97 | |
| 98 | X=$($GPT show -b -i $FUTURE_NUM ${DEV}) |
| 99 | Y=$($GPT show -s -i $FUTURE_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 100 | [ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 101 | |
| 102 | X=$($GPT show -b -i $RANDOM_NUM ${DEV}) |
| 103 | Y=$($GPT show -s -i $RANDOM_NUM ${DEV}) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 104 | [ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | echo "Set the boot partition.." |
| 108 | $GPT boot -i ${KERN_NUM} ${DEV} >/dev/null |
| 109 | |
| 110 | echo "Check the PMBR's idea of the boot partition..." |
| 111 | X=$($GPT boot ${DEV}) |
| 112 | Y=$($GPT show -u -i $KERN_NUM $DEV) |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame^] | 113 | [ "$X" = "$Y" ] || error |
| 114 | |
| 115 | |
| 116 | echo "Test the cgpt prioritize command..." |
| 117 | |
| 118 | # Input: sequence of priorities |
| 119 | # Output: ${DEV} has kernel partitions with the given priorities |
| 120 | make_pri() { |
| 121 | local idx=0 |
| 122 | $GPT create ${DEV} |
| 123 | for pri in "$@"; do |
| 124 | idx=$((idx+1)) |
| 125 | $GPT add -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV} |
| 126 | done |
| 127 | } |
| 128 | |
| 129 | # Output: returns string containing priorities of all kernels |
| 130 | get_pri() { |
| 131 | echo $( |
| 132 | for idx in $($GPT find -t kernel ${DEV} | sed -e s@${DEV}@@); do |
| 133 | $GPT show -i $idx -P ${DEV} |
| 134 | done |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | # Input: list of priorities |
| 139 | # Operation: expects ${DEV} to contain those kernel priorities |
| 140 | assert_pri() { |
| 141 | local expected="$*" |
| 142 | local actual=$(get_pri) |
| 143 | [ "$actual" = "$expected" ] || \ |
| 144 | error 1 "expected priority \"$expected\", actual priority \"$actual\"" |
| 145 | } |
| 146 | |
| 147 | |
| 148 | # no kernels at all. This should do nothing. |
| 149 | $GPT create ${DEV} |
| 150 | $GPT add -t rootfs -b 100 -s 1 ${DEV} |
| 151 | $GPT prioritize ${DEV} |
| 152 | assert_pri "" |
| 153 | |
| 154 | # common install/upgrade sequence |
| 155 | make_pri 2 0 0 |
| 156 | $GPT prioritize -i 1 ${DEV} |
| 157 | assert_pri 1 0 0 |
| 158 | $GPT prioritize -i 2 ${DEV} |
| 159 | assert_pri 1 2 0 |
| 160 | $GPT prioritize -i 1 ${DEV} |
| 161 | assert_pri 2 1 0 |
| 162 | $GPT prioritize -i 2 ${DEV} |
| 163 | assert_pri 1 2 0 |
| 164 | |
| 165 | # lots of kernels, all same starting priority, should go to priority 1 |
| 166 | make_pri 8 8 8 8 8 8 8 8 8 8 8 0 0 8 |
| 167 | $GPT prioritize ${DEV} |
| 168 | assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1 |
| 169 | |
| 170 | # now raise them all up again |
| 171 | $GPT prioritize -P 4 ${DEV} |
| 172 | assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4 |
| 173 | |
| 174 | # set one of them higher, should leave the rest alone |
| 175 | $GPT prioritize -P 5 -i 3 ${DEV} |
| 176 | assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4 |
| 177 | |
| 178 | # set one of them lower, should bring the rest down |
| 179 | $GPT prioritize -P 3 -i 4 ${DEV} |
| 180 | assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1 |
| 181 | |
| 182 | # raise a group by including the friends of one partition |
| 183 | $GPT prioritize -P 6 -i 1 -f ${DEV} |
| 184 | assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6 |
| 185 | |
| 186 | # resurrect one, should not affect the others |
| 187 | make_pri 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 188 | $GPT prioritize -i 2 ${DEV} |
| 189 | assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0 |
| 190 | |
| 191 | # resurrect one and all its friends |
| 192 | make_pri 0 0 0 0 0 0 0 0 1 2 0 0 0 0 |
| 193 | $GPT prioritize -P 5 -i 2 -f ${DEV} |
| 194 | assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5 |
| 195 | |
| 196 | # no options should maintain the same order |
| 197 | $GPT prioritize ${DEV} |
| 198 | assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3 |
| 199 | |
| 200 | # squish all the ranks |
| 201 | make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7 |
| 202 | $GPT prioritize -P 6 ${DEV} |
| 203 | assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6 |
| 204 | |
| 205 | # squish the ranks by not leaving room |
| 206 | make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7 |
| 207 | $GPT prioritize -P 7 -i 3 ${DEV} |
| 208 | assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6 |
| 209 | |
| 210 | # squish the ranks while bringing the friends along |
| 211 | make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7 |
| 212 | $GPT prioritize -P 6 -i 3 -f ${DEV} |
| 213 | assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5 |
| 214 | |
| 215 | # squish them pretty hard |
| 216 | make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7 |
| 217 | $GPT prioritize -P 2 ${DEV} |
| 218 | assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2 |
| 219 | |
| 220 | # squish them really really hard (nobody gets reduced to zero, though) |
| 221 | make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7 |
| 222 | $GPT prioritize -P 1 -i 3 ${DEV} |
| 223 | assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1 |
| 224 | |
| 225 | # squish if we try to go too high |
| 226 | 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 |
| 227 | $GPT prioritize -i 3 ${DEV} |
| 228 | 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 |
| 229 | $GPT prioritize -i 5 ${DEV} |
| 230 | 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 |
| 231 | # but if I bring friends I don't have to squish |
| 232 | $GPT prioritize -i 1 -f ${DEV} |
| 233 | 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 |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 234 | |
| 235 | echo "Done." |
| 236 | |
| 237 | happy "All tests passed." |